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'; 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); $.post($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 handleGlobalEnterQuickSubmit(target) { const $target = $(target); const $form = $(target).closest('form'); if ($form.length) { // here use the event to trigger the submit event (instead of calling `submit()` method directly) // otherwise the `areYouSure` handler won't be executed, then there will be an annoying "confirm to leave" dialog $form.trigger('submit'); } else { // if no form, then the editor is for an AJAX request, dispatch an event to the target, let the target's event handler to do the AJAX request. // the 'ce-' prefix means this is a CustomEvent $target.trigger('ce-quick-submit'); } } export function initGlobalButtonClickOnEnter() { $(document).on('keypress', '.ui.button', (e) => { if (e.keyCode === 13 || e.keyCode === 32) { // enter key or space bar $(e.target).trigger('click'); } }); } export function initGlobalCommon() { // Show exact time $('.time-since').each(function () { $(this) .addClass('tooltip') .attr('data-content', $(this).attr('title')) .attr('title', ''); }); // 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. $('.dropdown:not(.custom)').dropdown({ fullTextSearch: 'exact' }); $('.jump.dropdown').dropdown({ action: 'hide', onShow() { $('.tooltip').popup('hide'); }, fullTextSearch: 'exact' }); $('.slide.up.dropdown').dropdown({ transition: 'slide up', fullTextSearch: 'exact' }); $('.upward.dropdown').dropdown({ direction: 'upward', fullTextSearch: 'exact' }); $('.ui.checkbox').checkbox(); $('.ui.progress').progress({ showActivity: false }); // init popups $('.tooltip').each((_, el) => { const $el = $(el); const attr = $el.attr('data-variation'); const attrs = attr ? attr.split(' ') : []; const variations = new Set([...attrs, 'inverted', 'tiny']); $el.attr('data-variation', [...variations].join(' ')).popup(); }); $('.top.menu .tooltip').popup({ onShow() { if ($('.top.menu .menu.transition').hasClass('visible')) { return false; } } }); $('.tabular.menu .item').tab(); $('.tabable.menu .item').tab(); $('.toggle.button').on('click', function () { $($(this).data('target')).slideToggle(100); }); // make table