dispatcher.js 18.6 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 */
P
Phil Hughes 已提交
2
/* global ProjectSelect */
3
/* global ShortcutsNavigation */
4
/* global IssuableIndex */
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* global ShortcutsIssuable */
/* global Milestone */
/* global IssuableForm */
/* global LabelsSelect */
/* global MilestoneSelect */
/* global Commit */
/* global NotificationsForm */
/* 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 ProjectShow */
/* global Labels */
/* global Shortcuts */
28
/* global Sidebar */
29
/* global ShortcutsWiki */
30

31
import Issue from './issue';
Z
Z.J. van de Weg 已提交
32
import BindInOut from './behaviors/bind_in_out';
33
import DeleteModal from './branches/branches_delete_modal';
34
import Group from './group';
S
Sam Rose 已提交
35
import GroupName from './group_name';
F
Filipa Lacerda 已提交
36
import GroupsList from './groups_list';
37
import ProjectsList from './projects_list';
38
import setupProjectEdit from './project_edit';
39
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
40
import BlobLinePermalinkUpdater from './blob/blob_line_permalink_updater';
41
import Landing from './landing';
E
Eric Eastwood 已提交
42
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
43
import UserCallout from './user_callout';
G
geoandri 已提交
44
import ShortcutsWiki from './shortcuts_wiki';
45
import Pipelines from './pipelines';
D
Douwe Maan 已提交
46
import BlobViewer from './blob/viewer/index';
47
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
48
import UsersSelect from './users_select';
49
import RefSelectDropdown from './ref_select_dropdown';
50
import GfmAutoComplete from './gfm_auto_complete';
51
import ShortcutsBlob from './shortcuts_blob';
52
import SigninTabsMemoizer from './signin_tabs_memoizer';
53
import Star from './star';
54
import Todos from './todos';
55
import TreeView from './tree';
56
import UsagePing from './usage_ping';
57
import UsernameValidator from './username_validator';
58
import VersionCheckImage from './version_check_image';
59
import Wikis from './wikis';
60
import ZenMode from './zen_mode';
61
import initSettingsPanels from './settings_panels';
62
import initExperimentalFlags from './experimental_flags';
63
import OAuthRememberMe from './oauth_remember_me';
64
import PerformanceBar from './performance_bar';
P
Phil Hughes 已提交
65 66 67
import initNotes from './init_notes';
import initLegacyFilters from './init_legacy_filters';
import initIssuableSidebar from './init_issuable_sidebar';
68

F
Fatih Acet 已提交
69 70 71 72 73 74 75 76 77 78
(function() {
  var Dispatcher;

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

  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
79
      this.initFieldErrors();
F
Fatih Acet 已提交
80 81 82 83
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
84
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
F
Fatih Acet 已提交
85 86 87 88 89 90
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
      path = page.split(':');
      shortcut_handler = null;
91

92 93 94 95 96 97 98 99 100 101 102 103
      $('.js-gfm-input').each((i, el) => {
        const gfm = new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources);
        const enableGFM = gl.utils.convertPermissionToBoolean(el.dataset.supportsAutocomplete);
        gfm.setup($(el), {
          emojis: true,
          members: enableGFM,
          issues: enableGFM,
          milestones: enableGFM,
          mergeRequests: enableGFM,
          labels: enableGFM,
        });
      });
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
      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 已提交
121

122 123 124 125 126 127
        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'),
128 129
        })
          .init();
130 131
      }

