From 6eaeb717e255b436988ca4434dd3a595d7e594ad Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Thu, 22 Nov 2018 22:55:22 +0100 Subject: [PATCH] remember tab item selection in popup --- js/lib/Comms.js | 3 +++ js/lib/libghettoui/popup/MenuItem.js | 10 ++++++++++ js/uw-bg.js | 17 ++++++++++++++++- res/popup/js/popup.js | 17 +++++++++++++---- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/js/lib/Comms.js b/js/lib/Comms.js index fcc3183..b223746 100644 --- a/js/lib/Comms.js +++ b/js/lib/Comms.js @@ -309,6 +309,9 @@ class CommsServer { if (message.cmd === 'get-current-site') { port.postMessage({cmd: 'set-current-site', site: this.server.getVideoTab(), tabHostname: await this.getCurrentTabHostname()}); } + if (message.cmd === 'set-selected-tab') { + this.server.setSelectedTab(cmd.selectedMenu, cmd.selectedSubitem); + } if (message.cmd === 'get-config') { if(Debug.debug) { diff --git a/js/lib/libghettoui/popup/MenuItem.js b/js/lib/libghettoui/popup/MenuItem.js index 907db64..7fcf627 100644 --- a/js/lib/libghettoui/popup/MenuItem.js +++ b/js/lib/libghettoui/popup/MenuItem.js @@ -33,6 +33,16 @@ class MenuItem extends BaseElement { } } } + + existsSubitem(subitemName) { + for(let item of this.subitemList) { + if (item.name === subitemName) { + return true; + } + } + return false; + } + selectFirstSubitem() { for(let item of this.subitemList) { item.unselect(); diff --git a/js/uw-bg.js b/js/uw-bg.js index 6cff81b..1c0f25f 100644 --- a/js/uw-bg.js +++ b/js/uw-bg.js @@ -16,6 +16,11 @@ class UWServer { this.videoTabs = {}; this.currentTabId = 0; this._gctimeout = undefined; + + this.selectedSubitem = { + 'siteSettings': undefined, + 'videoSettings': undefined, + } } async setup() { @@ -101,6 +106,11 @@ class UWServer { if(Debug.debug) { console.log("TAB SWITCHED!", this.currentSite) } + + this.selectedSubitem = { + 'siteSettings': undefined, + 'videoSettings': undefined, + } //TODO: change extension icon based on whether there's any videos on current page } @@ -192,6 +202,10 @@ class UWServer { } } + setSelectedTab(menu, subitem) { + this.selectedSubitem[menu] = subitem; + } + getVideoTab() { // friendly reminder: if current tab doesn't have a video, // there won't be anything in this.videoTabs[this.currentTabId] @@ -203,7 +217,8 @@ class UWServer { // a video registered for it return { host: this.currentSite, - frames: [] + frames: [], + selected: this.selectedSubitem } } } diff --git a/res/popup/js/popup.js b/res/popup/js/popup.js index 9dc9544..d137689 100644 --- a/res/popup/js/popup.js +++ b/res/popup/js/popup.js @@ -32,6 +32,7 @@ port.onMessage.addListener( (m,p) => processReceivedMessage(m,p)); var _video_settings_tab_items = []; +var selectedSubitemLoaded = false; //#region build ui var tablist = { @@ -46,16 +47,22 @@ for (let t in tablist) { } - - function loadFrames(videoTab) { tablist['siteSettings'].removeSubitems(); tablist['videoSettings'].removeSubitems(); + console.log("VIDEO TAB", videoTab) + if (!selectedSubitemLoaded) { + if (videoTab.selected) { + selectedSubitem = videoTab.selected; + selectedSubitemLoaded = true; + } + } function onTabitemClick(item) { tablist[selectedMenu].selectSubitem(item); selectedSubitem[selectedMenu] = item; + port.postMessage({cmd: 'select-tab', selectedMenu: selectedMenu, selectedSubitem: item}); } for (var option of [{id: '__playing', label: 'Currently playing'}, {id: '__all', label: 'All'}]) { @@ -87,12 +94,14 @@ function loadFrames(videoTab) { tablist['videoSettings'].insertSubitem(newItem); } - if (! selectedSubitem.siteSettings) { + console.log("TIME TO SELECT SUBITEM", selectedSubitem, "\nexists subitem in site settings/video settings?", selectedSubitem && tablist['siteSettings'].existsSubitem(selectedSubitem.siteSettings), selectedSubitem && tablist['videoSettings'].existsSubitem(selectedSubitem.videoSettings)) + + if (! selectedSubitem.siteSettings || !tablist['siteSettings'].existsSubitem(selectedSubitem.siteSettings)) { selectedSubitem['siteSettings'] = tablist['siteSettings'].selectFirstSubitem(); } else { tablist['siteSettings'].selectSubitem(selectedSubitem.siteSettings) } - if (! selectedSubitem.videoSettings) { + if (! selectedSubitem.videoSettings || !tablist['videoSettings'].existsSubitem(selectedSubitem.videoSettings)) { selectedSubitem['videoSettings'] = tablist['videoSettings'].selectFirstSubitem(); } else { tablist['videoSettings'].selectSubitem(selectedSubitem.videoSettings);