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

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

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

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

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