aboutsummaryrefslogtreecommitdiff
path: root/src/app/js
diff options
context:
space:
mode:
author0neGal <mail@0negal.com>2024-06-19 23:46:18 +0200
committer0neGal <mail@0negal.com>2024-06-19 23:46:18 +0200
commitc9adaaf47d18946b93b32bed8237d87d0e02f405 (patch)
treed731c217a8da48dbd564e40d59caa8f1b5b46230 /src/app/js
parentfce388b3d9b520459413988e190f916f334a8d48 (diff)
downloadViper-c9adaaf47d18946b93b32bed8237d87d0e02f405.tar.gz
Viper-c9adaaf47d18946b93b32bed8237d87d0e02f405.zip
select previous selection when a popup is closed
Diffstat (limited to 'src/app/js')
-rw-r--r--src/app/js/navigate.js57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/app/js/navigate.js b/src/app/js/navigate.js
index 9f2a19f..465a7ec 100644
--- a/src/app/js/navigate.js
+++ b/src/app/js/navigate.js
@@ -7,7 +7,20 @@ let navigate = {};
// sets `#selection` to the correct position, size and border radius,
// according to what is currently the `.active-selection`, if none is
// found, it'll instead be hidden
-navigate.selection = () => {
+navigate.selection = (new_selection) => {
+ if (new_selection) {
+ let selected = document.querySelectorAll(".active-selection");
+
+ // make sure just `new_selection` has `.active-selection`
+ for (let i = 0; i < selected.length; i++) {
+ if (selected[i] != new_selection) {
+ selected[i].classList.remove("active-selection");
+ }
+ }
+
+ new_selection.classList.add("active-selection");
+ }
+
// shorthands
let selection_el = document.getElementById("selection");
let active_el = document.querySelector(".active-selection");
@@ -765,9 +778,13 @@ navigate.get_relative_el = (relative_el, direction) => {
}
}
+// contains a list of the last selections we had before a popup was
+// opened, letting us go back to those selections when they're closed
+let last_popup_selections = [];
+
// attempt to reselect the default selection when a popup is either
// closed or opened
-events.on("popup-changed", () => {
+events.on("popup-changed", (e) => {
// get the active selection
let active_el = document.querySelector(".active-selection");
@@ -776,6 +793,42 @@ events.on("popup-changed", () => {
return;
}
+ // add `active_el` to `last_popup_selections` if we opened a popup
+ if (e.new_state) {
+ last_popup_selections.push({
+ el: active_el,
+ popup: e.popup
+ })
+ } else { // we're closing a popup
+ // this may contain the element we had opened before the popup
+ // we're closing was opened
+ let last_selection;
+
+ // remove selections that are for this popup
+ last_popup_selections = last_popup_selections.filter((item) => {
+ // is this selection for this popup?
+ let is_popup = item.popup == e.popup;
+
+ // set `last_selection` to `.el` if its this popup we're
+ // closing, thereby getting the last selection made before
+ // we opened this popup
+ if (is_popup) {
+ last_selection = item.el;
+ }
+
+ return ! is_popup;
+ })
+
+ // select `last_selection` if one was found
+ if (last_selection) {
+ setTimeout(() => {
+ navigate.selection(last_selection);
+ }, 150) // needed due to popup animation
+
+ return;
+ }
+ }
+
// remove the currently active selection
active_el.classList.remove("active-selection");