[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 = [
"data-userclass" => $user->class->name,
"data-base-href" => get_base_href(),
"data-base-link" => make_link(""),
];
print emptyHTML(

View file

@ -43,7 +43,7 @@ function updateCompletions(element) {
else {
element.completer_timeout = setTimeout(() => {
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()
).then((json) => {
if(element.selected_completion !== -1) {

View file

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

View file

@ -4,6 +4,13 @@ function shm_cookie_set(name, value) {
function shm_cookie_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) {
window.dispatchEvent(new CustomEvent("shm_log", {detail: {section, message}}));