dispatcher.js 18.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 */
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 32
/* global IssuableContext */
/* global IssueStatusSelect */
/* global SubscriptionSelect */
33

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

F
Fatih Acet 已提交
72 73 74 75 76 77 78 79 80 81
(function() {
  var Dispatcher;

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

  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
82
      this.initFieldErrors();
F
Fatih Acet 已提交
83 84 85 86
      this.initPageScripts();
    }

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

95 96 97 98 99 100 101 102 103 104 105 106
      $('.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,
        });
      });
107

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

125 126 127 128 129 130
        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'),
131 132
        })
          .init();
133 134
      }

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

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

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

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

      if (document.querySelector('#peek')) {
        new PerformanceBar({ container: '#peek' });
      }
F
Fatih Acet 已提交
546 547 548
    };

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

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

F
Fatih Acet 已提交
561 562
    return Dispatcher;
  })();
563
}).call(window);