Skip to content

Commit

Permalink
save home interface using JS close #203
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-peugnet committed Dec 27, 2023
1 parent f46960d commit 73949da
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 64 deletions.
7 changes: 6 additions & 1 deletion app/view/templates/homemenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,12 @@

<span id="save-workspace">

<form action="<?= $this->url('workspaceupdate') ?>" method="post" id="workspace-form">
<form
action="<?= $this->url('workspaceupdate') ?>"
method="post"
data-api="<?= $this->url('apiworkspaceupdate') ?>"
id="workspace-form"
>
<input type="hidden" name="showeoptionspanel" value="0">
<button type="submit">
<i class="fa fa-edit"></i>
Expand Down
4 changes: 0 additions & 4 deletions assets/css/home.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ aside #edit input[type="submit"] {
width: 100%;
}

section.bookmarks {
min-width: 2em;
}

.bookmarks .block {
height: 100%;
}
Expand Down
60 changes: 1 addition & 59 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import icon from 'leaflet/dist/images/marker-icon.png';
import icon_2x from 'leaflet/dist/images/marker-icon-2x.png';
import shadow from 'leaflet/dist/images/marker-shadow.png';
import * as L from 'leaflet';
import { initWorkspaceForm, submitHandler } from './fn/fn';

/* ________________ LEAFLET ________________ */

Expand Down Expand Up @@ -245,25 +246,6 @@ function initForm() {
});
}

function initWorkspaceForm() {
let form = document.getElementById('workspace-form');
let inputs = form.elements;
for (const input of inputs) {
input.oninput = workspaceChanged;
}
let submits = form.querySelectorAll('[type="submit"]');
for (const submit of submits) {
if (submit instanceof HTMLElement) {
submit.style.display = 'none';
}
}

form.addEventListener('submit', function(event) {
event.preventDefault();
submitHandler(this, noop);
});
}

function initEditors(theme) {
// disable CodeMirror's default ctrl+D shortcut (delete line)
delete CodeMirror.keyMap['default']['Ctrl-D'];
Expand Down Expand Up @@ -474,28 +456,6 @@ function themeChangeHandler(e) {
theme = e.target.value;
}

/**
* Manage submit event
* @param {HTMLFormElement} form
*/
function submitHandler(form, onSuccess) {
var xhr = new XMLHttpRequest();
var fd = new FormData(form);

xhr.addEventListener('load', function(event) {
if (httpOk(xhr.status)) {
onSuccess(xhr.response);
} else {
alert('Error while trying to update: ' + xhr.statusText);
}
});
xhr.addEventListener('error', function(event) {
alert('Network error while trying to update.');
});
xhr.open(form.method, form.dataset.api);
xhr.send(fd);
}

/**
* Manage a beforeUnloadEvent
* @param {BeforeUnloadEvent} e
Expand All @@ -522,21 +482,3 @@ function saved(data) {
function onSuccess(response) {
saved(JSON.parse(response));
}

/**
* @param {InputEvent} e
*/
function workspaceChanged(e) {
let elem = e.target;
elem.form.requestSubmit();
}

/**
* Check if an HTTP response status indicates a success.
* @param {number} status
*/
function httpOk(status) {
return status >= 200 && status < 300;
}

function noop() {}
58 changes: 58 additions & 0 deletions src/fn/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,61 @@ export function activateSelectAll() {
selectAllInput.addEventListener('click', selectAll);
}
}

/**
* Manage submit event for a given form
* @param {HTMLFormElement} form
* @param {(response: any) => void} onSuccess
*/
export function submitHandler(form, onSuccess = () => {}) {
var xhr = new XMLHttpRequest();
var fd = new FormData(form);

xhr.addEventListener('load', function(event) {
if (httpOk(xhr.status)) {
onSuccess(xhr.response);
} else {
alert('Error while trying to update: ' + xhr.statusText);
}
});
xhr.addEventListener('error', function(event) {
alert('Network error while trying to update.');
});
xhr.open(form.method, form.dataset.api);
xhr.send(fd);
}

/**
* Check if an HTTP response status indicates a success.
* @param {number} status
*/
function httpOk(status) {
return status >= 200 && status < 300;
}

export function initWorkspaceForm() {
let form = document.getElementById('workspace-form');
let inputs = form.elements;
for (const input of inputs) {
input.oninput = workspaceChanged;
}
let submits = form.querySelectorAll('[type="submit"]');
for (const submit of submits) {
if (submit instanceof HTMLElement) {
submit.style.display = 'none';
}
}

form.addEventListener('submit', function(event) {
event.preventDefault();
submitHandler(this);
});
}

/**
* @param {InputEvent} e
*/
function workspaceChanged(e) {
let elem = e.target;
elem.form.requestSubmit();
}
2 changes: 2 additions & 0 deletions src/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
activateCheckall,
activateCloseSubmenus,
activateSelectAll,
initWorkspaceForm,
} from './fn/fn';

window.addEventListener('load', () => {
activateCheckall('pagesid[]', 'checkall');
activateSelectAll();
activateCloseSubmenus();
initWorkspaceForm();
});

0 comments on commit 73949da

Please sign in to comment.