application.js 8.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 */
2 3 4 5 6 7 8
/* global bp */
/* global Cookies */
/* global Flash */
/* global ConfirmDangerModal */
/* global AwardsHandler */
/* global Aside */

9 10 11 12 13 14 15 16 17 18 19 20 21 22
function requireAll(context) { return context.keys().map(context); }

window.$ = window.jQuery = require('jquery');
require('jquery-ui/ui/autocomplete');
require('jquery-ui/ui/draggable');
require('jquery-ui/ui/effect-highlight');
require('jquery-ui/ui/sortable');
require('jquery-ujs');
require('vendor/jquery.endless-scroll');
require('vendor/jquery.highlight');
require('vendor/jquery.waitforimages');
require('vendor/jquery.caret');
require('vendor/jquery.atwho');
require('vendor/jquery.scrollTo');
23
window.Cookies = require('js-cookie');
24 25 26 27 28 29 30 31 32 33 34 35 36
require('./autosave');
require('bootstrap/js/affix');
require('bootstrap/js/alert');
require('bootstrap/js/button');
require('bootstrap/js/collapse');
require('bootstrap/js/dropdown');
require('bootstrap/js/modal');
require('bootstrap/js/scrollspy');
require('bootstrap/js/tab');
require('bootstrap/js/transition');
require('bootstrap/js/tooltip');
require('bootstrap/js/popover');
require('select2/select2.js');
P
Phil Hughes 已提交
37
window.Pikaday = require('pikaday');
38 39
window._ = require('underscore');
window.Dropzone = require('dropzone');
P
Phil Hughes 已提交
40
window.Sortable = require('vendor/Sortable');
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
require('mousetrap');
require('mousetrap/plugins/pause/mousetrap-pause');
require('./shortcuts');
require('./shortcuts_navigation');
require('./shortcuts_dashboard_navigation');
require('./shortcuts_issuable');
require('./shortcuts_network');
require('vendor/jquery.nicescroll');
requireAll(require.context('./behaviors',  false, /^\.\/.*\.(js|es6)$/));
requireAll(require.context('./blob',       false, /^\.\/.*\.(js|es6)$/));
requireAll(require.context('./templates',  false, /^\.\/.*\.(js|es6)$/));
requireAll(require.context('./commit',     false, /^\.\/.*\.(js|es6)$/));
requireAll(require.context('./extensions', false, /^\.\/.*\.(js|es6)$/));
requireAll(require.context('./lib/utils',  false, /^\.\/.*\.(js|es6)$/));
requireAll(require.context('./u2f',        false, /^\.\/.*\.(js|es6)$/));
56
requireAll(require.context('./droplab',    false, /^\.\/.*\.(js|es6)$/));
57
requireAll(require.context('.',            false, /^\.\/(?!application\.js).*\.(js|es6)$/));
58 59 60
require('vendor/fuzzaldrin-plus');
window.ES6Promise = require('vendor/es6-promise.auto');
window.ES6Promise.polyfill();
F
Fatih Acet 已提交
61

