dispatcher.js 13.0 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

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

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

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

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

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

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

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

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