F
Fatih Acet 已提交
132
      switch (page) {
133 134 135
        case 'profiles:preferences:show':
          initExperimentalFlags();
          break;
136 137
        case 'sessions:new':
          new UsernameValidator();
138
          new SigninTabsMemoizer();
139
          new OAuthRememberMe({ container: $(".omniauth-container") }).bindEvents();
140
          break;
141
        case 'projects:boards:show':
142
        case 'projects:boards:index':
143
          shortcut_handler = new ShortcutsNavigation();
144
          new UsersSelect();
145
          break;
146
        case 'projects:merge_requests:index':
F
Fatih Acet 已提交
147
        case 'projects:issues:index':
148
          if (gl.FilteredSearchManager && document.querySelector('.filtered-search')) {
149 150
            const filteredSearchManager = new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
            filteredSearchManager.setup();
C
Clement Ho 已提交
151
          }
152 153 154
          const pagePrefix = page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_';
          IssuableIndex.init(pagePrefix);

F
Fatih Acet 已提交
155
          shortcut_handler = new ShortcutsNavigation();
156
          new UsersSelect();
F
Fatih Acet 已提交
157 158 159 160 161
          break;
        case 'projects:issues:show':
          new Issue();
          shortcut_handler = new ShortcutsIssuable();
          new ZenMode();
162 163
          initIssuableSidebar();
          initNotes();
F
Fatih Acet 已提交
164
          break;
P
Phil Hughes 已提交
165 166 167
        case 'dashboard:milestones:index':
          new ProjectSelect();
          break;
F
Fatih Acet 已提交
168 169 170 171
        case 'projects:milestones:show':
        case 'groups:milestones:show':
        case 'dashboard:milestones:show':
          new Milestone();
172
          new Sidebar();
F
Fatih Acet 已提交
173
          break;
174 175
        case 'dashboard:issues':
        case 'dashboard:merge_requests':
176 177
        case 'groups:issues':
        case 'groups:merge_requests':
P
Phil Hughes 已提交
178
          new ProjectSelect();
179
          initLegacyFilters();
180
          break;
F
Fatih Acet 已提交
181
        case 'dashboard:todos:index':
182
          new Todos();
F
Fatih Acet 已提交
183
          break;
184 185 186 187 188 189 190 191
        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 已提交
192 193
        case 'explore:groups:index':
          new GroupsList();
194 195 196 197 198 199 200 201 202

          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 已提交
203
          break;
F
Fatih Acet 已提交
204 205
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
206
        case 'projects:milestones:update':
207 208 209
        case 'groups:milestones:new':
        case 'groups:milestones:edit':
        case 'groups:milestones:update':
F
Fatih Acet 已提交
210
          new ZenMode();
211
          new gl.DueDateSelectors();
212
          new gl.GLForm($('.milestone-form'), true);
F
Fatih Acet 已提交
213 214
          break;
        case 'projects:compare:show':
M
Mike Greiling 已提交
215
          new gl.Diff();
F
Fatih Acet 已提交
216
          break;
217 218
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
219
          new DeleteModal();
220
          break;
F
Fatih Acet 已提交
221 222 223
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
224
          new gl.GLForm($('.issue-form'), true);
F
Fatih Acet 已提交
225
          new IssuableForm($('.issue-form'));
226 227
          new LabelsSelect();
          new MilestoneSelect();
228
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
229
          break;
D
Douwe Maan 已提交
230 231
        case 'projects:merge_requests:creations:new':
        case 'projects:merge_requests:creations:diffs':
F
Fatih Acet 已提交
232
        case 'projects:merge_requests:edit':
M
Mike Greiling 已提交
233
          new gl.Diff();
F
Fatih Acet 已提交
234
          shortcut_handler = new ShortcutsNavigation();
235
          new gl.GLForm($('.merge-request-form'), true);
F
Fatih Acet 已提交
236
          new IssuableForm($('.merge-request-form'));
237 238
          new LabelsSelect();
          new MilestoneSelect();
239
          new gl.IssuableTemplateSelectors();
240
          new AutoWidthDropdownSelect($('.js-target-branch-select')).init();
F
Fatih Acet 已提交
241 242 243
          break;
        case 'projects:tags:new':
          new ZenMode();
244
          new gl.GLForm($('.tag-form'), true);
245
          new RefSelectDropdown($('.js-branch-select'), window.gl.availableRefs);
F
Fatih Acet 已提交
246
          break;
247 248 249
        case 'projects:snippets:show':
          initNotes();
          break;
250 251 252 253
        case 'projects:snippets:new':
        case 'projects:snippets:edit':
        case 'projects:snippets:create':
        case 'projects:snippets:update':
254 255
          new gl.GLForm($('.snippet-form'), true);
          break;
256 257 258 259
        case 'snippets:new':
        case 'snippets:edit':
        case 'snippets:create':
        case 'snippets:update':
260
          new gl.GLForm($('.snippet-form'), false);
261
          break;
F
Fatih Acet 已提交
262 263
        case 'projects:releases:edit':
          new ZenMode();
264
          new gl.GLForm($('.release-form'), true);
F
Fatih Acet 已提交
265
          break;
266
        case 'projects:merge_requests:conflicts:show':
F
Fatih Acet 已提交
267
        case 'projects:merge_requests:show':
M
Mike Greiling 已提交
268
          new gl.Diff();
F
Fatih Acet 已提交
269 270
          shortcut_handler = new ShortcutsIssuable(true);
          new ZenMode();
271 272
          initIssuableSidebar();
          initNotes();
F
Fatih Acet 已提交
273 274
          break;
        case 'dashboard:activity':
275
          new gl.Activities();
F
Fatih Acet 已提交
276 277 278
          break;
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
279
          new gl.Diff();
F
Fatih Acet 已提交
280 281
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
282 283 284
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
285
          initNotes();
F
Fatih Acet 已提交
286
          break;
287
        case 'projects:commit:pipelines':
288
          new MiniPipelineGraph({
289
            container: '.js-commit-pipeline-graph',
290 291
          }).bindEvents();
          break;
F
Fatih Acet 已提交
292 293 294 295 296 297 298 299 300
        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 已提交
301 302
          }
          if ($('.blob-viewer').length) {
D
Douwe Maan 已提交
303
            new BlobViewer();
F
Fatih Acet 已提交
304 305
          }
          break;