J
José Iván 已提交
62
(function () {
B
Bryce Johnson 已提交
63
  document.addEventListener('beforeunload', function () {
W
winniehell 已提交
64 65 66 67 68 69
    // Unbind scroll events
    $(document).off('scroll');
    // Close any open tooltips
    $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
  });

70 71 72
  window.addEventListener('hashchange', gl.utils.handleLocationHash);
  window.addEventListener('load', function onLoad() {
    window.removeEventListener('load', onLoad, false);
73
    gl.utils.handleLocationHash();
74
  }, false);
F
Fatih Acet 已提交
75

J
José Iván 已提交
76 77 78 79 80 81 82 83
  $(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;
84 85 86 87

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

88
    // `hashchange` is not triggered when link target is already in window.location
89
    $body.on('click', 'a[href^="#"]', function() {
90
      var href = this.getAttribute('href');
91
      if (href.substr(1) === gl.utils.getLocationHash()) {
92 93 94 95
        setTimeout(gl.utils.handleLocationHash, 1);
      }
    });

W
winniehell 已提交
96 97 98 99 100 101 102 103 104
    // prevent default action for disabled buttons
    $('.btn').click(function(e) {
      if ($(this).hasClass('disabled')) {
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
      }
    });

J
José Iván 已提交
105
    $('.nav-sidebar').niceScroll({
F
Fatih Acet 已提交
106 107
      cursoropacitymax: '0.4',
      cursorcolor: '#FFF',
J
José Iván 已提交
108
      cursorborder: '1px solid #FFF'
F
Fatih Acet 已提交
109
    });
J
José Iván 已提交
110 111
    $('.js-select-on-focus').on('focusin', function () {
      return $(this).select().one('mouseup', function (e) {
F
Fatih Acet 已提交
112 113
        return e.preventDefault();
      });
114 115
    // Click a .js-select-on-focus field, select the contents
    // Prevent a mouseup event from deselecting the input
F
Fatih Acet 已提交
116
    });
J
José Iván 已提交
117
    $('.remove-row').bind('ajax:success', function () {
118 119 120
      $(this).tooltip('destroy')
        .closest('li')
        .fadeOut();
F
Fatih Acet 已提交
121
    });
J
José Iván 已提交
122
    $('.js-remove-tr').bind('ajax:before', function () {
F
Fatih Acet 已提交
123 124
      return $(this).hide();
    });
J
José Iván 已提交
125
    $('.js-remove-tr').bind('ajax:success', function () {
F
Fatih Acet 已提交
126 127 128 129
      return $(this).closest('tr').fadeOut();
    });
    $('select.select2').select2({
      width: 'resolve',
130
      // Initialize select2 selects
F
Fatih Acet 已提交
131 132
      dropdownAutoWidth: true
    });
J
José Iván 已提交
133 134
    $('.js-select2').bind('select2-close', function () {
      return setTimeout((function () {
F
Fatih Acet 已提交
135 136 137
        $('.select2-container-active').removeClass('select2-container-active');
        return $(':focus').blur();
      }), 1);
138
    // Close select2 on escape
F
Fatih Acet 已提交
139
    });
140
    // Initialize tooltips
141
    $.fn.tooltip.Constructor.DEFAULTS.trigger = 'hover';
F
Fatih Acet 已提交
142 143
    $body.tooltip({
      selector: '.has-tooltip, [data-toggle="tooltip"]',
J
José Iván 已提交
144
      placement: function (_, el) {
145
        return $(el).data('placement') || 'bottom';
F
Fatih Acet 已提交
146 147
      }
    });
J
José Iván 已提交
148
    $('.trigger-submit').on('change', function () {
F
Fatih Acet 已提交
149
      return $(this).parents('form').submit();
150
    // Form submitter
F
Fatih Acet 已提交
151 152
    });
    gl.utils.localTimeAgo($('abbr.timeago, .js-timeago'), true);
153
    // Flash
J
José Iván 已提交
154 155
    if ($flash.length > 0) {
      $flash.click(function () {
F
Fatih Acet 已提交
156 157
        return $(this).fadeOut();
      });
J
José Iván 已提交
158
      $flash.show();
F
Fatih Acet 已提交
159
    }
160
    // Disable form buttons while a form is submitting
J
José Iván 已提交
161
    $body.on('ajax:complete, ajax:beforeSend, submit', 'form', function (e) {
F
Fatih Acet 已提交
162 163 164 165 166 167 168 169 170 171
      var buttons;
      buttons = $('[type="submit"]', this);
      switch (e.type) {
        case 'ajax:beforeSend':
        case 'submit':
          return buttons.disable();
        default:
          return buttons.enable();
      }
    });
J
José Iván 已提交
172 173
    $(document).ajaxError(function (e, xhrObj) {
      var ref = xhrObj.status;
F
Fatih Acet 已提交
174 175
      if (xhrObj.status === 401) {
        return new Flash('You need to be logged in.', 'alert');
J
José Iván 已提交
176
      } else if (ref === 404 || ref === 500) {
F
Fatih Acet 已提交
177 178 179
        return new Flash('Something went wrong on our end.', 'alert');
      }
    });
J
José Iván 已提交
180
    $('.account-box').hover(function () {
181
      // Show/Hide the profile menu when hovering the account box
F
Fatih Acet 已提交
182 183
      return $(this).toggleClass('hover');
    });
J
José Iván 已提交
184
    $document.on('click', '.diff-content .js-show-suppressed-diff', function () {
F
Fatih Acet 已提交
185 186 187 188
      var $container;
      $container = $(this).parent();
      $container.next('table').show();
      return $container.remove();
189
    // Commit show suppressed diff
F
Fatih Acet 已提交
190
    });
J
José Iván 已提交
191
    $('.navbar-toggle').on('click', function () {
F
Fatih Acet 已提交
192 193 194 195 196
      $('.header-content .title').toggle();
      $('.header-content .header-logo').toggle();
      $('.header-content .navbar-collapse').toggle();
      return $('.navbar-toggle').toggleClass('active');
    });
197
    // Show/hide comments on diff
J
José Iván 已提交
198
    $body.on('click', '.js-toggle-diff-comments', function (e) {
199 200
      var $this = $(this);
      var notesHolders = $this.closest('.diff-file').find('.notes_holder');
J
José Iván 已提交
201
      $this.toggleClass('active');
202
      if ($this.hasClass('active')) {
203
        notesHolders.show().find('.hide').show();
204 205 206
      } else {
        notesHolders.hide();
      }
207
      $this.trigger('blur');
F
Fatih Acet 已提交
208 209
      return e.preventDefault();
    });
J
José Iván 已提交
210 211 212 213 214
    $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');
F
Fatih Acet 已提交
215 216 217
      e.preventDefault();
      return new ConfirmDangerModal(form, text);
    });
J
José Iván 已提交
218 219
    $('input[type="search"]').each(function () {
      var $this = $(this);
F
Fatih Acet 已提交
220 221
      $this.attr('value', $this.val());
    });
J
José Iván 已提交
222
    $document.off('keyup', 'input[type="search"]').on('keyup', 'input[type="search"]', function () {
F
Fatih Acet 已提交
223 224 225 226
      var $this;
      $this = $(this);
      return $this.attr('value', $this.val());
    });
J
José Iván 已提交
227
    $document.off('breakpoint:change').on('breakpoint:change', function (e, breakpoint) {
F
Fatih Acet 已提交
228 229 230 231 232 233 234 235
      var $gutterIcon;
      if (breakpoint === 'sm' || breakpoint === 'xs') {
        $gutterIcon = $sidebarGutterToggle.find('i');
        if ($gutterIcon.hasClass('fa-angle-double-right')) {
          return $sidebarGutterToggle.trigger('click');
        }
      }
    });
J
José Iván 已提交
236
    fitSidebarForSize = function () {
F
Fatih Acet 已提交
237 238 239 240 241 242 243
      var oldBootstrapBreakpoint;
      oldBootstrapBreakpoint = bootstrapBreakpoint;
      bootstrapBreakpoint = bp.getBreakpointSize();
      if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
        return $document.trigger('breakpoint:change', [bootstrapBreakpoint]);
      }
    };
J
José Iván 已提交
244
    $window.off('resize.app').on('resize.app', function () {
F
Fatih Acet 已提交
245 246 247 248
      return fitSidebarForSize();
    });
    gl.awardsHandler = new AwardsHandler();
    new Aside();
249 250
    // bind sidebar events
    new gl.Sidebar();
P
Phil Hughes 已提交
251 252

    gl.utils.initTimeagoTimeout();
253
  });
F
Fatih Acet 已提交
254
}).call(this);