From 4c9b142099501700099dff7fc3489e9fbc21ee8f Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sun, 24 Mar 2024 10:09:36 +0100 Subject: [PATCH] Close all nested drawers when navigating --- panel/src/panel/drawer.js | 6 ++++++ panel/src/panel/history.js | 3 +++ panel/src/panel/history.test.js | 18 ++++++++++++++++++ panel/src/panel/panel.js | 3 ++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/panel/src/panel/drawer.js b/panel/src/panel/drawer.js index a7c60c09d0..a1f8669ceb 100644 --- a/panel/src/panel/drawer.js +++ b/panel/src/panel/drawer.js @@ -30,12 +30,18 @@ export default (panel) => { /** * Closes the drawer and goes back to the * parent one if it has been stored + * @param {String|true} id Which drawer to close, true for all */ async close(id) { if (this.isOpen === false) { return; } + // Force close all drawers + if (id === true) { + this.history.clear(); + } + // Compare the drawer id to avoid closing // the wrong drawer. This is particularly useful // in nested drawers. diff --git a/panel/src/panel/history.js b/panel/src/panel/history.js index 34acf3cbae..f22c5e8775 100644 --- a/panel/src/panel/history.js +++ b/panel/src/panel/history.js @@ -20,6 +20,9 @@ export default () => { at(index) { return this.milestones.at(index); }, + clear() { + this.milestones = []; + }, get(id = null) { if (id === null) { return this.milestones; diff --git a/panel/src/panel/history.test.js b/panel/src/panel/history.test.js index 1a8af9ed04..46e6302d88 100644 --- a/panel/src/panel/history.test.js +++ b/panel/src/panel/history.test.js @@ -57,6 +57,24 @@ describe.concurrent("panel.drawer.history", () => { expect(history.has("b")).toStrictEqual(false); }); + it("should clear items", async () => { + const history = History(); + + history.add({ + id: "a" + }); + + history.add({ + id: "b" + }); + + expect(history.get()).toStrictEqual([{ id: "a" }, { id: "b" }]); + + history.clear(); + + expect(history.get()).toStrictEqual([]); + }); + it("should go to milestone", async () => { const history = History(); diff --git a/panel/src/panel/panel.js b/panel/src/panel/panel.js index 9196fa2453..9677af4195 100644 --- a/panel/src/panel/panel.js +++ b/panel/src/panel/panel.js @@ -373,7 +373,8 @@ export default { // modals will be closed if the response is null or false. // on undefined, the state of the modal stays untouched else if (state[modal] !== undefined) { - this[modal].close(); + // force close all nested modals + this[modal].close(true); } }