dispatcher.js 17.0 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';
Z
Z.J. van de Weg 已提交
37
import BindInOut from './behaviors/bind_in_out';
38
import DeleteModal from './branches/branches_delete_modal';
39
import Group from './group';
S
Sam Rose 已提交
40
import GroupName from './group_name';
F
Filipa Lacerda 已提交
41
import GroupsList from './groups_list';
42
import ProjectsList from './projects_list';
43
import setupProjectEdit from './project_edit';
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
import RefSelectDropdown from './ref_select_dropdown';
56
import GfmAutoComplete from './gfm_auto_complete';
57
import ShortcutsBlob from './shortcuts_blob';
58

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

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

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

    Dispatcher.prototype.initPageScripts = function() {
74
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
F
Fatih Acet 已提交
75 76 77 78 79 80
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
      path = page.split(':');
      shortcut_handler = null;
81

82 83
      new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources).setup();

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      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 已提交
100

101 102 103 104 105 106
        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'),
107 108
        })
          .init();
109 110
      }

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

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

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

    Dispatcher.prototype.initSearch = function() {
507
      // Only when search form is present
F
Fatih Acet 已提交
508
      if ($('.search').length) {
509
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
510 511 512
      }
    };

513
    Dispatcher.prototype.initFieldErrors = function() {
514
      $('.gl-show-field-errors').each((i, form) => {
515 516
        new gl.GlFieldErrors(form);
      });
517 518
    };

F
Fatih Acet 已提交
519 520
    return Dispatcher;
  })();
521
}).call(window);