From e2c996c95a03a276997a340c0aab9e06ada441e1 Mon Sep 17 00:00:00 2001 From: kghs-aver Date: Fri, 31 Jan 2025 21:51:20 +0530 Subject: [PATCH 1/2] popup only when there is unsaved changes --- ArduinoFrontend/src/app/Libs/Workspace.ts | 13 ++-- .../src/app/simulator/simulator.component.ts | 60 +++++++++++-------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/ArduinoFrontend/src/app/Libs/Workspace.ts b/ArduinoFrontend/src/app/Libs/Workspace.ts index 34870b34..abe48594 100644 --- a/ArduinoFrontend/src/app/Libs/Workspace.ts +++ b/ArduinoFrontend/src/app/Libs/Workspace.ts @@ -57,6 +57,7 @@ export class Workspace { * If simulation is on progress or not */ static simulating = false; + static hasUnsavedChanges = true; /** * If circuit is loaded or not */ @@ -167,6 +168,7 @@ export class Workspace { for (const fn of window.DragListeners) { fn.fn(element); } + Workspace.hasUnsavedChanges = true; } /** @@ -266,8 +268,8 @@ export class Workspace { * @param event Before Unload Event */ static BeforeUnload(event) { - event.preventDefault(); - event.returnValue = 'did you save the stuff?'; + event.preventDefault(); + event.returnValue = 'did you save the stuff?'; } /** * Event Listener for mousemove on html body @@ -577,6 +579,7 @@ export class Workspace { window['scope'][classString].push(obj); // Push dump to Undo stack & Reset UndoUtils.pushChangeToUndoAndReset({ keyName: obj.keyName, event: 'add', element: obj.save() }); + Workspace.hasUnsavedChanges = true; } /** Function updates the position of wires */ static updateWires() { @@ -662,7 +665,6 @@ export class Workspace { SaveOffline.Save(saveObj, callback); } }); - } /** @@ -678,7 +680,6 @@ export class Workspace { Workspace.translateX = data.canvas.x; Workspace.translateY = data.canvas.y; Workspace.scale = data.canvas.scale; - // Update the translation and scaling window.queue = 0; const ele = (window['canvas'].canvas as HTMLElement); @@ -731,6 +732,8 @@ export class Workspace { window.hideLoading(); } }, 100); + //console.log('Unsaved Changes:', Workspace.hasUnsavedChanges); + Workspace.hasUnsavedChanges = false; UndoUtils.resetStacks(); } /** This function recreates the wire object */ @@ -884,6 +887,7 @@ export class Workspace { } else { window['showToast']('No Element Selected'); } + Workspace.hasUnsavedChanges = true; } /** Function to copy component fro Workspace */ @@ -919,6 +923,7 @@ export class Workspace { const obj = new myClass(window['canvas'], pt.x, pt.y); window['scope'][key].push(obj); // obj.copy(Workspace.copiedItem) + Workspace.hasUnsavedChanges = true; } } diff --git a/ArduinoFrontend/src/app/simulator/simulator.component.ts b/ArduinoFrontend/src/app/simulator/simulator.component.ts index 04c556c3..8182e002 100644 --- a/ArduinoFrontend/src/app/simulator/simulator.component.ts +++ b/ArduinoFrontend/src/app/simulator/simulator.component.ts @@ -606,6 +606,7 @@ export class SimulatorComponent implements OnInit, OnDestroy { // Update Project to DB SaveOnline.Save(this.projectTitle, this.description, this.api, branch, newVersionId, (out) => { AlertService.showAlert('Updated'); + Workspace.hasUnsavedChanges = false; // Set flag to false once saved if (out['duplicate']) { // TODO: if duplicate, refresh the route with same versionId and same branch this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => { @@ -674,6 +675,7 @@ export class SimulatorComponent implements OnInit, OnDestroy { } ); }); + Workspace.hasUnsavedChanges = false; } } /** Function saves or updates the project offline */ @@ -686,6 +688,7 @@ export class SimulatorComponent implements OnInit, OnDestroy { // Save circuit if id is not presenr if (this.projectId) { Workspace.SaveCircuit(this.projectTitle, this.description, callback, this.projectId); + Workspace.hasUnsavedChanges = false; } else { // save circuit and add query parameters Workspace.SaveCircuit(this.projectTitle, this.description, (v) => { @@ -706,6 +709,7 @@ export class SimulatorComponent implements OnInit, OnDestroy { } }); } + Workspace.hasUnsavedChanges = false; // Set flag to false once saved } /** @@ -816,35 +820,39 @@ export class SimulatorComponent implements OnInit, OnDestroy { * Handles routeLinks */ HandleRouter(callback) { - AlertService.showOptions( - 'Save changes to the untitled circuit? Your changes will be lost if you do not save it.', - () => { - AlertService.showCustom( - SaveProjectDialogComponent, - { - onChangeProjectTitle: (e) => { - this.projectTitle = e.target.value || ''; - return this.projectTitle; + if (!Workspace.hasUnsavedChanges || Workspace.checkIfWorkspaceEmpty()) { + callback(); + } else { + AlertService.showOptions( + 'Save changes to the untitled circuit? Your changes will be lost if you do not save it.', + () => { + AlertService.showCustom( + SaveProjectDialogComponent, + { + onChangeProjectTitle: (e) => { + this.projectTitle = e.target.value || ''; + return this.projectTitle; + }, + projectTitle: this.projectTitle, }, - projectTitle: this.projectTitle, - }, - (value) => { - if (value) { - this.SaveProjectOff(() => { - callback(); - }); + (value) => { + if (value) { + this.SaveProjectOff(() => { + callback(); + }); + } } - } - ); - }, - () => { - callback(); - }, - () => { }, - 'Save', - 'Don\'t save', - 'Cancel' + ); + }, + () => { + callback(); + }, + () => { }, + 'Save', + 'Don\'t save', + 'Cancel' ); + } } /** * Open Gallery Project From 45c612461b21586d107a4ac6c80fff5a94c389a3 Mon Sep 17 00:00:00 2001 From: kghs-aver Date: Fri, 31 Jan 2025 21:56:21 +0530 Subject: [PATCH 2/2] linting fix --- ArduinoFrontend/src/app/Libs/Workspace.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ArduinoFrontend/src/app/Libs/Workspace.ts b/ArduinoFrontend/src/app/Libs/Workspace.ts index abe48594..b66dd126 100644 --- a/ArduinoFrontend/src/app/Libs/Workspace.ts +++ b/ArduinoFrontend/src/app/Libs/Workspace.ts @@ -732,7 +732,6 @@ export class Workspace { window.hideLoading(); } }, 100); - //console.log('Unsaved Changes:', Workspace.hasUnsavedChanges); Workspace.hasUnsavedChanges = false; UndoUtils.resetStacks(); }