debian-mirror-gitlab/app/assets/javascripts/main.js

257 lines
7.3 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
/* eslint-disable import/first */
2018-05-09 12:01:36 +05:30
/* global $ */
2017-08-17 22:00:37 +05:30
import jQuery from 'jquery';
import Cookies from 'js-cookie';
2018-03-17 18:26:18 +05:30
import svg4everybody from 'svg4everybody';
2017-08-17 22:00:37 +05:30
// expose common libraries as globals (TODO: remove these)
window.jQuery = jQuery;
window.$ = jQuery;
// lib/utils
2018-03-27 19:54:05 +05:30
import { handleLocationHash, addSelectOnFocusBehaviour } from './lib/utils/common_utils';
2018-03-17 18:26:18 +05:30
import { localTimeAgo } from './lib/utils/datetime_utility';
import { getLocationHash, visitUrl } from './lib/utils/url_utility';
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
// behaviors
import './behaviors/';
2017-08-17 22:00:37 +05:30
// everything else
2017-09-10 17:25:29 +05:30
import loadAwardsHandler from './awards_handler';
import bp from './breakpoints';
2018-03-17 18:26:18 +05:30
import Flash, { removeFlashClickListener } from './flash';
2017-08-17 22:00:37 +05:30
import './gl_dropdown';
2018-03-17 18:26:18 +05:30
import initTodoToggle from './header';
import initImporterStatus from './importer_status';
import initLayoutNav from './layout_nav';
import './feature_highlight/feature_highlight_options';
2017-09-10 17:25:29 +05:30
import LazyLoader from './lazy_loader';
2018-03-17 18:26:18 +05:30
import initLogoAnimation from './logo';
2017-08-17 22:00:37 +05:30
import './milestone_select';
2018-03-17 18:26:18 +05:30
import './projects_dropdown';
import initBreadcrumbs from './breadcrumb';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
import initDispatcher from './dispatcher';
2017-09-10 17:25:29 +05:30
2018-03-27 19:54:05 +05:30
// inject test utilities if necessary
if (process.env.NODE_ENV !== 'production' && gon && gon.test_env) {
$.fx.off = true;
import(/* webpackMode: "eager" */ './test_utils/');
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
svg4everybody();
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
document.addEventListener('beforeunload', () => {
2017-08-17 22:00:37 +05:30
// Unbind scroll events
$(document).off('scroll');
// Close any open tooltips
$('.has-tooltip, [data-toggle="tooltip"]').tooltip('destroy');
2017-09-10 17:25:29 +05:30
// Close any open popover
$('[data-toggle="popover"]').popover('destroy');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
window.addEventListener('hashchange', handleLocationHash);
2017-08-17 22:00:37 +05:30
window.addEventListener('load', function onLoad() {
window.removeEventListener('load', onLoad, false);
2018-03-17 18:26:18 +05:30
handleLocationHash();
2017-08-17 22:00:37 +05:30
}, false);
2017-09-10 17:25:29 +05:30
gl.lazyLoader = new LazyLoader({
scrollContainer: window,
2018-03-17 18:26:18 +05:30
observerNode: '#content-body',
2017-09-10 17:25:29 +05:30
});
2018-03-27 19:54:05 +05:30
document.addEventListener('DOMContentLoaded', () => {
2018-03-17 18:26:18 +05:30
const $body = $('body');
const $document = $(document);
const $window = $(window);
const $sidebarGutterToggle = $('.js-sidebar-toggle');
let bootstrapBreakpoint = bp.getBreakpointSize();
initBreadcrumbs();
initLayoutNav();
initImporterStatus();
initTodoToggle();
initLogoAnimation();
2017-08-17 22:00:37 +05:30
// 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
2018-03-17 18:26:18 +05:30
$body.on('click', 'a[href^="#"]', function clickHashLinkCallback() {
const href = this.getAttribute('href');
if (href.substr(1) === getLocationHash()) {
setTimeout(handleLocationHash, 1);
2017-08-17 22:00:37 +05:30
}
});
if (bootstrapBreakpoint === 'xs') {
2018-03-17 18:26:18 +05:30
const $rightSidebar = $('aside.right-sidebar, .layout-page');
2017-08-17 22:00:37 +05:30
$rightSidebar
.removeClass('right-sidebar-expanded')
.addClass('right-sidebar-collapsed');
}
// prevent default action for disabled buttons
2018-03-17 18:26:18 +05:30
$('.btn').click(function clickDisabledButtonCallback(e) {
2017-08-17 22:00:37 +05:30
if ($(this).hasClass('disabled')) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
2018-03-17 18:26:18 +05:30
return true;
2017-08-17 22:00:37 +05:30
});
2018-03-27 19:54:05 +05:30
addSelectOnFocusBehaviour('.js-select-on-focus');
2018-03-17 18:26:18 +05:30
$('.remove-row').on('ajax:success', function removeRowAjaxSuccessCallback() {
2017-08-17 22:00:37 +05:30
$(this).tooltip('destroy')
.closest('li')
.fadeOut();
});
2018-03-17 18:26:18 +05:30
$('.js-remove-tr').on('ajax:before', function removeTRAjaxBeforeCallback() {
$(this).hide();
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
$('.js-remove-tr').on('ajax:success', function removeTRAjaxSuccessCallback() {
$(this).closest('tr').fadeOut();
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
// Initialize select2 selects
2017-08-17 22:00:37 +05:30
$('select.select2').select2({
width: 'resolve',
2018-03-17 18:26:18 +05:30
dropdownAutoWidth: true,
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
// Close select2 on escape
2018-03-17 18:26:18 +05:30
$('.js-select2').on('select2-close', () => {
setTimeout(() => {
$('.select2-container-active').removeClass('select2-container-active');
$(':focus').blur();
}, 1);
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
// Initialize tooltips
$.fn.tooltip.Constructor.DEFAULTS.trigger = 'hover';
$body.tooltip({
selector: '.has-tooltip, [data-toggle="tooltip"]',
2018-03-17 18:26:18 +05:30
placement(tip, el) {
2017-08-17 22:00:37 +05:30
return $(el).data('placement') || 'bottom';
2018-03-17 18:26:18 +05:30
},
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
// Initialize popovers
$body.popover({
selector: '[data-toggle="popover"]',
2018-03-17 18:26:18 +05:30
trigger: 'focus',
// set the viewport to the main content, excluding the navigation bar, so
// the navigation can't overlap the popover
viewport: '.layout-page',
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
// Form submitter
2018-03-17 18:26:18 +05:30
$('.trigger-submit').on('change', function triggerSubmitCallback() {
$(this).parents('form').submit();
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
localTimeAgo($('abbr.timeago, .js-timeago'), true);
2017-08-17 22:00:37 +05:30
// Disable form buttons while a form is submitting
2018-03-17 18:26:18 +05:30
$body.on('ajax:complete, ajax:beforeSend, submit', 'form', function ajaxCompleteCallback(e) {
const $buttons = $('[type="submit"], .js-disable-on-submit', this);
2017-08-17 22:00:37 +05:30
switch (e.type) {
case 'ajax:beforeSend':
case 'submit':
2018-03-17 18:26:18 +05:30
return $buttons.disable();
2017-08-17 22:00:37 +05:30
default:
2018-03-17 18:26:18 +05:30
return $buttons.enable();
2017-08-17 22:00:37 +05:30
}
});
2018-03-17 18:26:18 +05:30
$(document).ajaxError((e, xhrObj) => {
const ref = xhrObj.status;
if (ref === 401) {
Flash('You need to be logged in.');
2017-08-17 22:00:37 +05:30
} else if (ref === 404 || ref === 500) {
2018-03-17 18:26:18 +05:30
Flash('Something went wrong on our end.');
2017-08-17 22:00:37 +05:30
}
});
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
// Commit show suppressed diff
2018-03-17 18:26:18 +05:30
$document.on('click', '.diff-content .js-show-suppressed-diff', function showDiffCallback() {
const $container = $(this).parent();
$container.next('table').show();
$container.remove();
});
$('.navbar-toggle').on('click', () => {
$('.header-content').toggleClass('menu-expanded');
gl.lazyLoader.loadCheck();
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
// Show/hide comments on diff
2018-03-17 18:26:18 +05:30
$body.on('click', '.js-toggle-diff-comments', function toggleDiffCommentsCallback(e) {
const $this = $(this);
const notesHolders = $this.closest('.diff-file').find('.notes_holder');
e.preventDefault();
2017-08-17 22:00:37 +05:30
$this.toggleClass('active');
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
if ($this.hasClass('active')) {
notesHolders.show().find('.hide, .content').show();
} else {
notesHolders.hide().find('.content').hide();
}
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
$(document).trigger('toggle.comments');
});
2018-03-17 18:26:18 +05:30
$document.on('breakpoint:change', (e, breakpoint) => {
2017-08-17 22:00:37 +05:30
if (breakpoint === 'sm' || breakpoint === 'xs') {
2018-03-17 18:26:18 +05:30
const $gutterIcon = $sidebarGutterToggle.find('i');
2017-08-17 22:00:37 +05:30
if ($gutterIcon.hasClass('fa-angle-double-right')) {
2018-03-17 18:26:18 +05:30
$sidebarGutterToggle.trigger('click');
2017-08-17 22:00:37 +05:30
}
}
});
2018-03-17 18:26:18 +05:30
function fitSidebarForSize() {
const oldBootstrapBreakpoint = bootstrapBreakpoint;
2017-08-17 22:00:37 +05:30
bootstrapBreakpoint = bp.getBreakpointSize();
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
if (bootstrapBreakpoint !== oldBootstrapBreakpoint) {
2018-03-17 18:26:18 +05:30
$document.trigger('breakpoint:change', [bootstrapBreakpoint]);
2017-08-17 22:00:37 +05:30
}
2018-03-17 18:26:18 +05:30
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$window.on('resize.app', fitSidebarForSize);
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
loadAwardsHandler();
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
$('form.filter-form').on('submit', function filterFormSubmitCallback(event) {
2017-09-10 17:25:29 +05:30
const link = document.createElement('a');
link.href = this.action;
const action = `${this.action}${link.search === '' ? '?' : '&'}`;
event.preventDefault();
2018-03-17 18:26:18 +05:30
visitUrl(`${action}${$(this).serialize()}`);
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
const flashContainer = document.querySelector('.flash-container');
if (flashContainer && flashContainer.children.length) {
flashContainer.querySelectorAll('.flash-alert, .flash-notice, .flash-success').forEach((flashEl) => {
removeFlashClickListener(flashEl);
});
}
initDispatcher();
2017-08-17 22:00:37 +05:30
});