306
        case 'projects:edit':
307
          setupProjectEdit();
308
          break;
F
Filipa Lacerda 已提交
309
        case 'projects:pipelines:builds':
310
        case 'projects:pipelines:failures':
L
Luke Bennett 已提交
311
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
312
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
313
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
F
Filipa Lacerda 已提交
314

315
          new Pipelines({
F
Filipa Lacerda 已提交
316
            initTabs: true,
317
            pipelineStatusUrl,
F
Filipa Lacerda 已提交
318 319 320 321 322 323
            tabsOptions: {
              action: controllerAction,
              defaultAction: 'pipelines',
              parentEl: '.pipelines-tabs',
            },
          });
L
Luke Bennett 已提交
324
          break;
F
Fatih Acet 已提交
325
        case 'groups:activity':
326
          new gl.Activities();
F
Fatih Acet 已提交
327 328 329 330 331
          break;
        case 'groups:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          new NotificationsDropdown();
332
          new ProjectsList();
F
Fatih Acet 已提交
333 334
          break;
        case 'groups:group_members:index':
335
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
336
          new gl.Members();
F
Fatih Acet 已提交
337 338
          new UsersSelect();
          break;
339
        case 'projects:project_members:index':
340 341
          new gl.MemberExpirationDate('.js-access-expiration-date-groups');
          new GroupsSelect();
342
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
343
          new gl.Members();
F
Fatih Acet 已提交
344 345 346
          new UsersSelect();
          break;
        case 'groups:new':
L
Luke "Jared" Bennett 已提交
347
        case 'admin:groups:new':
348 349
        case 'groups:create':
        case 'admin:groups:create':
Z
Z.J. van de Weg 已提交
350
          BindInOut.initAll();
351 352 353
          new Group();
          new GroupAvatar();
          break;
F
Fatih Acet 已提交
354 355 356 357 358 359 360
        case 'groups:edit':
        case 'admin:groups:edit':
          new GroupAvatar();
          break;
        case 'projects:tree:show':
          shortcut_handler = new ShortcutsNavigation();
          new TreeView();
D
Douwe Maan 已提交
361
          new BlobViewer();
F
Fatih Acet 已提交
362 363 364 365 366
          break;
        case 'projects:find_file:show':
          shortcut_handler = true;
          break;
        case 'projects:blob:show':
D
Douwe Maan 已提交
367
          new BlobViewer();
368
          initBlob();
369
          break;
F
Fatih Acet 已提交
370
        case 'projects:blame:show':
371
          initBlob();
F
Fatih Acet 已提交
372
          break;
373 374
        case 'groups:labels:new':
        case 'groups:labels:edit':
F
Fatih Acet 已提交
375 376 377 378
        case 'projects:labels:new':
        case 'projects:labels:edit':
          new Labels();
          break;
379
        case 'groups:labels:index':
F
Fatih Acet 已提交
380 381
        case 'projects:labels:index':
          if ($('.prioritized-labels').length) {
B
Bryce Johnson 已提交
382
            new gl.LabelManager();
F
Fatih Acet 已提交
383
          }
384
          new gl.ProjectLabelSubscription('.label-subscription');
F
Fatih Acet 已提交
385 386
          break;
        case 'projects:network:show':
387 388
          // Ensure we don't create a particular shortcut handler here. This is
          // already created, where the network graph is created.
F
Fatih Acet 已提交
389 390 391 392 393 394
          shortcut_handler = true;
          break;
        case 'projects:forks:new':
          new ProjectFork();
          break;
        case 'projects:artifacts:browse':
395
          new ShortcutsNavigation();
F
Fatih Acet 已提交
396 397
          new BuildArtifacts();
          break;
398
        case 'projects:artifacts:file':
399
          new ShortcutsNavigation();
400 401
          new BlobViewer();
          break;
402
        case 'help:index':
403
          VersionCheckImage.bindErrorEvent($('img.js-version-status-badge'));
404
          break;
F
Fatih Acet 已提交
405 406
        case 'search:show':
          new Search();
