dispatcher.js 15.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';
E
Eric Eastwood 已提交
47
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
48
import UserCallout from './user_callout';
K
Kushal Pandya 已提交
49
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
G
geoandri 已提交
50
import ShortcutsWiki from './shortcuts_wiki';
51

52 53
const ShortcutsBlob = require('./shortcuts_blob');

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

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

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

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

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

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

F
Fatih Acet 已提交
104
      switch (page) {
105 106
        case 'sessions:new':
          new UsernameValidator();
107
          new ActiveTabMemoizer();
108
          break;
109
        case 'projects:boards:show':
110
        case 'projects:boards:index':
111 112
          shortcut_handler = new ShortcutsNavigation();
          break;
113 114 115
        case 'projects:builds:show':
          new Build();
          break;
116
        case 'projects:merge_requests:index':
F
Fatih Acet 已提交
117
        case 'projects:issues:index':
C
Clement Ho 已提交
118
          if (gl.FilteredSearchManager) {
C
Clement Ho 已提交
119
            new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
C
Clement Ho 已提交
120
          }
F
Fatih Acet 已提交
121
          Issuable.init();
122
          new gl.IssuableBulkActions({
A
Alfredo Sumaran 已提交
123
            prefixId: page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_',
124
          });
F
Fatih Acet 已提交
125 126 127 128 129 130 131 132 133 134 135
          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();
136
          new Sidebar();
F
Fatih Acet 已提交
137 138
          break;
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
139
          new gl.Todos();
F
Fatih Acet 已提交
140
          break;
141 142 143 144 145 146 147 148
        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 已提交
149 150 151 152
        case 'dashboard:groups:index':
        case 'explore:groups:index':
          new GroupsList();
          break;
F
Fatih Acet 已提交
153 154
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
155
        case 'projects:milestones:update':
156 157 158
        case 'groups:milestones:new':
        case 'groups:milestones:edit':
        case 'groups:milestones:update':
F
Fatih Acet 已提交
159
          new ZenMode();
160
          new gl.DueDateSelectors();
L
Luke "Jared" Bennett 已提交
161
          new gl.GLForm($('.milestone-form'));
F
Fatih Acet 已提交
162 163
          break;
        case 'projects:compare:show':
M
Mike Greiling 已提交
164
          new gl.Diff();
F
Fatih Acet 已提交
165
          break;
166 167 168
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
          break;
F
Fatih Acet 已提交
169 170 171
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
172
          new gl.GLForm($('.issue-form'));
F
Fatih Acet 已提交
173
          new IssuableForm($('.issue-form'));
174 175
          new LabelsSelect();
          new MilestoneSelect();
176
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
177 178
          break;
        case 'projects:merge_requests:new':
179
        case 'projects:merge_requests:new_diffs':
F
Fatih Acet 已提交
180
        case 'projects:merge_requests:edit':
M
Mike Greiling 已提交
181
          new gl.Diff();
F
Fatih Acet 已提交
182
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
183
          new gl.GLForm($('.merge-request-form'));
F
Fatih Acet 已提交
184
          new IssuableForm($('.merge-request-form'));
185 186
          new LabelsSelect();
          new MilestoneSelect();
187
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
188 189 190
          break;
        case 'projects:tags:new':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
191
          new gl.GLForm($('.tag-form'));
F
Fatih Acet 已提交
192 193 194
          break;
        case 'projects:releases:edit':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
195
          new gl.GLForm($('.release-form'));
F
Fatih Acet 已提交
196 197
          break;
        case 'projects:merge_requests:show':
M
Mike Greiling 已提交
198
          new gl.Diff();
F
Fatih Acet 已提交
199 200 201 202 203 204 205 206
          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 已提交
207
          new gl.Diff();
F
Fatih Acet 已提交
208 209 210 211
          new ZenMode();
          new MergedButtons();
          break;
        case 'dashboard:activity':
212
          new gl.Activities();
F
Fatih Acet 已提交
213 214 215
          break;
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
216
          new gl.Diff();
F
Fatih Acet 已提交
217 218
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
219 220 221
          new MiniPipelineGraph({
            container: '.js-commit-pipeline-graph',
          }).bindEvents();
F
Fatih Acet 已提交
222
          break;
223
        case 'projects:commit:pipelines':
224
          new MiniPipelineGraph({
225
            container: '.js-commit-pipeline-graph',
226 227
          }).bindEvents();
          break;
F
Fatih Acet 已提交
228 229 230 231 232 233 234 235 236 237 238
        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 已提交
239
        case 'projects:pipelines:builds':
L
Luke Bennett 已提交
240
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
241
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
242
          const pipelineStatusUrl = `${document.querySelector('.js-pipeline-tab-link a').getAttribute('href')}/status.json`;
F
Filipa Lacerda 已提交
243 244 245

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

    Dispatcher.prototype.initSearch = function() {
459
      // Only when search form is present
F
Fatih Acet 已提交
460
      if ($('.search').length) {
461
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
462 463 464
      }
    };

465
    Dispatcher.prototype.initFieldErrors = function() {
466
      $('.gl-show-field-errors').each((i, form) => {
467 468
        new gl.GlFieldErrors(form);
      });
469 470
    };

F
Fatih Acet 已提交
471 472
    return Dispatcher;
  })();
473
}).call(window);