Skip to content
Snippets Groups Projects
Commit 70cd9806 authored by Maximilian Giller's avatar Maximilian Giller
Browse files

Added pop up on tab close with unsaved changes

parent ee911b88
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ export function initInteractions() { ...@@ -12,6 +12,7 @@ export function initInteractions() {
}); });
jQuery("button#save").on("click", () => { jQuery("button#save").on("click", () => {
saveGraphJson(SPACE, graph.getCleanData()); // space_id defined globaly through knowledge-space.php saveGraphJson(SPACE, graph.getCleanData()); // space_id defined globaly through knowledge-space.php
graph.saveChanges();
}); });
// Fill space dropdown // Fill space dropdown
......
export default class ManagedData { export default class ManagedData {
constructor(data) { constructor(data) {
this.data = data; this.data = data;
this.history = []; // Newest state is always at 0 this.history = []; // Newest state is always at 0
this.historyPosition = 0; this.historyPosition = 0;
this.unsavedChanges = false;
this.storeCurrentData("Initial state"); this.storeCurrentData("Initial state", false);
}
updateUnsavedChangesHandler() {
if (this.hasUnsavedChanges()) {
console.log("Unsaved changes...");
window.addEventListener("beforeunload", this.handleBeforeUnload);
} else {
console.log("Everything saved...");
window.removeEventListener("beforeunload", this.handleBeforeUnload);
}
}
handleBeforeUnload(e) {
var confirmationMessage = 'If you leave before saving, unsaved changes will be lost.';
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Gecko + Webkit, Safari, Chrome etc.
}
hasUnsavedChanges() {
return this.unsavedChanges;
}
saveChanges() {
this.unsavedChanges = false;
this.updateUnsavedChangesHandler();
} }
onUndo() {} onUndo() {}
...@@ -31,7 +58,10 @@ export default class ManagedData { ...@@ -31,7 +58,10 @@ export default class ManagedData {
step(direction = 1) { step(direction = 1) {
var newHistoryPosition = this.historyPosition + Math.sign(direction); var newHistoryPosition = this.historyPosition + Math.sign(direction);
if (newHistoryPosition >= this.history.length || newHistoryPosition < 0) { if (
newHistoryPosition >= this.history.length ||
newHistoryPosition < 0
) {
return false; return false;
} }
...@@ -45,7 +75,7 @@ export default class ManagedData { ...@@ -45,7 +75,7 @@ export default class ManagedData {
return data; return data;
} }
storeCurrentData(description) { storeCurrentData(description, relevantChanges = true) {
var formattedData = this.storableData(this.data); var formattedData = this.storableData(this.data);
this.history.unshift({ this.history.unshift({
...@@ -56,5 +86,10 @@ export default class ManagedData { ...@@ -56,5 +86,10 @@ export default class ManagedData {
// Forget about the currently stored potential future // Forget about the currently stored potential future
this.history.splice(0, this.historyPosition); this.history.splice(0, this.historyPosition);
this.historyPosition = 0; this.historyPosition = 0;
if (!this.hasUnsavedChanges() && relevantChanges) {
this.unsavedChanges = true;
this.updateUnsavedChangesHandler();
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment