dispatcher.js 12.9 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 35 36 37
/* global UsernameValidator */
/* global ActiveTabMemoizer */
/* global ShortcutsNavigation */
/* global Build */
/* global Issuable */
/* global Issue */
/* 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 ShortcutsDashboardNavigation */
/* global Project */
/* global ProjectAvatar */
/* global CompareAutocomplete */
/* global ProjectNew */
/* global Star */
/* global ProjectShow */
/* global Labels */
/* global Shortcuts */

Z
Z.J. van de Weg 已提交
38
import BindInOut from './behaviors/bind_in_out';
F
Filipa Lacerda 已提交
39
import GroupsList from './groups_list';
40
import ProjectsList from './projects_list';
41

42
const ShortcutsBlob = require('./shortcuts_blob');
43
const UserCallout = require('./user_callout');
44

F
Fatih Acet 已提交
45 46 47 48 49 50 51 52 53 54
(function() {
  var Dispatcher;

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

  Dispatcher = (function() {
    function Dispatcher() {
      this.initSearch();
55
      this.initFieldErrors();
F
Fatih Acet 已提交
56 57 58 59 60 61 62 63 64 65 66 67
      this.initPageScripts();
    }

    Dispatcher.prototype.initPageScripts = function() {
      var page, path, shortcut_handler;
      page = $('body').attr('data-page');
      if (!page) {
        return false;
      }
      path = page.split(':');
      shortcut_handler = null;
      switch (page) {
68 69
        case 'sessions:new':
          new UsernameValidator();
70
          new ActiveTabMemoizer();
71
          break;
72
        case 'projects:boards:show':
73
        case 'projects:boards:index':
74 75
          shortcut_handler = new ShortcutsNavigation();
          break;
76 77 78
        case 'projects:builds:show':
          new Build();
          break;
79
        case 'projects:merge_requests:index':
F
Fatih Acet 已提交
80
        case 'projects:issues:index':
C
Clement Ho 已提交
81
          if (gl.FilteredSearchManager) {
C
Clement Ho 已提交
82
            new gl.FilteredSearchManager(page === 'projects:issues:index' ? 'issues' : 'merge_requests');
C
Clement Ho 已提交
83
          }
F
Fatih Acet 已提交
84
          Issuable.init();
85
          new gl.IssuableBulkActions({
A
Alfredo Sumaran 已提交
86
            prefixId: page === 'projects:merge_requests:index' ? 'merge_request_' : 'issue_',
87
          });
F
Fatih Acet 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
          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();
          break;
        case 'dashboard:todos:index':
B
Bryce Johnson 已提交
101
          new gl.Todos();
F
Fatih Acet 已提交
102
          break;
103 104 105 106 107 108 109 110
        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 已提交
111 112 113 114
        case 'dashboard:groups:index':
        case 'explore:groups:index':
          new GroupsList();
          break;
F
Fatih Acet 已提交
115 116
        case 'projects:milestones:new':
        case 'projects:milestones:edit':
117
        case 'projects:milestones:update':
F
Fatih Acet 已提交
118
          new ZenMode();
119
          new gl.DueDateSelectors();
L
Luke "Jared" Bennett 已提交
120
          new gl.GLForm($('.milestone-form'));
F
Fatih Acet 已提交
121 122 123 124 125
          break;
        case 'groups:milestones:new':
          new ZenMode();
          break;
        case 'projects:compare:show':
M
Mike Greiling 已提交
126
          new gl.Diff();
F
Fatih Acet 已提交
127
          break;
128 129 130
        case 'projects:branches:index':
          gl.AjaxLoadingSpinner.init();
          break;
F
Fatih Acet 已提交
131 132 133
        case 'projects:issues:new':
        case 'projects:issues:edit':
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
134
          new gl.GLForm($('.issue-form'));
F
Fatih Acet 已提交
135
          new IssuableForm($('.issue-form'));
136 137
          new LabelsSelect();
          new MilestoneSelect();
138
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
139 140
          break;
        case 'projects:merge_requests:new':
141
        case 'projects:merge_requests:new_diffs':
F
Fatih Acet 已提交
142
        case 'projects:merge_requests:edit':
M
Mike Greiling 已提交
143
          new gl.Diff();
F
Fatih Acet 已提交
144
          shortcut_handler = new ShortcutsNavigation();
L
Luke "Jared" Bennett 已提交
145
          new gl.GLForm($('.merge-request-form'));
F
Fatih Acet 已提交
146
          new IssuableForm($('.merge-request-form'));
147 148
          new LabelsSelect();
          new MilestoneSelect();
149
          new gl.IssuableTemplateSelectors();
F
Fatih Acet 已提交
150 151 152
          break;
        case 'projects:tags:new':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
153
          new gl.GLForm($('.tag-form'));
F
Fatih Acet 已提交
154 155 156
          break;
        case 'projects:releases:edit':
          new ZenMode();
L
Luke "Jared" Bennett 已提交
157
          new gl.GLForm($('.release-form'));
F
Fatih Acet 已提交
158 159
          break;
        case 'projects:merge_requests:show':
M
Mike Greiling 已提交
160
          new gl.Diff();
F
Fatih Acet 已提交
161 162 163 164 165 166 167 168
          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 已提交
169
          new gl.Diff();
F
Fatih Acet 已提交
170 171 172 173
          new ZenMode();
          new MergedButtons();
          break;
        case 'dashboard:activity':
174
          new gl.Activities();
F
Fatih Acet 已提交
175 176 177
          break;
        case 'projects:commit:show':
          new Commit();
M
Mike Greiling 已提交
178
          new gl.Diff();
F
Fatih Acet 已提交
179 180 181
          new ZenMode();
          shortcut_handler = new ShortcutsNavigation();
          break;
182 183 184 185 186
        case 'projects:commit:pipelines':
          new gl.MiniPipelineGraph({
            container: '.js-pipeline-table',
          }).bindEvents();
          break;
F
Fatih Acet 已提交
187 188 189 190 191 192 193 194 195 196 197
        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 已提交
198
        case 'projects:pipelines:builds':
L
Luke Bennett 已提交
199
        case 'projects:pipelines:show':
F
Filipa Lacerda 已提交
200
          const { controllerAction } = document.querySelector('.js-pipeline-container').dataset;
F
Filipa Lacerda 已提交
201 202 203 204 205 206 207 208 209

          new gl.Pipelines({
            initTabs: true,
            tabsOptions: {
              action: controllerAction,
              defaultAction: 'pipelines',
              parentEl: '.pipelines-tabs',
            },
          });
L
Luke Bennett 已提交
210
          break;
F
Fatih Acet 已提交
211
        case 'groups:activity':
212
          new gl.Activities();
F
Fatih Acet 已提交
213 214 215 216 217
          break;
        case 'groups:show':
          shortcut_handler = new ShortcutsNavigation();
          new NotificationsForm();
          new NotificationsDropdown();
218
          new ProjectsList();
F
Fatih Acet 已提交
219 220
          break;
        case 'groups:group_members:index':
221
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
222
          new gl.Members();
F
Fatih Acet 已提交
223 224
          new UsersSelect();
          break;
225
        case 'projects:members:show':
226 227
          new gl.MemberExpirationDate('.js-access-expiration-date-groups');
          new GroupsSelect();
228
          new gl.MemberExpirationDate();
P
Phil Hughes 已提交
229
          new gl.Members();
F
Fatih Acet 已提交
230 231 232
          new UsersSelect();
          break;
        case 'groups:new':
L
Luke "Jared" Bennett 已提交
233
        case 'admin:groups:new':
234 235
        case 'groups:create':
        case 'admin:groups:create':
Z
Z.J. van de Weg 已提交
236
          BindInOut.initAll();
L
Luke "Jared" Bennett 已提交
237 238
        case 'groups:new':
        case 'admin:groups:new':
F
Fatih Acet 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        case 'groups:edit':
        case 'admin:groups:edit':
          new GroupAvatar();
          break;
        case 'projects:tree:show':
          shortcut_handler = new ShortcutsNavigation();
          new TreeView();
          break;
        case 'projects:find_file:show':
          shortcut_handler = true;
          break;
        case 'projects:blob:show':
        case 'projects:blame:show':
          new LineHighlighter();
          shortcut_handler = new ShortcutsNavigation();
254 255 256 257 258 259
          const fileBlobPermalinkUrlElement = document.querySelector('.js-data-file-blob-permalink-url');
          const fileBlobPermalinkUrl = fileBlobPermalinkUrlElement && fileBlobPermalinkUrlElement.getAttribute('href');
          new ShortcutsBlob({
            skipResetBindings: true,
            fileBlobPermalinkUrl,
          });
F
Fatih Acet 已提交
260
          break;
261 262
        case 'groups:labels:new':
        case 'groups:labels:edit':
F
Fatih Acet 已提交
263 264 265 266 267 268
        case 'projects:labels:new':
        case 'projects:labels:edit':
          new Labels();
          break;
        case 'projects:labels:index':
          if ($('.prioritized-labels').length) {
B
Bryce Johnson 已提交
269
            new gl.LabelManager();
F
Fatih Acet 已提交
270 271 272
          }
          break;
        case 'projects:network:show':
273 274
          // Ensure we don't create a particular shortcut handler here. This is
          // already created, where the network graph is created.
F
Fatih Acet 已提交
275 276 277 278 279 280 281 282
          shortcut_handler = true;
          break;
        case 'projects:forks:new':
          new ProjectFork();
          break;
        case 'projects:artifacts:browse':
          new BuildArtifacts();
          break;
283 284 285
        case 'help:index':
          gl.VersionCheckImage.bindErrorEvent($('img.js-version-status-badge'));
          break;
F
Fatih Acet 已提交
286 287
        case 'search:show':
          new Search();
288 289
          break;
        case 'projects:protected_branches:index':
290 291
          new gl.ProtectedBranchCreate();
          new gl.ProtectedBranchEditList();
292
          break;
293
        case 'projects:ci_cd:show':
S
Stan Hu 已提交
294 295
          new gl.ProjectVariables();
          break;
296 297 298 299
        case 'ci:lints:create':
        case 'ci:lints:show':
          new gl.CILintEditor();
          break;
300 301 302
        case 'users:show':
          new UserCallout();
          break;
F
Fatih Acet 已提交
303 304
      }
      switch (path.first()) {
305 306 307 308 309 310 311 312 313 314 315
        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 已提交
316 317 318 319 320 321 322 323
        case 'admin':
          new Admin();
          switch (path[1]) {
            case 'groups':
              new UsersSelect();
              break;
            case 'projects':
              new NamespaceSelects();
324 325
              break;
            case 'labels':
326
              switch (path[2]) {
327
                case 'new':
328 329 330
                case 'edit':
                  new Labels();
              }
331
            case 'abuse_reports':
332
              new gl.AbuseReports();
333
              break;
F
Fatih Acet 已提交
334 335 336 337 338
          }
          break;
        case 'dashboard':
        case 'root':
          shortcut_handler = new ShortcutsDashboardNavigation();
339
          new UserCallout();
F
Fatih Acet 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
          break;
        case 'profiles':
          new NotificationsForm();
          new NotificationsDropdown();
          break;
        case 'projects':
          new Project();
          new ProjectAvatar();
          switch (path[1]) {
            case 'compare':
              new CompareAutocomplete();
              break;
            case 'edit':
              shortcut_handler = new ShortcutsNavigation();
              new ProjectNew();
              break;
            case 'new':
              new ProjectNew();
              break;
            case 'show':
360
              new Star();
F
Fatih Acet 已提交
361 362 363 364 365
              new ProjectNew();
              new ProjectShow();
              new NotificationsDropdown();
              break;
            case 'wikis':
366
              new gl.Wikis();
F
Fatih Acet 已提交
367 368
              shortcut_handler = new ShortcutsNavigation();
              new ZenMode();
L
Luke "Jared" Bennett 已提交
369
              new gl.GLForm($('.wiki-form'));
F
Fatih Acet 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
              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();
          }
      }
392
      // If we haven't installed a custom shortcut handler, install the default one
F
Fatih Acet 已提交
393
      if (!shortcut_handler) {
394
        new Shortcuts();
F
Fatih Acet 已提交
395 396 397 398
      }
    };

    Dispatcher.prototype.initSearch = function() {
399
      // Only when search form is present
F
Fatih Acet 已提交
400
      if ($('.search').length) {
401
        return new gl.SearchAutocomplete();
F
Fatih Acet 已提交
402 403 404
      }
    };

405
    Dispatcher.prototype.initFieldErrors = function() {
406
      $('.gl-show-field-errors').each((i, form) => {
407 408
        new gl.GlFieldErrors(form);
      });
409 410
    };

F
Fatih Acet 已提交
411 412
    return Dispatcher;
  })();
413
}).call(window);