aboutsummaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2019-01-30 21:15:17 +0100
committervincent-peugnet <v.peugnet@free.fr>2019-01-30 23:16:26 +0100
commit8ac5a09605992f1e434cb793ceca7497c6f46d23 (patch)
tree091f9879f8f94b849300fef2d1a1238f3881f78e /assets
parent396a5776cd043e899e4897ec50fc8edc52585512 (diff)
downloadwcms-8ac5a09605992f1e434cb793ceca7497c6f46d23.tar.gz
wcms-8ac5a09605992f1e434cb793ceca7497c6f46d23.zip
feat: editBy to store the current editor of an art
Diffstat (limited to 'assets')
-rw-r--r--assets/js/edit.js41
-rw-r--r--assets/js/worker.js37
2 files changed, 74 insertions, 4 deletions
diff --git a/assets/js/edit.js b/assets/js/edit.js
index d838017..ab7471f 100644
--- a/assets/js/edit.js
+++ b/assets/js/edit.js
@@ -1,5 +1,7 @@
let form;
let unsavedChanges = false;
+const arturl = basepath + artid;
+const myWorker = new Worker(jspath + 'worker.js');
window.onload = () => {
form = document.getElementById('update');
@@ -12,6 +14,19 @@ window.onload = () => {
form.onsubmit = submitHandler;
window.onkeydown = keyboardHandler;
window.onbeforeunload = confirmExit;
+ myWorker.onmessage = function (e) {
+ switch (e.data.type) {
+ case 'quitEditing':
+ myWorker.postMessage({ type: 'stillEditing' })
+ break;
+ }
+ }
+
+ myWorker.postMessage({
+ type: 'init',
+ arturl: arturl,
+ });
+ myWorker.postMessage({ type: 'stillEditing' });
};
/**
@@ -39,7 +54,6 @@ function keyboardHandler(e) {
*/
function changeHandler(e) {
unsavedChanges = true;
- // console.log({unsavedChanges});
}
/**
@@ -55,8 +69,27 @@ function submitHandler(e) {
* @param {BeforeUnloadEvent} e
*/
function confirmExit(e) {
- // console.log({unsavedChanges});
if (unsavedChanges) {
- return "You have unsaved changes, do you really want to leave this page?";
+ const url = arturl + '/removeeditby';
+ console.log('send quit editing')
+ fetch(url, { method: 'POST' })
+ .then(handleErrors)
+ .then((response) => {
+ console.log(response);
+ setTimeout(() => {
+ myWorker.postMessage({ type: 'stillEditing' });
+ }, 1500);
+ });
+ return 'You have unsaved changes, do you really want to leave this page?';
+ } else {
+ myWorker.postMessage({ type: 'quitEditing' });
}
-} \ No newline at end of file
+}
+
+async function handleErrors(response) {
+ if (!response.ok) {
+ const data = await response.json();
+ throw Error(`${response.statusText}. ${data.message}`);
+ }
+ return response.json();
+}
diff --git a/assets/js/worker.js b/assets/js/worker.js
new file mode 100644
index 0000000..846acba
--- /dev/null
+++ b/assets/js/worker.js
@@ -0,0 +1,37 @@
+let arturl;
+
+onmessage = function (e) {
+ switch (e.data.type) {
+ case 'init':
+ arturl = e.data.arturl;
+ break;
+ case 'stillEditing':
+ stillEditing();
+ break;
+ case 'quitEditing':
+ quitEditing();
+ break;
+ };
+}
+
+function stillEditing() {
+ console.log('send still editing');
+ const url = arturl + '/editby';
+ const req = new XMLHttpRequest();
+ req.open('POST', url, false);
+ req.send(null);
+
+ const res = JSON.parse(req.responseText);
+ console.log(res);
+}
+
+function quitEditing() {
+ console.log('send quit editing');
+ const url = arturl + '/removeeditby';
+ const req = new XMLHttpRequest();
+ req.open('POST', url, false);
+ req.send(null);
+
+ const res = JSON.parse(req.responseText);
+ console.log(res);
+} \ No newline at end of file