www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 4bfbaf5747409e6d2dd1108570962f896a015d57
parent 894c9b1fb29b4cf2f4a4ed78eac4d24bc44424ec
Author: Simon Kornblith <simon@simonster.com>
Date:   Tue, 22 Feb 2011 00:02:05 +0000

closes #1791, add locate options to context menu


Diffstat:
Mchrome/content/zotero/locateMenu.js | 174+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mchrome/content/zotero/zoteroPane.js | 3+++
2 files changed, 129 insertions(+), 48 deletions(-)

diff --git a/chrome/content/zotero/locateMenu.js b/chrome/content/zotero/locateMenu.js @@ -45,58 +45,13 @@ var Zotero_LocateMenu = new function() { var selectedItems = [item for each(item in ZoteroPane.getSelectedItems()) if(!item.isNote())]; if(selectedItems.length) { - var optionsToShow = {}; - - // check which view options are available - for each(var item in selectedItems) { - for(var viewOption in ViewOptions) { - if(!optionsToShow[viewOption]) { - optionsToShow[viewOption] = ViewOptions[viewOption].canHandleItem(item); - } - } - } - - // add available view options to menu - for(var viewOption in optionsToShow) { - if(!optionsToShow[viewOption]) continue; - - var menuitem = _createMenuItem(Zotero.getString("locate."+viewOption+".label"), - null, Zotero.getString("locate."+viewOption+".tooltip")); - menuitem.setAttribute("class", "menuitem-iconic"); - menuitem.setAttribute("image", ViewOptions[viewOption].icon); - locateMenu.appendChild(menuitem); - - let myViewOption = viewOption; - menuitem.addEventListener("command", function(event) { - ViewOptions[myViewOption].handleItems(selectedItems, event); - }, false) - } - - // check for custom locate engines - var customEngines = Zotero.LocateManager.getVisibleEngines(); - var availableEngines = []; - - // check which engines can translate an item - for each(var engine in customEngines) { - // require a submission for at least one selected item - for each(var item in selectedItems) { - if(engine.getItemSubmission(item)) { - availableEngines.push(engine); - break; - } - } - } + _addViewOptions(locateMenu, selectedItems, true); + var availableEngines = _getAvailableLocateEngines(selectedItems); // add engines that are available for selected items if(availableEngines.length) { locateMenu.appendChild(document.createElement("menuseparator")); - for each(var engine in availableEngines) { - var menuitem = _createMenuItem(engine.name, null, engine.description); - menuitem.setAttribute("class", "menuitem-iconic"); - menuitem.setAttribute("image", engine.icon); - locateMenu.appendChild(menuitem); - menuitem.addEventListener("command", _locateItem, false); - } + _addLocateEngines(locateMenu, availableEngines, true); } } else { // add "no items selected" @@ -137,6 +92,129 @@ var Zotero_LocateMenu = new function() { } /** + * Clear the bottom part of the context menu and add locate options + * @param {menupopup} menu The menu to add context menu items to + */ + this.buildContextMenu = function(menu) { + // remove old menu items + while(menu.lastChild && menu.lastChild.getAttribute("zotero-locate")) { + menu.removeChild(menu.lastChild); + } + + // get selected items + var selectedItems = [item for each(item in ZoteroPane.getSelectedItems()) if(!item.isNote())]; + + // if no items selected, stop now + if(!selectedItems.length) return; + + // add view options + _addViewOptions(menu, selectedItems, false, true); + + /*// look for locate engines + var availableEngines = _getAvailableLocateEngines(selectedItems); + if(availableEngines.length) { + // if locate engines are available, make a new submenu + var submenu = document.createElement("menu"); + submenu.setAttribute("zotero-locate", "true"); + submenu.setAttribute("label", Zotero.getString("locate.locateEngines")); + + // add locate engines to the submenu + _addLocateEngines(submenuPopup, availableEngines, true); + + submenu.appendChild(submenuPopup); + menu.appendChild(submenu); + }*/ + } + + /** + * Add view options to a menu + * @param {menupopup} locateMenu The menu to add menu items to + * @param {Zotero.Item[]} selectedItems The items to create view options based upon + * @param {Boolean} showIcons Whether menu items should have associated icons + * @param {Boolean} addSeparator Whether a separator should be added before any menu items + */ + function _addViewOptions(locateMenu, selectedItems, showIcons, addSeparator) { + var optionsToShow = {}; + var haveItems = false; + + // check which view options are available + for each(var item in selectedItems) { + for(var viewOption in ViewOptions) { + if(!optionsToShow[viewOption]) { + optionsToShow[viewOption] = ViewOptions[viewOption].canHandleItem(item); + haveItems = true; + } + } + } + + if(haveItems && addSeparator) { + var sep = document.createElement("menuseparator"); + sep.setAttribute("zotero-locate", "true"); + locateMenu.appendChild(sep); + } + + // add available view options to menu + for(var viewOption in optionsToShow) { + if(!optionsToShow[viewOption]) continue; + + var menuitem = _createMenuItem(Zotero.getString("locate."+viewOption+".label"), + null, Zotero.getString("locate."+viewOption+".tooltip")); + if(showIcons) { + menuitem.setAttribute("class", "menuitem-iconic"); + menuitem.setAttribute("image", ViewOptions[viewOption].icon); + } + menuitem.setAttribute("zotero-locate", "true"); + locateMenu.appendChild(menuitem); + + let myViewOption = viewOption; + menuitem.addEventListener("command", function(event) { + ViewOptions[myViewOption].handleItems(selectedItems, event); + }, false) + } + } + + /** + * Get available locate engines that can handle a set of items + * @param {Zotero.Item[]} selectedItems The items to look or locate engines for + * @return {Zotero.LocateManater.LocateEngine[]} An array of locate engines capable of handling + * the given items + */ + function _getAvailableLocateEngines(selectedItems) { + // check for custom locate engines + var customEngines = Zotero.LocateManager.getVisibleEngines(); + var availableEngines = []; + + // check which engines can translate an item + for each(var engine in customEngines) { + // require a submission for at least one selected item + for each(var item in selectedItems) { + if(engine.getItemSubmission(item)) { + availableEngines.push(engine); + break; + } + } + } + + return availableEngines; + } + + /** + * Add locate engine options to a menu + * @param {menupopup} menu The menu to add menu items to + * @param {Zotero.LocateManater.LocateEngine[]} engines The list of engines to add to the menu + * @param {Boolean} showIcons Whether menu items should have associated icons + */ + function _addLocateEngines(menu, engines, showIcons) { + for each(var engine in engines) { + var menuitem = _createMenuItem(engine.name, null, engine.description); + menuitem.setAttribute("class", "menuitem-iconic"); + menuitem.setAttribute("image", engine.icon); + menu.appendChild(menuitem); + menuitem.addEventListener("command", _locateItem, false); + } + } + + /** * Create a new menuitem XUL element */ function _createMenuItem( label, id, tooltiptext ) { diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js @@ -2303,6 +2303,9 @@ var ZoteroPane = new function() { menu.childNodes[show[i]].setAttribute('hidden', false); } + + // add locate menu options + Zotero_LocateMenu.buildContextMenu(menu); }