diff options
Diffstat (limited to 'app/assets/javascripts/ide/ide_router.js')
-rw-r--r-- | app/assets/javascripts/ide/ide_router.js | 153 |
1 files changed, 79 insertions, 74 deletions
diff --git a/app/assets/javascripts/ide/ide_router.js b/app/assets/javascripts/ide/ide_router.js index 0fab3ee0f3b..152f77effa3 100644 --- a/app/assets/javascripts/ide/ide_router.js +++ b/app/assets/javascripts/ide/ide_router.js @@ -2,8 +2,8 @@ import Vue from 'vue'; import IdeRouter from '~/ide/ide_router_extension'; import { joinPaths } from '~/lib/utils/url_utility'; import flash from '~/flash'; -import store from './stores'; import { __ } from '~/locale'; +import { syncRouterAndStore } from './sync_router_and_store'; Vue.use(IdeRouter); @@ -33,80 +33,85 @@ const EmptyRouterComponent = { }, }; -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+/', - redirect: to => joinPaths(to.path, '/-/'), - }, - { - path: ':targetmode(edit|tree|blob)', - redirect: to => joinPaths(to.path, '/master/-/'), - }, - { - path: 'merge_requests/:mrid', - component: EmptyRouterComponent, - }, - { - path: '', - redirect: to => joinPaths(to.path, '/edit/master/-/'), - }, - ], - }, - ], -}); +// eslint-disable-next-line import/prefer-default-export +export const createRouter = store => { + 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+/', + redirect: to => joinPaths(to.path, '/-/'), + }, + { + path: ':targetmode(edit|tree|blob)', + redirect: to => joinPaths(to.path, '/master/-/'), + }, + { + path: 'merge_requests/:mrid', + component: EmptyRouterComponent, + }, + { + path: '', + redirect: to => joinPaths(to.path, '/edit/master/-/'), + }, + ], + }, + ], + }); -router.beforeEach((to, from, next) => { - if (to.params.namespace && to.params.project) { - 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; + router.beforeEach((to, from, next) => { + if (to.params.namespace && to.params.project) { + 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; - if (branchId) { - store.dispatch('openBranch', { - projectId, - branchId, - basePath, - }); - } else if (mergeRequestId) { - store.dispatch('openMergeRequest', { - projectId, - mergeRequestId, - targetProjectId: to.query.target_project, - }); - } - }) - .catch(e => { - flash( - __('Error while loading the project data. Please try again.'), - 'alert', - document, - null, - false, - true, - ); - throw e; - }); - } + if (branchId) { + store.dispatch('openBranch', { + projectId, + branchId, + basePath, + }); + } else if (mergeRequestId) { + store.dispatch('openMergeRequest', { + projectId, + mergeRequestId, + targetProjectId: to.query.target_project, + }); + } + }) + .catch(e => { + flash( + __('Error while loading the project data. Please try again.'), + 'alert', + document, + null, + false, + true, + ); + throw e; + }); + } - next(); -}); + next(); + }); -export default router; + syncRouterAndStore(router, store); + + return router; +}; |