main.js 11.8 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 5 6 7
/* global bp */
/* global Cookies */
/* global Flash */
/* global ConfirmDangerModal */
/* global Aside */

8 9 10 11 12 13 14 15
import jQuery from 'jquery';
import _ from 'underscore';
import Cookies from 'js-cookie';
import Pikaday from 'pikaday';
import Dropzone from 'dropzone';
import Sortable from 'vendor/Sortable';

// libraries with import side-effects
16 17 18 19
import 'mousetrap';
import 'mousetrap/plugins/pause/mousetrap-pause';
import 'vendor/fuzzaldrin-plus';
import promisePolyfill from 'es6-promise';
20

E
Eric Eastwood 已提交
21
// extensions
22 23 24 25 26 27 28 29
import './extensions/string';
import './extensions/array';
import './extensions/custom_event';
import './extensions/element';
import './extensions/jquery';
import './extensions/object';

promisePolyfill.polyfill();
E
Eric Eastwood 已提交
30

31 32 33 34 35 36 37 38 39 40
// expose common libraries as globals (TODO: remove these)
window.jQuery = jQuery;
window.$ = jQuery;
window._ = _;
window.Cookies = Cookies;
window.Pikaday = Pikaday;
window.Dropzone = Dropzone;
window.Sortable = Sortable;

// shortcuts
41 42 43 44 45 46 47
import './shortcuts';
import './shortcuts_blob';
import './shortcuts_dashboard_navigation';
import './shortcuts_navigation';
import './shortcuts_find_file';
import './shortcuts_issuable';
import './shortcuts_network';
48 49

// behaviors
50 51 52 53 54 55 56 57
import './behaviors/autosize';
import './behaviors/details_behavior';
import './behaviors/quick_submit';
import './behaviors/requires_input';
import './behaviors/toggler_behavior';
import './behaviors/bind_in_out';
import { installGlEmojiElement } from './behaviors/gl_emoji';
installGlEmojiElement();
58 59

// blob
60 61 62 63 64 65 66 67 68
import './blob/blob_ci_yaml';
import './blob/blob_dockerfile_selector';
import './blob/blob_dockerfile_selectors';
import './blob/blob_file_dropzone';
import './blob/blob_gitignore_selector';
import './blob/blob_gitignore_selectors';
import './blob/blob_license_selector';
import './blob/blob_license_selectors';
import './blob/template_selector';
69 70
import './blob/create_branch_dropdown';
import './blob/target_branch_dropdown';
71 72

// templates
73 74
import './templates/issuable_template_selector';
import './templates/issuable_template_selectors';
75 76

// commit
77 78
import './commit/file';
import './commit/image_file';
79 80

// lib/utils
81 82 83 84 85 86 87 88 89
import './lib/utils/animate';
import './lib/utils/bootstrap_linked_tabs';
import './lib/utils/common_utils';
import './lib/utils/datetime_utility';
import './lib/utils/notify';
import './lib/utils/pretty_time';
import './lib/utils/text_utility';
import './lib/utils/type_utility';
import './lib/utils/url_utility';
90 91

// u2f
92 93 94 95
import './u2f/authenticate';
import './u2f/error';
import './u2f/register';
import './u2f/util';
96 97

// droplab
98 99 100 101
import './droplab/droplab';
import './droplab/droplab_ajax';
import './droplab/droplab_ajax_filter';
import './droplab/droplab_filter';
102 103

