[core] add shm_make_link JS function, to generate links from JS which work both with and without niceurls, fixes #1160, fixes #1151

This commit is contained in:
Shish 2024-06-05 13:58:59 +01:00 committed by Shish
parent b6088f95c3
commit db148da479
4 changed files with 12 additions and 4 deletions

View file

@ -557,6 +557,7 @@ class BasePage
$body_attrs = [ $body_attrs = [
"data-userclass" => $user->class->name, "data-userclass" => $user->class->name,
"data-base-href" => get_base_href(), "data-base-href" => get_base_href(),
"data-base-link" => make_link(""),
]; ];
print emptyHTML( print emptyHTML(

View file

@ -43,7 +43,7 @@ function updateCompletions(element) {
else { else {
element.completer_timeout = setTimeout(() => { element.completer_timeout = setTimeout(() => {
const wordWithoutMinus = word.replace(/^-/, ''); const wordWithoutMinus = word.replace(/^-/, '');
fetch((document.body.getAttribute("data-base-href") ?? "") + '/api/internal/autocomplete?s=' + wordWithoutMinus).then( fetch(shm_make_link('api/internal/autocomplete', {s: wordWithoutMinus})).then(
(response) => response.json() (response) => response.json()
).then((json) => { ).then((json) => {
if(element.selected_completion !== -1) { if(element.selected_completion !== -1) {

View file

@ -150,7 +150,7 @@ function renderEditor(noteDiv, note) {
save.innerText = 'Save'; save.innerText = 'Save';
save.addEventListener('click', () => { save.addEventListener('click', () => {
if(note.note_id == null) { if(note.note_id == null) {
fetch('/note/create_note', { fetch(shm_make_link('note/create_note'), {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -169,7 +169,7 @@ function renderEditor(noteDiv, note) {
alert(error); alert(error);
}); });
} else { } else {
fetch('/note/update_note', { fetch(shm_make_link('note/update_note'), {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -205,7 +205,7 @@ function renderEditor(noteDiv, note) {
deleteNote.innerText = 'Delete'; deleteNote.innerText = 'Delete';
deleteNote.addEventListener('click', () => { deleteNote.addEventListener('click', () => {
// TODO: delete note from server // TODO: delete note from server
fetch('/note/delete_note', { fetch(shm_make_link('note/delete_note'), {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'

View file

@ -4,6 +4,13 @@ function shm_cookie_set(name, value) {
function shm_cookie_get(name) { function shm_cookie_get(name) {
return Cookies.get(name); return Cookies.get(name);
} }
function shm_make_link(page, query) {
let base = (document.body.getAttribute("data-base-link") ?? "");
let joiner = base.indexOf("?") === -1 ? "?" : "&";
let url = base + page;
if(query) url += joiner + new URLSearchParams(query).toString();
return url;
}
function shm_log(section, ...message) { function shm_log(section, ...message) {
window.dispatchEvent(new CustomEvent("shm_log", {detail: {section, message}})); window.dispatchEvent(new CustomEvent("shm_log", {detail: {section, message}}));