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.J. van de Weg's avatar
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';
Sam Rose's avatar
Sam Rose committed
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's avatar
Mike Greiling committed
47
import UserCallout from './user_callout';
48
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
geoandri's avatar
geoandri committed
49
import ShortcutsWiki from './shortcuts_wiki';
50
import Pipelines from './pipelines';
Douwe Maan's avatar
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's avatar
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's avatar
Fatih Acet committed
71 72 73 74
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
75
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
Fatih Acet's avatar
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's avatar
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's avatar
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's avatar
Clement Ho committed
141
          }
142 143 144
          const pagePrefix = page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_';
          IssuableIndex.init(pagePrefix);

Fatih Acet's avatar
Fatih Acet committed
145
          shortcut_handler = new ShortcutsNavigation();
146
          new UsersSelect();
Fatih Acet's avatar
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's avatar
Fatih Acet committed
158
          break;
159 160 161 162
        case 'groups:issues':
        case 'groups:merge_requests':
          new UsersSelect();
          break;
Fatih Acet's avatar
Fatih Acet committed
163
        case 'dashboard:todos:index':
Bryce Johnson's avatar
Bryce Johnson committed
164
          new gl.Todos();
Fatih Acet's avatar
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's avatar
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's avatar
Fatih Acet committed
192
          new ZenMode();
193
          new gl.DueDateSelectors();
194
          new gl.GLForm($('.milestone-form'), true);
Fatih Acet's avatar
Fatih Acet committed
195 196
          break;
        case 'projects:compare:show':
197
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
198
          break;
199 200
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
201
          new DeleteModal();
202
          break;
Fatih Acet's avatar
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's avatar
Fatih Acet committed
207
          new IssuableForm($('.issue-form'));
208 209
          new LabelsSelect();
          new MilestoneSelect();
210
          new gl.IssuableTemplateSelectors();
Fatih Acet's avatar
Fatih Acet committed
211 212
          break;
        case 'projects:merge_requests:new':
213
        case 'projects:merge_requests:new_diffs':
Fatih Acet's avatar
Fatih Acet committed
214
        case 'projects:merge_requests:edit':
215
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
216
          shortcut_handler = new ShortcutsNavigation();
217
          new gl.GLForm($('.merge-request-form'), true);
Fatih Acet's avatar
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's avatar
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's avatar
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's avatar
Fatih Acet committed
241 242
        case 'projects:releases:edit':
          new ZenMode();
243
          new gl.GLForm($('.release-form'), true);
Fatih Acet's avatar
Fatih Acet committed
244 245
          break;
        case 'projects:merge_requests:show':
246
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
247 248 249 250
          shortcut_handler = new ShortcutsIssuable(true);
          new ZenMode();
          break;
        case "projects:merge_requests:diffs":
251
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
252 253 254
          new ZenMode();
          break;
        case 'dashboard:activity':
255
          new gl.Activities();
Fatih Acet's avatar
Fatih Acet committed
256
          break;
257 258 259 260
        case 'dashboard:issues':
        case 'dashboard:merge_requests':
          new UsersSelect();
          break;
Fatih Acet's avatar
Fatih Acet committed
261 262
        case 'projects:commit:show':
          new Commit();
263
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
264 265
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
266 267 268
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
Fatih Acet's avatar
Fatih Acet committed
269
          break;
270
        case 'projects:commit:pipelines':
271
          new MiniPipelineGraph({
272
            container: '.js-commit-pipeline-graph',
273 274
          }).bindEvents();
          break;
Fatih Acet's avatar
Fatih Acet committed
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();
284 285
          }
          if ($('.blob-viewer').length) {
286
            new BlobViewer();
Fatih Acet's avatar
Fatih Acet committed
287 288
          }
          break;
289
        case 'projects:edit':
290
          setupProjectEdit();
291
          break;
Filipa Lacerda's avatar
Filipa Lacerda committed
292
        case 'projects:pipelines:builds':
293
        case 'projects:pipelines:failures':
Luke Bennett's avatar
Luke Bennett committed
294
        case 'projects:pipelines:show':
Filipa Lacerda's avatar
Filipa Lacerda committed
295
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
296
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
Filipa Lacerda's avatar
Filipa Lacerda committed
297

298
          new Pipelines({
Filipa Lacerda's avatar
Filipa Lacerda committed
299
            initTabs: true,
300
            pipelineStatusUrl,
Filipa Lacerda's avatar
Filipa Lacerda committed
301 302 303 304 305 306
            tabsOptions: {
              action: controllerAction,
              defaultAction: 'pipelines',
              parentEl: '.pipelines-tabs',
            },
          });
Luke Bennett's avatar
Luke Bennett committed
307
          break;
Fatih Acet's avatar
Fatih Acet committed
308
        case 'groups:activity':
309
          new gl.Activities();
Fatih Acet's avatar
Fatih Acet committed
310 311 312 313 314
          break;
        case 'groups:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          new NotificationsDropdown();
315
          new ProjectsList();
Fatih Acet's avatar
Fatih Acet committed
316 317
          break;
        case 'groups:group_members:index':
318
          new gl.MemberExpirationDate();
319
          new gl.Members();
Fatih Acet's avatar
Fatih Acet committed
320 321
          new UsersSelect();
          break;
