router.js 3.44 KB
Newer Older
1 2
import Vue from 'vue';
import VueRouter from 'vue-router';
3
import { __, s__ } from '~/locale';
4 5
import IterationCadenceForm from './components/iteration_cadence_form.vue';
import IterationCadenceList from './components/iteration_cadences_list.vue';
6
import IterationForm from './components/iteration_form.vue';
Simon Knox's avatar
Simon Knox committed
7
import IterationReport from './components/iteration_report.vue';
8 9 10

Vue.use(VueRouter);

11 12 13 14 15
function checkPermission(permission) {
  return (to, from, next) => {
    if (permission) {
      next();
    } else {
16
      next({ name: 'index' });
17 18 19 20
    }
  };
}

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
function renderChildren(children) {
  return {
    component: {
      render(createElement) {
        return createElement('router-view');
      },
    },
    children: [
      ...children,
      {
        path: '*',
        redirect: '/',
      },
    ],
  };
}

38 39 40 41
export default function createRouter({ base, permissions = {} }) {
  const routes = [
    {
      path: '/',
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      meta: {
        breadcrumb: s__('Iterations|Iteration cadences'),
      },
      ...renderChildren([
        {
          name: 'index',
          path: '',
          component: IterationCadenceList,
        },
        {
          name: 'new',
          path: 'new',
          component: IterationCadenceForm,
          beforeEnter: checkPermission(permissions.canCreateCadence),
          meta: {
            breadcrumb: s__('Iterations|New iteration cadence'),
          },
        },
        {
          path: '/:cadenceId',
          ...renderChildren([
            {
              name: 'cadence',
              path: '/:cadenceId',
              redirect: '/',
            },
            {
              name: 'edit',
              path: '/:cadenceId/edit',
              component: IterationCadenceForm,
              beforeEnter: checkPermission(permissions.canEditCadence),
              meta: {
                breadcrumb: __('Edit'),
              },
            },
            {
              path: 'iterations',
              meta: {
                breadcrumb: __('Iterations'),
              },
              ...renderChildren([
                {
                  name: 'iterations',
                  path: '/:cadenceId/iterations',
                  redirect: '/',
                },
                {
                  name: 'newIteration',
                  path: '/:cadenceId/iterations/new',
                  component: IterationForm,
                  beforeEnter: checkPermission(permissions.canCreateIteration),
                  meta: {
                    breadcrumb: s__('Iterations|New iteration'),
                  },
                },
                {
                  path: ':iterationId',
                  ...renderChildren([
                    {
                      name: 'iteration',
                      path: '/:cadenceId/iterations/:iterationId',
                      component: IterationReport,
                    },
                    {
                      name: 'editIteration',
107
                      path: '/:cadenceId/iterations/:iterationId/edit',
108 109 110 111 112 113 114 115 116 117 118 119 120
                      component: IterationForm,
                      beforeEnter: checkPermission(permissions.canEditIteration),
                      meta: {
                        breadcrumb: __('Edit'),
                      },
                    },
                  ]),
                },
              ]),
            },
          ]),
        },
      ]),
121 122
    },
  ];
123 124 125 126 127 128 129 130 131

  const router = new VueRouter({
    base,
    mode: 'history',
    routes,
  });

  return router;
}