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 100
        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'),
        });
101 102
      }

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

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

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

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

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