322
        case 'projects:members:show':
323 324
          new gl.MemberExpirationDate('.js-access-expiration-date-groups');
          new GroupsSelect();
325
          new gl.MemberExpirationDate();
326
          new gl.Members();
Fatih Acet's avatar
Fatih Acet committed
327 328 329
          new UsersSelect();
          break;
        case 'groups:new':
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
330
        case 'admin:groups:new':
331 332
        case 'groups:create':
        case 'admin:groups:create':
Z.J. van de Weg's avatar
Z.J. van de Weg committed
333
          BindInOut.initAll();
334 335 336
          new Group();
          new GroupAvatar();
          break;
Fatih Acet's avatar
Fatih Acet committed
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();
344
          new BlobViewer();
Fatih Acet's avatar
Fatih Acet committed
345 346 347 348 349
          break;
        case 'projects:find_file:show':
          shortcut_handler = true;
          break;
        case 'projects:blob:show':
Douwe Maan's avatar
Douwe Maan committed
350
          new BlobViewer();
351
          initBlob();
352
          break;
Fatih Acet's avatar
Fatih Acet committed
353
        case 'projects:blame:show':
354
          initBlob();
Fatih Acet's avatar
Fatih Acet committed
355
          break;
356 357
        case 'groups:labels:new':
        case 'groups:labels:edit':
Fatih Acet's avatar
Fatih Acet committed
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) {
364
            new gl.LabelManager();
Fatih Acet's avatar
Fatih Acet committed
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.
Fatih Acet's avatar
Fatih Acet committed
370 371 372 373 374 375
          shortcut_handler = true;
          break;
        case 'projects:forks:new':
          new ProjectFork();
          break;
        case 'projects:artifacts:browse':
376
          new ShortcutsNavigation();
Fatih Acet's avatar
Fatih Acet committed
377 378
          new BuildArtifacts();
          break;
379
        case 'projects:artifacts:file':
380
          new ShortcutsNavigation();
381 382
          new BlobViewer();
          break;
383 384 385
        case 'help:index':
          gl.VersionCheckImage.bindErrorEvent($('img.js-version-status-badge'));
          break;
Fatih Acet's avatar
Fatih Acet committed
386 387
        case 'search:show':
          new Search();
388
          break;
389
        case 'projects:repository:show':
390
          // Initialize Protected Branch Settings
391 392
          new gl.ProtectedBranchCreate();
          new gl.ProtectedBranchEditList();
393
          // Initialize Protected Tag Settings
394 395
          new ProtectedTagCreate();
          new ProtectedTagEditList();
396 397
          // Initialize expandable settings panels
          initSettingsPanels();
398
          break;
399
        case 'projects:ci_cd:show':
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;
Douwe Maan's avatar
Douwe Maan committed
412 413 414 415
        case 'snippets:show':
          new LineHighlighter();
          new BlobViewer();
          break;
416 417 418
        case 'import:fogbugz:new_user_map':
          new UsersSelect();
          break;
Fatih Acet's avatar
Fatih Acet committed
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();
Fatih Acet's avatar
Fatih Acet committed
432 433 434
        case 'admin':
          new Admin();
          switch (path[1]) {
435
            case 'cohorts':
436
              new gl.UsagePing();
437
              break;
Fatih Acet's avatar
Fatih Acet committed
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;
Fatih Acet's avatar
Fatih Acet committed
453 454 455 456
          }
          break;
        case 'dashboard':
        case 'root':
457
          new UserCallout();
Fatih Acet's avatar
Fatih Acet committed
458
          break;
Sam Rose's avatar
Sam Rose committed
459 460 461
        case 'groups':
          new GroupName();
          break;
Fatih Acet's avatar
Fatih Acet committed
462 463 464 465 466 467 468
        case 'profiles':
          new NotificationsForm();
          new NotificationsDropdown();
          break;
        case 'projects':
          new Project();
          new ProjectAvatar();
Sam Rose's avatar
Sam Rose committed
469
          new GroupName();
Fatih Acet's avatar
Fatih Acet committed
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();
Fatih Acet's avatar
Fatih Acet committed
483 484 485 486 487
              new ProjectNew();
              new ProjectShow();
              new NotificationsDropdown();
              break;
            case 'wikis':
488
              new gl.Wikis();
489
              shortcut_handler = new ShortcutsWiki();
Fatih Acet's avatar
Fatih Acet committed
490
              new ZenMode();
491
              new gl.GLForm($('.wiki-form'), true);
Fatih Acet's avatar
Fatih Acet committed
492 493 494 495 496
              break;
            case 'snippets':
              shortcut_handler = new ShortcutsNavigation();
              if (path[2] === 'show') {
                new ZenMode();
Douwe Maan's avatar
Douwe Maan committed
497 498
                new LineHighlighter();
                new BlobViewer();
Fatih Acet's avatar
Fatih Acet committed
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
Fatih Acet's avatar
Fatih Acet committed
517
      if (!shortcut_handler) {
518
        new Shortcuts();
Fatih Acet's avatar
Fatih Acet committed
519 520 521 522
      }
    };

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

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

Fatih Acet's avatar
Fatih Acet committed
535 536
    return Dispatcher;
  })();
537
}).call(window);