dispatcher.js 16.0 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 5 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 32 33
/* global UsernameValidator */
/* global ActiveTabMemoizer */
/* global ShortcutsNavigation */
/* global Build */
/* global Issuable */
/* global ShortcutsIssuable */
/* global ZenMode */
/* global Milestone */
/* global IssuableForm */
/* global LabelsSelect */
/* global MilestoneSelect */
/* global Commit */
/* global NotificationsForm */
/* global TreeView */
/* global NotificationsDropdown */
/* global UsersSelect */
/* 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 */
34
/* global Sidebar */
35
/* global ShortcutsWiki */
36

37
import Issue from './issue';
38

Z
Z.J. van de Weg 已提交
39
import BindInOut from './behaviors/bind_in_out';
40
import DeleteModal from './branches/branches_delete_modal';
41
import Group from './group';
S
Sam Rose 已提交
42
import GroupName from './group_name';
F
Filipa Lacerda 已提交
43
import GroupsList from './groups_list';
44
import ProjectsList from './projects_list';
45
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
46
import BlobLinePermalinkUpdater from './blob/blob_line_permalink_updater';
47
import Landing from './landing';
E
Eric Eastwood 已提交
48
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
49
import UserCallout from './user_callout';
K
Kushal Pandya 已提交
50
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
G
geoandri 已提交
51
import ShortcutsWiki from './shortcuts_wiki';
52
import Pipelines from './pipelines';
D
Douwe Maan 已提交
53
import BlobViewer from './blob/viewer/index';
54
import AutoWidthDropdownSelect from './issuable/auto_width_dropdown_select';
55

56 57
const ShortcutsBlob = require('./shortcuts_blob');

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

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

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

    Dispatcher.prototype.initPageScripts = function() {
73
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
F
Fatih Acet 已提交
74 75 76 77 78 79
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
      path = page.split(':');
      shortcut_handler = null;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

      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 已提交
97

98 99 100 101 102 103
        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'),
104 105
        })
          .init();
106 107
      }

F
Fatih Acet 已提交
108
      switch (page) {
109 110
        case 'sessions:new':
          new UsernameValidator();
111
          new ActiveTabMemoizer();
112
          break;
113
        case 'projects:boards:show':
114
        case 'projects:boards:index':
115 116
          shortcut_handler = new ShortcutsNavigation();
          break;
117 118 119
        case 'projects:builds:show':
          new Build();
          break;
120
        case 'projects:merge_requests:index':
F
Fatih Acet 已提交
121
        case 'projects:issues:index':
C
Clement Ho 已提交
122
          if (gl.FilteredSearchManager) {
C
Clement Ho 已提交
123
            new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
C
Clement Ho 已提交
124
          }
F
Fatih Acet 已提交
125
          Issuable.init();
126
          new gl.IssuableBulkActions({
A
Alfredo Sumaran 已提交
127
            prefixId: page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_',
128
          });
F
Fatih Acet 已提交
129 130 131 132 133 134 135 136 137 138 139
          shortcut_handler = new ShortcutsNavigation();
          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();
140
          new Sidebar();
F
Fatih Acet 已提交
141 142
          break;
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
143
          new gl.Todos();
F
Fatih Acet 已提交
144
          break;
145 146 147 148 149 150 151 152
        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 已提交
153
        case 'dashboard:groups:index':
154 155
          new GroupsList();
          break;
F
Filipa Lacerda 已提交
156 157
        case 'explore:groups:index':
          new GroupsList();
158 159 160 161 162 163 164 165 166

          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 已提交
167
          break;
F
Fatih Acet 已提交
168 169
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
170
        case 'projects:milestones:update':
171 172 173
        case 'groups:milestones:new':
        case 'groups:milestones:edit':
        case 'groups:milestones:update':
F
Fatih Acet 已提交
174
          new ZenMode();
175
          new gl.DueDateSelectors();
L
Luke "Jared" Bennett 已提交
176
          new gl.GLForm($('.milestone-form'));
F
Fatih Acet 已提交
177 178
          break;
        case 'projects:compare:show':
M
Mike Greiling 已提交
179
          new gl.Diff();
F
Fatih Acet 已提交
180
          break;
181 182
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
183
          new DeleteModal();
184
          break;
F
Fatih Acet 已提交
185 186 187
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
188
          new gl.GLForm($('.issue-form'));
F
Fatih Acet 已提交
189
          new IssuableForm($('.issue-form'));
190 191
          new LabelsSelect();
          new MilestoneSelect();
192
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
193 194
          break;
        case 'projects:merge_requests:new':
195
        case 'projects:merge_requests:new_diffs':
F
Fatih Acet 已提交
196
        case 'projects:merge_requests:edit':
M
Mike Greiling 已提交
197
          new gl.Diff();
F
Fatih Acet 已提交
198
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
199
          new gl.GLForm($('.merge-request-form'));
F
Fatih Acet 已提交
200
          new IssuableForm($('.merge-request-form'));
201 202
          new LabelsSelect();
          new MilestoneSelect();
203
          new gl.IssuableTemplateSelectors();
204
          new AutoWidthDropdownSelect($('.js-target-branch-select')).init();
F
Fatih Acet 已提交
205 206 207
          break;
        case 'projects:tags:new':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
208
          new gl.GLForm($('.tag-form'));
F
Fatih Acet 已提交
209 210 211
          break;
        case 'projects:releases:edit':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
212
          new gl.GLForm($('.release-form'));
F
Fatih Acet 已提交
213 214
          break;
        case 'projects:merge_requests:show':
M
Mike Greiling 已提交
215
          new gl.Diff();
F
Fatih Acet 已提交
216 217 218 219
          shortcut_handler = new ShortcutsIssuable(true);
          new ZenMode();
          break;
        case "projects:merge_requests:diffs":
M
Mike Greiling 已提交
220
          new gl.Diff();
F
Fatih Acet 已提交
221 222 223
          new ZenMode();
          break;
        case 'dashboard:activity':
224
          new gl.Activities();
F
Fatih Acet 已提交
225 226 227
          break;
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
228
          new gl.Diff();
F
Fatih Acet 已提交
229 230
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
231 232 233
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
F
Fatih Acet 已提交
234
          break;
235
        case 'projects:commit:pipelines':
236
          new MiniPipelineGraph({
237
            container: '.js-commit-pipeline-graph',
238 239
          }).bindEvents();
          break;
F
Fatih Acet 已提交
240 241 242 243 244 245 246 247 248
        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 已提交
249
            new BlobViewer();
F
Fatih Acet 已提交
250 251
          }
          break;
F
Filipa Lacerda 已提交
252
        case 'projects:pipelines:builds':
253
        case 'projects:pipelines:failures':
L
Luke Bennett 已提交
254
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
255
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
256
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
F
Filipa Lacerda 已提交
257

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

    Dispatcher.prototype.initSearch = function() {
484
      // Only when search form is present
F
Fatih Acet 已提交
485
      if ($('.search').length) {
486
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
487 488 489
      }
    };

490
    Dispatcher.prototype.initFieldErrors = function() {
491
      $('.gl-show-field-errors').each((i, form) => {
492 493
        new gl.GlFieldErrors(form);
      });
494 495
    };

F
Fatih Acet 已提交
496 497
    return Dispatcher;
  })();
498
}).call(window);