main.js 10.9 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, quotes, consistent-return, prefer-arrow-callback, comma-dangle, object-shorthand, no-new, max-len, no-multi-spaces, import/newline-after-import, import/first */
2 3 4 5
/* global Flash */
/* global ConfirmDangerModal */
/* global Aside */

6 7 8 9 10
import jQuery from 'jquery';
import _ from 'underscore';
import Cookies from 'js-cookie';
import Dropzone from 'dropzone';
import Sortable from 'vendor/Sortable';
11
import svg4everybody from 'svg4everybody';
12 13

// libraries with import side-effects
14 15 16
import 'mousetrap';
import 'mousetrap/plugins/pause/mousetrap-pause';
import 'vendor/fuzzaldrin-plus';
17 18 19 20 21 22 23 24 25

// expose common libraries as globals (TODO: remove these)
window.jQuery = jQuery;
window.$ = jQuery;
window._ = _;
window.Dropzone = Dropzone;
window.Sortable = Sortable;

// shortcuts
26 27 28 29 30 31 32
import './shortcuts';
import './shortcuts_blob';
import './shortcuts_dashboard_navigation';
import './shortcuts_navigation';
import './shortcuts_find_file';
import './shortcuts_issuable';
import './shortcuts_network';
33 34

// templates
35 36
import './templates/issuable_template_selector';
import './templates/issuable_template_selectors';
37 38

// commit
39 40
import './commit/file';
import './commit/image_file';
41 42

// lib/utils
43
import './lib/utils/bootstrap_linked_tabs';
44
import { handleLocationHash } from './lib/utils/common_utils';
45 46 47 48
import './lib/utils/datetime_utility';
import './lib/utils/pretty_time';
import './lib/utils/text_utility';
import './lib/utils/url_utility';
49

50 51 52
// behaviors
import './behaviors/';

53
// u2f
54 55 56 57
import './u2f/authenticate';
import './u2f/error';
import './u2f/register';
import './u2f/util';
58 59

// everything else
60 61 62 63 64 65 66
import './abuse_reports';
import './activities';
import './admin';
import './ajax_loading_spinner';
import './api';
import './aside';
import './autosave';
67
import loadAwardsHandler from './awards_handler';
68
import bp from './breakpoints';
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
import './broadcast_message';
import './build';
import './build_artifacts';
import './build_variables';
import './ci_lint_editor';
import './commit';
import './commits';
import './compare';
import './compare_autocomplete';
import './confirm_danger_modal';
import './copy_as_gfm';
import './copy_to_clipboard';
import './create_label';
import './diff';
import './dropzone_input';
import './due_date_select';
import './files_comment_button';
import './flash';
import './gl_dropdown';
import './gl_field_error';
import './gl_field_errors';
import './gl_form';
import './group_avatar';
import './group_label_subscription';
import './groups_select';
import './header';
import './importer_status';
96
import './issuable_index';
97 98 99 100 101 102 103 104
import './issuable_context';
import './issuable_form';
import './issue';
import './issue_status_select';
import './label_manager';
import './labels';
import './labels_select';
import './layout_nav';
105
import LazyLoader from './lazy_loader';
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
import './line_highlighter';
import './logo';
import './member_expiration_date';
import './members';
import './merge_request';
import './merge_request_tabs';
import './milestone';
import './milestone_select';
import './mini_pipeline_graph_dropdown';
import './namespace_select';
import './new_branch_form';
import './new_commit_form';
import './notes';
import './notifications_dropdown';
import './notifications_form';
import './pager';
import './pipelines';
import './preview_markdown';
import './project';
import './project_avatar';
import './project_find_file';
import './project_fork';
import './project_import';
import './project_label_subscription';
import './project_new';
import './project_select';
import './project_show';
import './project_variables';
K
kushalpandya 已提交
134
import './projects_dropdown';
135
import './projects_list';
136
import './syntax_highlight';
137
import './render_math';
138
import './render_gfm';
139 140 141 142 143 144 145
import './right_sidebar';
import './search';
import './search_autocomplete';
import './smart_interval';
import './star';
import './subscription';
import './subscription_select';
146
import initBreadcrumbs from './breadcrumb';
M
Mike Greiling 已提交
147

148 149
import './dispatcher';

