debian-mirror-gitlab/app/assets/javascripts/repository/router.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { escapeRegExp } from 'lodash';
2019-09-04 21:01:54 +05:30
import Vue from 'vue';
import VueRouter from 'vue-router';
2021-11-18 22:05:49 +05:30
import { joinPaths, webIDEUrl } from '~/lib/utils/url_utility';
2021-04-29 21:17:54 +05:30
import BlobPage from './pages/blob.vue';
2019-09-04 21:01:54 +05:30
import IndexPage from './pages/index.vue';
import TreePage from './pages/tree.vue';
Vue.use(VueRouter);
export default function createRouter(base, baseRef) {
2020-05-24 23:13:21 +05:30
const treePathRoute = {
component: TreePage,
2021-03-08 18:12:59 +05:30
props: (route) => ({
2020-05-24 23:13:21 +05:30
path: route.params.path?.replace(/^\//, '') || '/',
}),
};
2021-04-29 21:17:54 +05:30
const blobPathRoute = {
component: BlobPage,
props: (route) => ({
path: route.params.path,
2021-06-08 01:23:25 +05:30
projectPath: base,
2021-04-29 21:17:54 +05:30
}),
};
2021-11-18 22:05:49 +05:30
const router = new VueRouter({
2019-09-04 21:01:54 +05:30
mode: 'history',
base: joinPaths(gon.relative_url_root || '', base),
routes: [
{
2020-05-24 23:13:21 +05:30
name: 'treePathDecoded',
// Sometimes the ref needs decoding depending on how the backend sends it to us
path: `(/-)?/tree/${decodeURI(baseRef)}/:path*`,
...treePathRoute,
},
{
2019-09-04 21:01:54 +05:30
name: 'treePath',
2020-05-24 23:13:21 +05:30
// Support without decoding as well just in case the ref doesn't need to be decoded
2020-11-24 15:15:51 +05:30
path: `(/-)?/tree/${escapeRegExp(baseRef)}/:path*`,
2020-05-24 23:13:21 +05:30
...treePathRoute,
2019-09-04 21:01:54 +05:30
},
2021-04-29 21:17:54 +05:30
{
name: 'blobPathDecoded',
// Sometimes the ref needs decoding depending on how the backend sends it to us
path: `(/-)?/blob/${decodeURI(baseRef)}/:path*`,
...blobPathRoute,
},
{
name: 'blobPath',
// Support without decoding as well just in case the ref doesn't need to be decoded
path: `(/-)?/blob/${escapeRegExp(baseRef)}/:path*`,
...blobPathRoute,
},
2019-09-04 21:01:54 +05:30
{
path: '/',
name: 'projectRoot',
component: IndexPage,
},
],
});
2021-11-18 22:05:49 +05:30
router.afterEach((to) => {
const needsClosingSlash = !to.name.includes('blobPath');
window.gl.webIDEPath = webIDEUrl(
joinPaths(
'/',
base,
'edit',
decodeURI(baseRef),
'-',
to.params.path || '',
needsClosingSlash && '/',
),
);
});
return router;
2019-09-04 21:01:54 +05:30
}