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

36
import Issue from './issue';
37

Z
Z.J. van de Weg 已提交
38
import BindInOut from './behaviors/bind_in_out';
39
import DeleteModal from './branches/branches_delete_modal';
40
import Group from './group';
S
Sam Rose 已提交
41
import GroupName from './group_name';
F
Filipa Lacerda 已提交
42
import GroupsList from './groups_list';
43
import ProjectsList from './projects_list';
44
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
45
import BlobLinePermalinkUpdater from './blob/blob_line_permalink_updater';
46
import Landing from './landing';
E
Eric Eastwood 已提交
47
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
48
import UserCallout from './user_callout';
K
Kushal Pandya 已提交
49
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
G
geoandri 已提交
50
import ShortcutsWiki from './shortcuts_wiki';
51
import Pipelines from './pipelines';
D
Douwe Maan 已提交
52
import BlobViewer from './blob/viewer/index';
53
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
54
import UsersSelect from './users_select';
55

56 57
const ShortcutsBlob = require('./shortcuts_blob');

F
Fatih Acet 已提交
58 59 60 61 62 63 64 65 66 67
(function() {
  var Dispatcher;

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

  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
68
      this.initFieldErrors();
F
Fatih Acet 已提交
69 70 71 72
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
73
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
F
Fatih Acet 已提交
74 75 76 77 78 79
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
      path = page.split(':');
      shortcut_handler = null;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

      function initBlob() {
        new LineHighlighter();

        new BlobLinePermalinkUpdater(
          document.querySelector('#blob-content-holder'),
          '.diff-line-num[data-line-number]',
          document.querySelectorAll('.js-data-file-blob-permalink-url, .js-blob-blame-link'),
        );

        shortcut_handler = new ShortcutsNavigation();
        fileBlobPermalinkUrlElement = document.querySelector('.js-data-file-blob-permalink-url');
        fileBlobPermalinkUrl = fileBlobPermalinkUrlElement && fileBlobPermalinkUrlElement.getAttribute('href');
        new ShortcutsBlob({
          skipResetBindings: true,
          fileBlobPermalinkUrl,
        });
E
Eric Eastwood 已提交
97

98 99 100 101 102 103
        new BlobForkSuggestion({
          openButtons: document.querySelectorAll('.js-edit-blob-link-fork-toggler'),
          forkButtons: document.querySelectorAll('.js-fork-suggestion-button'),
          cancelButtons: document.querySelectorAll('.js-cancel-fork-suggestion-button'),
          suggestionSections: document.querySelectorAll('.js-file-fork-suggestion-section'),
          actionTextPieces: document.querySelectorAll('.js-file-fork-suggestion-section-action'),
104 105
        })
          .init();
106 107
      }

F
Fatih Acet 已提交
108
      switch (page) {
109 110
        case 'sessions:new':
          new UsernameValidator();
111
          new ActiveTabMemoizer();
112
          break;
113
        case 'projects:boards:show':
114
        case 'projects:boards:index':
115
          shortcut_handler = new ShortcutsNavigation();
116
          new UsersSelect();
117
          break;
118 119 120
        case 'projects:builds:show':
          new Build();
          break;
121
        case 'projects:merge_requests:index':
F
Fatih Acet 已提交
122
        case 'projects:issues:index':
C
Clement Ho 已提交
123
          if (gl.FilteredSearchManager) {
C
Clement Ho 已提交
124
            new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
C
Clement Ho 已提交
125
          }
F
Fatih Acet 已提交
126
          Issuable.init();
127
          new gl.IssuableBulkActions({
A
Alfredo Sumaran 已提交
128
            prefixId: page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_',
129
          });
F
Fatih Acet 已提交
130
          shortcut_handler = new ShortcutsNavigation();
131
          new UsersSelect();
F
Fatih Acet 已提交
132 133 134 135 136 137 138 139 140 141
          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();
142
          new Sidebar();
F
Fatih Acet 已提交
143
          break;
144 145 146 147
        case 'groups:issues':
        case 'groups:merge_requests':
          new UsersSelect();
          break;
F
Fatih Acet 已提交
148
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
149
          new gl.Todos();
F
Fatih Acet 已提交
150
          break;
151 152 153 154 155 156 157 158
        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 已提交
159
        case 'dashboard:groups:index':
160 161
          new GroupsList();
          break;
F
Filipa Lacerda 已提交
162 163
        case 'explore:groups:index':
          new GroupsList();
164 165 166 167 168 169 170 171 172

          const landingElement = document.querySelector('.js-explore-groups-landing');
          if (!landingElement) break;
          const exploreGroupsLanding = new Landing(
            landingElement,
            landingElement.querySelector('.dismiss-button'),
            'explore_groups_landing_dismissed',
          );
          exploreGroupsLanding.toggle();
F
Filipa Lacerda 已提交
173
          break;
F
Fatih Acet 已提交
174 175
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
176
        case 'projects:milestones:update':
177 178 179
        case 'groups:milestones:new':
        case 'groups:milestones:edit':
        case 'groups:milestones:update':
F
Fatih Acet 已提交
180
          new ZenMode();
181
          new gl.DueDateSelectors();
L
Luke "Jared" Bennett 已提交
182
          new gl.GLForm($('.milestone-form'));
F
Fatih Acet 已提交
183 184
          break;
        case 'projects:compare:show':
M
Mike Greiling 已提交
185
          new gl.Diff();
F
Fatih Acet 已提交
186
          break;
187 188
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
189
          new DeleteModal();
190
          break;
F
Fatih Acet 已提交
191 192 193
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
194
          new gl.GLForm($('.issue-form'));
F
Fatih Acet 已提交
195
          new IssuableForm($('.issue-form'));
196 197
          new LabelsSelect();
          new MilestoneSelect();
198
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
199 200
          break;
        case 'projects:merge_requests:new':
201
        case 'projects:merge_requests:new_diffs':
F
Fatih Acet 已提交
202
        case 'projects:merge_requests:edit':
M
Mike Greiling 已提交
203
          new gl.Diff();
F
Fatih Acet 已提交
204
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
205
          new gl.GLForm($('.merge-request-form'));
F
Fatih Acet 已提交
206
          new IssuableForm($('.merge-request-form'));
207 208
          new LabelsSelect();
          new MilestoneSelect();
209
          new gl.IssuableTemplateSelectors();
210
          new AutoWidthDropdownSelect($('.js-target-branch-select')).init();
F
Fatih Acet 已提交
211 212 213
          break;
        case 'projects:tags:new':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
214
          new gl.GLForm($('.tag-form'));
F
Fatih Acet 已提交
215 216 217
          break;
        case 'projects:releases:edit':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
218
          new gl.GLForm($('.release-form'));
F
Fatih Acet 已提交
219 220
          break;
        case 'projects:merge_requests:show':
M
Mike Greiling 已提交
221
          new gl.Diff();
F
Fatih Acet 已提交
222 223 224 225
          shortcut_handler = new ShortcutsIssuable(true);
          new ZenMode();
          break;
        case "projects:merge_requests:diffs":
M
Mike Greiling 已提交
226
          new gl.Diff();
F
Fatih Acet 已提交
227 228 229
          new ZenMode();
          break;
        case 'dashboard:activity':
230
          new gl.Activities();
F
Fatih Acet 已提交
231
          break;
232 233 234 235
        case 'dashboard:issues':
        case 'dashboard:merge_requests':
          new UsersSelect();
          break;
F
Fatih Acet 已提交
236 237
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
238
          new gl.Diff();
F
Fatih Acet 已提交
239 240
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
241 242 243
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
F
Fatih Acet 已提交
244
          break;
245
        case 'projects:commit:pipelines':
246
          new MiniPipelineGraph({
247
            container: '.js-commit-pipeline-graph',
248 249
          }).bindEvents();
          break;
F
Fatih Acet 已提交
250 251 252 253 254 255 256 257 258
        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();
D
Douwe Maan 已提交
259
            new BlobViewer();
F
Fatih Acet 已提交
260 261
          }
          break;
F
Filipa Lacerda 已提交
262
        case 'projects:pipelines:builds':
263
        case 'projects:pipelines:failures':
L
Luke Bennett 已提交
264
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
265
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
266
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
F
Filipa Lacerda 已提交
267

268
          new Pipelines({
F
Filipa Lacerda 已提交
269
            initTabs: true,
270
            pipelineStatusUrl,
F
Filipa Lacerda 已提交
271 272 273 274 275 276
            tabsOptions: {
              action: controllerAction,
              defaultAction: 'pipelines',
              parentEl: '.pipelines-tabs',
            },
          });
L
Luke Bennett 已提交
277
          break;
F
Fatih Acet 已提交
278
        case 'groups:activity':
279
          new gl.Activities();
F
Fatih Acet 已提交
280 281 282 283 284
          break;
        case 'groups:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          new NotificationsDropdown();
285
          new ProjectsList();
F
Fatih Acet 已提交
286 287
          break;
        case 'groups:group_members:index':
288
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
289
          new gl.Members();
F
Fatih Acet 已提交
290 291
          new UsersSelect();
          break;
292
        case 'projects:members:show':
293 294
          new gl.MemberExpirationDate('.js-access-expiration-date-groups');
          new GroupsSelect();
295
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
296
          new gl.Members();
F
Fatih Acet 已提交
297 298 299
          new UsersSelect();
          break;
        case 'groups:new':
L
Luke "Jared" Bennett 已提交
300
        case 'admin:groups:new':
301 302
        case 'groups:create':
        case 'admin:groups:create':
Z
Z.J. van de Weg 已提交
303
          BindInOut.initAll();
304 305 306
          new Group();
          new GroupAvatar();
          break;
F
Fatih Acet 已提交
307 308 309 310 311 312 313
        case 'groups:edit':
        case 'admin:groups:edit':
          new GroupAvatar();
          break;
        case 'projects:tree:show':
          shortcut_handler = new ShortcutsNavigation();
          new TreeView();
D
Douwe Maan 已提交
314
          new BlobViewer();
315
          gl.TargetBranchDropDown.bootstrap();
F
Fatih Acet 已提交
316 317 318 319
          break;
        case 'projects:find_file:show':
          shortcut_handler = true;
          break;
320 321 322 323 324 325
        case 'projects:blob:new':
          gl.TargetBranchDropDown.bootstrap();
          break;
        case 'projects:blob:create':
          gl.TargetBranchDropDown.bootstrap();
          break;
F
Fatih Acet 已提交
326
        case 'projects:blob:show':
D
Douwe Maan 已提交
327
          new BlobViewer();
328
          gl.TargetBranchDropDown.bootstrap();
329
          initBlob();
330 331 332 333
          break;
        case 'projects:blob:edit':
          gl.TargetBranchDropDown.bootstrap();
          break;
F
Fatih Acet 已提交
334
        case 'projects:blame:show':
335
          initBlob();
F
Fatih Acet 已提交
336
          break;
337 338
        case 'groups:labels:new':
        case 'groups:labels:edit':
F
Fatih Acet 已提交
339 340 341 342 343 344
        case 'projects:labels:new':
        case 'projects:labels:edit':
          new Labels();
          break;
        case 'projects:labels:index':
          if ($('.prioritized-labels').length) {
B
Bryce Johnson 已提交
345
            new gl.LabelManager();
F
Fatih Acet 已提交
346 347 348
          }
          break;
        case 'projects:network:show':
349 350
          // Ensure we don't create a particular shortcut handler here. This is
          // already created, where the network graph is created.
F
Fatih Acet 已提交
351 352 353 354 355 356 357 358
          shortcut_handler = true;
          break;
        case 'projects:forks:new':
          new ProjectFork();
          break;
        case 'projects:artifacts:browse':
          new BuildArtifacts();
          break;
359 360 361
        case 'projects:artifacts:file':
          new BlobViewer();
          break;
362 363 364
        case 'help:index':
          gl.VersionCheckImage.bindErrorEvent($('img.js-version-status-badge'));
          break;
F
Fatih Acet 已提交
365 366
        case 'search:show':
          new Search();
367
          break;
368
        case 'projects:repository:show':
369
          // Initialize Protected Branch Settings
370 371
          new gl.ProtectedBranchCreate();
          new gl.ProtectedBranchEditList();
372
          // Initialize Protected Tag Settings
K
Kushal Pandya 已提交
373 374
          new ProtectedTagCreate();
          new ProtectedTagEditList();
375
          break;
376
        case 'projects:ci_cd:show':
S
Stan Hu 已提交
377 378
          new gl.ProjectVariables();
          break;
379 380 381 382
        case 'ci:lints:create':
        case 'ci:lints:show':
          new gl.CILintEditor();
          break;
383 384 385
        case 'users:show':
          new UserCallout();
          break;
D
Douwe Maan 已提交
386 387 388 389
        case 'snippets:show':
          new LineHighlighter();
          new BlobViewer();
          break;
390 391 392
        case 'import:fogbugz:new_user_map':
          new UsersSelect();
          break;
F
Fatih Acet 已提交
393 394
      }
      switch (path.first()) {
395 396 397 398 399 400 401 402 403 404 405
        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 已提交
406 407 408
        case 'admin':
          new Admin();
          switch (path[1]) {
S
Sean McGivern 已提交
409
            case 'cohorts':
410
              new gl.UsagePing();
411
              break;
F
Fatih Acet 已提交
412 413 414 415 416
            case 'groups':
              new UsersSelect();
              break;
            case 'projects':
              new NamespaceSelects();
417 418
              break;
            case 'labels':
419
              switch (path[2]) {
420
                case 'new':
421 422 423
                case 'edit':
                  new Labels();
              }
424
            case 'abuse_reports':
425
              new gl.AbuseReports();
426
              break;
F
Fatih Acet 已提交
427 428 429 430
          }
          break;
        case 'dashboard':
        case 'root':
431
          new UserCallout();
F
Fatih Acet 已提交
432
          break;
S
Sam Rose 已提交
433 434 435
        case 'groups':
          new GroupName();
          break;
F
Fatih Acet 已提交
436 437 438 439 440 441 442
        case 'profiles':
          new NotificationsForm();
          new NotificationsDropdown();
          break;
        case 'projects':
          new Project();
          new ProjectAvatar();
S
Sam Rose 已提交
443
          new GroupName();
F
Fatih Acet 已提交
444 445 446 447 448 449 450 451 452 453 454 455
          switch (path[1]) {
            case 'compare':
              new CompareAutocomplete();
              break;
            case 'edit':
              shortcut_handler = new ShortcutsNavigation();
              new ProjectNew();
              break;
            case 'new':
              new ProjectNew();
              break;
            case 'show':
456
              new Star();
F
Fatih Acet 已提交
457 458 459 460 461
              new ProjectNew();
              new ProjectShow();
              new NotificationsDropdown();
              break;
            case 'wikis':
462
              new gl.Wikis();
463
              shortcut_handler = new ShortcutsWiki();
F
Fatih Acet 已提交
464
              new ZenMode();
L
Luke "Jared" Bennett 已提交
465
              new gl.GLForm($('.wiki-form'));
F
Fatih Acet 已提交
466 467 468 469 470
              break;
            case 'snippets':
              shortcut_handler = new ShortcutsNavigation();
              if (path[2] === 'show') {
                new ZenMode();
D
Douwe Maan 已提交
471 472
                new LineHighlighter();
                new BlobViewer();
F
Fatih Acet 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
              }
              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();
          }
      }
490
      // If we haven't installed a custom shortcut handler, install the default one
F
Fatih Acet 已提交
491
      if (!shortcut_handler) {
492
        new Shortcuts();
F
Fatih Acet 已提交
493 494 495 496
      }
    };

    Dispatcher.prototype.initSearch = function() {
497
      // Only when search form is present
F
Fatih Acet 已提交
498
      if ($('.search').length) {
499
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
500 501 502
      }
    };

503
    Dispatcher.prototype.initFieldErrors = function() {
504
      $('.gl-show-field-errors').each((i, form) => {
505 506
        new gl.GlFieldErrors(form);
      });
507 508
    };

F
Fatih Acet 已提交
509 510
    return Dispatcher;
  })();
511
}).call(window);