dispatcher.js 17.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 */
2 3 4
/* global UsernameValidator */
/* global ActiveTabMemoizer */
/* global ShortcutsNavigation */
5
/* global IssuableIndex */
6 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
/* 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 */
32
/* global Sidebar */
33
/* global ShortcutsWiki */
34

35
import Issue from './issue';
Z
Z.J. van de Weg 已提交
36
import BindInOut from './behaviors/bind_in_out';
37
import DeleteModal from './branches/branches_delete_modal';
38
import Group from './group';
S
Sam Rose 已提交
39
import GroupName from './group_name';
F
Filipa Lacerda 已提交
40
import GroupsList from './groups_list';
41
import ProjectsList from './projects_list';
42
import setupProjectEdit from './project_edit';
43
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
44
import BlobLinePermalinkUpdater from './blob/blob_line_permalink_updater';
45
import Landing from './landing';
E
Eric Eastwood 已提交
46
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
47
import UserCallout from './user_callout';
K
Kushal Pandya 已提交
48
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
G
geoandri 已提交
49
import ShortcutsWiki from './shortcuts_wiki';
50
import Pipelines from './pipelines';
D
Douwe Maan 已提交
51
import BlobViewer from './blob/viewer/index';
52
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
53
import UsersSelect from './users_select';
54
import RefSelectDropdown from './ref_select_dropdown';
55
import GfmAutoComplete from './gfm_auto_complete';
56
import ShortcutsBlob from './shortcuts_blob';
57
import initSettingsPanels from './settings_panels';
J
Jacob Schatz 已提交
58
import RepoBundle from './repo/repo_bundle';
59

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

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

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

    Dispatcher.prototype.initPageScripts = function() {
L
Luke "Jared" Bennett 已提交
75
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
F
Fatih Acet 已提交
76 77 78 79
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
80 81

      function getScrollBarWidth () {
L
Luke "Jared" Bennett 已提交
82 83
        var $outer = $('<div>').css({ visibility: 'hidden', width: 100, overflow: 'scroll' }).appendTo('body'),
          widthWithScroll = $('<div>').css({ width: '100%' }).appendTo($outer).outerWidth();
84 85
        $outer.remove();
        return 100 - widthWithScroll;
L
Luke "Jared" Bennett 已提交
86
      }
87 88 89

      $('body').attr('data-scroll-width', getScrollBarWidth());

F
Fatih Acet 已提交
90 91
      path = page.split(':');
      shortcut_handler = null;
92

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

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

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

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

F
Fatih Acet 已提交
152
          shortcut_handler = new ShortcutsNavigation();
153
          new UsersSelect();
F
Fatih Acet 已提交
154 155 156 157 158 159 160 161 162 163
          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();
164
          new Sidebar();
F
Fatih Acet 已提交
165
          break;
166 167 168 169
        case 'groups:issues':
        case 'groups:merge_requests':
          new UsersSelect();
          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 219
          break;
        case 'projects:merge_requests:new':
220
        case 'projects:merge_requests:new_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 "projects:merge_requests:diffs":
M
Mike Greiling 已提交
258
          new gl.Diff();
F
Fatih Acet 已提交
259 260 261
          new ZenMode();
          break;
        case 'dashboard:activity':
262
          new gl.Activities();
F
Fatih Acet 已提交
263
          break;
264 265 266 267
        case 'dashboard:issues':
        case 'dashboard:merge_requests':
          new UsersSelect();
          break;
F
Fatih Acet 已提交
268 269
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
270
          new gl.Diff();
F
Fatih Acet 已提交
271 272
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
273 274 275
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
F
Fatih Acet 已提交
276
          break;
277
        case 'projects:commit:pipelines':
278
          new MiniPipelineGraph({
279
            container: '.js-commit-pipeline-graph',
280 281
          }).bindEvents();
          break;
F
Fatih Acet 已提交
282 283 284 285 286 287 288 289
        case 'projects:commits:show':
        case 'projects:activity':
          shortcut_handler = new ShortcutsNavigation();
          break;
        case 'projects:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          break;
290
        case 'projects:edit':
291
          setupProjectEdit();
292
          break;
F
Filipa Lacerda 已提交
293
        case 'projects:pipelines:builds':
294
        case 'projects:pipelines:failures':
L
Luke Bennett 已提交
295
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
296
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
297
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
F
Filipa Lacerda 已提交
298

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

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

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

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