2022-01-28 14:00:11 -07:00
|
|
|
import $ from 'jquery';
|
|
|
|
import 'jquery.are-you-sure';
|
2021-10-16 11:28:04 -06:00
|
|
|
import {mqBinarySearch} from '../utils.js';
|
2022-12-23 09:03:11 -07:00
|
|
|
import {createDropzone} from './dropzone.js';
|
2021-10-16 11:28:04 -06:00
|
|
|
import {initCompColorPicker} from './comp/ColorPicker.js';
|
Show messages for users if the ROOT_URL is wrong, show JavaScript errors (#18971)
* ROOT_URL issues: some users did wrong to there app.ini config, then:
* The assets can not be loaded (AppSubUrl != "" and users try to access http://host:3000/)
*The ROOT_URL is wrong, then many URLs in Gitea are broken.
Now Gitea show enough information to users.
* JavaScript error issues, there are many users affected by JavaScript errors, some are caused by frontend bugs, some are caused by broken customized templates. If these JS errors can be found at first time, then maintainers do not need to ask about how bug occurs again and again.
* Some people like to modify the `head.tmpl`, so we separate the script part to `head_script.tmpl`, then it's much safer.
* use specialized CSS class "js-global-error", end users still have a chance to hide error messages by customized CSS styles.
2022-03-29 23:52:24 -06:00
|
|
|
import {showGlobalErrorMessage} from '../bootstrap.js';
|
2023-01-25 08:52:10 -07:00
|
|
|
import {attachCheckboxAria, attachDropdownAria} from './aria.js';
|
2022-08-05 04:08:29 -06:00
|
|
|
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.js';
|
2022-08-09 06:37:34 -06:00
|
|
|
import {initTooltip} from '../modules/tippy.js';
|
2023-01-21 21:14:43 -07:00
|
|
|
import {svg} from '../svg.js';
|
2023-02-18 21:06:14 -07:00
|
|
|
import {hideElem, showElem, toggleElem} from '../utils/dom.js';
|
2021-10-16 11:28:04 -06:00
|
|
|
|
Show messages for users if the ROOT_URL is wrong, show JavaScript errors (#18971)
* ROOT_URL issues: some users did wrong to there app.ini config, then:
* The assets can not be loaded (AppSubUrl != "" and users try to access http://host:3000/)
*The ROOT_URL is wrong, then many URLs in Gitea are broken.
Now Gitea show enough information to users.
* JavaScript error issues, there are many users affected by JavaScript errors, some are caused by frontend bugs, some are caused by broken customized templates. If these JS errors can be found at first time, then maintainers do not need to ask about how bug occurs again and again.
* Some people like to modify the `head.tmpl`, so we separate the script part to `head_script.tmpl`, then it's much safer.
* use specialized CSS class "js-global-error", end users still have a chance to hide error messages by customized CSS styles.
2022-03-29 23:52:24 -06:00
|
|
|
const {appUrl, csrfToken} = window.config;
|
2021-10-16 11:28:04 -06:00
|
|
|
|
|
|
|
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);
|
2022-11-09 00:40:26 -07:00
|
|
|
$.get($this.data('url')).always(() => {
|
2021-10-16 11:28:04 -06:00
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.language-menu a[lang]').on('click', linkLanguageAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function initGlobalEnterQuickSubmit() {
|
2022-05-19 20:26:04 -06:00
|
|
|
$(document).on('keydown', '.js-quick-submit', (e) => {
|
|
|
|
if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.key === 'Enter')) {
|
|
|
|
handleGlobalEnterQuickSubmit(e.target);
|
|
|
|
return false;
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initGlobalButtonClickOnEnter() {
|
|
|
|
$(document).on('keypress', '.ui.button', (e) => {
|
|
|
|
if (e.keyCode === 13 || e.keyCode === 32) { // enter key or space bar
|
2023-02-21 06:36:53 -07:00
|
|
|
if (e.target.nodeName === 'BUTTON') return; // button already handles space&enter correctly
|
2021-10-16 11:28:04 -06:00
|
|
|
$(e.target).trigger('click');
|
2023-02-09 10:11:16 -07:00
|
|
|
e.preventDefault();
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-09 06:37:34 -06:00
|
|
|
export function initGlobalTooltips() {
|
|
|
|
for (const el of document.getElementsByClassName('tooltip')) {
|
|
|
|
initTooltip(el);
|
|
|
|
}
|
2022-07-05 06:28:31 -06:00
|
|
|
}
|
|
|
|
|
2021-10-16 11:28:04 -06:00
|
|
|
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.
|
2022-06-03 15:38:26 -06:00
|
|
|
const $uiDropdowns = $('.ui.dropdown');
|
|
|
|
$uiDropdowns.filter(':not(.custom)').dropdown({
|
2021-10-16 11:28:04 -06:00
|
|
|
fullTextSearch: 'exact'
|
|
|
|
});
|
2022-06-03 15:38:26 -06:00
|
|
|
$uiDropdowns.filter('.jump').dropdown({
|
2021-10-16 11:28:04 -06:00
|
|
|
action: 'hide',
|
|
|
|
onShow() {
|
2022-08-09 06:37:34 -06:00
|
|
|
// hide associated tooltip while dropdown is open
|
|
|
|
this._tippy?.hide();
|
|
|
|
this._tippy?.disable();
|
|
|
|
},
|
|
|
|
onHide() {
|
|
|
|
this._tippy?.enable();
|
2021-10-16 11:28:04 -06:00
|
|
|
},
|
|
|
|
fullTextSearch: 'exact'
|
|
|
|
});
|
2022-06-03 15:38:26 -06:00
|
|
|
$uiDropdowns.filter('.slide.up').dropdown({
|
2021-10-16 11:28:04 -06:00
|
|
|
transition: 'slide up',
|
|
|
|
fullTextSearch: 'exact'
|
|
|
|
});
|
2022-06-03 15:38:26 -06:00
|
|
|
$uiDropdowns.filter('.upward').dropdown({
|
2021-10-16 11:28:04 -06:00
|
|
|
direction: 'upward',
|
|
|
|
fullTextSearch: 'exact'
|
|
|
|
});
|
2022-06-03 15:38:26 -06:00
|
|
|
attachDropdownAria($uiDropdowns);
|
|
|
|
|
2023-01-25 08:52:10 -07:00
|
|
|
attachCheckboxAria($('.ui.checkbox'));
|
2021-11-17 20:26:50 -07:00
|
|
|
|
2021-10-16 11:28:04 -06:00
|
|
|
$('.tabular.menu .item').tab();
|
|
|
|
$('.tabable.menu .item').tab();
|
|
|
|
|
|
|
|
$('.toggle.button').on('click', function () {
|
2023-02-18 21:06:14 -07:00
|
|
|
toggleElem($($(this).data('target')));
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
|
2021-10-21 12:19:32 -06:00
|
|
|
// make table <tr> and <td> elements clickable like a link
|
|
|
|
$('tr[data-href], td[data-href]').on('click', function (e) {
|
|
|
|
const href = $(this).data('href');
|
|
|
|
if (e.target.nodeName === 'A') {
|
|
|
|
// if a user clicks on <a>, then the <tr> or <td> should not act as a link.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.ctrlKey || e.metaKey) {
|
|
|
|
// ctrl+click or meta+click opens a new window in modern browsers
|
|
|
|
window.open(href);
|
|
|
|
} else {
|
|
|
|
window.location = href;
|
|
|
|
}
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
2022-05-07 06:24:02 -06:00
|
|
|
|
2022-08-10 21:54:34 -06:00
|
|
|
// prevent multiple form submissions on forms containing .loading-button
|
|
|
|
document.addEventListener('submit', (e) => {
|
|
|
|
const btn = e.target.querySelector('.loading-button');
|
|
|
|
if (!btn) return;
|
|
|
|
if (btn.classList.contains('loading')) return e.preventDefault();
|
|
|
|
btn.classList.add('loading');
|
2022-05-07 06:24:02 -06:00
|
|
|
});
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
export function initGlobalDropzone() {
|
2021-10-16 11:28:04 -06:00
|
|
|
// Dropzone
|
|
|
|
for (const el of document.querySelectorAll('.dropzone')) {
|
|
|
|
const $dropzone = $(el);
|
2021-11-09 02:27:25 -07:00
|
|
|
const _promise = createDropzone(el, {
|
2021-10-16 11:28:04 -06:00
|
|
|
url: $dropzone.data('upload-url'),
|
2021-10-21 01:37:43 -06:00
|
|
|
headers: {'X-Csrf-Token': csrfToken},
|
2021-10-16 11:28:04 -06:00
|
|
|
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() {
|
2022-06-25 13:49:56 -06:00
|
|
|
this.on('success', (file, data) => {
|
|
|
|
file.uuid = data.uuid;
|
2021-10-16 11:28:04 -06:00
|
|
|
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
|
|
|
|
$dropzone.find('.files').append(input);
|
2023-01-18 22:33:40 -07:00
|
|
|
// Create a "Copy Link" element, to conveniently copy the image
|
|
|
|
// or file link as Markdown to the clipboard
|
2023-01-21 21:14:43 -07:00
|
|
|
const copyLinkElement = document.createElement('div');
|
2023-02-13 10:59:59 -07:00
|
|
|
copyLinkElement.className = 'gt-tc';
|
2023-01-21 21:14:43 -07:00
|
|
|
// The a element has a hardcoded cursor: pointer because the default is overridden by .dropzone
|
|
|
|
copyLinkElement.innerHTML = `<a href="#" style="cursor: pointer;">${svg('octicon-copy', 14, 'copy link')} Copy link</a>`;
|
2023-01-18 22:33:40 -07:00
|
|
|
copyLinkElement.addEventListener('click', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
let fileMarkdown = `[${file.name}](/attachments/${file.uuid})`;
|
|
|
|
if (file.type.startsWith('image/')) {
|
|
|
|
fileMarkdown = `!${fileMarkdown}`;
|
|
|
|
}
|
|
|
|
navigator.clipboard.writeText(fileMarkdown);
|
|
|
|
});
|
|
|
|
file.previewTemplate.appendChild(copyLinkElement);
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
this.on('removedfile', (file) => {
|
|
|
|
$(`#${file.uuid}`).remove();
|
|
|
|
if ($dropzone.data('remove-url')) {
|
|
|
|
$.post($dropzone.data('remove-url'), {
|
|
|
|
file: file.uuid,
|
2021-10-21 01:37:43 -06:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initGlobalLinkActions() {
|
2023-03-13 21:34:09 -06:00
|
|
|
function showDeletePopup(e) {
|
|
|
|
e.preventDefault();
|
2021-10-16 11:28:04 -06:00
|
|
|
const $this = $(this);
|
|
|
|
const dataArray = $this.data();
|
|
|
|
let filter = '';
|
|
|
|
if ($this.data('modal-id')) {
|
|
|
|
filter += `#${$this.data('modal-id')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dialog = $(`.delete.modal${filter}`);
|
|
|
|
dialog.find('.name').text($this.data('name'));
|
|
|
|
for (const [key, value] of Object.entries(dataArray)) {
|
|
|
|
if (key && key.startsWith('data')) {
|
|
|
|
dialog.find(`.${key}`).text(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog.modal({
|
|
|
|
closable: false,
|
|
|
|
onApprove() {
|
|
|
|
if ($this.data('type') === 'form') {
|
2022-01-16 04:19:26 -07:00
|
|
|
$($this.data('form')).trigger('submit');
|
2021-10-16 11:28:04 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const postData = {
|
2021-10-21 01:37:43 -06:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 11:28:04 -06:00
|
|
|
};
|
|
|
|
for (const [key, value] of Object.entries(dataArray)) {
|
|
|
|
if (key && key.startsWith('data')) {
|
2022-02-17 23:50:36 -07:00
|
|
|
postData[key.slice(4)] = value;
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
if (key === 'id') {
|
|
|
|
postData['id'] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$.post($this.data('url'), postData).done((data) => {
|
|
|
|
window.location.href = data.redirect;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).modal('show');
|
|
|
|
}
|
|
|
|
|
2023-03-13 21:34:09 -06:00
|
|
|
function showAddAllPopup(e) {
|
|
|
|
e.preventDefault();
|
2021-10-16 11:28:04 -06:00
|
|
|
const $this = $(this);
|
|
|
|
let filter = '';
|
|
|
|
if ($this.attr('id')) {
|
|
|
|
filter += `#${$this.attr('id')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const dialog = $(`.addall.modal${filter}`);
|
|
|
|
dialog.find('.name').text($this.data('name'));
|
|
|
|
|
|
|
|
dialog.modal({
|
|
|
|
closable: false,
|
|
|
|
onApprove() {
|
|
|
|
if ($this.data('type') === 'form') {
|
2022-01-16 04:19:26 -07:00
|
|
|
$($this.data('form')).trigger('submit');
|
2021-10-16 11:28:04 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.post($this.data('url'), {
|
2021-10-21 01:37:43 -06:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 11:28:04 -06:00
|
|
|
id: $this.data('id')
|
|
|
|
}).done((data) => {
|
|
|
|
window.location.href = data.redirect;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).modal('show');
|
|
|
|
}
|
|
|
|
|
|
|
|
function linkAction(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const $this = $(this);
|
|
|
|
const redirect = $this.data('redirect');
|
2022-11-25 13:58:20 -07:00
|
|
|
$this.prop('disabled', true);
|
2021-10-16 11:28:04 -06:00
|
|
|
$.post($this.data('url'), {
|
2021-10-21 01:37:43 -06:00
|
|
|
_csrf: csrfToken
|
2021-10-16 11:28:04 -06:00
|
|
|
}).done((data) => {
|
|
|
|
if (data.redirect) {
|
|
|
|
window.location.href = data.redirect;
|
|
|
|
} else if (redirect) {
|
|
|
|
window.location.href = redirect;
|
|
|
|
} else {
|
|
|
|
window.location.reload();
|
|
|
|
}
|
2022-11-25 13:58:20 -07:00
|
|
|
}).always(() => {
|
|
|
|
$this.prop('disabled', false);
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers.
|
|
|
|
$('.delete-button').on('click', showDeletePopup);
|
|
|
|
$('.link-action').on('click', linkAction);
|
|
|
|
|
|
|
|
// FIXME: this function is only used once, and not common, not well designed. should be refactored later
|
|
|
|
$('.add-all-button').on('click', showAddAllPopup);
|
|
|
|
|
|
|
|
// FIXME: this is only used once, and should be replace with `link-action` instead
|
|
|
|
$('.undo-button').on('click', function () {
|
|
|
|
const $this = $(this);
|
2022-11-25 13:58:20 -07:00
|
|
|
$this.prop('disabled', true);
|
2021-10-16 11:28:04 -06:00
|
|
|
$.post($this.data('url'), {
|
2021-10-21 01:37:43 -06:00
|
|
|
_csrf: csrfToken,
|
2021-10-16 11:28:04 -06:00
|
|
|
id: $this.data('id')
|
|
|
|
}).done((data) => {
|
|
|
|
window.location.href = data.redirect;
|
2022-11-25 13:58:20 -07:00
|
|
|
}).always(() => {
|
|
|
|
$this.prop('disabled', false);
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initGlobalButtons() {
|
2023-03-13 21:34:09 -06:00
|
|
|
// There are many "cancel button" elements in modal dialogs, Fomantic UI expects they are button-like elements but never submit a form.
|
|
|
|
// However, Gitea misuses the modal dialog and put the cancel buttons inside forms, so we must prevent the form submission.
|
|
|
|
// There are a few cancel buttons in non-modal forms, and there are some dynamically created forms (eg: the "Edit Issue Content")
|
|
|
|
$(document).on('click', 'form .ui.cancel.button', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.show-panel.button').on('click', function (e) {
|
|
|
|
e.preventDefault();
|
2023-02-18 21:06:14 -07:00
|
|
|
showElem($(this).data('panel'));
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
|
2023-03-13 21:34:09 -06:00
|
|
|
$('.hide-panel.button').on('click', function (e) {
|
2022-01-06 18:18:52 -07:00
|
|
|
// a `.hide-panel.button` can hide a panel, by `data-panel="selector"` or `data-panel-closest="selector"`
|
2023-03-13 21:34:09 -06:00
|
|
|
e.preventDefault();
|
2022-01-06 18:18:52 -07:00
|
|
|
let sel = $(this).attr('data-panel');
|
|
|
|
if (sel) {
|
2023-02-18 21:06:14 -07:00
|
|
|
hideElem($(sel));
|
2022-01-06 18:18:52 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sel = $(this).attr('data-panel-closest');
|
|
|
|
if (sel) {
|
2023-02-18 21:06:14 -07:00
|
|
|
hideElem($(this).closest(sel));
|
2022-01-06 18:18:52 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// should never happen, otherwise there is a bug in code
|
|
|
|
alert('Nothing to hide');
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
|
2023-03-13 21:34:09 -06:00
|
|
|
$('.show-modal').on('click', function (e) {
|
|
|
|
e.preventDefault();
|
2022-02-09 13:28:55 -07:00
|
|
|
const modalDiv = $($(this).attr('data-modal'));
|
|
|
|
for (const attrib of this.attributes) {
|
|
|
|
if (!attrib.name.startsWith('data-modal-')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const id = attrib.name.substring(11);
|
|
|
|
const target = modalDiv.find(`#${id}`);
|
|
|
|
if (target.is('input')) {
|
|
|
|
target.val(attrib.value);
|
|
|
|
} else {
|
|
|
|
target.text(attrib.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
modalDiv.modal('show');
|
|
|
|
const colorPickers = $($(this).attr('data-modal')).find('.color-picker');
|
2021-10-16 11:28:04 -06:00
|
|
|
if (colorPickers.length > 0) {
|
|
|
|
initCompColorPicker();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-03-13 21:34:09 -06:00
|
|
|
$('.delete-post.button').on('click', function (e) {
|
|
|
|
e.preventDefault();
|
2021-10-16 11:28:04 -06:00
|
|
|
const $this = $(this);
|
2022-02-09 13:28:55 -07:00
|
|
|
$.post($this.attr('data-request-url'), {
|
2021-10-21 01:37:43 -06:00
|
|
|
_csrf: csrfToken
|
2021-10-16 11:28:04 -06:00
|
|
|
}).done(() => {
|
2022-02-09 13:28:55 -07:00
|
|
|
window.location.href = $this.attr('data-done-url');
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
Show messages for users if the ROOT_URL is wrong, show JavaScript errors (#18971)
* ROOT_URL issues: some users did wrong to there app.ini config, then:
* The assets can not be loaded (AppSubUrl != "" and users try to access http://host:3000/)
*The ROOT_URL is wrong, then many URLs in Gitea are broken.
Now Gitea show enough information to users.
* JavaScript error issues, there are many users affected by JavaScript errors, some are caused by frontend bugs, some are caused by broken customized templates. If these JS errors can be found at first time, then maintainers do not need to ask about how bug occurs again and again.
* Some people like to modify the `head.tmpl`, so we separate the script part to `head_script.tmpl`, then it's much safer.
* use specialized CSS class "js-global-error", end users still have a chance to hide error messages by customized CSS styles.
2022-03-29 23:52:24 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Too many users set their ROOT_URL to wrong value, and it causes a lot of problems:
|
|
|
|
* * Cross-origin API request without correct cookie
|
|
|
|
* * Incorrect href in <a>
|
|
|
|
* * ...
|
|
|
|
* So we check whether current URL starts with AppUrl(ROOT_URL).
|
|
|
|
* If they don't match, show a warning to users.
|
|
|
|
*/
|
|
|
|
export function checkAppUrl() {
|
|
|
|
const curUrl = window.location.href;
|
2022-07-27 03:19:10 -06:00
|
|
|
// some users visit "https://domain/gitea" while appUrl is "https://domain/gitea/", there should be no warning
|
|
|
|
if (curUrl.startsWith(appUrl) || `${curUrl}/` === appUrl) {
|
Show messages for users if the ROOT_URL is wrong, show JavaScript errors (#18971)
* ROOT_URL issues: some users did wrong to there app.ini config, then:
* The assets can not be loaded (AppSubUrl != "" and users try to access http://host:3000/)
*The ROOT_URL is wrong, then many URLs in Gitea are broken.
Now Gitea show enough information to users.
* JavaScript error issues, there are many users affected by JavaScript errors, some are caused by frontend bugs, some are caused by broken customized templates. If these JS errors can be found at first time, then maintainers do not need to ask about how bug occurs again and again.
* Some people like to modify the `head.tmpl`, so we separate the script part to `head_script.tmpl`, then it's much safer.
* use specialized CSS class "js-global-error", end users still have a chance to hide error messages by customized CSS styles.
2022-03-29 23:52:24 -06:00
|
|
|
return;
|
|
|
|
}
|
2023-02-09 09:14:45 -07:00
|
|
|
showGlobalErrorMessage(`Your ROOT_URL in app.ini is "${appUrl}", it's unlikely matching the site you are visiting.
|
|
|
|
Mismatched ROOT_URL config causes wrong URL links for web UI/mail content/webhook notification.`);
|
Show messages for users if the ROOT_URL is wrong, show JavaScript errors (#18971)
* ROOT_URL issues: some users did wrong to there app.ini config, then:
* The assets can not be loaded (AppSubUrl != "" and users try to access http://host:3000/)
*The ROOT_URL is wrong, then many URLs in Gitea are broken.
Now Gitea show enough information to users.
* JavaScript error issues, there are many users affected by JavaScript errors, some are caused by frontend bugs, some are caused by broken customized templates. If these JS errors can be found at first time, then maintainers do not need to ask about how bug occurs again and again.
* Some people like to modify the `head.tmpl`, so we separate the script part to `head_script.tmpl`, then it's much safer.
* use specialized CSS class "js-global-error", end users still have a chance to hide error messages by customized CSS styles.
2022-03-29 23:52:24 -06:00
|
|
|
}
|