dispatcher.js 13.1 KB
Newer Older
1
import PrometheusGraph from './monitoring/prometheus_graph'; // TODO: Maybe Make this a bundle
2
/* 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 */
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
/* 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 ShortcutsDashboardNavigation */
/* global Project */
/* global ProjectAvatar */
/* global CompareAutocomplete */
/* global ProjectNew */
/* global Star */
/* global ProjectShow */
/* global Labels */
/* global Shortcuts */
37
import Issue from './issue';
38

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

44
const ShortcutsBlob = require('./shortcuts_blob');
45
const UserCallout = require('./user_callout');
46

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

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

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

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

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

409
    Dispatcher.prototype.initFieldErrors = function() {
410
      $('.gl-show-field-errors').each((i, form) => {
411 412
        new gl.GlFieldErrors(form);
      });
413 414
    };

F
Fatih Acet 已提交
415 416
    return Dispatcher;
  })();
417
}).call(window);