BigW Consortium Gitlab

main.js 7.63 KB
Newer Older
Phil Hughes committed
1
/* eslint-disable import/first */
2 3
/* global ConfirmDangerModal */

4 5
import jQuery from 'jquery';
import Cookies from 'js-cookie';
6
import svg4everybody from 'svg4everybody';
7 8 9 10

// expose common libraries as globals (TODO: remove these)
window.jQuery = jQuery;
window.$ = jQuery;
11 12

// lib/utils
13
import { handleLocationHash } from './lib/utils/common_utils';
Phil Hughes committed
14
import { localTimeAgo } from './lib/utils/datetime_utility';
15
import { getLocationHash, visitUrl } from './lib/utils/url_utility';
16

17 18 19
// behaviors
import './behaviors/';

20
// everything else
21
import loadAwardsHandler from './awards_handler';
22
import bp from './breakpoints';
23
import './confirm_danger_modal';
24
import Flash, { removeFlashClickListener } from './flash';
25
import './gl_dropdown';
26
import initTodoToggle from './header';
Filipa Lacerda committed
27
import initImporterStatus from './importer_status';
28
import initLayoutNav from './layout_nav';
29
import LazyLoader from './lazy_loader';
30
import initLogoAnimation from './logo';
31
import './milestone_select';
32
import './projects_dropdown';
33
import './render_gfm';
34
import initBreadcrumbs from './breadcrumb';
35

36 37
import './dispatcher';

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

41 42
svg4everybody();

Phil Hughes committed
43
document.addEventListener('beforeunload', () => {
44 45 46 47
  // Unbind scroll events
  $(document).off('scroll');
  // Close any open tooltips
  $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
48 49
  // Close any open popover
  $('[data-toggle="popover"]').popover('destroy');
50
});
Fatih Acet committed
51

52
window.addEventListener('hashchange', handleLocationHash);
53 54
window.addEventListener('load', function onLoad() {
  window.removeEventListener('load', onLoad, false);
55
  handleLocationHash();
56
}, false);
57

58 59
gl.lazyLoader = new LazyLoader({
  scrollContainer: window,
Phil Hughes committed
60
  observerNode: '#content-body',
61 62
});

