dispatcher.js 17.8 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 4 5
/* global UsernameValidator */
/* global ActiveTabMemoizer */
/* global ShortcutsNavigation */
6
/* global IssuableIndex */
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 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
import initSettingsPanels from './settings_panels';
59
import initExperimentalFlags from './experimental_flags';
60

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

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

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

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

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

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

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

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

F
Fatih Acet 已提交
146
          shortcut_handler = new ShortcutsNavigation();
147
          new UsersSelect();
F
Fatih Acet 已提交
148 149 150 151 152 153
          break;
        case 'projects:issues:show':
          new Issue();
          shortcut_handler = new ShortcutsIssuable();
          new ZenMode();
          break;
P
Phil Hughes 已提交
154
        case 'dashboard:milestones:index':
P
Phil Hughes 已提交
155 156
        case 'dashboard:issues':
        case 'dashboard:merge_requests':
P
Phil Hughes 已提交
157 158
          new ProjectSelect();
          break;
F
Fatih Acet 已提交
159 160 161 162
        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
        case 'groups:issues':
        case 'groups:merge_requests':
          new UsersSelect();
P
Phil Hughes 已提交
168
          new ProjectSelect();
169
          break;
F
Fatih Acet 已提交
170
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
171
          new gl.Todos();
F
Fatih Acet 已提交
172
          break;
173 174 175 176 177 178 179 180
        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 已提交
181 182
        case 'explore:groups:index':
          new GroupsList();
183 184 185 186 187 188 189 190 191

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

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

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

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

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