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

313 lines
9.2 KiB
JavaScript
Raw Normal View History

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-10-15 14:42:47 +05:30
// bootstrap webpack, common libs, polyfills, and behaviors
import './webpack';
import './commons';
import './behaviors';
2017-08-17 22:00:37 +05:30
// lib/utils
2020-10-24 23:57:45 +05:30
import applyGitLabUIConfig from '@gitlab/ui/dist/config';
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
2019-12-04 20:38:33 +05:30
import {
handleLocationHash,
addSelectOnFocusBehaviour,
getCspNonceValue,
} 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
// everything else
2017-09-10 17:25:29 +05:30
import loadAwardsHandler from './awards_handler';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as 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';
2020-06-23 00:09:42 +05:30
import initFrequentItemDropdowns from './frequent_items';
2018-03-17 18:26:18 +05:30
import initBreadcrumbs from './breadcrumb';
2018-11-20 20:47:30 +05:30
import initUsagePingConsent from './usage_ping_consent';
2018-12-05 23:21:45 +05:30
import initPerformanceBar from './performance_bar';
2020-07-28 23:09:34 +05:30
import initSearchAutocomplete from './search_autocomplete';
2018-12-05 23:21:45 +05:30
import GlFieldErrors from './gl_field_errors';
2019-02-15 15:39:39 +05:30
import initUserPopovers from './user_popovers';
2020-03-13 15:44:24 +05:30
import initBroadcastNotifications from './broadcast_notification';
2020-06-23 00:09:42 +05:30
import initPersistentUserCallouts from './persistent_user_callouts';
2019-12-04 20:38:33 +05:30
import { initUserTracking } from './tracking';
2019-09-04 21:01:54 +05:30
import { __ } from './locale';
2017-09-10 17:25:29 +05:30
2019-09-30 21:07:59 +05:30
import 'ee_else_ce/main_ee';
2020-07-28 23:09:34 +05:30
applyGitLabUIConfig();
2018-10-15 14:42:47 +05:30
// expose jQuery as global (TODO: remove these)
window.jQuery = jQuery;
window.$ = jQuery;
2019-12-04 20:38:33 +05:30
// Add nonce to jQuery script handler
jQuery.ajaxSetup({
converters: {
2020-04-22 19:07:51 +05:30
// eslint-disable-next-line @gitlab/require-i18n-strings, func-names
2019-12-04 20:38:33 +05:30
'text script': function(text) {
jQuery.globalEval(text, { nonce: getCspNonceValue() });
return text;
},
},
});
2020-03-13 15:44:24 +05:30
function disableJQueryAnimations() {
$.fx.off = true;
}
// Disable jQuery animations
2020-07-28 23:09:34 +05:30
if (gon?.disable_animations) {
2020-03-13 15:44:24 +05:30
disableJQueryAnimations();
}
2018-03-27 19:54:05 +05:30
// inject test utilities if necessary
2020-07-28 23:09:34 +05:30
if (process.env.NODE_ENV !== 'production' && gon?.test_env) {
2020-03-13 15:44:24 +05:30
disableJQueryAnimations();
2020-04-08 14:13:33 +05:30
import(/* webpackMode: "eager" */ './test_utils/'); // eslint-disable-line no-unused-expressions
2018-03-27 19:54:05 +05:30
}
2017-08-17 22:00:37 +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
2018-11-08 19:23:39 +05:30
$('.has-tooltip, [data-toggle="tooltip"]').tooltip('dispose');
2017-09-10 17:25:29 +05:30
// Close any open popover
2018-11-08 19:23:39 +05:30
$('[data-toggle="popover"]').popover('dispose');
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
window.addEventListener('hashchange', handleLocationHash);
2018-10-15 14:42:47 +05:30
window.addEventListener(
'load',
function onLoad() {
window.removeEventListener('load', onLoad, false);
handleLocationHash();
},
false,
);
2017-08-17 22:00:37 +05:30
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
});
2019-03-02 22:35:43 +05:30
// Put all initialisations here that can also wait after everything is rendered and ready
function deferredInitialisation() {
2018-03-17 18:26:18 +05:30
const $body = $('body');
initBreadcrumbs();
initImporterStatus();
initTodoToggle();
initLogoAnimation();
2018-11-20 20:47:30 +05:30
initUsagePingConsent();
2019-02-15 15:39:39 +05:30
initUserPopovers();
2020-03-13 15:44:24 +05:30
initBroadcastNotifications();
2020-06-23 00:09:42 +05:30
initFrequentItemDropdowns();
initPersistentUserCallouts();
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
if (document.querySelector('.search')) initSearchAutocomplete();
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() {
2018-10-15 14:42:47 +05:30
$(this)
2018-11-08 19:23:39 +05:30
.tooltip('dispose')
2017-08-17 22:00:37 +05:30
.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() {
2020-03-13 15:44:24 +05:30
// eslint-disable-next-line no-jquery/no-fade
2018-10-15 14:42:47 +05:30
$(this)
.closest('tr')
.fadeOut();
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
2019-09-30 21:07:59 +05:30
const glTooltipDelay = localStorage.getItem('gl-tooltip-delay');
const delay = glTooltipDelay ? JSON.parse(glTooltipDelay) : 0;
2017-08-17 22:00:37 +05:30
// Initialize tooltips
$body.tooltip({
selector: '.has-tooltip, [data-toggle="tooltip"]',
2018-11-08 19:23:39 +05:30
trigger: 'hover',
boundary: 'viewport',
2019-09-30 21:07:59 +05:30
delay,
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
2019-03-02 22:35:43 +05:30
loadAwardsHandler();
2020-10-24 23:57:45 +05:30
// Adding a helper class to activate animations only after all is rendered
setTimeout(() => $body.addClass('page-initialised'), 1000);
2019-03-02 22:35:43 +05:30
}
document.addEventListener('DOMContentLoaded', () => {
const $body = $('body');
const $document = $(document);
2020-07-28 23:09:34 +05:30
const bootstrapBreakpoint = bp.getBreakpointSize();
2019-03-02 22:35:43 +05:30
if (document.querySelector('#js-peek')) initPerformanceBar({ container: '#js-peek' });
2020-05-24 23:13:21 +05:30
initUserTracking();
2019-03-02 22:35:43 +05:30
initLayoutNav();
// 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
$body.on('click', 'a[href^="#"]', function clickHashLinkCallback() {
const href = this.getAttribute('href');
if (href.substr(1) === getLocationHash()) {
setTimeout(handleLocationHash, 1);
}
});
2020-07-28 23:09:34 +05:30
/**
* TODO: Apparently we are collapsing the right sidebar on certain screensizes per default
* except on issue board pages. Why can't we do it with CSS?
*
* Proposal: Expose a global sidebar API, which we could import wherever we are manipulating
* the visibility of the sidebar.
*
* Quick fix: Get rid of jQuery for this implementation
*/
2020-04-08 14:13:33 +05:30
const isBoardsPage = /(projects|groups):boards:show/.test(document.body.dataset.page);
if (!isBoardsPage && (bootstrapBreakpoint === 'sm' || bootstrapBreakpoint === 'xs')) {
2020-03-13 15:44:24 +05:30
const $rightSidebar = $('aside.right-sidebar');
const $layoutPage = $('.layout-page');
2019-03-02 22:35:43 +05:30
2020-03-13 15:44:24 +05:30
if ($rightSidebar.length > 0) {
$rightSidebar.removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
$layoutPage.removeClass('right-sidebar-expanded').addClass('right-sidebar-collapsed');
} else {
$layoutPage.removeClass('right-sidebar-expanded right-sidebar-collapsed');
}
2019-03-02 22:35:43 +05:30
}
// prevent default action for disabled buttons
$('.btn').click(function clickDisabledButtonCallback(e) {
if ($(this).hasClass('disabled')) {
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
return true;
});
localTimeAgo($('abbr.timeago, .js-timeago'), true);
2020-07-28 23:09:34 +05:30
/**
* This disables form buttons while a form is submitting
* We do not difinitively know all of the places where this is used
*
* TODO: Defer execution, migrate to behaviors, and add sentry logging
*/
2018-03-17 18:26:18 +05:30
$body.on('ajax:complete, ajax:beforeSend, submit', 'form', function ajaxCompleteCallback(e) {
2020-03-13 15:44:24 +05:30
const $buttons = $('[type="submit"], .js-disable-on-submit', this).not('.js-no-auto-disable');
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
2020-01-01 13:55:28 +05:30
// eslint-disable-next-line no-jquery/no-ajax-events
2018-03-17 18:26:18 +05:30
$(document).ajaxError((e, xhrObj) => {
const ref = xhrObj.status;
if (ref === 401) {
2019-09-04 21:01:54 +05:30
Flash(__('You need to be logged in.'));
2017-08-17 22:00:37 +05:30
} else if (ref === 404 || ref === 500) {
2019-09-04 21:01:54 +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
2019-03-02 22:35:43 +05:30
$('.navbar-toggler').on('click', () => {
$('.header-content').toggleClass('menu-expanded');
});
2020-07-28 23:09:34 +05:30
/**
* Show suppressed commit diff
*
* TODO: Move to commit diff pages
*/
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();
});
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')) {
2018-10-15 14:42:47 +05:30
notesHolders
.show()
.find('.hide, .content')
.show();
2017-08-17 22:00:37 +05:30
} else {
2018-10-15 14:42:47 +05:30
notesHolders
.hide()
.find('.content')
.hide();
2017-08-17 22:00:37 +05:30
}
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
$('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();
2019-12-21 20:55:43 +05:30
// eslint-disable-next-line no-jquery/no-serialize
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) {
2018-10-15 14:42:47 +05:30
flashContainer
.querySelectorAll('.flash-alert, .flash-notice, .flash-success')
.forEach(flashEl => {
removeFlashClickListener(flashEl);
});
2018-03-17 18:26:18 +05:30
}
2018-12-05 23:21:45 +05:30
// initialize field errors
$('.gl-show-field-errors').each((i, form) => new GlFieldErrors(form));
2019-03-02 22:35:43 +05:30
requestIdleCallback(deferredInitialisation);
2017-08-17 22:00:37 +05:30
});