// everything else
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
import './abuse_reports';
import './activities';
import './admin';
import './ajax_loading_spinner';
import './api';
import './aside';
import './autosave';
import AwardsHandler from './awards_handler';
import './breakpoints';
import './broadcast_message';
import './build';
import './build_artifacts';
import './build_variables';
import './ci_lint_editor';
import './commit';
import './commits';
import './compare';
import './compare_autocomplete';
import './confirm_danger_modal';
import './copy_as_gfm';
import './copy_to_clipboard';
import './create_label';
import './diff';
import './dispatcher';
import './dropzone_input';
import './due_date_select';
import './files_comment_button';
import './flash';
import './gfm_auto_complete';
import './gl_dropdown';
import './gl_field_error';
import './gl_field_errors';
import './gl_form';
import './group_avatar';
import './group_label_subscription';
import './groups_select';
import './header';
import './importer_status';
import './issuable';
import './issuable_context';
import './issuable_form';
import './issue';
import './issue_status_select';
import './issues_bulk_assignment';
import './label_manager';
import './labels';
import './labels_select';
import './layout_nav';
import './line_highlighter';
import './logo';
import './member_expiration_date';
import './members';
import './merge_request';
import './merge_request_tabs';
import './merge_request_widget';
import './merged_buttons';
import './milestone';
import './milestone_select';
import './mini_pipeline_graph_dropdown';
import './namespace_select';
import './new_branch_form';
import './new_commit_form';
import './notes';
import './notifications_dropdown';
import './notifications_form';
import './pager';
import './pipelines';
import './preview_markdown';
import './project';
import './project_avatar';
import './project_find_file';
import './project_fork';
import './project_import';
import './project_label_subscription';
import './project_new';
import './project_select';
import './project_show';
import './project_variables';
import './projects_list';
import './render_gfm';
import './render_math';
import './right_sidebar';
import './search';
import './search_autocomplete';
import './signin_tabs_memoizer';
import './single_file_diff';
import './smart_interval';
import './snippets_list';
import './star';
import './subbable_resource';
import './subscription';
import './subscription_select';
import './syntax_highlight';
import './task_list';
import './todos';
import './tree';
import './user';
import './user_tabs';
import './username_validator';
import './users_select';
import './version_check_image';
import './visibility_select';
import './wikis';
import './zen_mode';
208

