diff --git a/editor/js/interactions.js b/editor/js/interactions.js index 8d57cf2947305dde986f13aae79bc5306527c7e2..2c34de8152189f8640282ca6ff1b087430022e32 100644 --- a/editor/js/interactions.js +++ b/editor/js/interactions.js @@ -12,6 +12,7 @@ export function initInteractions() { }); jQuery("button#save").on("click", () => { saveGraphJson(SPACE, graph.getCleanData()); // space_id defined globaly through knowledge-space.php + graph.saveChanges(); }); // Fill space dropdown diff --git a/editor/js/manageddata.js b/editor/js/manageddata.js index e2f83bddb67a6e8dda15bb428a3fc56751ba3969..4f9058a35aca542c48479b05cf8dbdb1e75193ae 100644 --- a/editor/js/manageddata.js +++ b/editor/js/manageddata.js @@ -1,10 +1,37 @@ export default class ManagedData { constructor(data) { this.data = data; - this.history = []; // Newest state is always at 0 + this.history = []; // Newest state is always at 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() {} @@ -31,7 +58,10 @@ export default class ManagedData { step(direction = 1) { var newHistoryPosition = this.historyPosition + Math.sign(direction); - if (newHistoryPosition >= this.history.length || newHistoryPosition < 0) { + if ( + newHistoryPosition >= this.history.length || + newHistoryPosition < 0 + ) { return false; } @@ -45,7 +75,7 @@ export default class ManagedData { return data; } - storeCurrentData(description) { + storeCurrentData(description, relevantChanges = true) { var formattedData = this.storableData(this.data); this.history.unshift({ @@ -56,5 +86,10 @@ export default class ManagedData { // Forget about the currently stored potential future this.history.splice(0, this.historyPosition); this.historyPosition = 0; + + if (!this.hasUnsavedChanges() && relevantChanges) { + this.unsavedChanges = true; + this.updateUnsavedChangesHandler(); + } } }