Fixing workspace change duplicates

This commit is contained in:
HarryHeres 2024-09-07 20:58:55 +02:00
parent 1ae07eadf9
commit 9edbe7f657
No known key found for this signature in database

View file

@ -1,18 +1,21 @@
var ZenWorkspaces = { var ZenWorkspaces = {
async init() { async init() {
let docElement = document.documentElement; let docElement = document.documentElement;
if (docElement.getAttribute("chromehidden").includes("toolbar") if (
|| docElement.getAttribute("chromehidden").includes("menubar") docElement.getAttribute("chromehidden").includes("toolbar") ||
|| docElement.hasAttribute("privatebrowsingmode")) { docElement.getAttribute("chromehidden").includes("menubar") ||
console.warn("ZenWorkspaces: !!! ZenWorkspaces is disabled in hidden windows !!!"); docElement.hasAttribute("privatebrowsingmode")
) {
console.warn(
"ZenWorkspaces: !!! ZenWorkspaces is disabled in hidden windows !!!",
);
return; // We are in a hidden window, don't initialize ZenWorkspaces return; // We are in a hidden window, don't initialize ZenWorkspaces
} }
console.info("ZenWorkspaces: Initializing ZenWorkspaces..."); console.info("ZenWorkspaces: Initializing ZenWorkspaces...");
window.SessionStore.promiseInitialized.then(async () => { window.SessionStore.promiseInitialized.then(async () => {
await this.initializeWorkspaces(); await this.initializeWorkspaces();
console.info("ZenWorkspaces: ZenWorkspaces initialized"); console.info("ZenWorkspaces: ZenWorkspaces initialized");
}) });
}, },
get workspaceEnabled() { get workspaceEnabled() {
@ -51,7 +54,10 @@ var ZenWorkspaces = {
}, },
async initializeWorkspaces() { async initializeWorkspaces() {
Services.prefs.addObserver("zen.workspaces.enabled", this.onWorkspacesEnabledChanged.bind(this)); Services.prefs.addObserver(
"zen.workspaces.enabled",
this.onWorkspacesEnabledChanged.bind(this),
);
this.initializeWorkspacesButton(); this.initializeWorkspacesButton();
let file = new FileUtils.File(this._storeFile); let file = new FileUtils.File(this._storeFile);
if (!file.exists()) { if (!file.exists()) {
@ -63,9 +69,13 @@ var ZenWorkspaces = {
if (workspaces.workspaces.length === 0) { if (workspaces.workspaces.length === 0) {
await this.createAndSaveWorkspace("Default Workspace", true); await this.createAndSaveWorkspace("Default Workspace", true);
} else { } else {
let activeWorkspace = workspaces.workspaces.find(workspace => workspace.used); let activeWorkspace = workspaces.workspaces.find(
(workspace) => workspace.used,
);
if (!activeWorkspace) { if (!activeWorkspace) {
activeWorkspace = workspaces.workspaces.find(workspace => workspace.default); activeWorkspace = workspaces.workspaces.find(
(workspace) => workspace.default,
);
activeWorkspace.used = true; activeWorkspace.used = true;
await this.saveWorkspaces(); await this.saveWorkspaces();
} }
@ -90,7 +100,9 @@ var ZenWorkspaces = {
let workspaceID = tab.getAttribute("zen-workspace-id"); let workspaceID = tab.getAttribute("zen-workspace-id");
// If the tab is the last one in the workspace, create a new tab // If the tab is the last one in the workspace, create a new tab
if (workspaceID) { if (workspaceID) {
let tabs = gBrowser.tabs.filter(tab => tab.getAttribute("zen-workspace-id") === workspaceID); let tabs = gBrowser.tabs.filter(
(tab) => tab.getAttribute("zen-workspace-id") === workspaceID,
);
if (tabs.length === 1) { if (tabs.length === 1) {
this._createNewTabForWorkspace({ uuid: workspaceID }); this._createNewTabForWorkspace({ uuid: workspaceID });
// We still need to close other tabs in the workspace // We still need to close other tabs in the workspace
@ -102,7 +114,9 @@ var ZenWorkspaces = {
_kIcons: ["🏠", "📄", "💹", "💼", "📧", "✅", "👥"], _kIcons: ["🏠", "📄", "💹", "💼", "📧", "✅", "👥"],
_initializeWorkspaceCreationIcons() { _initializeWorkspaceCreationIcons() {
let container = document.getElementById("PanelUI-zen-workspaces-create-icons-container"); let container = document.getElementById(
"PanelUI-zen-workspaces-create-icons-container",
);
for (let icon of this._kIcons) { for (let icon of this._kIcons) {
let button = document.createXULElement("toolbarbutton"); let button = document.createXULElement("toolbarbutton");
button.className = "toolbarbutton-1"; button.className = "toolbarbutton-1";
@ -129,7 +143,7 @@ var ZenWorkspaces = {
button.onclick = ((event) => { button.onclick = ((event) => {
let wasSelected = button.hasAttribute("selected"); let wasSelected = button.hasAttribute("selected");
for (let button of container.children) { for (let button of container.children) {
button.removeAttribute("selected"); button.removeAttribute("selected");
} }
if (!wasSelected) { if (!wasSelected) {
button.setAttribute("selected", "true"); button.setAttribute("selected", "true");
@ -145,7 +159,9 @@ var ZenWorkspaces = {
if (typeof json.workspaces === "undefined") { if (typeof json.workspaces === "undefined") {
json.workspaces = []; json.workspaces = [];
} }
let existing = json.workspaces.findIndex(workspace => workspace.uuid === workspaceData.uuid); let existing = json.workspaces.findIndex(
(workspace) => workspace.uuid === workspaceData.uuid,
);
if (existing >= 0) { if (existing >= 0) {
json.workspaces[existing] = workspaceData; json.workspaces[existing] = workspaceData;
} else { } else {
@ -154,16 +170,23 @@ var ZenWorkspaces = {
console.info("ZenWorkspaces: Saving workspace", workspaceData); console.info("ZenWorkspaces: Saving workspace", workspaceData);
await IOUtils.writeJSON(this._storeFile, json); await IOUtils.writeJSON(this._storeFile, json);
this._workspaceCache = null; this._workspaceCache = null;
await this._updateWorkspacesChangeContextMenu();
}, },
async removeWorkspace(windowID) { async removeWorkspace(windowID) {
let json = await this._workspaces(); let json = await this._workspaces();
console.info("ZenWorkspaces: Removing workspace", windowID); console.info("ZenWorkspaces: Removing workspace", windowID);
await this.changeWorkspace(json.workspaces.find(workspace => workspace.uuid !== windowID)); await this.changeWorkspace(
json.workspaces.find((workspace) => workspace.uuid !== windowID),
);
this._deleteAllTabsInWorkspace(windowID); this._deleteAllTabsInWorkspace(windowID);
json.workspaces = json.workspaces.filter(workspace => workspace.uuid !== windowID); json.workspaces = json.workspaces.filter(
(workspace) => workspace.uuid !== windowID,
);
await this.unsafeSaveWorkspaces(json); await this.unsafeSaveWorkspaces(json);
await this._propagateWorkspaceData(); await this._propagateWorkspaceData();
await this._updateWorkspacesChangeContextMenu();
}, },
async saveWorkspaces() { async saveWorkspaces() {
@ -179,34 +202,55 @@ var ZenWorkspaces = {
// Workspaces dialog UI management // Workspaces dialog UI management
openSaveDialog() { openSaveDialog() {
let parentPanel = document.getElementById("PanelUI-zen-workspaces-multiview"); let parentPanel = document.getElementById(
"PanelUI-zen-workspaces-multiview",
);
PanelUI.showSubView("PanelUI-zen-workspaces-create", parentPanel); PanelUI.showSubView("PanelUI-zen-workspaces-create", parentPanel);
}, },
async openEditDialog(workspaceUuid) { async openEditDialog(workspaceUuid) {
this._workspaceEditDialog.setAttribute("data-workspace-uuid", workspaceUuid); this._workspaceEditDialog.setAttribute(
document.getElementById("PanelUI-zen-workspaces-edit-save").setAttribute("disabled", "true"); "data-workspace-uuid",
workspaceUuid,
);
document
.getElementById("PanelUI-zen-workspaces-edit-save")
.setAttribute("disabled", "true");
let workspaces = (await this._workspaces()).workspaces; let workspaces = (await this._workspaces()).workspaces;
let workspaceData = workspaces.find(workspace => workspace.uuid === workspaceUuid); let workspaceData = workspaces.find(
(workspace) => workspace.uuid === workspaceUuid,
);
this._workspaceEditInput.textContent = workspaceData.name; this._workspaceEditInput.textContent = workspaceData.name;
this._workspaceEditInput.value = workspaceData.name; this._workspaceEditInput.value = workspaceData.name;
this._workspaceEditInput.setAttribute("data-initial-value", workspaceData.name); this._workspaceEditInput.setAttribute(
this._workspaceEditIconsContainer "data-initial-value",
.setAttribute("data-initial-value", workspaceData.icon); workspaceData.name,
document.querySelectorAll("#PanelUI-zen-workspaces-edit-icons-container toolbarbutton") );
.forEach(button => { this._workspaceEditIconsContainer.setAttribute(
"data-initial-value",
workspaceData.icon,
);
document
.querySelectorAll(
"#PanelUI-zen-workspaces-edit-icons-container toolbarbutton",
)
.forEach((button) => {
if (button.label === workspaceData.icon) { if (button.label === workspaceData.icon) {
button.setAttribute("selected", "true"); button.setAttribute("selected", "true");
} else { } else {
button.removeAttribute("selected"); button.removeAttribute("selected");
} }
}); });
let parentPanel = document.getElementById("PanelUI-zen-workspaces-multiview"); let parentPanel = document.getElementById(
"PanelUI-zen-workspaces-multiview",
);
PanelUI.showSubView("PanelUI-zen-workspaces-edit", parentPanel); PanelUI.showSubView("PanelUI-zen-workspaces-edit", parentPanel);
}, },
closeWorkspacesSubView() { closeWorkspacesSubView() {
let parentPanel = document.getElementById("PanelUI-zen-workspaces-multiview"); let parentPanel = document.getElementById(
"PanelUI-zen-workspaces-multiview",
);
parentPanel.goBack(); parentPanel.goBack();
}, },
@ -222,7 +266,9 @@ var ZenWorkspaces = {
}, },
async _propagateWorkspaceData() { async _propagateWorkspaceData() {
let currentContainer = document.getElementById("PanelUI-zen-workspaces-current-info"); let currentContainer = document.getElementById(
"PanelUI-zen-workspaces-current-info",
);
let workspaceList = document.getElementById("PanelUI-zen-workspaces-list"); let workspaceList = document.getElementById("PanelUI-zen-workspaces-list");
const createWorkspaceElement = (workspace) => { const createWorkspaceElement = (workspace) => {
let element = document.createXULElement("toolbarbutton"); let element = document.createXULElement("toolbarbutton");
@ -239,33 +285,42 @@ var ZenWorkspaces = {
<image class="toolbarbutton-icon" id="zen-workspace-actions-menu-icon"></image> <image class="toolbarbutton-icon" id="zen-workspace-actions-menu-icon"></image>
</toolbarbutton> </toolbarbutton>
`); `);
// use text content instead of innerHTML to avoid XSS // use text content instead of innerHTML to avoid XSS
childs.querySelector(".zen-workspace-icon").textContent = this.getWorkspaceIcon(workspace); childs.querySelector(".zen-workspace-icon").textContent =
this.getWorkspaceIcon(workspace);
childs.querySelector(".zen-workspace-name").textContent = workspace.name; childs.querySelector(".zen-workspace-name").textContent = workspace.name;
childs.querySelector(".zen-workspace-actions").addEventListener("command", (event) => { childs
let button = event.target; .querySelector(".zen-workspace-actions")
this._contextMenuId = button.closest("toolbarbutton[zen-workspace-id]").getAttribute("zen-workspace-id"); .addEventListener("command", (event) => {
const popup = button.ownerDocument.getElementById( let button = event.target;
"zenWorkspaceActionsMenu" this._contextMenuId = button
); .closest("toolbarbutton[zen-workspace-id]")
popup.openPopup(button, "after_end"); .getAttribute("zen-workspace-id");
}); const popup = button.ownerDocument.getElementById(
"zenWorkspaceActionsMenu",
);
popup.openPopup(button, "after_end");
});
element.appendChild(childs); element.appendChild(childs);
element.onclick = (async () => { element.onclick = (async () => {
if (event.target.closest(".zen-workspace-actions")) { if (event.target.closest(".zen-workspace-actions")) {
return; // Ignore clicks on the actions button return; // Ignore clicks on the actions button
} }
await this.changeWorkspace(workspace) await this.changeWorkspace(workspace);
let panel = document.getElementById("PanelUI-zen-workspaces"); let panel = document.getElementById("PanelUI-zen-workspaces");
PanelMultiView.hidePopup(panel); PanelMultiView.hidePopup(panel);
document.getElementById("zen-workspaces-button").removeAttribute("open"); document
.getElementById("zen-workspaces-button")
.removeAttribute("open");
}).bind(this, workspace); }).bind(this, workspace);
return element; return element;
} };
let workspaces = await this._workspaces(); let workspaces = await this._workspaces();
let activeWorkspace = workspaces.workspaces.find(workspace => workspace.used); let activeWorkspace = workspaces.workspaces.find(
(workspace) => workspace.used,
);
currentContainer.innerHTML = ""; currentContainer.innerHTML = "";
workspaceList.innerHTML = ""; workspaceList.innerHTML = "";
workspaceList.parentNode.style.display = "flex"; workspaceList.parentNode.style.display = "flex";
@ -324,7 +379,9 @@ var ZenWorkspaces = {
if (!button) { if (!button) {
return; return;
} }
let activeWorkspace = (await this._workspaces()).workspaces.find(workspace => workspace.used); let activeWorkspace = (await this._workspaces()).workspaces.find(
(workspace) => workspace.used,
);
if (activeWorkspace) { if (activeWorkspace) {
button.innerHTML = ` button.innerHTML = `
<div class="zen-workspace-sidebar-icon"> <div class="zen-workspace-sidebar-icon">
@ -334,11 +391,15 @@ var ZenWorkspaces = {
`; `;
// use text content instead of innerHTML to avoid XSS // use text content instead of innerHTML to avoid XSS
button.querySelector(".zen-workspace-sidebar-name").textContent = activeWorkspace.name; button.querySelector(".zen-workspace-sidebar-name").textContent =
button.querySelector(".zen-workspace-sidebar-icon").textContent = this.getWorkspaceIcon(activeWorkspace); activeWorkspace.name;
button.querySelector(".zen-workspace-sidebar-icon").textContent =
this.getWorkspaceIcon(activeWorkspace);
if (!this.workspaceHasIcon(activeWorkspace)) { if (!this.workspaceHasIcon(activeWorkspace)) {
button.querySelector(".zen-workspace-sidebar-icon").setAttribute("no-icon", "true"); button
.querySelector(".zen-workspace-sidebar-icon")
.setAttribute("no-icon", "true");
} }
} }
}, },
@ -358,7 +419,9 @@ var ZenWorkspaces = {
}, },
get _workspaceEditIconsContainer() { get _workspaceEditIconsContainer() {
return document.getElementById("PanelUI-zen-workspaces-edit-icons-container"); return document.getElementById(
"PanelUI-zen-workspaces-edit-icons-container",
);
}, },
_deleteAllTabsInWorkspace(workspaceID) { _deleteAllTabsInWorkspace(workspaceID) {
@ -388,7 +451,9 @@ var ZenWorkspaces = {
}, },
_createNewTabForWorkspace(window) { _createNewTabForWorkspace(window) {
let tab = gZenUIManager.openAndChangeToTab(Services.prefs.getStringPref("browser.startup.homepage")); let tab = gZenUIManager.openAndChangeToTab(
Services.prefs.getStringPref("browser.startup.homepage"),
);
tab.setAttribute("zen-workspace-id", window.uuid); tab.setAttribute("zen-workspace-id", window.uuid);
}, },
@ -398,23 +463,31 @@ var ZenWorkspaces = {
return; return;
} }
this._workspaceCreateInput.value = ""; this._workspaceCreateInput.value = "";
let icon = document.querySelector("#PanelUI-zen-workspaces-create-icons-container [selected]"); let icon = document.querySelector(
"#PanelUI-zen-workspaces-create-icons-container [selected]",
);
icon?.removeAttribute("selected"); icon?.removeAttribute("selected");
await this.createAndSaveWorkspace(workspaceName, false, icon?.label); await this.createAndSaveWorkspace(workspaceName, false, icon?.label);
document.getElementById("PanelUI-zen-workspaces").hidePopup(true); document.getElementById("PanelUI-zen-workspaces").hidePopup(true);
}, },
async saveWorkspaceFromEdit() { async saveWorkspaceFromEdit() {
let workspaceUuid = this._workspaceEditDialog.getAttribute("data-workspace-uuid"); let workspaceUuid = this._workspaceEditDialog.getAttribute(
"data-workspace-uuid",
);
let workspaceName = this._workspaceEditInput.value; let workspaceName = this._workspaceEditInput.value;
if (!workspaceName) { if (!workspaceName) {
return; return;
} }
this._workspaceEditInput.value = ""; this._workspaceEditInput.value = "";
let icon = document.querySelector("#PanelUI-zen-workspaces-edit-icons-container [selected]"); let icon = document.querySelector(
"#PanelUI-zen-workspaces-edit-icons-container [selected]",
);
icon?.removeAttribute("selected"); icon?.removeAttribute("selected");
let workspaces = (await this._workspaces()).workspaces; let workspaces = (await this._workspaces()).workspaces;
let workspaceData = workspaces.find(workspace => workspace.uuid === workspaceUuid); let workspaceData = workspaces.find(
(workspace) => workspace.uuid === workspaceUuid,
);
workspaceData.name = workspaceName; workspaceData.name = workspaceName;
workspaceData.icon = icon?.label; workspaceData.icon = icon?.label;
await this.saveWorkspace(workspaceData); await this.saveWorkspace(workspaceData);
@ -434,11 +507,15 @@ var ZenWorkspaces = {
onWorkspaceEditChange() { onWorkspaceEditChange() {
let button = document.getElementById("PanelUI-zen-workspaces-edit-save"); let button = document.getElementById("PanelUI-zen-workspaces-edit-save");
let name = this._workspaceEditInput.value let name = this._workspaceEditInput.value;
let icon = document.querySelector("#PanelUI-zen-workspaces-edit-icons-container [selected]")?.label; let icon = document.querySelector(
if ( name === this._workspaceEditInput.getAttribute("data-initial-value") "#PanelUI-zen-workspaces-edit-icons-container [selected]",
&& icon === this._workspaceEditIconsContainer.getAttribute("data-initial-value")) )?.label;
{ if (
name === this._workspaceEditInput.getAttribute("data-initial-value") &&
icon ===
this._workspaceEditIconsContainer.getAttribute("data-initial-value")
) {
button.setAttribute("disabled", "true"); button.setAttribute("disabled", "true");
return; return;
} }
@ -457,7 +534,10 @@ var ZenWorkspaces = {
this.unsafeSaveWorkspaces(workspaces); this.unsafeSaveWorkspaces(workspaces);
console.info("ZenWorkspaces: Changing workspace to", window.uuid); console.info("ZenWorkspaces: Changing workspace to", window.uuid);
for (let tab of gBrowser.tabs) { for (let tab of gBrowser.tabs) {
if ((tab.getAttribute("zen-workspace-id") === window.uuid && !tab.pinned) || !tab.hasAttribute("zen-workspace-id")) { if (
(tab.getAttribute("zen-workspace-id") === window.uuid && !tab.pinned) ||
!tab.hasAttribute("zen-workspace-id")
) {
if (!firstTab) { if (!firstTab) {
firstTab = tab; firstTab = tab;
gBrowser.selectedTab = firstTab; gBrowser.selectedTab = firstTab;
@ -482,6 +562,33 @@ var ZenWorkspaces = {
await this.saveWorkspaces(); await this.saveWorkspaces();
await this._updateWorkspacesButton(); await this._updateWorkspacesButton();
await this._propagateWorkspaceData(); await this._propagateWorkspaceData();
await this._updateWorkspacesChangeContextMenu();
},
async _updateWorkspacesChangeContextMenu() {
const workspaces = await this._workspaces();
const menuPopup = document.getElementById(
"context-zen-change-workspace-tab-menu-popup",
);
menuPopup.innerHTML = "";
const activeWorkspace = workspaces.workspaces.find(
(workspace) => workspace.used,
);
for (let workspace of workspaces.workspaces) {
const menuItem = document.createXULElement("menuitem");
menuItem.setAttribute("label", workspace.name);
menuItem.setAttribute("zen-workspace-id", workspace.uuid);
if (workspace.uuid === activeWorkspace.uuid) {
menuItem.setAttribute("disabled", "true");
}
menuPopup.appendChild(menuItem);
}
}, },
_createWorkspaceData(name, isDefault, icon) { _createWorkspaceData(name, isDefault, icon) {
@ -496,7 +603,11 @@ var ZenWorkspaces = {
return window; return window;
}, },
async createAndSaveWorkspace(name = "New Workspace", isDefault = false, icon = undefined) { async createAndSaveWorkspace(
name = "New Workspace",
isDefault = false,
icon = undefined,
) {
if (!this.workspaceEnabled) { if (!this.workspaceEnabled) {
return; return;
} }
@ -510,7 +621,9 @@ var ZenWorkspaces = {
let workspaceID = tab.getAttribute("zen-workspace-id"); let workspaceID = tab.getAttribute("zen-workspace-id");
if (!workspaceID) { if (!workspaceID) {
let workspaces = await this._workspaces(); let workspaces = await this._workspaces();
let activeWorkspace = workspaces.workspaces.find(workspace => workspace.used); let activeWorkspace = workspaces.workspaces.find(
(workspace) => workspace.used,
);
if (!activeWorkspace || tab.hasAttribute("hidden")) { if (!activeWorkspace || tab.hasAttribute("hidden")) {
return; return;
} }
@ -523,22 +636,41 @@ var ZenWorkspaces = {
_contextMenuId: null, _contextMenuId: null,
async updateContextMenu(_) { async updateContextMenu(_) {
console.assert(this._contextMenuId, "No context menu ID set"); console.assert(this._contextMenuId, "No context menu ID set");
document.querySelector(`#PanelUI-zen-workspaces [zen-workspace-id="${this._contextMenuId}"] .zen-workspace-actions`).setAttribute("active", "true"); document
.querySelector(
`#PanelUI-zen-workspaces [zen-workspace-id="${this._contextMenuId}"] .zen-workspace-actions`,
)
.setAttribute("active", "true");
const workspaces = await this._workspaces(); const workspaces = await this._workspaces();
let deleteMenuItem = document.getElementById("context_zenDeleteWorkspace"); let deleteMenuItem = document.getElementById("context_zenDeleteWorkspace");
if (workspaces.workspaces.length <= 1 || workspaces.workspaces.find(workspace => workspace.uuid === this._contextMenuId).default) { if (
workspaces.workspaces.length <= 1 ||
workspaces.workspaces.find(
(workspace) => workspace.uuid === this._contextMenuId,
).default
) {
deleteMenuItem.setAttribute("disabled", "true"); deleteMenuItem.setAttribute("disabled", "true");
} else { } else {
deleteMenuItem.removeAttribute("disabled"); deleteMenuItem.removeAttribute("disabled");
} }
let defaultMenuItem = document.getElementById("context_zenSetAsDefaultWorkspace"); let defaultMenuItem = document.getElementById(
if (workspaces.workspaces.find(workspace => workspace.uuid === this._contextMenuId).default) { "context_zenSetAsDefaultWorkspace",
);
if (
workspaces.workspaces.find(
(workspace) => workspace.uuid === this._contextMenuId,
).default
) {
defaultMenuItem.setAttribute("disabled", "true"); defaultMenuItem.setAttribute("disabled", "true");
} else { } else {
defaultMenuItem.removeAttribute("disabled"); defaultMenuItem.removeAttribute("disabled");
} }
let openMenuItem = document.getElementById("context_zenOpenWorkspace"); let openMenuItem = document.getElementById("context_zenOpenWorkspace");
if (workspaces.workspaces.find(workspace => workspace.uuid === this._contextMenuId).used) { if (
workspaces.workspaces.find(
(workspace) => workspace.uuid === this._contextMenuId,
).used
) {
openMenuItem.setAttribute("disabled", "true"); openMenuItem.setAttribute("disabled", "true");
} else { } else {
openMenuItem.removeAttribute("disabled"); openMenuItem.removeAttribute("disabled");
@ -546,7 +678,9 @@ var ZenWorkspaces = {
}, },
onContextMenuClose() { onContextMenuClose() {
let target = document.querySelector(`#PanelUI-zen-workspaces [zen-workspace-id="${this._contextMenuId}"] .zen-workspace-actions`); let target = document.querySelector(
`#PanelUI-zen-workspaces [zen-workspace-id="${this._contextMenuId}"] .zen-workspace-actions`,
);
if (target) { if (target) {
target.removeAttribute("active"); target.removeAttribute("active");
} }
@ -564,7 +698,9 @@ var ZenWorkspaces = {
async openWorkspace() { async openWorkspace() {
let workspaces = await this._workspaces(); let workspaces = await this._workspaces();
let workspace = workspaces.workspaces.find(workspace => workspace.uuid === this._contextMenuId); let workspace = workspaces.workspaces.find(
(workspace) => workspace.uuid === this._contextMenuId,
);
await this.changeWorkspace(workspace); await this.changeWorkspace(workspace);
}, },
@ -583,48 +719,43 @@ var ZenWorkspaces = {
async changeWorkspaceShortcut() { async changeWorkspaceShortcut() {
// Cycle through workspaces // Cycle through workspaces
let workspaces = await this._workspaces(); let workspaces = await this._workspaces();
let activeWorkspace = workspaces.workspaces.find(workspace => workspace.used); let activeWorkspace = workspaces.workspaces.find(
(workspace) => workspace.used,
);
let workspaceIndex = workspaces.workspaces.indexOf(activeWorkspace); let workspaceIndex = workspaces.workspaces.indexOf(activeWorkspace);
let nextWorkspace = workspaces.workspaces[workspaceIndex + 1] || workspaces.workspaces[0]; let nextWorkspace =
workspaces.workspaces[workspaceIndex + 1] || workspaces.workspaces[0];
this.changeWorkspace(nextWorkspace); this.changeWorkspace(nextWorkspace);
}, },
_initializeWorkspaceTabContextMenus() { _initializeWorkspaceTabContextMenus() {
const contextMenu = document.getElementById("tabContextMenu"); const menu = document.createXULElement("menu");
const element = window.MozXULElement.parseXULToFragment(` menu.setAttribute("id", "context-zen-change-workspace-tab");
<menuseparator/> menu.setAttribute("data-l10n-id", "context-zen-change-workspace-tab");
<menu id="context-zen-change-workspace-tab" data-l10n-id="context-zen-change-workspace-tab">
<menupopup oncommand="ZenWorkspaces.changeTabWorkspace(event.target.getAttribute('zen-workspace-id'))">
</menupopup>
</menu>
`);
document.getElementById("context_closeDuplicateTabs").after(element);
contextMenu.addEventListener("popupshowing", async (event) => { const menuPopup = document.createXULElement("menupopup");
const menu = document.getElementById("context-zen-change-workspace-tab").querySelector("menupopup"); menuPopup.setAttribute("id", "context-zen-change-workspace-tab-menu-popup");
menu.innerHTML = ""; menuPopup.setAttribute(
const workspaces = await this._workspaces(); "oncommand",
const activeWorkspace = workspaces.workspaces.find(workspace => workspace.used); "ZenWorkspaces.changeTabWorkspace(event.target.getAttribute('zen-workspace-id'))",
for (let workspace of workspaces.workspaces) { );
const menuItem = window.MozXULElement.parseXULToFragment(`
<menuitem label="${gZenUIManager.createValidXULText(workspace.name)}" zen-workspace-id="${workspace.uuid}" /> menu.appendChild(menuPopup);
`);
if (workspace.uuid === activeWorkspace.uuid) { document.getElementById("context_closeDuplicateTabs").after(menu);
menuItem.querySelector("menuitem").setAttribute("disabled", "true");
}
menu.appendChild(menuItem);
}
});
}, },
async changeTabWorkspace(workspaceID) { async changeTabWorkspace(workspaceID) {
const tabs = TabContextMenu.contextTab.multiselected const tabs = TabContextMenu.contextTab.multiselected
? gBrowser.selectedTabs : [TabContextMenu.contextTab]; ? gBrowser.selectedTabs
: [TabContextMenu.contextTab];
for (let tab of tabs) { for (let tab of tabs) {
tab.setAttribute("zen-workspace-id", workspaceID); tab.setAttribute("zen-workspace-id", workspaceID);
} }
const workspaces = await this._workspaces(); const workspaces = await this._workspaces();
await this.changeWorkspace(workspaces.workspaces.find(workspace => workspace.uuid === workspaceID)); await this.changeWorkspace(
workspaces.workspaces.find((workspace) => workspace.uuid === workspaceID),
);
}, },
}; };