BigW Consortium Gitlab

dispatcher.js 17.6 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.J. van de Weg committed
36
import BindInOut from './behaviors/bind_in_out';
37
import DeleteModal from './branches/branches_delete_modal';
38
import Group from './group';
39
import GroupName from './group_name';
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';
46
import BlobForkSuggestion from './blob/blob_fork_suggestion';
Mike Greiling committed
47
import UserCallout from './user_callout';
48
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
geoandri committed
49
import ShortcutsWiki from './shortcuts_wiki';
50
import Pipelines from './pipelines';
Douwe Maan committed
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';
58
import initExperimentalFlags from './experimental_flags';
59

Fatih Acet committed
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();
Fatih Acet committed
71 72 73 74
      this.initPageScripts();
    }

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

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

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      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,
        });
112

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

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

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

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

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

    Dispatcher.prototype.initSearch = function() {
519
      // Only when search form is present
Fatih Acet committed
520
      if ($('.search').length) {
521
        return new gl.SearchAutocomplete();
Fatih Acet committed
522 523 524
      }
    };

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

Fatih Acet committed
531 532
    return Dispatcher;
  })();
533
}).call(window);