From 2ad537e4c660b2b5b5facde276733051c655d1fd Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 9 Nov 2016 22:49:09 -0500 Subject: [PATCH] Fix console error trying to add toolbar icon more than once This was appearing a lot in tests. --- chrome/content/zotero/icon.js | 326 +++++++++++++++++----------------- 1 file changed, 166 insertions(+), 160 deletions(-) diff --git a/chrome/content/zotero/icon.js b/chrome/content/zotero/icon.js index 34f427ca4..5c438b3b2 100644 --- a/chrome/content/zotero/icon.js +++ b/chrome/content/zotero/icon.js @@ -28,180 +28,186 @@ Components.utils.import("resource://zotero/config.js"); Components.utils.import("resource:///modules/CustomizableUI.jsm"); -// Necessary for connector mode, for some reason var Zotero = Components.classes["@zotero.org/Zotero;1"] .getService(Components.interfaces.nsISupports) .wrappedJSObject; - var comboButtonsID = 'zotero-toolbar-buttons'; +addIcon(); -CustomizableUI.addListener({ - onWidgetAdded: function (id, area, position) { - if (id == comboButtonsID) { - // When dropping combo button into panel, add two independent buttons instead - if (area == CustomizableUI.AREA_PANEL) { - let mainID = getSingleID('main'); - let saveID = getSingleID('save'); - CustomizableUI.removeWidgetFromArea(id); - // Remove independent main and save buttons first if they're already in panel - CustomizableUI.removeWidgetFromArea(mainID); - CustomizableUI.removeWidgetFromArea(saveID); - CustomizableUI.addWidgetToArea(mainID, area, position); - let placement = CustomizableUI.getPlacementOfWidget(mainID) - let mainPos = placement.position; - CustomizableUI.addWidgetToArea(saveID, area, mainPos + 1); - return; - } - - var isUpgrade = false; - try { - isUpgrade = Zotero.Prefs.get("firstRunGuidanceShown.saveIcon"); - } catch(e) {} - var property = "firstRunGuidance.toolbarButton." + (isUpgrade ? "upgrade" : "new"); - var shortcut = Zotero.getString( - Zotero.isMac ? "general.keys.cmdShift" : "general.keys.ctrlShift" - ) + Zotero.Prefs.get("keys.openZotero"); - - let widget = CustomizableUI.getWidget(id); - for (let instance of widget.instances) { - let doc = instance.node.ownerDocument; +function addIcon() { + if (Zotero.toolbarIconAdded) { + return; + } + + CustomizableUI.addListener({ + onWidgetAdded: function (id, area, position) { + if (id == comboButtonsID) { + // When dropping combo button into panel, add two independent buttons instead + if (area == CustomizableUI.AREA_PANEL) { + let mainID = getSingleID('main'); + let saveID = getSingleID('save'); + CustomizableUI.removeWidgetFromArea(id); + // Remove independent main and save buttons first if they're already in panel + CustomizableUI.removeWidgetFromArea(mainID); + CustomizableUI.removeWidgetFromArea(saveID); + CustomizableUI.addWidgetToArea(mainID, area, position); + let placement = CustomizableUI.getPlacementOfWidget(mainID) + let mainPos = placement.position; + CustomizableUI.addWidgetToArea(saveID, area, mainPos + 1); + return; + } - updateItemForArea(instance.node, area); + var isUpgrade = false; + try { + isUpgrade = Zotero.Prefs.get("firstRunGuidanceShown.saveIcon"); + } catch(e) {} + var property = "firstRunGuidance.toolbarButton." + (isUpgrade ? "upgrade" : "new"); + var shortcut = Zotero.getString( + Zotero.isMac ? "general.keys.cmdShift" : "general.keys.ctrlShift" + ) + Zotero.Prefs.get("keys.openZotero"); - doc.getElementById("zotero-main-button-guidance").show({ - text: Zotero.getString(property, shortcut) - }); - doc.getElementById("zotero-save-button-guidance").show(); - } - } - else if (id == getSingleID('save')) { - let widget = CustomizableUI.getWidget(id); - for (let instance of widget.instances) { - instance.node.ownerDocument.defaultView.Zotero_Browser.updateStatus(); - } - } - }, - - onWidgetOverflow: function (node, container) { - if (node.id == comboButtonsID) { - node.classList.add("toolbarbutton-1"); - } - }, - - onWidgetUnderflow: function (node, container) { - if (node.id == comboButtonsID) { - node.classList.remove("toolbarbutton-1"); - } - }, - - // Save icon in panel isn't in DOM until menu is shown once and therefore isn't updated - // on page loads, so update the icon status when the panel is first shown so that it - // doesn't remain disabled - onAreaNodeRegistered: function (area, node) { - if (area == CustomizableUI.AREA_PANEL) { - var placement = CustomizableUI.getPlacementOfWidget(comboButtonsID) - var update = false; - let singleID = getSingleID('save'); - if (placement && placement.area == CustomizableUI.AREA_PANEL) { - update = true; - } - else { - placement = CustomizableUI.getPlacementOfWidget(singleID); - if (placement && placement.area == CustomizableUI.AREA_PANEL) { - update = true; + let widget = CustomizableUI.getWidget(id); + for (let instance of widget.instances) { + let doc = instance.node.ownerDocument; + + updateItemForArea(instance.node, area); + + doc.getElementById("zotero-main-button-guidance").show({ + text: Zotero.getString(property, shortcut) + }); + doc.getElementById("zotero-save-button-guidance").show(); } } - if (update) { - let widget = CustomizableUI.getWidget(singleID); + else if (id == getSingleID('save')) { + let widget = CustomizableUI.getWidget(id); for (let instance of widget.instances) { instance.node.ownerDocument.defaultView.Zotero_Browser.updateStatus(); } } + }, + + onWidgetOverflow: function (node, container) { + if (node.id == comboButtonsID) { + node.classList.add("toolbarbutton-1"); + } + }, + + onWidgetUnderflow: function (node, container) { + if (node.id == comboButtonsID) { + node.classList.remove("toolbarbutton-1"); + } + }, + + // Save icon in panel isn't in DOM until menu is shown once and therefore isn't updated + // on page loads, so update the icon status when the panel is first shown so that it + // doesn't remain disabled + onAreaNodeRegistered: function (area, node) { + if (area == CustomizableUI.AREA_PANEL) { + var placement = CustomizableUI.getPlacementOfWidget(comboButtonsID) + var update = false; + let singleID = getSingleID('save'); + if (placement && placement.area == CustomizableUI.AREA_PANEL) { + update = true; + } + else { + placement = CustomizableUI.getPlacementOfWidget(singleID); + if (placement && placement.area == CustomizableUI.AREA_PANEL) { + update = true; + } + } + if (update) { + let widget = CustomizableUI.getWidget(singleID); + for (let instance of widget.instances) { + instance.node.ownerDocument.defaultView.Zotero_Browser.updateStatus(); + } + } + } } - } -}) - -// Create the combo buttons, which go in the toolbar by default -CustomizableUI.createWidget({ - id: comboButtonsID, - type: 'custom', - label: ZOTERO_CONFIG.CLIENT_NAME, - tooltiptext: ZOTERO_CONFIG.CLIENT_NAME, - defaultArea: CustomizableUI.AREA_NAVBAR, - onBuild: function (document) { - const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; - - var item = document.createElementNS(kNSXUL, "toolbaritem"); - item.setAttribute("id", comboButtonsID); - item.setAttribute("label", "Zotero (Combo)"); // TODO: localize - // Set this as an attribute in addition to the property to make sure we can style correctly. - item.setAttribute("removable", "true"); - item.classList.add("chromeclass-toolbar-additional"); - - ['main', 'save'].map(button => { - return { - name: button, - id: getID(button), - tooltiptext: getTooltipText(button), - oncommand: getCommand(button) - }; - }).forEach(function(attribs, index) { - if (index != 0) { - item.appendChild(document.createElementNS(kNSXUL, "separator")); - } - let button = document.createElementNS(kNSXUL, "toolbarbutton"); - if (attribs.name == 'main') { - button.setAttribute('label', Zotero.clientName); - } - else if (attribs.name == 'save') { - button.setAttribute('label', Zotero.getString('ingester.saveToZotero')); - button.setAttribute('disabled', 'true'); - button.setAttribute('type', 'menu-button'); - let menupopup = document.createElementNS(kNSXUL, "menupopup"); - menupopup.setAttribute('onpopupshowing', "Zotero_Browser.onStatusPopupShowing(event)"); - button.appendChild(menupopup); - } - delete attribs.name; - setAttributes(button, attribs); - item.appendChild(button); - }); - - updateItemForArea(item, this.currentArea) - - return item; - } -}); - -// Create the independent Z button, which isn't shown by default -CustomizableUI.createWidget({ - id: getSingleID('main'), - label: Zotero.clientName, - tooltiptext: getTooltipText('main'), - defaultArea: false, - onCommand: function (event) { - event.target.ownerDocument.defaultView.ZoteroOverlay.toggleDisplay(); - } -}); - -// Create the independent save button, which isn't shown by default -CustomizableUI.createWidget({ - id: getSingleID('save'), - label: Zotero.getString('ingester.saveToZotero'), - tooltiptext: getTooltipText('save'), - defaultArea: false, - onCommand: function (event) { - event.target.ownerDocument.defaultView.Zotero_Browser.scrapeThisPage(null, event); - }, - onCreated: function (button) { - const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; - button.setAttribute('disabled', 'true'); - button.setAttribute('type', 'menu-button'); - let menupopup = document.createElementNS(kNSXUL, "menupopup"); - menupopup.setAttribute('onpopupshowing', "Zotero_Browser.onStatusPopupShowing(event)"); - button.appendChild(menupopup); - } -}); - + }) + + // Create the combo buttons, which go in the toolbar by default + CustomizableUI.createWidget({ + id: comboButtonsID, + type: 'custom', + label: ZOTERO_CONFIG.CLIENT_NAME, + tooltiptext: ZOTERO_CONFIG.CLIENT_NAME, + defaultArea: CustomizableUI.AREA_NAVBAR, + onBuild: function (document) { + const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + + var item = document.createElementNS(kNSXUL, "toolbaritem"); + item.setAttribute("id", comboButtonsID); + item.setAttribute("label", "Zotero (Combo)"); // TODO: localize + // Set this as an attribute in addition to the property to make sure we can style correctly. + item.setAttribute("removable", "true"); + item.classList.add("chromeclass-toolbar-additional"); + + ['main', 'save'].map(button => { + return { + name: button, + id: getID(button), + tooltiptext: getTooltipText(button), + oncommand: getCommand(button) + }; + }).forEach(function(attribs, index) { + if (index != 0) { + item.appendChild(document.createElementNS(kNSXUL, "separator")); + } + let button = document.createElementNS(kNSXUL, "toolbarbutton"); + if (attribs.name == 'main') { + button.setAttribute('label', Zotero.clientName); + } + else if (attribs.name == 'save') { + button.setAttribute('label', Zotero.getString('ingester.saveToZotero')); + button.setAttribute('disabled', 'true'); + button.setAttribute('type', 'menu-button'); + let menupopup = document.createElementNS(kNSXUL, "menupopup"); + menupopup.setAttribute('onpopupshowing', "Zotero_Browser.onStatusPopupShowing(event)"); + button.appendChild(menupopup); + } + delete attribs.name; + setAttributes(button, attribs); + item.appendChild(button); + }); + + updateItemForArea(item, this.currentArea) + + return item; + } + }); + + // Create the independent Z button, which isn't shown by default + CustomizableUI.createWidget({ + id: getSingleID('main'), + label: Zotero.clientName, + tooltiptext: getTooltipText('main'), + defaultArea: false, + onCommand: function (event) { + event.target.ownerDocument.defaultView.ZoteroOverlay.toggleDisplay(); + } + }); + + // Create the independent save button, which isn't shown by default + CustomizableUI.createWidget({ + id: getSingleID('save'), + label: Zotero.getString('ingester.saveToZotero'), + tooltiptext: getTooltipText('save'), + defaultArea: false, + onCommand: function (event) { + event.target.ownerDocument.defaultView.Zotero_Browser.scrapeThisPage(null, event); + }, + onCreated: function (button) { + const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; + button.setAttribute('disabled', 'true'); + button.setAttribute('type', 'menu-button'); + let menupopup = document.createElementNS(kNSXUL, "menupopup"); + menupopup.setAttribute('onpopupshowing', "Zotero_Browser.onStatusPopupShowing(event)"); + button.appendChild(menupopup); + } + }); + + Zotero.toolbarIconAdded = true; +} function getID(button) { switch (button) {