BigW Consortium Gitlab

main.js 9.4 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
/* global ConfirmDangerModal */
/* global Aside */

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

// libraries with import side-effects
13 14
import 'mousetrap';
import 'mousetrap/plugins/pause/mousetrap-pause';
15 16 17 18 19 20 21 22

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

23
// templates
24 25
import './templates/issuable_template_selector';
import './templates/issuable_template_selectors';
26

27
import './commit/image_file';
28 29

// lib/utils
30
import { handleLocationHash } from './lib/utils/common_utils';
31 32
import './lib/utils/datetime_utility';
import './lib/utils/url_utility';
33

34 35 36
// behaviors
import './behaviors/';

37
// everything else
38 39 40
import './activities';
import './admin';
import './aside';
41
import loadAwardsHandler from './awards_handler';
42
import bp from './breakpoints';
43 44 45 46
import './commits';
import './compare';
import './compare_autocomplete';
import './confirm_danger_modal';
47
import Flash, { removeFlashClickListener } from './flash';
48 49 50 51
import './gl_dropdown';
import './gl_field_error';
import './gl_field_errors';
import './gl_form';
52
import initTodoToggle from './header';
Filipa Lacerda committed
53
import initImporterStatus from './importer_status';
54
import './layout_nav';
55
import LazyLoader from './lazy_loader';
56
import './line_highlighter';
57
import initLogoAnimation from './logo';
58 59 60 61 62 63 64 65 66 67
import './merge_request';
import './merge_request_tabs';
import './milestone_select';
import './notes';
import './notifications_dropdown';
import './notifications_form';
import './pager';
import './preview_markdown';
import './project_find_file';
import './project_import';
68
import './projects_dropdown';
69
import './projects_list';
70 71
import './syntax_highlight';
import './render_gfm';
72 73 74
import './right_sidebar';
import './search';
import './search_autocomplete';
75
import initBreadcrumbs from './breadcrumb';
76

77 78
import './dispatcher';

Mike Greiling committed
79
// eslint-disable-next-line global-require, import/no-commonjs
80
if (process.env.NODE_ENV !== 'production') require('./test_utils/');
81

82 83
Dropzone.autoDiscover = false;

84 85
svg4everybody();

86 87 88 89 90
document.addEventListener('beforeunload', function () {
  // Unbind scroll events
  $(document).off('scroll');
  // Close any open tooltips
  $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
91 92
  // Close any open popover
  $('[data-toggle="popover"]').popover('destroy');
93
});
Fatih Acet committed
94

95
window.addEventListener('hashchange', handleLocationHash);
96 97
window.addEventListener('load', function onLoad() {
  window.removeEventListener('load', onLoad, false);
98
  handleLocationHash();
99
}, false);
100

101 102 103 104 105
gl.lazyLoader = new LazyLoader({
  scrollContainer: window,
  observerNode: '#content-body'
});

106 107 108 109 110 111 112
$(function () {
  var $body = $('body');
  var $document = $(document);
  var $window = $(window);
  var $sidebarGutterToggle = $('.js-sidebar-toggle');
  var bootstrapBreakpoint = bp.getBreakpointSize();
  var fitSidebarForSize;
113

114
  initBreadcrumbs();
Filipa Lacerda committed
115
  initImporterStatus();
116
  initTodoToggle();
117
  initLogoAnimation();
118

119 120
  // Set the default path for all cookies to GitLab's root directory
  Cookies.defaults.path = gon.relative_url_root || '/';
121

122 123 124 125
  // `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()) {
126
      setTimeout(handleLocationHash, 1);
127 128
    }
  });
129

130 131 132 133 134 135 136 137
  if (bootstrapBreakpoint === 'xs') {
    const $rightSidebar = $('aside.right-sidebar, .page-with-sidebar');

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

138 139 140 141 142 143
  // prevent default action for disabled buttons
  $('.btn').click(function(e) {
    if ($(this).hasClass('disabled')) {
      e.preventDefault();
      e.stopImmediatePropagation();
      return false;
Fatih Acet committed
144
    }
145 146 147 148
  });

  $('.js-select-on-focus').on('focusin', function () {
    return $(this).select().one('mouseup', function (e) {
Fatih Acet committed
149 150
      return e.preventDefault();
    });
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  // 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';
    }
  });
185 186
  // Initialize popovers
  $body.popover({
187
    selector: '[data-toggle="popover"]',
188 189 190 191
    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'
192
  });
193 194 195 196 197 198 199 200
  $('.trigger-submit').on('change', function () {
    return $(this).parents('form').submit();
  // Form submitter
  });
  gl.utils.localTimeAgo($('abbr.timeago, .js-timeago'), true);
  // Disable form buttons while a form is submitting
  $body.on('ajax:complete, ajax:beforeSend, submit', 'form', function (e) {
    var buttons;
201
    buttons = $('[type="submit"], .js-disable-on-submit', this);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    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
  });
229 230 231 232
  $('.navbar-toggle').on('click', () => {
    $('.header-content').toggleClass('menu-expanded');
    gl.lazyLoader.loadCheck();
  });
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  // 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');
Fatih Acet committed
269
      }
270
    }
271
  });
272 273 274 275 276 277 278 279 280 281 282
  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();
  });
283
  loadAwardsHandler();
284 285
  new Aside();

286
  gl.utils.renderTimeago();
287 288

  $(document).trigger('init.scrolling-tabs');
289 290

  $('form.filter-form').on('submit', function (event) {
291 292 293 294 295
    const link = document.createElement('a');
    link.href = this.action;

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

296
    event.preventDefault();
297
    gl.utils.visitUrl(`${action}${$(this).serialize()}`);
298
  });
299 300 301 302

  const flashContainer = document.querySelector('.flash-container');

  if (flashContainer && flashContainer.children.length) {
303 304 305
    flashContainer.querySelectorAll('.flash-alert, .flash-notice, .flash-success').forEach((flashEl) => {
      removeFlashClickListener(flashEl);
    });
306
  }
307
});