Phil Hughes committed
63 64 65 66 67 68
$(() => {
  const $body = $('body');
  const $document = $(document);
  const $window = $(window);
  const $sidebarGutterToggle = $('.js-sidebar-toggle');
  let bootstrapBreakpoint = bp.getBreakpointSize();
69

70
  initBreadcrumbs();
71
  initLayoutNav();
Filipa Lacerda committed
72
  initImporterStatus();
73
  initTodoToggle();
74
  initLogoAnimation();
75

76 77
  // Set the default path for all cookies to GitLab's root directory
  Cookies.defaults.path = gon.relative_url_root || '/';
78

79
  // `hashchange` is not triggered when link target is already in window.location
Phil Hughes committed
80 81
  $body.on('click', 'a[href^="#"]', function clickHashLinkCallback() {
    const href = this.getAttribute('href');
82
    if (href.substr(1) === getLocationHash()) {
83
      setTimeout(handleLocationHash, 1);
84 85
    }
  });
86

87
  if (bootstrapBreakpoint === 'xs') {
88
    const $rightSidebar = $('aside.right-sidebar, .layout-page');
89 90 91 92 93 94

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

95
  // prevent default action for disabled buttons
Phil Hughes committed
96
  $('.btn').click(function clickDisabledButtonCallback(e) {
97 98 99 100
    if ($(this).hasClass('disabled')) {
      e.preventDefault();
      e.stopImmediatePropagation();
      return false;
Fatih Acet committed
101
    }
Phil Hughes committed
102 103

    return true;
104 105 106 107
  });

  // Click a .js-select-on-focus field, select the contents
  // Prevent a mouseup event from deselecting the input
Phil Hughes committed
108 109 110 111
  $('.js-select-on-focus').on('focusin', function selectOnFocusCallback() {
    $(this).select().one('mouseup', (e) => {
      e.preventDefault();
    });
112
  });
Phil Hughes committed
113 114

  $('.remove-row').on('ajax:success', function removeRowAjaxSuccessCallback() {
115 116 117 118
    $(this).tooltip('destroy')
      .closest('li')
      .fadeOut();
  });
Phil Hughes committed
119 120 121

  $('.js-remove-tr').on('ajax:before', function removeTRAjaxBeforeCallback() {
    $(this).hide();
122
  });
Phil Hughes committed
123 124 125

  $('.js-remove-tr').on('ajax:success', function removeTRAjaxSuccessCallback() {
    $(this).closest('tr').fadeOut();
126
  });
Phil Hughes committed
127 128

  // Initialize select2 selects
129 130
  $('select.select2').select2({
    width: 'resolve',
Phil Hughes committed
131
    dropdownAutoWidth: true,
132
  });
Phil Hughes committed
133

134
  // Close select2 on escape
Phil Hughes committed
135 136 137 138 139
  $('.js-select2').on('select2-close', () => {
    setTimeout(() => {
      $('.select2-container-active').removeClass('select2-container-active');
      $(':focus').blur();
    }, 1);
140
  });
Phil Hughes committed
141

142 143 144 145
  // Initialize tooltips
  $.fn.tooltip.Constructor.DEFAULTS.trigger = 'hover';
  $body.tooltip({
    selector: '.has-tooltip, [data-toggle="tooltip"]',
Phil Hughes committed
146
    placement(tip, el) {
147
      return $(el).data('placement') || 'bottom';
Phil Hughes committed
148
    },
149
  });
Phil Hughes committed
150

151 152
  // Initialize popovers
  $body.popover({
153
    selector: '[data-toggle="popover"]',
154 155 156
    trigger: 'focus',
    // set the viewport to the main content, excluding the navigation bar, so
    // the navigation can't overlap the popover
Phil Hughes committed
157
    viewport: '.layout-page',
158
  });
Phil Hughes committed
159

160
  // Form submitter
Phil Hughes committed
161 162
  $('.trigger-submit').on('change', function triggerSubmitCallback() {
    $(this).parents('form').submit();
163
  });
Phil Hughes committed
164

165
  localTimeAgo($('abbr.timeago, .js-timeago'), true);
Phil Hughes committed
166

167
  // Disable form buttons while a form is submitting
Phil Hughes committed
168 169
  $body.on('ajax:complete, ajax:beforeSend, submit', 'form', function ajaxCompleteCallback(e) {
    const $buttons = $('[type="submit"], .js-disable-on-submit', this);
170 171 172
    switch (e.type) {
      case 'ajax:beforeSend':
      case 'submit':
Phil Hughes committed
173
        return $buttons.disable();
174
      default:
Phil Hughes committed
175
        return $buttons.enable();
176 177
    }
  });
Phil Hughes committed
178 179 180 181 182 183

  $(document).ajaxError((e, xhrObj) => {
    const ref = xhrObj.status;

    if (ref === 401) {
      Flash('You need to be logged in.');
184
    } else if (ref === 404 || ref === 500) {
Phil Hughes committed
185
      Flash('Something went wrong on our end.');
186 187
    }
  });
Phil Hughes committed
188

189
  // Commit show suppressed diff
Phil Hughes committed
190 191 192 193
  $document.on('click', '.diff-content .js-show-suppressed-diff', function showDiffCallback() {
    const $container = $(this).parent();
    $container.next('table').show();
    $container.remove();
194
  });
Phil Hughes committed
195

196 197 198 199
  $('.navbar-toggle').on('click', () => {
    $('.header-content').toggleClass('menu-expanded');
    gl.lazyLoader.loadCheck();
  });
Phil Hughes committed
200

201
  // Show/hide comments on diff
Phil Hughes committed
202 203 204 205 206 207
  $body.on('click', '.js-toggle-diff-comments', function toggleDiffCommentsCallback(e) {
    const $this = $(this);
    const notesHolders = $this.closest('.diff-file').find('.notes_holder');

    e.preventDefault();

208
    $this.toggleClass('active');
Phil Hughes committed
209

210 211 212 213 214
    if ($this.hasClass('active')) {
      notesHolders.show().find('.hide, .content').show();
    } else {
      notesHolders.hide().find('.content').hide();
    }
Phil Hughes committed
215

216 217
    $(document).trigger('toggle.comments');
  });
Phil Hughes committed
218 219 220 221 222

  $document.on('click', '.js-confirm-danger', (e) => {
    const btn = $(e.target);
    const form = btn.closest('form');
    const text = btn.data('confirm-danger-message');
223
    e.preventDefault();
Phil Hughes committed
224 225 226

    // eslint-disable-next-line no-new
    new ConfirmDangerModal(form, text);
227
  });
Phil Hughes committed
228 229

  $document.on('breakpoint:change', (e, breakpoint) => {
230
    if (breakpoint === 'sm' || breakpoint === 'xs') {
Phil Hughes committed
231
      const $gutterIcon = $sidebarGutterToggle.find('i');
232
      if ($gutterIcon.hasClass('fa-angle-double-right')) {
Phil Hughes committed
233
        $sidebarGutterToggle.trigger('click');
Fatih Acet committed
234
      }
235
    }
236
  });
Phil Hughes committed
237 238 239

  function fitSidebarForSize() {
    const oldBootstrapBreakpoint = bootstrapBreakpoint;
240
    bootstrapBreakpoint = bp.getBreakpointSize();
Phil Hughes committed
241

242
    if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
Phil Hughes committed
243
      $document.trigger('breakpoint:change', [bootstrapBreakpoint]);
244
    }
Phil Hughes committed
245
  }
246

Phil Hughes committed
247 248 249
  $window.on('resize.app', fitSidebarForSize);

  loadAwardsHandler();
250

Phil Hughes committed
251
  $('form.filter-form').on('submit', function filterFormSubmitCallback(event) {
252 253 254 255 256
    const link = document.createElement('a');
    link.href = this.action;

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

257
    event.preventDefault();
258
    visitUrl(`${action}${$(this).serialize()}`);
259
  });
260 261 262 263

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

  if (flashContainer && flashContainer.children.length) {
264 265 266
    flashContainer.querySelectorAll('.flash-alert, .flash-notice, .flash-success').forEach((flashEl) => {
      removeFlashClickListener(flashEl);
    });
267
  }
268
});