applicationManager.js (4335B)
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/. 4 5 /* import-globals-from ../main.js */ 6 7 var gAppManagerDialog = { 8 _removed: [], 9 10 onLoad() { 11 document.mozSubdialogReady = this.init(); 12 }, 13 14 async init() { 15 this.handlerInfo = window.arguments[0]; 16 17 document.addEventListener("dialogaccept", function () { 18 gAppManagerDialog.onOK(); 19 }); 20 21 let gMainPane = window.parent.gMainPane; 22 23 document 24 .getElementById("cmd_remove") 25 .addEventListener("command", () => this.remove()); 26 27 const appDescElem = document.getElementById("appDescription"); 28 if (this.handlerInfo.wrappedHandlerInfo instanceof Ci.nsIMIMEInfo) { 29 let { typeDescription } = this.handlerInfo; 30 let typeStr; 31 if (typeDescription.id) { 32 MozXULElement.insertFTLIfNeeded("browser/preferences/preferences.ftl"); 33 typeStr = await document.l10n.formatValue( 34 typeDescription.id, 35 typeDescription.args 36 ); 37 } else { 38 typeStr = typeDescription.raw; 39 } 40 document.l10n.setAttributes(appDescElem, "app-manager-handle-file", { 41 type: typeStr, 42 }); 43 } else { 44 document.l10n.setAttributes(appDescElem, "app-manager-handle-protocol", { 45 type: this.handlerInfo.typeDescription.raw, 46 }); 47 } 48 49 let list = document.getElementById("appList"); 50 list.addEventListener("select", () => this.onSelect()); 51 let listFragment = document.createDocumentFragment(); 52 for (let app of this.handlerInfo.possibleApplicationHandlers.enumerate()) { 53 if (!gMainPane.isValidHandlerApp(app)) { 54 continue; 55 } 56 57 let item = document.createXULElement("richlistitem"); 58 listFragment.append(item); 59 item.app = app; 60 61 let image = document.createXULElement("image"); 62 image.setAttribute("src", gMainPane._getIconURLForHandlerApp(app)); 63 item.appendChild(image); 64 65 let label = document.createXULElement("label"); 66 label.setAttribute("value", app.name); 67 item.appendChild(label); 68 } 69 list.append(listFragment); 70 71 // Triggers onSelect which populates label 72 list.selectedIndex = 0; 73 74 // We want to block on those elements being localized because the 75 // result will impact the size of the subdialog. 76 await document.l10n.translateElements([ 77 appDescElem, 78 document.getElementById("appType"), 79 ]); 80 }, 81 82 onOK: function appManager_onOK() { 83 if (this._removed.length) { 84 for (var i = 0; i < this._removed.length; ++i) { 85 this.handlerInfo.removePossibleApplicationHandler(this._removed[i]); 86 } 87 88 this.handlerInfo.store(); 89 } 90 }, 91 92 remove: function appManager_remove() { 93 var list = document.getElementById("appList"); 94 this._removed.push(list.selectedItem.app); 95 var index = list.selectedIndex; 96 var element = list.selectedItem; 97 list.removeItemFromSelection(element); 98 element.remove(); 99 if (list.itemCount == 0) { 100 // The list is now empty, make the bottom part disappear 101 document.getElementById("appDetails").hidden = true; 102 } else { 103 // Select the item at the same index, if we removed the last 104 // item of the list, select the previous item 105 if (index == list.itemCount) { 106 --index; 107 } 108 list.selectedIndex = index; 109 } 110 }, 111 112 onSelect: function appManager_onSelect() { 113 var list = document.getElementById("appList"); 114 if (!list.selectedItem) { 115 document.getElementById("remove").disabled = true; 116 return; 117 } 118 document.getElementById("remove").disabled = false; 119 var app = list.selectedItem.app; 120 var address = ""; 121 if (app instanceof Ci.nsILocalHandlerApp) { 122 address = app.executable.path; 123 } else if (app instanceof Ci.nsIWebHandlerApp) { 124 address = app.uriTemplate; 125 } 126 document.getElementById("appLocation").value = address; 127 const l10nId = 128 app instanceof Ci.nsILocalHandlerApp 129 ? "app-manager-local-app-info" 130 : "app-manager-web-app-info"; 131 const appTypeElem = document.getElementById("appType"); 132 document.l10n.setAttributes(appTypeElem, l10nId); 133 }, 134 }; 135 136 window.addEventListener("load", () => gAppManagerDialog.onLoad());