From c6f4fda801bc29802fd52f244683fbb74fa2201f Mon Sep 17 00:00:00 2001 From: He4eT Date: Sun, 21 Jan 2024 20:36:39 +0100 Subject: [PATCH] pages/search: store: closeCurrentTab --- pages/search/modules/actionboxHandlers.js | 9 +++++---- pages/search/modules/inputHandlers.js | 12 +++--------- pages/search/modules/store.js | 3 +++ pages/search/search.js | 1 + 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pages/search/modules/actionboxHandlers.js b/pages/search/modules/actionboxHandlers.js index 6a1cd91..9076951 100644 --- a/pages/search/modules/actionboxHandlers.js +++ b/pages/search/modules/actionboxHandlers.js @@ -31,8 +31,8 @@ function openHelp () { location.href = helpLink } -function closeCurrentTab () { - window.close() +function closeCurrentTab (store) { + store.actions.closeCurrentTab() } function reloadCurrentTab () { @@ -46,11 +46,12 @@ const getTabByLabel = (store, label) => { return tabs.find(tab => tab.label === label) } -function switchToTab (store, label) { +function switchToTab (store, label, flush) { const tab = getTabByLabel(store, label) if (tab) { + flush() store.actions.goToTab(tab.id) - closeCurrentTab() + store.actions.closeCurrentTab() } } diff --git a/pages/search/modules/inputHandlers.js b/pages/search/modules/inputHandlers.js index e75ba61..986a3ab 100644 --- a/pages/search/modules/inputHandlers.js +++ b/pages/search/modules/inputHandlers.js @@ -38,12 +38,6 @@ export const attachInputHandlers = (store) => { const actionbox = document.getElementById('actionbox') actionbox.value = '' - /* Switch to tab */ - const switchToTab = (tabId) => { - store.actions.goToTab(parseInt(tabId)) - window.close() - } - /* Arrow keys */ document.addEventListener('keydown', (e) => { focusButtonsWithArrows(e, () => { @@ -58,8 +52,8 @@ export const attachInputHandlers = (store) => { /* Buttons */ document.addEventListener('click', (event) => { if (event.target.classList.contains('tab')) { - const tabId = event.target.dataset.id - switchToTab(tabId) + const tabId = parseInt(event.target.dataset.id) + store.actions.goToTab(tabId) } }) @@ -82,7 +76,7 @@ export const attachInputHandlers = (store) => { let visibleTabs = store.getCurrentState().results if (visibleTabs.length > 0) { const tabId = visibleTabs[0].obj.id - switchToTab(tabId) + store.actions.goToTab(tabId) } } }) diff --git a/pages/search/modules/store.js b/pages/search/modules/store.js index 23d3f50..0fdc31a 100644 --- a/pages/search/modules/store.js +++ b/pages/search/modules/store.js @@ -4,6 +4,7 @@ const fuzzysort = window.fuzzysort export const init = ({ tabs: browserTabs, onStateUpdate, + closeCurrentTab, }) => { /* Initial state */ const state = { @@ -66,7 +67,9 @@ export const init = ({ goToTab(id) { browserTabs.update(id, { active: true }) .then(updateState) + .then(closeCurrentTab) }, + closeCurrentTab, closeTab(id) { browserTabs.remove(id) .then(updateState) diff --git a/pages/search/search.js b/pages/search/search.js index 1a7cc3f..016ad48 100644 --- a/pages/search/search.js +++ b/pages/search/search.js @@ -5,6 +5,7 @@ import * as inputHandlers from './modules/inputHandlers.js' const store = Store.init({ tabs: browser.tabs, onStateUpdate: dom.updateSearchResults, + closeCurrentTab: () => window.close(), }) void inputHandlers.attachInputHandlers(store)