gitlab-org--gitlab-foss/app/assets/javascripts/main.js

269 lines
7.6 KiB
JavaScript
Raw Normal View History

2017-12-19 05:12:32 -05:00
/* eslint-disable import/first */
/* global ConfirmDangerModal */
import jQuery from 'jquery';
import Cookies from 'js-cookie';
import svg4everybody from 'svg4everybody';
// expose common libraries as globals (TODO: remove these)
window.jQuery = jQuery;
window.$ = jQuery;
// lib/utils
import { handleLocationHash } from './lib/utils/common_utils';
2017-12-19 05:12:32 -05:00
import { localTimeAgo } from './lib/utils/datetime_utility';
import { getLocationHash, visitUrl } from './lib/utils/url_utility';
// behaviors
import './behaviors/';
// everything else
import loadAwardsHandler from './awards_handler';
import bp from './breakpoints';
import './confirm_danger_modal';
import Flash, { removeFlashClickListener } from './flash';
import './gl_dropdown';
import initTodoToggle from './header';
2017-10-26 14:03:48 -04:00
import initImporterStatus from './importer_status';
2017-12-19 03:32:51 -05:00
import initLayoutNav from './layout_nav';
import LazyLoader from './lazy_loader';
import initLogoAnimation from './logo';
import './milestone_select';
2017-09-04 11:48:20 -04:00
import './projects_dropdown';
import './render_gfm';
import initBreadcrumbs from './breadcrumb';
2017-04-04 13:47:12 -04:00
import './dispatcher';
2017-05-18 16:11:20 -04:00
// eslint-disable-next-line global-require, import/no-commonjs
2017-04-04 13:47:12 -04:00
if (process.env.NODE_ENV !== 'production') require('./test_utils/');
svg4everybody();
2017-12-19 05:12:32 -05:00
document.addEventListener('beforeunload', () => {
// Unbind scroll events
$(document).off('scroll');
// Close any open tooltips
$('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
2017-06-23 16:52:43 -04:00
// Close any open popover
$('[data-toggle="popover"]').popover('destroy');
});
2016-07-24 16:45:11 -04:00
window.addEventListener('hashchange', handleLocationHash);
window.addEventListener('load', function onLoad() {
window.removeEventListener('load', onLoad, false);
handleLocationHash();
}, false);
gl.lazyLoader = new LazyLoader({
scrollContainer: window,
2017-12-19 05:12:32 -05:00
observerNode: '#content-body',
});
2017-12-19 05:12:32 -05:00
$(() => {
const $body = $('body');
const $document = $(document);
const $window = $(window);
const $sidebarGutterToggle = $('.js-sidebar-toggle');
let bootstrapBreakpoint = bp.getBreakpointSize();
initBreadcrumbs();
2017-12-19 03:32:51 -05:00
initLayoutNav();
2017-10-26 14:03:48 -04:00
initImporterStatus();
initTodoToggle();
initLogoAnimation();
// Set the default path for all cookies to GitLab's root directory
Cookies.defaults.path = gon.relative_url_root || '/';
// `hashchange` is not triggered when link target is already in window.location
2017-12-19 05:12:32 -05:00
$body.on('click', 'a[href^="#"]', function clickHashLinkCallback() {
const href = this.getAttribute('href');
if (href.substr(1) === getLocationHash()) {
setTimeout(handleLocationHash, 1);
}
});
2016-11-06 19:16:39 -05:00
if (bootstrapBreakpoint === 'xs') {
const $rightSidebar = $('aside.right-sidebar, .layout-page');
$rightSidebar
.removeClass('right-sidebar-expanded')
.addClass('right-sidebar-collapsed');
}
// prevent default action for disabled buttons
2017-12-19 05:12:32 -05:00
$('.btn').click(function clickDisabledButtonCallback(e) {
if ($(this).hasClass('disabled')) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
2016-07-24 16:45:11 -04:00
}
2017-12-19 05:12:32 -05:00
return true;
});
// Click a .js-select-on-focus field, select the contents
// Prevent a mouseup event from deselecting the input
2017-12-19 05:12:32 -05:00
$('.js-select-on-focus').on('focusin', function selectOnFocusCallback() {
$(this).select().one('mouseup', (e) => {
e.preventDefault();
});
});
2017-12-19 05:12:32 -05:00
$('.remove-row').on('ajax:success', function removeRowAjaxSuccessCallback() {
$(this).tooltip('destroy')
.closest('li')
.fadeOut();
});
2017-12-19 05:12:32 -05:00
$('.js-remove-tr').on('ajax:before', function removeTRAjaxBeforeCallback() {
$(this).hide();
});
2017-12-19 05:12:32 -05:00
$('.js-remove-tr').on('ajax:success', function removeTRAjaxSuccessCallback() {
$(this).closest('tr').fadeOut();
});
2017-12-19 05:12:32 -05:00
// Initialize select2 selects
$('select.select2').select2({
width: 'resolve',
2017-12-19 05:12:32 -05:00
dropdownAutoWidth: true,
});
2017-12-19 05:12:32 -05:00
// Close select2 on escape
2017-12-19 05:12:32 -05:00
$('.js-select2').on('select2-close', () => {
setTimeout(() => {
$('.select2-container-active').removeClass('select2-container-active');
$(':focus').blur();
}, 1);
});
2017-12-19 05:12:32 -05:00
// Initialize tooltips
$.fn.tooltip.Constructor.DEFAULTS.trigger = 'hover';
$body.tooltip({
selector: '.has-tooltip, [data-toggle="tooltip"]',
2017-12-19 05:12:32 -05:00
placement(tip, el) {
return $(el).data('placement') || 'bottom';
2017-12-19 05:12:32 -05:00
},
});
2017-12-19 05:12:32 -05:00
2017-06-23 16:52:43 -04:00
// Initialize popovers
$body.popover({
selector: '[data-toggle="popover"]',
trigger: 'focus',
// set the viewport to the main content, excluding the navigation bar, so
// the navigation can't overlap the popover
2017-12-19 05:12:32 -05:00
viewport: '.layout-page',
2017-06-23 16:52:43 -04:00
});
2017-12-19 05:12:32 -05:00
// Form submitter
2017-12-19 05:12:32 -05:00
$('.trigger-submit').on('change', function triggerSubmitCallback() {
$(this).parents('form').submit();
});
2017-12-19 05:12:32 -05:00
localTimeAgo($('abbr.timeago, .js-timeago'), true);
2017-12-19 05:12:32 -05:00
// Disable form buttons while a form is submitting
2017-12-19 05:12:32 -05:00
$body.on('ajax:complete, ajax:beforeSend, submit', 'form', function ajaxCompleteCallback(e) {
const $buttons = $('[type="submit"], .js-disable-on-submit', this);
switch (e.type) {
case 'ajax:beforeSend':
case 'submit':
2017-12-19 05:12:32 -05:00
return $buttons.disable();
default:
2017-12-19 05:12:32 -05:00
return $buttons.enable();
}
});
2017-12-19 05:12:32 -05:00
$(document).ajaxError((e, xhrObj) => {
const ref = xhrObj.status;
if (ref === 401) {
Flash('You need to be logged in.');
} else if (ref === 404 || ref === 500) {
2017-12-19 05:12:32 -05:00
Flash('Something went wrong on our end.');
}
});
2017-12-19 05:12:32 -05:00
// Commit show suppressed diff
2017-12-19 05:12:32 -05:00
$document.on('click', '.diff-content .js-show-suppressed-diff', function showDiffCallback() {
const $container = $(this).parent();
$container.next('table').show();
$container.remove();
});
2017-12-19 05:12:32 -05:00
$('.navbar-toggle').on('click', () => {
$('.header-content').toggleClass('menu-expanded');
gl.lazyLoader.loadCheck();
});
2017-12-19 05:12:32 -05:00
// Show/hide comments on diff
2017-12-19 05:12:32 -05:00
$body.on('click', '.js-toggle-diff-comments', function toggleDiffCommentsCallback(e) {
const $this = $(this);
const notesHolders = $this.closest('.diff-file').find('.notes_holder');
e.preventDefault();
$this.toggleClass('active');
2017-12-19 05:12:32 -05:00
if ($this.hasClass('active')) {
notesHolders.show().find('.hide, .content').show();
} else {
notesHolders.hide().find('.content').hide();
}
2017-12-19 05:12:32 -05:00
$(document).trigger('toggle.comments');
});
2017-12-19 05:12:32 -05:00
$document.on('click', '.js-confirm-danger', (e) => {
const btn = $(e.target);
const form = btn.closest('form');
const text = btn.data('confirm-danger-message');
e.preventDefault();
2017-12-19 05:12:32 -05:00
// eslint-disable-next-line no-new
new ConfirmDangerModal(form, text);
});
2017-12-19 05:12:32 -05:00
$document.on('breakpoint:change', (e, breakpoint) => {
if (breakpoint === 'sm' || breakpoint === 'xs') {
2017-12-19 05:12:32 -05:00
const $gutterIcon = $sidebarGutterToggle.find('i');
if ($gutterIcon.hasClass('fa-angle-double-right')) {
2017-12-19 05:12:32 -05:00
$sidebarGutterToggle.trigger('click');
2016-07-24 16:45:11 -04:00
}
}
});
2017-12-19 05:12:32 -05:00
function fitSidebarForSize() {
const oldBootstrapBreakpoint = bootstrapBreakpoint;
bootstrapBreakpoint = bp.getBreakpointSize();
2017-12-19 05:12:32 -05:00
if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
2017-12-19 05:12:32 -05:00
$document.trigger('breakpoint:change', [bootstrapBreakpoint]);
}
2017-12-19 05:12:32 -05:00
}
2017-12-19 05:12:32 -05:00
$window.on('resize.app', fitSidebarForSize);
loadAwardsHandler();
2017-12-19 05:12:32 -05:00
$('form.filter-form').on('submit', function filterFormSubmitCallback(event) {
const link = document.createElement('a');
link.href = this.action;
const action = `${this.action}${link.search === '' ? '?' : '&'}`;
event.preventDefault();
visitUrl(`${action}${$(this).serialize()}`);
});
const flashContainer = document.querySelector('.flash-container');
if (flashContainer && flashContainer.children.length) {
flashContainer.querySelectorAll('.flash-alert, .flash-notice, .flash-success').forEach((flashEl) => {
removeFlashClickListener(flashEl);
});
}
});