2021-02-14 13:09:20 -05:00
|
|
|
import { escapeRegExp } from 'lodash';
|
2019-05-20 04:41:28 -04:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueRouter from 'vue-router';
|
2021-10-11 02:13:09 -04:00
|
|
|
import { joinPaths, webIDEUrl } from '~/lib/utils/url_utility';
|
2021-03-29 08:09:14 -04:00
|
|
|
import BlobPage from './pages/blob.vue';
|
2019-05-20 04:41:28 -04:00
|
|
|
import IndexPage from './pages/index.vue';
|
|
|
|
import TreePage from './pages/tree.vue';
|
|
|
|
|
|
|
|
Vue.use(VueRouter);
|
|
|
|
|
|
|
|
export default function createRouter(base, baseRef) {
|
2020-05-14 11:08:14 -04:00
|
|
|
const treePathRoute = {
|
|
|
|
component: TreePage,
|
2020-12-23 19:10:25 -05:00
|
|
|
props: (route) => ({
|
2020-05-14 11:08:14 -04:00
|
|
|
path: route.params.path?.replace(/^\//, '') || '/',
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2021-03-29 08:09:14 -04:00
|
|
|
const blobPathRoute = {
|
|
|
|
component: BlobPage,
|
|
|
|
props: (route) => ({
|
|
|
|
path: route.params.path,
|
2021-05-03 02:10:30 -04:00
|
|
|
projectPath: base,
|
2021-03-29 08:09:14 -04:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2021-10-11 02:13:09 -04:00
|
|
|
const router = new VueRouter({
|
2019-05-20 04:41:28 -04:00
|
|
|
mode: 'history',
|
|
|
|
base: joinPaths(gon.relative_url_root || '', base),
|
|
|
|
routes: [
|
|
|
|
{
|
2020-05-14 11:08:14 -04:00
|
|
|
name: 'treePathDecoded',
|
|
|
|
// Sometimes the ref needs decoding depending on how the backend sends it to us
|
|
|
|
path: `(/-)?/tree/${decodeURI(baseRef)}/:path*`,
|
|
|
|
...treePathRoute,
|
|
|
|
},
|
|
|
|
{
|
2019-05-20 04:41:28 -04:00
|
|
|
name: 'treePath',
|
2020-05-14 11:08:14 -04:00
|
|
|
// Support without decoding as well just in case the ref doesn't need to be decoded
|
2020-09-07 17:08:26 -04:00
|
|
|
path: `(/-)?/tree/${escapeRegExp(baseRef)}/:path*`,
|
2020-05-14 11:08:14 -04:00
|
|
|
...treePathRoute,
|
2019-05-20 04:41:28 -04:00
|
|
|
},
|
2021-03-29 08:09:14 -04:00
|
|
|
{
|
|
|
|
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-05-24 05:39:18 -04:00
|
|
|
{
|
|
|
|
path: '/',
|
|
|
|
name: 'projectRoot',
|
|
|
|
component: IndexPage,
|
|
|
|
},
|
2019-05-20 04:41:28 -04:00
|
|
|
],
|
|
|
|
});
|
2021-10-11 02:13:09 -04:00
|
|
|
|
|
|
|
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-05-20 04:41:28 -04:00
|
|
|
}
|