dispatcher.js 10.7 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-arrow-callback, wrap-iife, no-shadow, consistent-return, one-var, one-var-declaration-per-line, camelcase, default-case, no-new, quotes, no-duplicate-case, no-case-declarations, no-fallthrough, max-len */
2
import Flash from './flash';
3
import GfmAutoComplete from './gfm_auto_complete';
Clement Ho's avatar
Clement Ho committed
4
import { convertPermissionToBoolean } from './lib/utils/common_utils';
5
import GlFieldErrors from './gl_field_errors';
6
import Shortcuts from './shortcuts';
7
import SearchAutocomplete from './search_autocomplete';
8

9
var Dispatcher;
Fatih Acet's avatar
Fatih Acet committed
10

11
(function() {
Fatih Acet's avatar
Fatih Acet committed
12 13 14
  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
15
      this.initFieldErrors();
Fatih Acet's avatar
Fatih Acet committed
16 17 18 19
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
20
      var path, shortcut_handler;
21
      const page = $('body').attr('data-page');
Fatih Acet's avatar
Fatih Acet committed
22 23 24
      if (!page) {
        return false;
      }
25

26
      const fail = () => Flash('Error loading dynamic module');
27
      const callDefault = m => m.default();
28

Fatih Acet's avatar
Fatih Acet committed
29 30
      path = page.split(':');
      shortcut_handler = null;
31

32
      $('.js-gfm-input:not(.js-vue-textarea)').each((i, el) => {
33
        const gfm = new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources);
34
        const enableGFM = convertPermissionToBoolean(el.dataset.supportsAutocomplete);
35 36 37 38 39 40 41 42 43
        gfm.setup($(el), {
          emojis: true,
          members: enableGFM,
          issues: enableGFM,
          milestones: enableGFM,
          mergeRequests: enableGFM,
          labels: enableGFM,
        });
      });
44

Fatih Acet's avatar
Fatih Acet committed
45
      switch (page) {
46
        case 'projects:merge_requests:index':
47
        case 'projects:issues:index':
Fatih Acet's avatar
Fatih Acet committed
48 49 50
        case 'projects:issues:show':
        case 'projects:issues:new':
        case 'projects:issues:edit':
51 52
        case 'projects:merge_requests:creations:new':
        case 'projects:merge_requests:creations:diffs':
Fatih Acet's avatar
Fatih Acet committed
53 54 55
        case 'projects:merge_requests:edit':
        case 'projects:merge_requests:show':
        case 'projects:commit:show':
56 57
        case 'projects:activity':
        case 'projects:commits:show':
Fatih Acet's avatar
Fatih Acet committed
58
        case 'projects:show':
Clement Ho's avatar
Clement Ho committed
59
          shortcut_handler = true;
Fatih Acet's avatar
Fatih Acet committed
60 61
          break;
        case 'groups:activity':
62 63 64
          import('./pages/groups/activity')
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
65 66
          break;
        case 'groups:show':
67
          shortcut_handler = true;
Fatih Acet's avatar
Fatih Acet committed
68 69
          break;
        case 'groups:group_members:index':
70 71 72
          import('./pages/groups/group_members/index')
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
73
          break;
74
        case 'projects:project_members:index':
75
          import('./pages/projects/project_members')
76 77
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
78
          break;
79
        case 'groups:create':
80 81 82 83 84 85 86 87 88
        case 'groups:new':
          import('./pages/groups/new')
            .then(callDefault)
            .catch(fail);
          break;
        case 'groups:edit':
          import('./pages/groups/edit')
            .then(callDefault)
            .catch(fail);
89
          break;
Phil Hughes's avatar
Phil Hughes committed
90 91
        case 'admin:groups:create':
        case 'admin:groups:new':
Phil Hughes's avatar
Phil Hughes committed
92 93 94
          import('./pages/admin/groups/new')
            .then(callDefault)
            .catch(fail);
Phil Hughes's avatar
Phil Hughes committed
95
          break;
Fatih Acet's avatar
Fatih Acet committed
96
        case 'admin:groups:edit':
Phil Hughes's avatar
Phil Hughes committed
97 98 99
          import('./pages/admin/groups/edit')
            .then(callDefault)
            .catch(fail);
Phil Hughes's avatar
Phil Hughes committed
100
          break;
101
        case 'projects:tree:show':
102
          import('./pages/projects/tree/show')
103
            .then(callDefault)
104
            .catch(fail);
105
          shortcut_handler = true;
106
          break;
Fatih Acet's avatar
Fatih Acet committed
107
        case 'projects:find_file:show':
108 109 110
          import('./pages/projects/find_file/show')
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
111 112
          shortcut_handler = true;
          break;
113
        case 'projects:blob:show':
114 115 116 117
          import('./pages/projects/blob/show')
            .then(callDefault)
            .catch(fail);
          shortcut_handler = true;
118
          break;
Fatih Acet's avatar
Fatih Acet committed
119
        case 'projects:blame:show':
120 121 122 123
          import('./pages/projects/blame/show')
            .then(callDefault)
            .catch(fail);
          shortcut_handler = true;
Fatih Acet's avatar
Fatih Acet committed
124
          break;
125
        case 'groups:labels:new':
126 127 128 129
          import('./pages/groups/labels/new')
            .then(callDefault)
            .catch(fail);
          break;
130
        case 'groups:labels:edit':
131 132 133
          import('./pages/groups/labels/edit')
            .then(callDefault)
            .catch(fail);
Clement Ho's avatar
Clement Ho committed
134
          break;
Fatih Acet's avatar
Fatih Acet committed
135
        case 'projects:labels:new':
Jacob Schatz's avatar
Jacob Schatz committed
136 137 138 139
          import('./pages/projects/labels/new')
            .then(callDefault)
            .catch(fail);
          break;
Fatih Acet's avatar
Fatih Acet committed
140
        case 'projects:labels:edit':
Clement Ho's avatar
Clement Ho committed
141
          import('./pages/projects/labels/edit')
Jacob Schatz's avatar
Jacob Schatz committed
142 143
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
144
          break;
145 146 147 148 149
        case 'groups:labels:index':
          import('./pages/groups/labels/index')
            .then(callDefault)
            .catch(fail);
          break;
Fatih Acet's avatar
Fatih Acet committed
150
        case 'projects:labels:index':
Jacob Schatz's avatar
Jacob Schatz committed
151 152 153 154
          import('./pages/projects/labels/index')
            .then(callDefault)
            .catch(fail);
          break;
Fatih Acet's avatar
Fatih Acet committed
155
        case 'projects:network:show':
156 157
          // Ensure we don't create a particular shortcut handler here. This is
          // already created, where the network graph is created.
Fatih Acet's avatar
Fatih Acet committed
158 159 160
          shortcut_handler = true;
          break;
        case 'projects:forks:new':
161
          import('./pages/projects/forks/new')
Jacob Schatz's avatar
Jacob Schatz committed
162 163
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
164 165
          break;
        case 'projects:artifacts:browse':
166
          import('./pages/projects/artifacts/browse')
167 168
            .then(callDefault)
            .catch(fail);
169
          shortcut_handler = true;
Fatih Acet's avatar
Fatih Acet committed
170
          break;
171
        case 'projects:artifacts:file':
172
          import('./pages/projects/artifacts/file')
173 174
            .then(callDefault)
            .catch(fail);
175
          shortcut_handler = true;
176
          break;
Fatih Acet's avatar
Fatih Acet committed
177
        case 'search:show':
178 179 180
          import('./pages/search/show')
            .then(callDefault)
            .catch(fail);
181
          break;
182
        case 'projects:settings:repository:show':
183 184 185
          import('./pages/projects/settings/repository/show')
            .then(callDefault)
            .catch(fail);
186
          break;
187
        case 'projects:settings:ci_cd:show':
188 189 190 191
          import('./pages/projects/settings/ci_cd/show')
            .then(callDefault)
            .catch(fail);
          break;
Shinya Maeda's avatar
Shinya Maeda committed
192
        case 'groups:settings:ci_cd:show':
193 194 195
          import('./pages/groups/settings/ci_cd/show')
            .then(callDefault)
            .catch(fail);
196
          break;
197 198
        case 'ci:lints:create':
        case 'ci:lints:show':
Clement Ho's avatar
Clement Ho committed
199 200 201
          import('./pages/ci/lints')
            .then(callDefault)
            .catch(fail);
202
          break;
203
        case 'admin:conversational_development_index:show':
Clement Ho's avatar
Clement Ho committed
204 205 206
          import('./pages/admin/conversational_development_index/show')
            .then(callDefault)
            .catch(fail);
207
          break;
208
        case 'import:fogbugz:new_user_map':
Clement Ho's avatar
Clement Ho committed
209 210 211
          import('./pages/import/fogbugz/new_user_map')
            .then(callDefault)
            .catch(fail);
212
          break;
213
        case 'profiles:personal_access_tokens:index':
214 215 216 217
          import('./pages/profiles/personal_access_tokens')
            .then(callDefault)
            .catch(fail);
          break;
218
        case 'admin:impersonation_tokens:index':
Phil Hughes's avatar
Phil Hughes committed
219 220 221
          import('./pages/admin/impersonation_tokens')
            .then(callDefault)
            .catch(fail);
Phil Hughes's avatar
Phil Hughes committed
222
          break;
223 224 225 226 227
        case 'dashboard:groups:index':
          import('./pages/dashboard/groups/index')
            .then(callDefault)
            .catch(fail);
          break;
Fatih Acet's avatar
Fatih Acet committed
228
      }
229
      switch (path[0]) {
Fatih Acet's avatar
Fatih Acet committed
230 231
        case 'admin':
          switch (path[1]) {
232
            case 'broadcast_messages':
Phil Hughes's avatar
Phil Hughes committed
233 234 235
              import('./pages/admin/broadcast_messages')
                .then(callDefault)
                .catch(fail);
236
              break;
237
            case 'cohorts':
Phil Hughes's avatar
Phil Hughes committed
238 239 240
              import('./pages/admin/cohorts')
                .then(callDefault)
                .catch(fail);
241
              break;
Fatih Acet's avatar
Fatih Acet committed
242
            case 'groups':
Phil Hughes's avatar
Phil Hughes committed
243 244
              switch (path[2]) {
                case 'show':
Phil Hughes's avatar
Phil Hughes committed
245 246 247
                  import('./pages/admin/groups/show')
                    .then(callDefault)
                    .catch(fail);
Phil Hughes's avatar
Phil Hughes committed
248 249
                  break;
              }
Fatih Acet's avatar
Fatih Acet committed
250 251
              break;
            case 'projects':
Phil Hughes's avatar
Phil Hughes committed
252 253 254
              import('./pages/admin/projects')
                .then(callDefault)
                .catch(fail);
255 256
              break;
            case 'labels':
257
              switch (path[2]) {
258
                case 'new':
Phil Hughes's avatar
Phil Hughes committed
259 260 261
                  import('./pages/admin/labels/new')
                    .then(callDefault)
                    .catch(fail);
Phil Hughes's avatar
Phil Hughes committed
262
                  break;
263
                case 'edit':
Phil Hughes's avatar
Phil Hughes committed
264 265 266
                  import('./pages/admin/labels/edit')
                    .then(callDefault)
                    .catch(fail);
Phil Hughes's avatar
Phil Hughes committed
267
                  break;
268
              }
269
            case 'abuse_reports':
Phil Hughes's avatar
Phil Hughes committed
270 271 272
              import('./pages/admin/abuse_reports')
                .then(callDefault)
                .catch(fail);
273
              break;
Fatih Acet's avatar
Fatih Acet committed
274 275 276
          }
          break;
        case 'profiles':
277
          import('./pages/profiles/index')
278 279
            .then(callDefault)
            .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
280 281
          break;
        case 'projects':
282 283 284 285
          import('./pages/projects')
            .then(callDefault)
            .catch(fail);
          shortcut_handler = true;
Fatih Acet's avatar
Fatih Acet committed
286 287
          switch (path[1]) {
            case 'compare':
288 289 290
              import('./pages/projects/compare')
                .then(callDefault)
                .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
291
              break;
292 293 294
            case 'create':
            case 'new':
              import('./pages/projects/new')
Jacob Schatz's avatar
Jacob Schatz committed
295 296
                .then(callDefault)
                .catch(fail);
Fatih Acet's avatar
Fatih Acet committed
297 298
              break;
            case 'wikis':
299 300 301 302
              import('./pages/projects/wikis')
                .then(callDefault)
                .catch(fail);
              shortcut_handler = true;
Fatih Acet's avatar
Fatih Acet committed
303 304
              break;
          }
305
          break;
Fatih Acet's avatar
Fatih Acet committed
306
      }
307
      // If we haven't installed a custom shortcut handler, install the default one
Fatih Acet's avatar
Fatih Acet committed
308
      if (!shortcut_handler) {
309
        new Shortcuts();
Fatih Acet's avatar
Fatih Acet committed
310
      }
311 312

      if (document.querySelector('#peek')) {
313 314 315
        import('./performance_bar')
          .then(m => new m.default({ container: '#peek' })) // eslint-disable-line new-cap
          .catch(fail);
316
      }
Fatih Acet's avatar
Fatih Acet committed
317 318 319
    };

    Dispatcher.prototype.initSearch = function() {
320
      // Only when search form is present
Fatih Acet's avatar
Fatih Acet committed
321
      if ($('.search').length) {
322
        return new SearchAutocomplete();
Fatih Acet's avatar
Fatih Acet committed
323 324 325
      }
    };

326
    Dispatcher.prototype.initFieldErrors = function() {
327
      $('.gl-show-field-errors').each((i, form) => {
328
        new GlFieldErrors(form);
329
      });
330 331
    };

Fatih Acet's avatar
Fatih Acet committed
332 333
    return Dispatcher;
  })();
334
})();
335

336 337 338
export default function initDispatcher() {
  return new Dispatcher();
}