407
          break;
D
Douwe Maan 已提交
408
        case 'projects:settings:repository:show':
409 410
          // Initialize expandable settings panels
          initSettingsPanels();
411
          break;
D
Douwe Maan 已提交
412
        case 'projects:settings:ci_cd:show':
S
Shinya Maeda 已提交
413
        case 'groups:settings:ci_cd:show':
S
Stan Hu 已提交
414 415
          new gl.ProjectVariables();
          break;
416 417 418 419
        case 'ci:lints:create':
        case 'ci:lints:show':
          new gl.CILintEditor();
          break;
420 421 422
        case 'users:show':
          new UserCallout();
          break;
423 424 425
        case 'admin:conversational_development_index:show':
          new UserCallout();
          break;
D
Douwe Maan 已提交
426 427 428
        case 'snippets:show':
          new LineHighlighter();
          new BlobViewer();
429
          initNotes();
D
Douwe Maan 已提交
430
          break;
431 432 433
        case 'import:fogbugz:new_user_map':
          new UsersSelect();
          break;
434 435 436 437
        case 'profiles:personal_access_tokens:index':
        case 'admin:impersonation_tokens:index':
          new gl.DueDateSelectors();
          break;
F
Fatih Acet 已提交
438 439
      }
      switch (path.first()) {
440 441 442 443 444 445 446 447 448 449 450
        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 已提交
451 452 453
        case 'admin':
          new Admin();
          switch (path[1]) {
S
Sean McGivern 已提交
454
            case 'cohorts':
455
              new UsagePing();
456
              break;
F
Fatih Acet 已提交
457 458 459 460 461
            case 'groups':
              new UsersSelect();
              break;
            case 'projects':
              new NamespaceSelects();
462 463
              break;
            case 'labels':
464
              switch (path[2]) {
465
                case 'new':
466 467 468
                case 'edit':
                  new Labels();
              }
469
            case 'abuse_reports':
470
              new gl.AbuseReports();
471
              break;
F
Fatih Acet 已提交
472 473 474 475
          }
          break;
        case 'dashboard':
        case 'root':
476
          new UserCallout();
F
Fatih Acet 已提交
477
          break;
S
Sam Rose 已提交
478 479 480
        case 'groups':
          new GroupName();
          break;
F
Fatih Acet 已提交
481 482 483 484 485 486 487
        case 'profiles':
          new NotificationsForm();
          new NotificationsDropdown();
          break;
        case 'projects':
          new Project();
          new ProjectAvatar();
S
Sam Rose 已提交
488
          new GroupName();
F
Fatih Acet 已提交
489 490 491 492 493 494 495 496 497 498 499 500
          switch (path[1]) {
            case 'compare':
              new CompareAutocomplete();
              break;
            case 'edit':
              shortcut_handler = new ShortcutsNavigation();
              new ProjectNew();
              break;
            case 'new':
              new ProjectNew();
              break;
            case 'show':
501
              new Star();
F
Fatih Acet 已提交
502 503 504 505 506
              new ProjectNew();
              new ProjectShow();
              new NotificationsDropdown();
              break;
            case 'wikis':
507
              new Wikis();
508
              shortcut_handler = new ShortcutsWiki();
F
Fatih Acet 已提交
509
              new ZenMode();
510
              new gl.GLForm($('.wiki-form'), true);
F
Fatih Acet 已提交
511 512 513 514 515
              break;
            case 'snippets':
              shortcut_handler = new ShortcutsNavigation();
              if (path[2] === 'show') {
                new ZenMode();
D
Douwe Maan 已提交
516 517
                new LineHighlighter();
                new BlobViewer();
F
Fatih Acet 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
              }
              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();
          }
      }
535
      // If we haven't installed a custom shortcut handler, install the default one
F
Fatih Acet 已提交
536
      if (!shortcut_handler) {
537
        new Shortcuts();
F
Fatih Acet 已提交
538
      }
539 540 541 542

      if (document.querySelector('#peek')) {
        new PerformanceBar({ container: '#peek' });
      }
F
Fatih Acet 已提交
543 544 545
    };

    Dispatcher.prototype.initSearch = function() {
546
      // Only when search form is present
F
Fatih Acet 已提交
547
      if ($('.search').length) {
548
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
549 550 551
      }
    };

552
    Dispatcher.prototype.initFieldErrors = function() {
553
      $('.gl-show-field-errors').each((i, form) => {
554 555
        new gl.GlFieldErrors(form);
      });
556 557
    };

F
Fatih Acet 已提交
558 559
    return Dispatcher;
  })();
560
}).call(window);