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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
function requireAll(context) { return context.keys().map(context); }

window.$ = window.jQuery = require('jquery');
require('jquery-ui/ui/autocomplete');
require('jquery-ui/ui/datepicker');
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');
require('vendor/jquery.turbolinks');
window.Cookies = require('vendor/js.cookie');
require('vendor/turbolinks');
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');
window._ = require('underscore');
window.Dropzone = require('dropzone');
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)$/));
57
requireAll(require.context('./droplab',    false, /^\.\/.*\.(js|es6)$/));
58
requireAll(require.context('.',            false, /^\.\/(?!application\.js).*\.(js|es6)$/));
59 60 61
require('vendor/fuzzaldrin-plus');
window.ES6Promise = require('vendor/es6-promise.auto');
window.ES6Promise.polyfill();
F
Fatih Acet 已提交
62

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

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

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

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

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

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

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