application.js 8.6 KB
Newer Older
1 2 3 4 5 6 7 8
/* eslint-disable func-names, space-before-function-paren, no-var, quotes, consistent-return, prefer-arrow-callback, comma-dangle, object-shorthand, no-new, max-len */
/* global bp */
/* global Cookies */
/* global Flash */
/* global ConfirmDangerModal */
/* global AwardsHandler */
/* global Aside */

9
// This is a manifest file that'll be compiled into including all the files listed below.
10
// Add new JavaScript code in separate files in this directory and they'll automatically
11 12 13 14
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
F
Fatih Acet 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
/*= require jquery2 */
/*= require jquery-ui/autocomplete */
/*= require jquery-ui/datepicker */
/*= require jquery-ui/draggable */
/*= require jquery-ui/effect-highlight */
/*= require jquery-ui/sortable */
/*= require jquery_ujs */
/*= require jquery.endless-scroll */
/*= require jquery.highlight */
/*= require jquery.waitforimages */
/*= require jquery.atwho */
/*= require jquery.scrollTo */
/*= require jquery.turbolinks */
28
/*= require js.cookie */
F
Fatih Acet 已提交
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
/*= require turbolinks */
/*= require autosave */
/*= require bootstrap/affix */
/*= require bootstrap/alert */
/*= require bootstrap/button */
/*= require bootstrap/collapse */
/*= require bootstrap/dropdown */
/*= require bootstrap/modal */
/*= require bootstrap/scrollspy */
/*= require bootstrap/tab */
/*= require bootstrap/transition */
/*= require bootstrap/tooltip */
/*= require bootstrap/popover */
/*= require select2 */
/*= require underscore */
/*= require dropzone */
/*= require mousetrap */
/*= require mousetrap/pause */
/*= require shortcuts */
/*= require shortcuts_navigation */
/*= require shortcuts_dashboard_navigation */
/*= require shortcuts_issuable */
/*= require shortcuts_network */
/*= require jquery.nicescroll */
/*= require date.format */
/*= require_directory ./behaviors */
/*= require_directory ./blob */
56
/*= require_directory ./templates */
F
Fatih Acet 已提交
57 58 59 60 61 62
/*= require_directory ./commit */
/*= require_directory ./extensions */
/*= require_directory ./lib/utils */
/*= require_directory ./u2f */
/*= require_directory . */
/*= require fuzzaldrin-plus */
F
Filipa Lacerda 已提交
63
/*= require es6-promise.auto */
F
Fatih Acet 已提交
64

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

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

J
José Iván 已提交
79 80 81 82 83 84 85 86 87
  $(function () {
    var $body = $('body');
    var $document = $(document);
    var $window = $(window);
    var $sidebarGutterToggle = $('.js-sidebar-toggle');
    var $flash = $('.flash-container');
    var bootstrapBreakpoint = bp.getBreakpointSize();
    var checkInitialSidebarSize;
    var fitSidebarForSize;
88 89 90 91

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

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

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

    // bind sidebar events
    new gl.Sidebar();
255
  });
F
Fatih Acet 已提交
256
}).call(this);