Refactoring of hide/show algorithm.
This commit is contained in:
parent
f87eece6cc
commit
76ad3fcb0f
2 changed files with 34 additions and 91 deletions
66
index.js
66
index.js
|
@ -32,16 +32,21 @@ let ContainerService =
|
||||||
// only these methods are allowed. We have a 1:1 mapping between messages
|
// only these methods are allowed. We have a 1:1 mapping between messages
|
||||||
// and methods. These methods must return a promise.
|
// and methods. These methods must return a promise.
|
||||||
let methods = [
|
let methods = [
|
||||||
'queryTabs',
|
|
||||||
'hideTabs',
|
'hideTabs',
|
||||||
'showTabs',
|
'showTabs',
|
||||||
'removeTabs',
|
|
||||||
'sortTabs',
|
'sortTabs',
|
||||||
'openTab',
|
'openTab',
|
||||||
'queryIdentities',
|
'queryIdentities',
|
||||||
'getIdentitiesState',
|
'getIdentity',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Map of identities.
|
||||||
|
ContextualIdentityService.getIdentities().forEach(identity => {
|
||||||
|
this._identitiesState[identity.userContextId] = {hiddenTabUrls: []};
|
||||||
|
});
|
||||||
|
|
||||||
|
// WebExtension startup
|
||||||
|
|
||||||
webExtension.startup().then(api => {
|
webExtension.startup().then(api => {
|
||||||
api.browser.runtime.onMessage.addListener((message, sender, sendReply) => {
|
api.browser.runtime.onMessage.addListener((message, sender, sendReply) => {
|
||||||
if ("method" in message && methods.indexOf(message.method) != -1) {
|
if ("method" in message && methods.indexOf(message.method) != -1) {
|
||||||
|
@ -54,27 +59,19 @@ let ContainerService =
|
||||||
// utility methods
|
// utility methods
|
||||||
|
|
||||||
_convert(identity) {
|
_convert(identity) {
|
||||||
let hiddenTabUrls = [];
|
|
||||||
|
|
||||||
if (identity.userContextId in this._identitiesState) {
|
|
||||||
hiddenTabUrls = this._identitiesState[identity.userContextId].hiddenTabUrls;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: ContextualIdentityService.getUserContextLabel(identity.userContextId),
|
name: ContextualIdentityService.getUserContextLabel(identity.userContextId),
|
||||||
icon: identity.icon,
|
icon: identity.icon,
|
||||||
color: identity.color,
|
color: identity.color,
|
||||||
userContextId: identity.userContextId,
|
userContextId: identity.userContextId,
|
||||||
hiddenTabUrls: hiddenTabUrls
|
hasHiddenTabs: !!this._identitiesState[identity.userContextId].hiddenTabUrls.length,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
// Tabs management
|
// Tabs management
|
||||||
|
|
||||||
queryTabs(args) {
|
hideTabs(args) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let tabList = [];
|
|
||||||
|
|
||||||
for (let tab of tabs) {
|
for (let tab of tabs) {
|
||||||
let xulTab = viewFor(tab);
|
let xulTab = viewFor(tab);
|
||||||
let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10);
|
let userContextId = parseInt(xulTab.getAttribute('usercontextid') || 0, 10);
|
||||||
|
@ -83,39 +80,24 @@ let ContainerService =
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
tabList.push({
|
this._identitiesState[args.userContextId].hiddenTabUrls.push(tab.url);
|
||||||
id: tab.id,
|
tab.close();
|
||||||
url: tab.url,
|
|
||||||
userContextId: userContextId,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(tabList);
|
resolve(null);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
hideTabs(args) {
|
|
||||||
this._identitiesState[args.userContextId].hiddenTabUrls = args.tabUrlsToSave;
|
|
||||||
return Promise.resolve(null);
|
|
||||||
},
|
|
||||||
|
|
||||||
showTabs(args) {
|
showTabs(args) {
|
||||||
return new Promise((resolve, reject) => {
|
let promises = [];
|
||||||
let hiddenTabUrls = this._identitiesState[args.userContextId].hiddenTabUrls;
|
|
||||||
this._identitiesState[args.userContextId].hiddenTabUrls = [];
|
|
||||||
resolve(hiddenTabUrls);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
removeTabs(args) {
|
for (let url of this._identitiesState[args.userContextId].hiddenTabUrls) {
|
||||||
return new Promise((resolve, reject) => {
|
promises.push(this.openTab({ userContextId: args.userContextId, url }));
|
||||||
for (let tab of tabs) {
|
|
||||||
if (args.tabIds.indexOf(tab.id) != -1) {
|
|
||||||
tab.close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
resolve(null);
|
this._identitiesState[args.userContextId].hiddenTabUrls = [];
|
||||||
});
|
|
||||||
|
return Promise.all(promises);
|
||||||
},
|
},
|
||||||
|
|
||||||
sortTabs(args) {
|
sortTabs(args) {
|
||||||
|
@ -187,17 +169,15 @@ let ContainerService =
|
||||||
ContextualIdentityService.getIdentities().forEach(identity => {
|
ContextualIdentityService.getIdentities().forEach(identity => {
|
||||||
let convertedIdentity = this._convert(identity);
|
let convertedIdentity = this._convert(identity);
|
||||||
identities.push(convertedIdentity);
|
identities.push(convertedIdentity);
|
||||||
if (!(convertedIdentity.userContextId in this._identitiesState)) {
|
|
||||||
this._identitiesState[convertedIdentity.userContextId] = {hiddenTabUrls: []};
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
resolve(identities);
|
resolve(identities);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getIdentitiesState(args) {
|
getIdentity(args) {
|
||||||
return Promise.resolve(this._identitiesState);
|
let identity = ContextualIdentityService.getIdentityFromId(args.userContextId);
|
||||||
|
return Promise.resolve(identity ? this._convert(identity) : null);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,51 +2,15 @@
|
||||||
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
|
const CONTAINER_HIDE_SRC = '/img/container-hide.svg';
|
||||||
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
|
const CONTAINER_UNHIDE_SRC = '/img/container-unhide.svg';
|
||||||
|
|
||||||
function hideContainerTabs(userContextId) {
|
function showOrHideContainerTabs(userContextId, hasHiddenTabs) {
|
||||||
const tabIdsToRemove = [];
|
|
||||||
const tabUrlsToSave = [];
|
|
||||||
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
|
|
||||||
|
|
||||||
browser.runtime.sendMessage({
|
|
||||||
method: 'queryTabs',
|
|
||||||
userContextId: userContextId
|
|
||||||
}).then(tabs=> {
|
|
||||||
tabs.forEach(tab=> {
|
|
||||||
tabIdsToRemove.push(tab.id);
|
|
||||||
tabUrlsToSave.push(tab.url);
|
|
||||||
});
|
|
||||||
browser.runtime.sendMessage({
|
|
||||||
method: 'hideTabs',
|
|
||||||
userContextId: userContextId,
|
|
||||||
tabUrlsToSave: tabUrlsToSave
|
|
||||||
}).then(()=> {
|
|
||||||
return browser.runtime.sendMessage({
|
|
||||||
method: 'removeTabs',
|
|
||||||
tabIds: tabIdsToRemove
|
|
||||||
});
|
|
||||||
}).then(() => {
|
|
||||||
hideorshowIcon.src = CONTAINER_UNHIDE_SRC;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function showContainerTabs(userContextId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
|
const hideorshowIcon = document.querySelector(`#uci-${userContextId}-hideorshow-icon`);
|
||||||
|
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'showTabs',
|
method: hasHiddenTabs ? 'showTabs' : 'hideTabs',
|
||||||
userContextId: userContextId
|
userContextId: userContextId
|
||||||
}).then(hiddenTabUrls=> {
|
|
||||||
hiddenTabUrls.forEach(url=> {
|
|
||||||
browser.runtime.sendMessage({
|
|
||||||
method: 'openTab',
|
|
||||||
userContextId: userContextId,
|
|
||||||
url: url
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
hideorshowIcon.src = CONTAINER_HIDE_SRC;
|
hideorshowIcon.src = hasHiddenTabs ? CONTAINER_HIDE_SRC : CONTAINER_UNHIDE_SRC;
|
||||||
}).then(resolve);
|
}).then(resolve);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -84,7 +48,7 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
identities.forEach(identity=> {
|
identities.forEach(identity=> {
|
||||||
let hideOrShowIconSrc = CONTAINER_HIDE_SRC;
|
let hideOrShowIconSrc = CONTAINER_HIDE_SRC;
|
||||||
|
|
||||||
if (identity.hiddenTabUrls.length) {
|
if (identity.hasHiddenTabs) {
|
||||||
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
|
hideOrShowIconSrc = CONTAINER_UNHIDE_SRC;
|
||||||
}
|
}
|
||||||
const identityRow = `
|
const identityRow = `
|
||||||
|
@ -124,15 +88,14 @@ browser.runtime.sendMessage({method: 'queryIdentities'}).then(identities=> {
|
||||||
const userContextId = e.target.parentElement.parentElement.dataset.identityCookieStoreId;
|
const userContextId = e.target.parentElement.parentElement.dataset.identityCookieStoreId;
|
||||||
|
|
||||||
if (e.target.matches('.hideorshow-icon')) {
|
if (e.target.matches('.hideorshow-icon')) {
|
||||||
browser.runtime.sendMessage({method: 'getIdentitiesState'}).then(identitiesState=> {
|
browser.runtime.sendMessage({
|
||||||
if (identitiesState[userContextId].hiddenTabUrls.length) {
|
method: 'getIdentity',
|
||||||
showContainerTabs(userContextId);
|
userContextId
|
||||||
} else {
|
}).then(identity=> {
|
||||||
hideContainerTabs(userContextId);
|
showOrHideContainerTabs(userContextId, identity.hasHiddenTabs);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} else if (e.target.matches('.newtab-icon')) {
|
} else if (e.target.matches('.newtab-icon')) {
|
||||||
showContainerTabs(userContextId).then(() => {
|
showOrHideContainerTabs(userContextId, true).then(() => {
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
method: 'openTab',
|
method: 'openTab',
|
||||||
userContextId: userContextId})
|
userContextId: userContextId})
|
||||||
|
|
Loading…
Add table
Reference in a new issue