import $ from 'jquery'; import 'jquery.are-you-sure'; import {createDropzone} from './dropzone.js'; import {initCompColorPicker} from './comp/ColorPicker.js'; import {showGlobalErrorMessage} from '../bootstrap.js'; import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js'; import {svg} from '../svg.js'; import {hideElem, showElem, toggleElem} from '../utils/dom.js'; import {htmlEscape} from 'escape-goat'; import {createTippy} from '../modules/tippy.js'; const {appUrl, appSubUrl, csrfToken, i18n} = 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 navbar = document.getElementById('navbar'); const btn = document.getElementById('navbar-expand-toggle'); if (!navbar || !btn) return; btn.addEventListener('click', () => { const isExpanded = btn.classList.contains('active'); navbar.classList.toggle('navbar-menu-open', !isExpanded); btn.classList.toggle('active', !isExpanded); }); } 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', 'div.ui.button,span.ui.button', (e) => { if (e.code === ' ' || e.code === 'Enter') { $(e.target).trigger('click'); e.preventDefault(); } }); } // doRedirect does real redirection to bypass the browser's limitations of "location" // more details are in the backend's fetch-redirect handler function doRedirect(redirect) { const form = document.createElement('form'); const input = document.createElement('input'); form.method = 'post'; form.action = `${appSubUrl}/-/fetch-redirect`; input.type = 'hidden'; input.name = 'redirect'; input.value = redirect; form.append(input); document.body.append(form); form.submit(); } async function formFetchAction(e) { if (!e.target.classList.contains('form-fetch-action')) return; e.preventDefault(); const formEl = e.target; if (formEl.classList.contains('is-loading')) return; formEl.classList.add('is-loading'); if (formEl.clientHeight < 50) { formEl.classList.add('small-loading-icon'); } const formMethod = formEl.getAttribute('method') || 'get'; const formActionUrl = formEl.getAttribute('action'); const formData = new FormData(formEl); const [submitterName, submitterValue] = [e.submitter?.getAttribute('name'), e.submitter?.getAttribute('value')]; if (submitterName) { formData.append(submitterName, submitterValue || ''); } let reqUrl = formActionUrl; const reqOpt = {method: formMethod.toUpperCase(), headers: {'X-Csrf-Token': csrfToken}}; if (formMethod.toLowerCase() === 'get') { const params = new URLSearchParams(); for (const [key, value] of formData) { params.append(key, value.toString()); } const pos = reqUrl.indexOf('?'); if (pos !== -1) { reqUrl = reqUrl.slice(0, pos); } reqUrl += `?${params.toString()}`; } else { reqOpt.body = formData; } let errorTippy; const onError = (msg) => { formEl.classList.remove('is-loading', 'small-loading-icon'); if (errorTippy) errorTippy.destroy(); // TODO: use a better toast UI instead of the tippy. If the form height is large, the tippy position is not good errorTippy = createTippy(formEl, { content: msg, interactive: true, showOnCreate: true, hideOnClick: true, role: 'alert', theme: 'form-fetch-error', trigger: 'manual', arrow: false, }); }; const doRequest = async () => { try { const resp = await fetch(reqUrl, reqOpt); if (resp.status === 200) { const {redirect} = await resp.json(); formEl.classList.remove('dirty'); // remove the areYouSure check before reloading if (redirect) { doRedirect(redirect); } else { window.location.reload(); } } else if (resp.status >= 400 && resp.status < 500) { const data = await resp.json(); // the code was quite messy, sometimes the backend uses "err", sometimes it uses "error", and even "user_error" // but at the moment, as a new approach, we only use "errorMessage" here, backend can use JSONError() to respond. onError(data.errorMessage || `server error: ${resp.status}`); } else { onError(`server error: ${resp.status}`); } } catch (e) { console.error('error when doRequest', e); onError(i18n.network_error); } }; // TODO: add "confirm" support like "link-action" in the future await doRequest(); } export function initGlobalCommon() { // 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(); // 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 popup-directions, prevent Fomantic from guessing the popup direction. // With default "direction: auto", if the viewport height is small, Fomantic would show the popup upward, // if the dropdown is at the beginning of the page, then the top part would be clipped by the window view. // eg: Issue List "Sort" dropdown // But we can not set "direction: downward" for all dropdowns, because there is a bug in dropdown menu positioning when calculating the "left" position, // which would make some dropdown popups slightly shift out of the right viewport edge in some cases. // eg: the "Create New Repo" menu on the navbar. $uiDropdowns.filter('.upward').dropdown('setting', 'direction', 'upward'); $uiDropdowns.filter('.downward').dropdown('setting', 'direction', 'downward'); $('.ui.checkbox').checkbox(); $('.tabular.menu .item').tab(); document.addEventListener('submit', formFetchAction); } export function initGlobalDropzone() { // Dropzone for (const el of document.querySelectorAll('.dropzone')) { const $dropzone = $(el); const _promise = createDropzone(el, { url: $dropzone.data('upload-url'), headers: {'X-Csrf-Token': csrfToken}, maxFiles: $dropzone.data('max-file'), maxFilesize: $dropzone.data('max-size'), acceptedFiles: (['*/*', ''].includes($dropzone.data('accepts'))) ? null : $dropzone.data('accepts'), addRemoveLinks: true, dictDefaultMessage: $dropzone.data('default-message'), dictInvalidFileType: $dropzone.data('invalid-input-type'), dictFileTooBig: $dropzone.data('file-too-big'), dictRemoveFile: $dropzone.data('remove-file'), timeout: 0, thumbnailMethod: 'contain', thumbnailWidth: 480, thumbnailHeight: 480, init() { this.on('success', (file, data) => { file.uuid = data.uuid; const input = $(``).val(data.uuid); $dropzone.find('.files').append(input); // Create a "Copy Link" element, to conveniently copy the image // or file link as Markdown to the clipboard const copyLinkElement = document.createElement('div'); copyLinkElement.className = 'gt-tc'; // The a element has a hardcoded cursor: pointer because the default is overridden by .dropzone copyLinkElement.innerHTML = `${svg('octicon-copy', 14, 'copy link')} Copy link`; copyLinkElement.addEventListener('click', (e) => { e.preventDefault(); let fileMarkdown = `[${file.name}](/attachments/${file.uuid})`; if (file.type.startsWith('image/')) { fileMarkdown = `!${fileMarkdown}`; } else if (file.type.startsWith('video/')) { fileMarkdown = ``; } navigator.clipboard.writeText(fileMarkdown); }); file.previewTemplate.append(copyLinkElement); }); this.on('removedfile', (file) => { $(`#${file.uuid}`).remove(); if ($dropzone.data('remove-url')) { $.post($dropzone.data('remove-url'), { file: file.uuid, _csrf: csrfToken, }); } }); }, }); } } function linkAction(e) { e.preventDefault(); // A "link-action" can post AJAX request to its "data-url" // Then the browser is redirected to: the "redirect" in response, or "data-redirect" attribute, or current URL by reloading. // If the "link-action" has "data-modal-confirm" attribute, a confirm modal dialog will be shown before taking action. const $this = $(this); const redirect = $this.attr('data-redirect'); const doRequest = () => { $this.prop('disabled', true); $.post($this.attr('data-url'), { _csrf: csrfToken }).done((data) => { if (data && data.redirect) { window.location.href = data.redirect; } else if (redirect) { window.location.href = redirect; } else { window.location.reload(); } }).always(() => { $this.prop('disabled', false); }); }; const modalConfirmHtml = htmlEscape($this.attr('data-modal-confirm') || ''); if (!modalConfirmHtml) { doRequest(); return; } const okButtonColor = $this.hasClass('red') || $this.hasClass('yellow') || $this.hasClass('orange') || $this.hasClass('negative') ? 'orange' : 'green'; const $modal = $(`