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
/* 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';
K
Kushal Pandya 已提交
44
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
G
geoandri 已提交
45
import ShortcutsWiki from './shortcuts_wiki';
46
import Pipelines from './pipelines';
D
Douwe Maan 已提交
47
import BlobViewer from './blob/viewer/index';
48
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
49
import UsersSelect from './users_select';
50
import RefSelectDropdown from './ref_select_dropdown';
51
import GfmAutoComplete from './gfm_auto_complete';
52
import ShortcutsBlob from './shortcuts_blob';
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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