ide_router.js 2.99 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1 2 3 4 5 6 7 8 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import Vue from 'vue';
import VueRouter from 'vue-router';
import flash from '~/flash';
import store from './stores';

Vue.use(VueRouter);

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

const router = new VueRouter({
  mode: 'history',
  base: `${gon.relative_url_root}/-/ide/`,
  routes: [
    {
      path: '/project/:namespace/:project',
      component: EmptyRouterComponent,
      children: [
        {
          path: ':targetmode/:branch/*',
          component: EmptyRouterComponent,
        },
        {
          path: 'mr/:mrid',
          component: EmptyRouterComponent,
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.params.namespace && to.params.project) {
57 58 59 60 61 62 63
    store
      .dispatch('getProjectData', {
        namespace: to.params.namespace,
        projectId: to.params.project,
      })
      .then(() => {
        const fullProjectId = `${to.params.namespace}/${to.params.project}`;
Phil Hughes's avatar
Phil Hughes committed
64

65 66 67 68 69
        if (to.params.branch) {
          store.dispatch('getBranchData', {
            projectId: fullProjectId,
            branchId: to.params.branch,
          });
Phil Hughes's avatar
Phil Hughes committed
70

71 72 73 74 75 76 77 78
          store
            .dispatch('getFiles', {
              projectId: fullProjectId,
              branchId: to.params.branch,
            })
            .then(() => {
              if (to.params[0]) {
                const path =
Phil Hughes's avatar
Phil Hughes committed
79 80 81 82 83 84 85 86 87 88 89
                  to.params[0].slice(-1) === '/' ? to.params[0].slice(0, -1) : to.params[0];
                const treeEntry = Object.keys(store.state.entries).reduce((acc, key) => {
                  const file = store.state.entries[key];
                  if (key === path && !file.pending) {
                    return file;
                  }

                  return acc;
                }, {});

                if (Object.keys(treeEntry).length) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                  store.dispatch('handleTreeEntryAction', treeEntry);
                }
              }
            })
            .catch(e => {
              flash(
                'Error while loading the branch files. Please try again.',
                'alert',
                document,
                null,
                false,
                true,
              );
              throw e;
            });
        }
      })
      .catch(e => {
        flash(
          'Error while loading the project data. Please try again.',
          'alert',
          document,
          null,
          false,
          true,
        );
        throw e;
      });
Phil Hughes's avatar
Phil Hughes committed
118 119 120 121 122 123
  }

  next();
});

export default router;