dispatcher.js 12.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 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
/* global UsernameValidator */
/* global ActiveTabMemoizer */
/* global ShortcutsNavigation */
/* global Build */
/* global Issuable */
/* global Issue */
/* global ShortcutsIssuable */
/* global ZenMode */
/* global Milestone */
/* global IssuableForm */
/* global LabelsSelect */
/* global MilestoneSelect */
/* global MergedButtons */
/* global Commit */
/* global NotificationsForm */
/* global TreeView */
/* global NotificationsDropdown */
/* global UsersSelect */
/* global GroupAvatar */
/* global LineHighlighter */
/* global ProjectFork */
/* global BuildArtifacts */
/* global GroupsSelect */
/* global Search */
/* global Admin */
/* global NamespaceSelects */
/* global ShortcutsDashboardNavigation */
/* global Project */
/* global ProjectAvatar */
/* global CompareAutocomplete */
/* global ProjectNew */
/* global Star */
/* global ProjectShow */
/* global Labels */
/* global Shortcuts */

F
Filipa Lacerda 已提交
38
import GroupsList from './groups_list';
39
import ProjectsList from './projects_list';
F
Filipa Lacerda 已提交
40

41
const ShortcutsBlob = require('./shortcuts_blob');
42
const UserCallout = require('./user_callout');
43

