dispatcher.js 17.9 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
/* global ActiveTabMemoizer */
/* 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 28 29
/* global ShortcutsIssuable */
/* 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 */
30
/* global Sidebar */
31
/* global ShortcutsWiki */
32

33
import Issue from './issue';
Z
Z.J. van de Weg 已提交
34
import BindInOut from './behaviors/bind_in_out';
35
import DeleteModal from './branches/branches_delete_modal';
36
import Group from './group';
S
Sam Rose 已提交
37
import GroupName from './group_name';
F
Filipa Lacerda 已提交
38
import GroupsList from './groups_list';
39
import ProjectsList from './projects_list';
40
import setupProjectEdit from './project_edit';
41
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
42
import BlobLinePermalinkUpdater from './blob/blob_line_permalink_updater';
43
import Landing from './landing';
E
Eric Eastwood 已提交
44
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
45
import UserCallout from './user_callout';
K
Kushal Pandya 已提交
46
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
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 UsernameValidator from './username_validator';
56
import VersionCheckImage from './version_check_image';
57
import Wikis from './wikis';
58
import ZenMode from './zen_mode';
59
import initSettingsPanels from './settings_panels';
60
import initExperimentalFlags from './experimental_flags';
61
import OAuthRememberMe from './oauth_remember_me';
62

F
Fatih Acet 已提交
63 64 65 66 67 68 69 70 71 72
(function() {
  var Dispatcher;

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

  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
73
      this.initFieldErrors();
F
Fatih Acet 已提交
74 75 76 77
      this.initPageScripts();
    }

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

86 87 88 89 90 91 92 93 94 95 96 97
      $('.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,
        });
      });
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      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 已提交
115

116 117 118 119 120 121
        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'),
122 123
        })
          .init();
124 125
      }

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

F
Fatih Acet 已提交
149
          shortcut_handler = new ShortcutsNavigation();
150
          new UsersSelect();
F
Fatih Acet 已提交
151 152 153 154 155 156 157 158 159 160
          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();
161
          new Sidebar();
F
Fatih Acet 已提交
162
          break;
163 164 165 166
        case 'groups:issues':
        case 'groups:merge_requests':
          new UsersSelect();
          break;
F
Fatih Acet 已提交
167
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
168
          new gl.Todos();
F
Fatih Acet 已提交
169
          break;
170 171 172 173 174 175 176 177
        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 已提交
178 179
        case 'explore:groups:index':
          new GroupsList();
180 181 182 183 184 185 186 187 188

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

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

    Dispatcher.prototype.initSearch = function() {
523
      // Only when search form is present
F
Fatih Acet 已提交
524
      if ($('.search').length) {
525
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
526 527 528
      }
    };

529
    Dispatcher.prototype.initFieldErrors = function() {
530
      $('.gl-show-field-errors').each((i, form) => {
531 532
        new gl.GlFieldErrors(form);
      });
533 534
    };

F
Fatih Acet 已提交
535 536
    return Dispatcher;
  })();
537
}).call(window);