dispatcher.js 14.8 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 36
/* global Sidebar */

37
import Issue from './issue';
38

Z
Z.J. van de Weg 已提交
39
import BindInOut from './behaviors/bind_in_out';
40
import Group from './group';
S
Sam Rose 已提交
41
import GroupName from './group_name';
F
Filipa Lacerda 已提交
42
import GroupsList from './groups_list';
43
import ProjectsList from './projects_list';
44
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
45
import BlobLinePermalinkUpdater from './blob/blob_line_permalink_updater';
E
Eric Eastwood 已提交
46
import BlobForkSuggestion from './blob/blob_fork_suggestion';
M
Mike Greiling 已提交
47
import UserCallout from './user_callout';
K
Kushal Pandya 已提交
48
import { ProtectedTagCreate, ProtectedTagEditList } from './protected_tags';
49

50 51
const ShortcutsBlob = require('./shortcuts_blob');

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

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

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

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

      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 已提交
91 92 93 94 95 96

        new BlobForkSuggestion(
          document.querySelector('.js-edit-blob-link-fork-toggler'),
          document.querySelector('.js-cancel-fork-suggestion'),
          document.querySelector('.js-file-fork-suggestion-section'),
        );
97 98
      }

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

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

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

461
    Dispatcher.prototype.initFieldErrors = function() {
462
      $('.gl-show-field-errors').each((i, form) => {
463 464
        new gl.GlFieldErrors(form);
      });
465 466
    };

F
Fatih Acet 已提交
467 468
    return Dispatcher;
  })();
469
}).call(window);