F
Fatih Acet 已提交
44 45 46 47 48 49 50 51 52 53
(function() {
  var Dispatcher;

  $(function() {
    return new Dispatcher();
  });

  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
54
      this.initFieldErrors();
F
Fatih Acet 已提交
55 56 57 58 59 60 61 62 63 64 65 66
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
      var page, path, shortcut_handler;
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
      path = page.split(':');
      shortcut_handler = null;
      switch (page) {
67 68
        case 'sessions:new':
          new UsernameValidator();
69
          new ActiveTabMemoizer();
70
          break;
71
        case 'projects:boards:show':
72
        case 'projects:boards:index':
73 74
          shortcut_handler = new ShortcutsNavigation();
          break;
75 76 77
        case 'projects:builds:show':
          new Build();
          break;
78
        case 'projects:merge_requests:index':
F
Fatih Acet 已提交
79
        case 'projects:issues:index':
C
Clement Ho 已提交
80
          if (gl.FilteredSearchManager) {
C
Clement Ho 已提交
81
            new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
C
Clement Ho 已提交
82
          }
F
Fatih Acet 已提交
83
          Issuable.init();
84
          new gl.IssuableBulkActions({
A
Alfredo Sumaran 已提交
85
            prefixId: page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_',
86
          });
F
Fatih Acet 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
          shortcut_handler = new ShortcutsNavigation();
          break;
        case 'projects:issues:show':
          new Issue();
          shortcut_handler = new ShortcutsIssuable();
          new ZenMode();
          break;
        case 'projects:milestones:show':
        case 'groups:milestones:show':
        case 'dashboard:milestones:show':
          new Milestone();
          break;
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
100
          new gl.Todos();
F
Fatih Acet 已提交
101
          break;
102 103 104 105 106 107 108 109
        case 'dashboard:projects:index':
        case 'dashboard:projects:starred':
        case 'explore:projects:index':
        case 'explore:projects:trending':
        case 'explore:projects:starred':
        case 'admin:projects:index':
          new ProjectsList();
          break;
F
Filipa Lacerda 已提交
110 111 112 113
        case 'dashboard:groups:index':
        case 'explore:groups:index':
          new GroupsList();
          break;
F
Fatih Acet 已提交
114 115
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
116
        case 'projects:milestones:update':
F
Fatih Acet 已提交
117
          new ZenMode();
118
          new gl.DueDateSelectors();
L
Luke "Jared" Bennett 已提交
119
          new gl.GLForm($('.milestone-form'));
F
Fatih Acet 已提交
120 121 122 123 124
          break;
        case 'groups:milestones:new':
          new ZenMode();
          break;
        case 'projects:compare:show':
M
Mike Greiling 已提交
125
          new gl.Diff();
F
Fatih Acet 已提交
126
          break;
127 128 129
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
          break;
F
Fatih Acet 已提交
130 131 132
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
133
          new gl.GLForm($('.issue-form'));
F
Fatih Acet 已提交
134
          new IssuableForm($('.issue-form'));
135 136
          new LabelsSelect();
          new MilestoneSelect();
137
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
138 139
          break;
        case 'projects:merge_requests:new':
140
        case 'projects:merge_requests:new_diffs':
F
Fatih Acet 已提交
141
        case 'projects:merge_requests:edit':
M
Mike Greiling 已提交
142
          new gl.Diff();
F
Fatih Acet 已提交
143
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
144
          new gl.GLForm($('.merge-request-form'));
F
Fatih Acet 已提交
145
          new IssuableForm($('.merge-request-form'));
146 147
          new LabelsSelect();
          new MilestoneSelect();
148
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
149 150 151
          break;
        case 'projects:tags:new':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
152
          new gl.GLForm($('.tag-form'));
F
Fatih Acet 已提交
153 154 155
          break;
        case 'projects:releases:edit':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
156
          new gl.GLForm($('.release-form'));
F
Fatih Acet 已提交
157 158
          break;
        case 'projects:merge_requests:show':
M
Mike Greiling 已提交
159
          new gl.Diff();
F
Fatih Acet 已提交
160 161 162 163 164 165 166 167
          shortcut_handler = new ShortcutsIssuable(true);
          new ZenMode();
          new MergedButtons();
          break;
        case 'projects:merge_requests:commits':
          new MergedButtons();
          break;
        case "projects:merge_requests:diffs":
M
Mike Greiling 已提交
168
          new gl.Diff();
F
Fatih Acet 已提交
169 170 171 172
          new ZenMode();
          new MergedButtons();
          break;
        case 'dashboard:activity':
173
          new gl.Activities();
F
Fatih Acet 已提交
174 175 176
          break;
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
177
          new gl.Diff();
F
Fatih Acet 已提交
178 179 180
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
          break;
181 182 183 184 185
        case 'projects:commit:pipelines':
          new gl.MiniPipelineGraph({
            container: '.js-pipeline-table',
          }).bindEvents();
          break;
F
Fatih Acet 已提交
186 187 188 189 190 191 192 193 194 195 196
        case 'projects:commits:show':
        case 'projects:activity':
          shortcut_handler = new ShortcutsNavigation();
          break;
        case 'projects:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          if ($('#tree-slider').length) {
            new TreeView();
          }
          break;
F
Filipa Lacerda 已提交
197
        case 'projects:pipelines:builds':
L
Luke Bennett 已提交
198
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
199
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
F
Filipa Lacerda 已提交
200 201 202 203 204 205 206 207 208

          new gl.Pipelines({
            initTabs: true,
            tabsOptions: {
              action: controllerAction,
              defaultAction: 'pipelines',
              parentEl: '.pipelines-tabs',
            },
          });
L
Luke Bennett 已提交
209
          break;
F
Fatih Acet 已提交
210
        case 'groups:activity':
211
          new gl.Activities();
F
Fatih Acet 已提交
212 213 214 215 216
          break;
        case 'groups:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          new NotificationsDropdown();
217
          new ProjectsList();
F
Fatih Acet 已提交
218 219
          break;
        case 'groups:group_members:index':
220
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
221
          new gl.Members();
F
Fatih Acet 已提交
222 223
          new UsersSelect();
          break;
224
        case 'projects:members:show':
225 226
          new gl.MemberExpirationDate('.js-access-expiration-date-groups');
          new GroupsSelect();
227
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
228
          new gl.Members();
F
Fatih Acet 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
          new UsersSelect();
          break;
        case 'groups:new':
        case 'groups:edit':
        case 'admin:groups:edit':
        case 'admin:groups:new':
          new GroupAvatar();
          break;
        case 'projects:tree:show':
          shortcut_handler = new ShortcutsNavigation();
          new TreeView();
          break;
        case 'projects:find_file:show':
          shortcut_handler = true;
          break;
        case 'projects:blob:show':
        case 'projects:blame:show':
          new LineHighlighter();
          shortcut_handler = new ShortcutsNavigation();
248 249 250 251 252 253
          const fileBlobPermalinkUrlElement = document.querySelector('.js-data-file-blob-permalink-url');
          const fileBlobPermalinkUrl = fileBlobPermalinkUrlElement && fileBlobPermalinkUrlElement.getAttribute('href');
          new ShortcutsBlob({
            skipResetBindings: true,
            fileBlobPermalinkUrl,
          });
F
Fatih Acet 已提交
254
          break;
255 256
        case 'groups:labels:new':
        case 'groups:labels:edit':
F
Fatih Acet 已提交
257 258 259 260 261 262
        case 'projects:labels:new':
        case 'projects:labels:edit':
          new Labels();
          break;
        case 'projects:labels:index':
          if ($('.prioritized-labels').length) {
B
Bryce Johnson 已提交
263
            new gl.LabelManager();
F
Fatih Acet 已提交
264 265 266
          }
          break;
        case 'projects:network:show':
267 268
          // Ensure we don't create a particular shortcut handler here. This is
          // already created, where the network graph is created.
F
Fatih Acet 已提交
269 270 271 272 273 274 275 276
          shortcut_handler = true;
          break;
        case 'projects:forks:new':
          new ProjectFork();
          break;
        case 'projects:artifacts:browse':
          new BuildArtifacts();
          break;
277 278 279
        case 'help:index':
          gl.VersionCheckImage.bindErrorEvent($('img.js-version-status-badge'));
          break;
F
Fatih Acet 已提交
280 281
        case 'search:show':
          new Search();
282 283
          break;
        case 'projects:protected_branches:index':
284 285
          new gl.ProtectedBranchCreate();
          new gl.ProtectedBranchEditList();
286
          break;
287
        case 'projects:ci_cd:show':
S
Stan Hu 已提交
288 289
          new gl.ProjectVariables();
          break;
290 291 292 293
        case 'ci:lints:create':
        case 'ci:lints:show':
          new gl.CILintEditor();
          break;
294 295 296
        case 'users:show':
          new UserCallout();
          break;
F
Fatih Acet 已提交
297 298
      }
      switch (path.first()) {
299 300 301 302 303 304 305 306 307 308 309
        case 'sessions':
        case 'omniauth_callbacks':
          if (!gon.u2f) break;
          gl.u2fAuthenticate = new gl.U2FAuthenticate(
            $('#js-authenticate-u2f'),
            '#js-login-u2f-form',
            gon.u2f,
            document.querySelector('#js-login-2fa-device'),
            document.querySelector('.js-2fa-form'),
          );
          gl.u2fAuthenticate.start();
F
Fatih Acet 已提交
310 311 312 313 314 315 316 317
        case 'admin':
          new Admin();
          switch (path[1]) {
            case 'groups':
              new UsersSelect();
              break;
            case 'projects':
              new NamespaceSelects();
318 319
              break;
            case 'labels':
320
              switch (path[2]) {
321
                case 'new':
322 323 324
                case 'edit':
                  new Labels();
              }
325
            case 'abuse_reports':
326
              new gl.AbuseReports();
327
              break;
F
Fatih Acet 已提交
328 329 330 331 332
          }
          break;
        case 'dashboard':
        case 'root':
          shortcut_handler = new ShortcutsDashboardNavigation();
333
          new UserCallout();
F
Fatih Acet 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
          break;
        case 'profiles':
          new NotificationsForm();
          new NotificationsDropdown();
          break;
        case 'projects':
          new Project();
          new ProjectAvatar();
          switch (path[1]) {
            case 'compare':
              new CompareAutocomplete();
              break;
            case 'edit':
              shortcut_handler = new ShortcutsNavigation();
              new ProjectNew();
              break;
            case 'new':
              new ProjectNew();
              break;
            case 'show':
354
              new Star();
F
Fatih Acet 已提交
355 356 357 358 359
              new ProjectNew();
              new ProjectShow();
              new NotificationsDropdown();
              break;
            case 'wikis':
360
              new gl.Wikis();
F
Fatih Acet 已提交
361 362
              shortcut_handler = new ShortcutsNavigation();
              new ZenMode();
L
Luke "Jared" Bennett 已提交
363
              new gl.GLForm($('.wiki-form'));
F
Fatih Acet 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
              break;
            case 'snippets':
              shortcut_handler = new ShortcutsNavigation();
              if (path[2] === 'show') {
                new ZenMode();
              }
              break;
            case 'labels':
            case 'graphs':
            case 'compare':
            case 'pipelines':
            case 'forks':
            case 'milestones':
            case 'project_members':
            case 'deploy_keys':
            case 'builds':
            case 'hooks':
            case 'services':
            case 'protected_branches':
              shortcut_handler = new ShortcutsNavigation();
          }
      }
386
      // If we haven't installed a custom shortcut handler, install the default one
F
Fatih Acet 已提交
387
      if (!shortcut_handler) {
388
        new Shortcuts();
F
Fatih Acet 已提交
389 390 391 392
      }
    };

    Dispatcher.prototype.initSearch = function() {
393
      // Only when search form is present
F
Fatih Acet 已提交
394
      if ($('.search').length) {
395
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
396 397 398
      }
    };

399
    Dispatcher.prototype.initFieldErrors = function() {
400
      $('.gl-show-field-errors').each((i, form) => {
401 402
        new gl.GlFieldErrors(form);
      });
403 404
    };

F
Fatih Acet 已提交
405 406
    return Dispatcher;
  })();
407
}).call(window);