ide_router.js 2.73 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1
import Vue from 'vue';
2
import IdeRouter from '~/ide/ide_router_extension';
3
import { joinPaths } from '~/lib/utils/url_utility';
Phil Hughes's avatar
Phil Hughes committed
4 5
import flash from '~/flash';
import store from './stores';
6
import { __ } from '~/locale';
Phil Hughes's avatar
Phil Hughes committed
7

8
Vue.use(IdeRouter);
Phil Hughes's avatar
Phil Hughes committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

/**
 * 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');
  },
};

36
const router = new IdeRouter({
Phil Hughes's avatar
Phil Hughes committed
37
  mode: 'history',
38
  base: joinPaths(gon.relative_url_root || '', '/-/ide/'),
Phil Hughes's avatar
Phil Hughes committed
39 40
  routes: [
    {
41
      path: '/project/:namespace+/:project',
Phil Hughes's avatar
Phil Hughes committed
42 43 44
      component: EmptyRouterComponent,
      children: [
        {
45
          path: ':targetmode(edit|tree|blob)/:branchid+/-/*',
Phil Hughes's avatar
Phil Hughes committed
46 47
          component: EmptyRouterComponent,
        },
48 49
        {
          path: ':targetmode(edit|tree|blob)/:branchid+/',
50
          redirect: to => joinPaths(to.path, '/-/'),
51 52 53
        },
        {
          path: ':targetmode(edit|tree|blob)',
54
          redirect: to => joinPaths(to.path, '/master/-/'),
55
        },
Phil Hughes's avatar
Phil Hughes committed
56
        {
Tim Zallmann's avatar
Tim Zallmann committed
57
          path: 'merge_requests/:mrid',
Phil Hughes's avatar
Phil Hughes committed
58 59
          component: EmptyRouterComponent,
        },
60 61
        {
          path: '',
62
          redirect: to => joinPaths(to.path, '/edit/master/-/'),
63
        },
Phil Hughes's avatar
Phil Hughes committed
64 65 66 67 68 69 70
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.params.namespace && to.params.project) {
71 72 73 74 75 76
    store
      .dispatch('getProjectData', {
        namespace: to.params.namespace,
        projectId: to.params.project,
      })
      .then(() => {
77
        const basePath = to.params.pathMatch || '';
78
        const projectId = `${to.params.namespace}/${to.params.project}`;
79
        const branchId = to.params.branchid;
80
        const mergeRequestId = to.params.mrid;
81 82

        if (branchId) {
83 84
          store.dispatch('openBranch', {
            projectId,
85
            branchId,
86 87 88 89 90 91 92
            basePath,
          });
        } else if (mergeRequestId) {
          store.dispatch('openMergeRequest', {
            projectId,
            mergeRequestId,
            targetProjectId: to.query.target_project,
93 94 95 96 97
          });
        }
      })
      .catch(e => {
        flash(
98
          __('Error while loading the project data. Please try again.'),
99 100 101 102 103 104 105 106
          'alert',
          document,
          null,
          false,
          true,
        );
        throw e;
      });
Phil Hughes's avatar
Phil Hughes committed
107 108 109 110 111 112
  }

  next();
});

export default router;