dispatcher.js 15.3 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.J. van de Weg's avatar
Z.J. van de Weg committed
40
import BindInOut from './behaviors/bind_in_out';
41
import Group from './group';
Sam Rose's avatar
Sam Rose committed
42
import GroupName from './group_name';
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 BlobForkSuggestion from './blob/blob_fork_suggestion';
Mike Greiling's avatar
Mike Greiling committed
48
import UserCallout from './user_callout';
49
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
geoandri's avatar
geoandri committed
50
import ShortcutsWiki from './shortcuts_wiki';
51
import SettingsDeployKeys from './settings/settings_repository';
Douwe Maan's avatar
Douwe Maan committed
52
import BlobViewer from './blob/viewer/index';
53

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

Fatih Acet's avatar
Fatih Acet committed
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();
Fatih Acet's avatar
Fatih Acet committed
67 68 69 70
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
71
      var page, path, shortcut_handler, fileBlobPermalinkUrlElement, fileBlobPermalinkUrl;
Fatih Acet's avatar
Fatih Acet committed
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,
        });
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
      }

Fatih Acet's avatar
Fatih Acet committed
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':
Fatih Acet's avatar
Fatih Acet committed
119
        case 'projects:issues:index':
Clement Ho's avatar
Clement Ho committed
120
          if (gl.FilteredSearchManager) {
121
            new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
Clement Ho's avatar
Clement Ho committed
122
          }
Fatih Acet's avatar
Fatih Acet committed
123
          Issuable.init();
124
          new gl.IssuableBulkActions({
Alfredo Sumaran's avatar
Alfredo Sumaran committed
125
            prefixId: page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_',
126
          });
Fatih Acet's avatar
Fatih Acet committed
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();
Fatih Acet's avatar
Fatih Acet committed
139 140
          break;
        case 'dashboard:todos:index':
Bryce Johnson's avatar
Bryce Johnson committed
141
          new gl.Todos();
Fatih Acet's avatar
Fatih Acet committed
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;
151 152 153 154
        case 'dashboard:groups:index':
        case 'explore:groups:index':
          new GroupsList();
          break;
Fatih Acet's avatar
Fatih Acet committed
155 156
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
157
        case 'projects:milestones:update':
158 159 160
        case 'groups:milestones:new':
        case 'groups:milestones:edit':
        case 'groups:milestones:update':
Fatih Acet's avatar
Fatih Acet committed
161
          new ZenMode();
162
          new gl.DueDateSelectors();
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
163
          new gl.GLForm($('.milestone-form'));
Fatih Acet's avatar
Fatih Acet committed
164 165
          break;
        case 'projects:compare:show':
166
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
167
          break;
168 169 170
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
          break;
Fatih Acet's avatar
Fatih Acet committed
171 172 173
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
174
          new gl.GLForm($('.issue-form'));
Fatih Acet's avatar
Fatih Acet committed
175
          new IssuableForm($('.issue-form'));
176 177
          new LabelsSelect();
          new MilestoneSelect();
178
          new gl.IssuableTemplateSelectors();
Fatih Acet's avatar
Fatih Acet committed
179 180
          break;
        case 'projects:merge_requests:new':
181
        case 'projects:merge_requests:new_diffs':
Fatih Acet's avatar
Fatih Acet committed
182
        case 'projects:merge_requests:edit':
183
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
184
          shortcut_handler = new ShortcutsNavigation();
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
185
          new gl.GLForm($('.merge-request-form'));
Fatih Acet's avatar
Fatih Acet committed
186
          new IssuableForm($('.merge-request-form'));
187 188
          new LabelsSelect();
          new MilestoneSelect();
189
          new gl.IssuableTemplateSelectors();
Fatih Acet's avatar
Fatih Acet committed
190 191 192
          break;
        case 'projects:tags:new':
          new ZenMode();
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
193
          new gl.GLForm($('.tag-form'));
Fatih Acet's avatar
Fatih Acet committed
194 195 196
          break;
        case 'projects:releases:edit':
          new ZenMode();
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
197
          new gl.GLForm($('.release-form'));
Fatih Acet's avatar
Fatih Acet committed
198 199
          break;
        case 'projects:merge_requests:show':
200
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
201 202 203 204 205 206 207 208
          shortcut_handler = new ShortcutsIssuable(true);
          new ZenMode();
          new MergedButtons();
          break;
        case 'projects:merge_requests:commits':
          new MergedButtons();
          break;
        case "projects:merge_requests:diffs":
209
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
210 211 212 213
          new ZenMode();
          new MergedButtons();
          break;
        case 'dashboard:activity':
214
          new gl.Activities();
Fatih Acet's avatar
Fatih Acet committed
215 216 217
          break;
        case 'projects:commit:show':
          new Commit();
218
          new gl.Diff();
Fatih Acet's avatar
Fatih Acet committed
219 220
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
221 222 223
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
Fatih Acet's avatar
Fatih Acet committed
224
          break;
225
        case 'projects:commit:pipelines':
226
          new MiniPipelineGraph({
227
            container: '.js-commit-pipeline-graph',
228 229
          }).bindEvents();
          break;
Fatih Acet's avatar
Fatih Acet committed
230 231 232 233 234 235 236 237 238 239 240
        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;
Filipa Lacerda's avatar
Filipa Lacerda committed
241
        case 'projects:pipelines:builds':
Luke Bennett's avatar
Luke Bennett committed
242
        case 'projects:pipelines:show':
Filipa Lacerda's avatar
Filipa Lacerda committed
243
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
244
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
Filipa Lacerda's avatar
Filipa Lacerda committed
245 246 247

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

    Dispatcher.prototype.initSearch = function() {
464
      // Only when search form is present
Fatih Acet's avatar
Fatih Acet committed
465
      if ($('.search').length) {
466
        return new gl.SearchAutocomplete();
Fatih Acet's avatar
Fatih Acet committed
467 468 469
      }
    };

470
    Dispatcher.prototype.initFieldErrors = function() {
471
      $('.gl-show-field-errors').each((i, form) => {
472 473
        new gl.GlFieldErrors(form);
      });
474 475
    };

Fatih Acet's avatar
Fatih Acet committed
476 477
    return Dispatcher;
  })();
478
}).call(window);