dispatcher.js 15.9 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 34
/* global UsernameValidator */
/* global ActiveTabMemoizer */
/* global ShortcutsNavigation */
/* global Build */
/* global Issuable */
/* global ShortcutsIssuable */
/* global ZenMode */
/* global Milestone */
/* global IssuableForm */
/* global LabelsSelect */
/* global MilestoneSelect */
/* global MergedButtons */
/* 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 */
35
/* global Sidebar */
36
/* global ShortcutsWiki */
37

38
import Issue from './issue';
39

Z
Z.J. van de Weg 已提交
40
import BindInOut from './behaviors/bind_in_out';
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';
D
Douwe Maan 已提交
52
import BlobViewer from './blob/viewer/index';
53

54 55
const ShortcutsBlob = require('./shortcuts_blob');

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

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

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

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

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

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

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

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

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

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

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

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