import $ from 'jquery'; import 'jquery.are-you-sure'; import {mqBinarySearch} from '../utils.js'; import {createDropzone} from './dropzone.js'; import {initCompColorPicker} from './comp/ColorPicker.js'; import {showGlobalErrorMessage} from '../bootstrap.js'; import {attachCheckboxAria, attachDropdownAria} from './aria.js'; import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js'; import {initTooltip} from '../modules/tippy.js'; import {svg} from '../svg.js'; import {hideElem, showElem, toggleElem} from '../utils/dom.js'; const {appUrl, csrfToken} = window.config; export function initGlobalFormDirtyLeaveConfirm() { // Warn users that try to leave a page after entering data into a form. // Except on sign-in pages, and for forms marked as 'ignore-dirty'. if ($('.user.signin').length === 0) { $('form:not(.ignore-dirty)').areYouSure(); } } export function initHeadNavbarContentToggle() { const content = $('#navbar'); const toggle = $('#navbar-expand-toggle'); let isExpanded = false; toggle.on('click', () => { isExpanded = !isExpanded; if (isExpanded) { content.addClass('shown'); toggle.addClass('active'); } else { content.removeClass('shown'); toggle.removeClass('active'); } }); } export function initFootLanguageMenu() { function linkLanguageAction() { const $this = $(this); $.get($this.data('url')).always(() => { window.location.reload(); }); } $('.language-menu a[lang]').on('click', linkLanguageAction); } export function initGlobalEnterQuickSubmit() { $(document).on('keydown', '.js-quick-submit', (e) => { if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.key === 'Enter')) { handleGlobalEnterQuickSubmit(e.target); return false; } }); } export function initGlobalButtonClickOnEnter() { $(document).on('keypress', '.ui.button', (e) => { if (e.keyCode === 13 || e.keyCode === 32) { // enter key or space bar if (e.target.nodeName === 'BUTTON') return; // button already handles space&enter correctly $(e.target).trigger('click'); e.preventDefault(); } }); } export function initGlobalTooltips() { for (const el of document.getElementsByClassName('tooltip')) { initTooltip(el); } } export function initGlobalCommon() { // Undo Safari emoji glitch fix at high enough zoom levels if (navigator.userAgent.match('Safari')) { $(window).resize(() => { const px = mqBinarySearch('width', 0, 4096, 1, 'px'); const em = mqBinarySearch('width', 0, 1024, 0.01, 'em'); if (em * 16 * 1.25 - px <= -1) { $('body').addClass('safari-above125'); } else { $('body').removeClass('safari-above125'); } }); } // Semantic UI modules. const $uiDropdowns = $('.ui.dropdown'); // do not init "custom" dropdowns, "custom" dropdowns are managed by their own code. $uiDropdowns.filter(':not(.custom)').dropdown({fullTextSearch: 'exact'}); // The "jump" means this dropdown is mainly used for "menu" purpose, // clicking an item will jump to somewhere else or trigger an action/function. // When a dropdown is used for non-refresh actions with tippy, // it must have this "jump" class to hide the tippy when dropdown is closed. $uiDropdowns.filter('.jump').dropdown({ action: 'hide', onShow() { // hide associated tooltip while dropdown is open this._tippy?.hide(); this._tippy?.disable(); }, onHide() { this._tippy?.enable(); // hide all tippy elements of items after a while. eg: use Enter to click "Copy Link" in the Issue Context Menu setTimeout(() => { const $dropdown = $(this); if ($dropdown.dropdown('is hidden')) { $(this).find('.menu > .item').each((_, item) => { item._tippy?.hide(); }); } }, 2000); }, }); // special animations/popup-directions $uiDropdowns.filter('.slide.up').dropdown({transition: 'slide up'}); $uiDropdowns.filter('.upward').dropdown({direction: 'upward'}); attachDropdownAria($uiDropdowns); attachCheckboxAria($('.ui.checkbox')); $('.tabular.menu .item').tab(); $('.tabable.menu .item').tab(); $('.toggle.button').on('click', function () { toggleElem($($(this).data('target'))); }); // make table