J
José Iván 已提交
209
(function () {
B
Bryce Johnson 已提交
210
  document.addEventListener('beforeunload', function () {
W
winniehell 已提交
211 212 213 214 215 216
    // Unbind scroll events
    $(document).off('scroll');
    // Close any open tooltips
    $('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
  });

217 218 219
  window.addEventListener('hashchange', gl.utils.handleLocationHash);
  window.addEventListener('load', function onLoad() {
    window.removeEventListener('load', onLoad, false);
220
    gl.utils.handleLocationHash();
221
  }, false);
F
Fatih Acet 已提交
222

J
José Iván 已提交
223 224 225 226 227 228 229 230
  $(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;
231 232 233 234

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

235
    // `hashchange` is not triggered when link target is already in window.location
236
    $body.on('click', 'a[href^="#"]', function() {
237
      var href = this.getAttribute('href');
238
      if (href.substr(1) === gl.utils.getLocationHash()) {
239 240 241 242
        setTimeout(gl.utils.handleLocationHash, 1);
      }
    });

W
winniehell 已提交
243 244 245 246 247 248 249 250 251
    // prevent default action for disabled buttons
    $('.btn').click(function(e) {
      if ($(this).hasClass('disabled')) {
        e.preventDefault();
        e.stopImmediatePropagation();
        return false;
      }
    });

J
José Iván 已提交
252 253
    $('.js-select-on-focus').on('focusin', function () {
      return $(this).select().one('mouseup', function (e) {
F
Fatih Acet 已提交
254 255
        return e.preventDefault();
      });
256 257
    // Click a .js-select-on-focus field, select the contents
    // Prevent a mouseup event from deselecting the input
F
Fatih Acet 已提交
258
    });
J
José Iván 已提交
259
    $('.remove-row').bind('ajax:success', function () {
260 261 262
      $(this).tooltip('destroy')
        .closest('li')
        .fadeOut();
F
Fatih Acet 已提交
263
    });
J
José Iván 已提交
264
    $('.js-remove-tr').bind('ajax:before', function () {
F
Fatih Acet 已提交
265 266
      return $(this).hide();
    });
J
José Iván 已提交
267
    $('.js-remove-tr').bind('ajax:success', function () {
F
Fatih Acet 已提交
268 269 270 271
      return $(this).closest('tr').fadeOut();
    });
    $('select.select2').select2({
      width: 'resolve',
272
      // Initialize select2 selects
F
Fatih Acet 已提交
273 274
      dropdownAutoWidth: true
    });
J
José Iván 已提交
275 276
    $('.js-select2').bind('select2-close', function () {
      return setTimeout((function () {
F
Fatih Acet 已提交
277 278 279
        $('.select2-container-active').removeClass('select2-container-active');
        return $(':focus').blur();
      }), 1);
280
    // Close select2 on escape
F
Fatih Acet 已提交
281
    });
282
    // Initialize tooltips
283
    $.fn.tooltip.Constructor.DEFAULTS.trigger = 'hover';
F
Fatih Acet 已提交
284 285
    $body.tooltip({
      selector: '.has-tooltip, [data-toggle="tooltip"]',
286
      placement: function (tip, el) {
287
        return $(el).data('placement') || 'bottom';
F
Fatih Acet 已提交
288 289
      }
    });
J
José Iván 已提交
290
    $('.trigger-submit').on('change', function () {
F
Fatih Acet 已提交
291
      return $(this).parents('form').submit();
292
    // Form submitter
F
Fatih Acet 已提交
293 294
    });
    gl.utils.localTimeAgo($('abbr.timeago, .js-timeago'), true);
295
    // Flash
J
José Iván 已提交
296 297
    if ($flash.length > 0) {
      $flash.click(function () {
F
Fatih Acet 已提交
298 299
        return $(this).fadeOut();
      });
J
José Iván 已提交
300
      $flash.show();
F
Fatih Acet 已提交
301
    }
302
    // Disable form buttons while a form is submitting
J
José Iván 已提交
303
    $body.on('ajax:complete, ajax:beforeSend, submit', 'form', function (e) {
F
Fatih Acet 已提交
304 305 306 307 308 309 310 311 312 313
      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 已提交
314 315
    $(document).ajaxError(function (e, xhrObj) {
      var ref = xhrObj.status;
F
Fatih Acet 已提交
316 317
      if (xhrObj.status === 401) {
        return new Flash('You need to be logged in.', 'alert');
J
José Iván 已提交
318
      } else if (ref === 404 || ref === 500) {
F
Fatih Acet 已提交
319 320 321
        return new Flash('Something went wrong on our end.', 'alert');
      }
    });
J
José Iván 已提交
322
    $('.account-box').hover(function () {
323
      // Show/Hide the profile menu when hovering the account box
F
Fatih Acet 已提交
324 325
      return $(this).toggleClass('hover');
    });
J
José Iván 已提交
326
    $document.on('click', '.diff-content .js-show-suppressed-diff', function () {
F
Fatih Acet 已提交
327 328 329 330
      var $container;
      $container = $(this).parent();
      $container.next('table').show();
      return $container.remove();
331
    // Commit show suppressed diff
F
Fatih Acet 已提交
332
    });
J
José Iván 已提交
333
    $('.navbar-toggle').on('click', function () {
F
Fatih Acet 已提交
334 335 336 337 338
      $('.header-content .title').toggle();
      $('.header-content .header-logo').toggle();
      $('.header-content .navbar-collapse').toggle();
      return $('.navbar-toggle').toggleClass('active');
    });
339
    // Show/hide comments on diff
J
José Iván 已提交
340
    $body.on('click', '.js-toggle-diff-comments', function (e) {
341 342
      var $this = $(this);
      var notesHolders = $this.closest('.diff-file').find('.notes_holder');
J
José Iván 已提交
343
      $this.toggleClass('active');
344
      if ($this.hasClass('active')) {
345
        notesHolders.show().find('.hide, .content').show();
346
      } else {
347
        notesHolders.hide().find('.content').hide();
348
      }
349
      $(document).trigger('toggle.comments');
F
Fatih Acet 已提交
350 351
      return e.preventDefault();
    });
J
José Iván 已提交
352 353 354 355 356
    $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 已提交
357 358 359
      e.preventDefault();
      return new ConfirmDangerModal(form, text);
    });
J
José Iván 已提交
360 361
    $('input[type="search"]').each(function () {
      var $this = $(this);
F
Fatih Acet 已提交
362 363
      $this.attr('value', $this.val());
    });
J
José Iván 已提交
364
    $document.off('keyup', 'input[type="search"]').on('keyup', 'input[type="search"]', function () {
F
Fatih Acet 已提交
365 366 367 368
      var $this;
      $this = $(this);
      return $this.attr('value', $this.val());
    });
J
José Iván 已提交
369
    $document.off('breakpoint:change').on('breakpoint:change', function (e, breakpoint) {
F
Fatih Acet 已提交
370 371 372 373 374 375 376 377
      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 已提交
378
    fitSidebarForSize = function () {
F
Fatih Acet 已提交
379 380 381 382 383 384 385
      var oldBootstrapBreakpoint;
      oldBootstrapBreakpoint = bootstrapBreakpoint;
      bootstrapBreakpoint = bp.getBreakpointSize();
      if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
        return $document.trigger('breakpoint:change', [bootstrapBreakpoint]);
      }
    };
J
José Iván 已提交
386
    $window.off('resize.app').on('resize.app', function () {
F
Fatih Acet 已提交
387 388 389 390
      return fitSidebarForSize();
    });
    gl.awardsHandler = new AwardsHandler();
    new Aside();
P
Phil Hughes 已提交
391 392

    gl.utils.initTimeagoTimeout();
393
  });
394
}).call(window);