debian-mirror-gitlab/app/assets/javascripts/ide/ide_router.js

113 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import Vue from 'vue';
2020-03-09 13:42:32 +05:30
import IdeRouter from '~/ide/ide_router_extension';
2019-07-31 22:56:46 +05:30
import { joinPaths } from '~/lib/utils/url_utility';
2018-05-09 12:01:36 +05:30
import flash from '~/flash';
import store from './stores';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2018-05-09 12:01:36 +05:30
2020-03-09 13:42:32 +05:30
Vue.use(IdeRouter);
2018-05-09 12:01:36 +05:30
/**
* Routes below /-/ide/:
/project/h5bp/html5-boilerplate/blob/master
/project/h5bp/html5-boilerplate/blob/master/app/js/test.js
/project/h5bp/html5-boilerplate/mr/123
/project/h5bp/html5-boilerplate/mr/123/app/js/test.js
/workspace/123
/workspace/project/h5bp/html5-boilerplate/blob/my-special-branch
/workspace/project/h5bp/html5-boilerplate/mr/123
/ = /workspace
/settings
*/
// Unfortunately Vue Router doesn't work without at least a fake component
// If you do only data handling
const EmptyRouterComponent = {
render(createElement) {
return createElement('div');
},
};
2020-03-09 13:42:32 +05:30
const router = new IdeRouter({
2018-05-09 12:01:36 +05:30
mode: 'history',
2019-07-31 22:56:46 +05:30
base: joinPaths(gon.relative_url_root || '', '/-/ide/'),
2018-05-09 12:01:36 +05:30
routes: [
{
2018-11-18 11:00:15 +05:30
path: '/project/:namespace+/:project',
2018-05-09 12:01:36 +05:30
component: EmptyRouterComponent,
children: [
{
2018-11-18 11:00:15 +05:30
path: ':targetmode(edit|tree|blob)/:branchid+/-/*',
2018-05-09 12:01:36 +05:30
component: EmptyRouterComponent,
},
2018-11-18 11:00:15 +05:30
{
path: ':targetmode(edit|tree|blob)/:branchid+/',
2019-07-31 22:56:46 +05:30
redirect: to => joinPaths(to.path, '/-/'),
2018-11-18 11:00:15 +05:30
},
{
path: ':targetmode(edit|tree|blob)',
2019-07-31 22:56:46 +05:30
redirect: to => joinPaths(to.path, '/master/-/'),
2018-11-18 11:00:15 +05:30
},
2018-05-09 12:01:36 +05:30
{
path: 'merge_requests/:mrid',
component: EmptyRouterComponent,
},
2018-11-18 11:00:15 +05:30
{
path: '',
2019-07-31 22:56:46 +05:30
redirect: to => joinPaths(to.path, '/edit/master/-/'),
2018-11-18 11:00:15 +05:30
},
2018-05-09 12:01:36 +05:30
],
},
],
});
router.beforeEach((to, from, next) => {
if (to.params.namespace && to.params.project) {
store
.dispatch('getProjectData', {
namespace: to.params.namespace,
projectId: to.params.project,
})
.then(() => {
2019-03-02 22:35:43 +05:30
const basePath = to.params.pathMatch || '';
2018-11-20 20:47:30 +05:30
const projectId = `${to.params.namespace}/${to.params.project}`;
2018-11-18 11:00:15 +05:30
const branchId = to.params.branchid;
2018-11-20 20:47:30 +05:30
const mergeRequestId = to.params.mrid;
2018-11-08 19:23:39 +05:30
if (branchId) {
2018-11-20 20:47:30 +05:30
store.dispatch('openBranch', {
projectId,
2018-11-08 19:23:39 +05:30
branchId,
2018-11-20 20:47:30 +05:30
basePath,
});
} else if (mergeRequestId) {
store.dispatch('openMergeRequest', {
projectId,
mergeRequestId,
targetProjectId: to.query.target_project,
2018-05-09 12:01:36 +05:30
});
}
})
.catch(e => {
flash(
2019-07-31 22:56:46 +05:30
__('Error while loading the project data. Please try again.'),
2018-05-09 12:01:36 +05:30
'alert',
document,
null,
false,
true,
);
throw e;
});
}
next();
});
export default router;