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

130 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import Vue from 'vue';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2020-03-13 15:44:24 +05:30
import IdeRouter from '~/ide/ide_router_extension';
2019-07-31 22:56:46 +05:30
import { joinPaths } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
2021-02-22 17:27:13 +05:30
import {
WEBIDE_MARK_FETCH_PROJECT_DATA_START,
WEBIDE_MARK_FETCH_PROJECT_DATA_FINISH,
WEBIDE_MEASURE_FETCH_PROJECT_DATA,
} from '~/performance/constants';
2021-03-11 19:13:27 +05:30
import { performanceMarkAndMeasure } from '~/performance/utils';
2020-06-23 00:09:42 +05:30
import { syncRouterAndStore } from './sync_router_and_store';
2018-05-09 12:01:36 +05:30
2020-03-13 15:44:24 +05:30
Vue.use(IdeRouter);
2018-05-09 12:01:36 +05:30
/**
* Routes below /-/ide/:
2021-09-04 01:27:46 +05:30
/project/h5bp/html5-boilerplate/blob/main
/project/h5bp/html5-boilerplate/blob/main/app/js/test.js
2018-05-09 12:01:36 +05:30
/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');
},
};
2021-09-04 01:27:46 +05:30
export const createRouter = (store, defaultBranch) => {
2020-06-23 00:09:42 +05:30
const router = new IdeRouter({
mode: 'history',
base: joinPaths(gon.relative_url_root || '', '/-/ide/'),
routes: [
{
path: '/project/:namespace+/:project',
component: EmptyRouterComponent,
children: [
{
path: ':targetmode(edit|tree|blob)/:branchid+/-/*',
component: EmptyRouterComponent,
},
{
path: ':targetmode(edit|tree|blob)/:branchid+/',
2021-03-08 18:12:59 +05:30
redirect: (to) => joinPaths(to.path, '/-/'),
2020-06-23 00:09:42 +05:30
},
{
path: ':targetmode(edit|tree|blob)',
2021-09-04 01:27:46 +05:30
redirect: (to) => joinPaths(to.path, `/${defaultBranch}/-/`),
2020-06-23 00:09:42 +05:30
},
{
path: 'merge_requests/:mrid',
component: EmptyRouterComponent,
},
{
path: '',
2021-09-04 01:27:46 +05:30
redirect: (to) => joinPaths(to.path, `/edit/${defaultBranch}/-/`),
2020-06-23 00:09:42 +05:30
},
],
},
],
});
2018-05-09 12:01:36 +05:30
2020-06-23 00:09:42 +05:30
router.beforeEach((to, from, next) => {
if (to.params.namespace && to.params.project) {
2021-02-22 17:27:13 +05:30
performanceMarkAndMeasure({ mark: WEBIDE_MARK_FETCH_PROJECT_DATA_START });
2020-06-23 00:09:42 +05:30
store
.dispatch('getProjectData', {
namespace: to.params.namespace,
projectId: to.params.project,
})
.then(() => {
const basePath = to.params.pathMatch || '';
const projectId = `${to.params.namespace}/${to.params.project}`;
const branchId = to.params.branchid;
const mergeRequestId = to.params.mrid;
2018-11-08 19:23:39 +05:30
2020-06-23 00:09:42 +05:30
if (branchId) {
2021-02-22 17:27:13 +05:30
performanceMarkAndMeasure({
mark: WEBIDE_MARK_FETCH_PROJECT_DATA_FINISH,
measures: [
{
name: WEBIDE_MEASURE_FETCH_PROJECT_DATA,
start: WEBIDE_MARK_FETCH_PROJECT_DATA_START,
},
],
});
2020-06-23 00:09:42 +05:30
store.dispatch('openBranch', {
projectId,
branchId,
basePath,
});
} else if (mergeRequestId) {
store.dispatch('openMergeRequest', {
projectId,
mergeRequestId,
targetProjectId: to.query.target_project,
});
}
})
2021-03-08 18:12:59 +05:30
.catch((e) => {
2021-09-30 23:02:18 +05:30
createFlash({
message: __('Error while loading the project data. Please try again.'),
fadeTransition: false,
addBodyClass: true,
});
2020-06-23 00:09:42 +05:30
throw e;
});
}
2018-05-09 12:01:36 +05:30
2020-06-23 00:09:42 +05:30
next();
});
2018-05-09 12:01:36 +05:30
2020-06-23 00:09:42 +05:30
syncRouterAndStore(router, store);
return router;
};