containers.js (4814B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* import-globals-from preferences.js */ 6 7 const defaultContainerIcon = "fingerprint"; 8 const defaultContainerColor = "blue"; 9 10 let gContainersPane = { 11 init() { 12 this._list = document.getElementById("containersView"); 13 14 document 15 .getElementById("backContainersButton") 16 .addEventListener("command", function () { 17 gotoPref("general"); 18 }); 19 20 document 21 .getElementById("containersAdd") 22 .addEventListener("command", function () { 23 gContainersPane.onAddButtonCommand(); 24 }); 25 26 this._rebuildView(); 27 }, 28 29 _rebuildView() { 30 const containers = ContextualIdentityService.getPublicIdentities(); 31 while (this._list.firstChild) { 32 this._list.firstChild.remove(); 33 } 34 for (let container of containers) { 35 let item = document.createXULElement("richlistitem"); 36 37 let outer = document.createXULElement("hbox"); 38 outer.setAttribute("flex", 1); 39 outer.setAttribute("align", "center"); 40 item.appendChild(outer); 41 42 let userContextIcon = document.createXULElement("hbox"); 43 userContextIcon.className = "userContext-icon"; 44 userContextIcon.classList.add("userContext-icon-inprefs"); 45 userContextIcon.classList.add("identity-icon-" + container.icon); 46 userContextIcon.classList.add("identity-color-" + container.color); 47 outer.appendChild(userContextIcon); 48 49 let label = document.createXULElement("label"); 50 label.className = "userContext-label-inprefs"; 51 label.setAttribute("flex", 1); 52 let containerName = ContextualIdentityService.getUserContextLabel( 53 container.userContextId 54 ); 55 label.textContent = containerName; 56 label.setAttribute("tooltiptext", containerName); 57 outer.appendChild(label); 58 59 let containerButtons = document.createXULElement("hbox"); 60 containerButtons.className = "container-buttons"; 61 item.appendChild(containerButtons); 62 63 let prefsButton = document.createXULElement("button"); 64 prefsButton.addEventListener("command", function (event) { 65 gContainersPane.onPreferenceCommand(event.originalTarget); 66 }); 67 prefsButton.setAttribute("value", container.userContextId); 68 document.l10n.setAttributes(prefsButton, "containers-settings-button"); 69 containerButtons.appendChild(prefsButton); 70 71 let removeButton = document.createXULElement("button"); 72 removeButton.addEventListener("command", function (event) { 73 gContainersPane.onRemoveCommand(event.originalTarget); 74 }); 75 removeButton.setAttribute("value", container.userContextId); 76 document.l10n.setAttributes(removeButton, "containers-remove-button"); 77 containerButtons.appendChild(removeButton); 78 79 this._list.appendChild(item); 80 } 81 }, 82 83 async onRemoveCommand(button) { 84 let userContextId = parseInt(button.getAttribute("value"), 10); 85 86 let count = ContextualIdentityService.countContainerTabs(userContextId); 87 if (count > 0) { 88 let [title, message, okButton, cancelButton] = 89 await document.l10n.formatValues([ 90 { id: "containers-remove-alert-title" }, 91 { id: "containers-remove-alert-msg", args: { count } }, 92 { id: "containers-remove-ok-button" }, 93 { id: "containers-remove-cancel-button" }, 94 ]); 95 96 let buttonFlags = 97 Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_0 + 98 Ci.nsIPrompt.BUTTON_TITLE_IS_STRING * Ci.nsIPrompt.BUTTON_POS_1; 99 100 let rv = Services.prompt.confirmEx( 101 window, 102 title, 103 message, 104 buttonFlags, 105 okButton, 106 cancelButton, 107 null, 108 null, 109 {} 110 ); 111 if (rv != 0) { 112 return; 113 } 114 115 await ContextualIdentityService.closeContainerTabs(userContextId); 116 } 117 118 ContextualIdentityService.remove(userContextId); 119 this._rebuildView(); 120 }, 121 122 onPreferenceCommand(button) { 123 this.openPreferenceDialog(button.getAttribute("value")); 124 }, 125 126 onAddButtonCommand() { 127 this.openPreferenceDialog(null); 128 }, 129 130 openPreferenceDialog(userContextId) { 131 let identity = { 132 name: "", 133 icon: defaultContainerIcon, 134 color: defaultContainerColor, 135 }; 136 if (userContextId) { 137 identity = 138 ContextualIdentityService.getPublicIdentityFromId(userContextId); 139 identity.name = ContextualIdentityService.getUserContextLabel( 140 identity.userContextId 141 ); 142 } 143 144 const params = { userContextId, identity }; 145 gSubDialog.open( 146 "chrome://browser/content/preferences/dialogs/containers.xhtml", 147 undefined, 148 params 149 ); 150 }, 151 };