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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const { ContextualIdentityService } = ChromeUtils.importESModule(
6 "resource://gre/modules/ContextualIdentityService.sys.mjs"
10 * We want to set the window title immediately to prevent flickers.
13 let params = window.arguments[0] || {};
15 let winElem = document.documentElement;
16 if (params.userContextId) {
17 document.l10n.setAttributes(winElem, "containers-window-update-settings2", {
18 name: params.identity.name,
21 document.l10n.setAttributes(winElem, "containers-window-new2");
26 let gContainersManager = {
56 let params = window.arguments[0] || {};
61 this._dialog = document.querySelector("dialog");
62 this.userContextId = aParams.userContextId || null;
63 this.identity = aParams.identity;
65 const iconWrapper = document.getElementById("iconWrapper");
66 iconWrapper.appendChild(this.createIconButtons());
68 const colorWrapper = document.getElementById("colorWrapper");
69 colorWrapper.appendChild(this.createColorSwatches());
71 if (this.identity.name) {
72 const name = document.getElementById("name");
73 name.value = this.identity.name;
77 document.addEventListener("dialogaccept", () => this.onApplyChanges());
79 // This is to prevent layout jank caused by the svgs and outlines rendering at different times
80 document.getElementById("containers-content").removeAttribute("hidden");
85 // Check if name is provided to determine if the form can be submitted
87 const name = document.getElementById("name");
88 this._dialog.setAttribute("buttondisabledaccept", !name.value.trim());
91 createIconButtons(defaultIcon) {
92 let radiogroup = document.createXULElement("radiogroup");
93 radiogroup.setAttribute("id", "icon");
94 radiogroup.className = "icon-buttons radio-buttons";
96 for (let icon of this.icons) {
97 let iconSwatch = document.createXULElement("radio");
98 iconSwatch.id = "iconbutton-" + icon;
99 iconSwatch.name = "icon";
100 iconSwatch.type = "radio";
101 iconSwatch.value = icon;
103 if (this.identity.icon && this.identity.icon == icon) {
104 iconSwatch.setAttribute("selected", true);
107 document.l10n.setAttributes(iconSwatch, `containers-icon-${icon}`);
108 let iconElement = document.createXULElement("hbox");
109 iconElement.className = "userContext-icon";
110 iconElement.classList.add("identity-icon-" + icon);
112 iconSwatch.appendChild(iconElement);
113 radiogroup.appendChild(iconSwatch);
119 createColorSwatches(defaultColor) {
120 let radiogroup = document.createXULElement("radiogroup");
121 radiogroup.setAttribute("id", "color");
122 radiogroup.className = "radio-buttons";
124 for (let color of this.colors) {
125 let colorSwatch = document.createXULElement("radio");
126 colorSwatch.id = "colorswatch-" + color;
127 colorSwatch.name = "color";
128 colorSwatch.type = "radio";
129 colorSwatch.value = color;
131 if (this.identity.color && this.identity.color == color) {
132 colorSwatch.setAttribute("selected", true);
135 document.l10n.setAttributes(colorSwatch, `containers-color-${color}`);
136 let iconElement = document.createXULElement("hbox");
137 iconElement.className = "userContext-icon";
138 iconElement.classList.add("identity-icon-circle");
139 iconElement.classList.add("identity-color-" + color);
141 colorSwatch.appendChild(iconElement);
142 radiogroup.appendChild(colorSwatch);
148 let icon = document.getElementById("icon").value;
149 let color = document.getElementById("color").value;
150 let name = document.getElementById("name").value;
152 if (!this.icons.includes(icon)) {
153 throw new Error("Internal error. The icon value doesn't match.");
156 if (!this.colors.includes(color)) {
157 throw new Error("Internal error. The color value doesn't match.");
160 if (this.userContextId) {
161 ContextualIdentityService.update(this.userContextId, name, icon, color);
163 ContextualIdentityService.create(name, icon, color);
165 window.parent.location.reload();