M
Mike Greiling 已提交
150
// eslint-disable-next-line global-require, import/no-commonjs
M
Mike Greiling 已提交
151
if (process.env.NODE_ENV !== 'production') require('./test_utils/');
152

153 154
Dropzone.autoDiscover = false;

155 156
svg4everybody();

157 158 159 160 161
document.addEventListener('beforeunload', function () {
  // Unbind scroll events
  $(document).off('scroll');
  // Close any open tooltips
  $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
A
Alexis Reigel 已提交
162 163
  // Close any open popover
  $('[data-toggle="popover"]').popover('destroy');
164
});
F
Fatih Acet 已提交
165

166
window.addEventListener('hashchange', handleLocationHash);
167 168
window.addEventListener('load', function onLoad() {
  window.removeEventListener('load', onLoad, false);
169
  handleLocationHash();
170
}, false);
171

172 173 174 175 176
gl.lazyLoader = new LazyLoader({
  scrollContainer: window,
  observerNode: '#content-body'
});

177 178 179 180 181 182 183 184
$(function () {
  var $body = $('body');
  var $document = $(document);
  var $window = $(window);
  var $sidebarGutterToggle = $('.js-sidebar-toggle');
  var $flash = $('.flash-container');
  var bootstrapBreakpoint = bp.getBreakpointSize();
  var fitSidebarForSize;
185

186 187
  initBreadcrumbs();

188 189
  // Set the default path for all cookies to GitLab's root directory
  Cookies.defaults.path = gon.relative_url_root || '/';
190

191 192 193 194
  // `hashchange` is not triggered when link target is already in window.location
  $body.on('click', 'a[href^="#"]', function() {
    var href = this.getAttribute('href');
    if (href.substr(1) === gl.utils.getLocationHash()) {
195
      setTimeout(handleLocationHash, 1);
196 197
    }
  });
W
winniehell 已提交
198

199 200 201 202 203 204 205 206
  if (bootstrapBreakpoint === 'xs') {
    const $rightSidebar = $('aside.right-sidebar, .page-with-sidebar');

    $rightSidebar
      .removeClass('right-sidebar-expanded')
      .addClass('right-sidebar-collapsed');
  }

207 208 209 210 211 212
  // prevent default action for disabled buttons
  $('.btn').click(function(e) {
    if ($(this).hasClass('disabled')) {
      e.preventDefault();
      e.stopImmediatePropagation();
      return false;
F
Fatih Acet 已提交
213
    }
214 215 216 217
  });

  $('.js-select-on-focus').on('focusin', function () {
    return $(this).select().one('mouseup', function (e) {
F
Fatih Acet 已提交
218 219
      return e.preventDefault();
    });
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  // Click a .js-select-on-focus field, select the contents
  // Prevent a mouseup event from deselecting the input
  });
  $('.remove-row').bind('ajax:success', function () {
    $(this).tooltip('destroy')
      .closest('li')
      .fadeOut();
  });
  $('.js-remove-tr').bind('ajax:before', function () {
    return $(this).hide();
  });
  $('.js-remove-tr').bind('ajax:success', function () {
    return $(this).closest('tr').fadeOut();
  });
  $('select.select2').select2({
    width: 'resolve',
    // Initialize select2 selects
    dropdownAutoWidth: true
  });
  $('.js-select2').bind('select2-close', function () {
    return setTimeout((function () {
      $('.select2-container-active').removeClass('select2-container-active');
      return $(':focus').blur();
    }), 1);
  // Close select2 on escape
  });
  // Initialize tooltips
  $.fn.tooltip.Constructor.DEFAULTS.trigger = 'hover';
  $body.tooltip({
    selector: '.has-tooltip, [data-toggle="tooltip"]',
    placement: function (tip, el) {
      return $(el).data('placement') || 'bottom';
    }
  });
A
Alexis Reigel 已提交
254 255
  // Initialize popovers
  $body.popover({
256
    selector: '[data-toggle="popover"]',
257 258 259 260
    trigger: 'focus',
    // set the viewport to the main content, excluding the navigation bar, so
    // the navigation can't overlap the popover
    viewport: '.page-with-sidebar'
A
Alexis Reigel 已提交
261
  });
262 263 264 265 266 267 268 269 270
  $('.trigger-submit').on('change', function () {
    return $(this).parents('form').submit();
  // Form submitter
  });
  gl.utils.localTimeAgo($('abbr.timeago, .js-timeago'), true);
  // Flash
  if ($flash.length > 0) {
    $flash.click(function () {
      return $(this).fadeOut();
F
Fatih Acet 已提交
271
    });
272 273 274 275 276
    $flash.show();
  }
  // Disable form buttons while a form is submitting
  $body.on('ajax:complete, ajax:beforeSend, submit', 'form', function (e) {
    var buttons;
L
Luke "Jared" Bennett 已提交
277
    buttons = $('[type="submit"], .js-disable-on-submit', this);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    switch (e.type) {
      case 'ajax:beforeSend':
      case 'submit':
        return buttons.disable();
      default:
        return buttons.enable();
    }
  });
  $(document).ajaxError(function (e, xhrObj) {
    var ref = xhrObj.status;
    if (xhrObj.status === 401) {
      return new Flash('You need to be logged in.', 'alert');
    } else if (ref === 404 || ref === 500) {
      return new Flash('Something went wrong on our end.', 'alert');
    }
  });
  $('.account-box').hover(function () {
    // Show/Hide the profile menu when hovering the account box
    return $(this).toggleClass('hover');
  });
  $document.on('click', '.diff-content .js-show-suppressed-diff', function () {
    var $container;
    $container = $(this).parent();
    $container.next('table').show();
    return $container.remove();
  // Commit show suppressed diff
  });
305
  $('.navbar-toggle').on('click', () => $('.header-content').toggleClass('menu-expanded'));
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  // Show/hide comments on diff
  $body.on('click', '.js-toggle-diff-comments', function (e) {
    var $this = $(this);
    var notesHolders = $this.closest('.diff-file').find('.notes_holder');
    $this.toggleClass('active');
    if ($this.hasClass('active')) {
      notesHolders.show().find('.hide, .content').show();
    } else {
      notesHolders.hide().find('.content').hide();
    }
    $(document).trigger('toggle.comments');
    return e.preventDefault();
  });
  $document.off('click', '.js-confirm-danger');
  $document.on('click', '.js-confirm-danger', function (e) {
    var btn = $(e.target);
    var form = btn.closest('form');
    var text = btn.data('confirm-danger-message');
    e.preventDefault();
    return new ConfirmDangerModal(form, text);
  });
  $('input[type="search"]').each(function () {
    var $this = $(this);
    $this.attr('value', $this.val());
  });
  $document.off('keyup', 'input[type="search"]').on('keyup', 'input[type="search"]', function () {
    var $this;
    $this = $(this);
    return $this.attr('value', $this.val());
  });
  $document.off('breakpoint:change').on('breakpoint:change', function (e, breakpoint) {
    var $gutterIcon;
    if (breakpoint === 'sm' || breakpoint === 'xs') {
      $gutterIcon = $sidebarGutterToggle.find('i');
      if ($gutterIcon.hasClass('fa-angle-double-right')) {
        return $sidebarGutterToggle.trigger('click');
F
Fatih Acet 已提交
342
      }
343
    }
344
  });
345 346 347 348 349 350 351 352 353 354 355
  fitSidebarForSize = function () {
    var oldBootstrapBreakpoint;
    oldBootstrapBreakpoint = bootstrapBreakpoint;
    bootstrapBreakpoint = bp.getBreakpointSize();
    if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
      return $document.trigger('breakpoint:change', [bootstrapBreakpoint]);
    }
  };
  $window.off('resize.app').on('resize.app', function () {
    return fitSidebarForSize();
  });
356
  loadAwardsHandler();
357 358
  new Aside();

359
  gl.utils.renderTimeago();
P
Phil Hughes 已提交
360 361

  $(document).trigger('init.scrolling-tabs');
P
Phil Hughes 已提交
362 363

  $('form.filter-form').on('submit', function (event) {
364 365 366 367 368
    const link = document.createElement('a');
    link.href = this.action;

    const action = `${this.action}${link.search === '' ? '?' : '&'}`;

P
Phil Hughes 已提交
369
    event.preventDefault();
370
    gl.utils.visitUrl(`${action}${$(this).serialize()}`);
P
Phil Hughes 已提交
371
  });
372
});