www

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

commit 045f1fbb7e86eb6ee1e9790892687a18ab8f868a
parent 36436d0b438226f3419f5bd27c2c2f1a979db7ce
Author: Dan Stillman <dstillman@zotero.org>
Date:   Fri,  8 Dec 2017 00:01:24 -0500

Add utility functions for building drop-down library lists

A XUL one for the current use in Advanced Search and an HTML one for
future uses. Sets the value to libraryID and adds data attributes for
editable/filesEditable on the HTML one.

Diffstat:
Mchrome/content/zotero/bindings/zoterosearch.xml | 50+++++++++++---------------------------------------
Mchrome/content/zotero/xpcom/utilities_internal.js | 47++++++++++++++++++++++++++++++++++++++++++++++-
Mtest/tests/advancedSearchTest.js | 2+-
3 files changed, 58 insertions(+), 41 deletions(-)

diff --git a/chrome/content/zotero/bindings/zoterosearch.xml b/chrome/content/zotero/bindings/zoterosearch.xml @@ -49,7 +49,16 @@ <![CDATA[ this.searchRef = val; - this.buildLibraryMenu(); + var libraryMenu = this.id('libraryMenu'); + var libraries = Zotero.Libraries.getAll(); + Zotero.Utilities.Internal.buildLibraryMenu( + libraryMenu, libraries, this.searchRef.libraryID + ); + if (this.searchRef.id) { + libraryMenu.disabled = true; + } + this.updateLibrary(); + var conditionsBox = this.id('conditions'); while(conditionsBox.hasChildNodes()) @@ -81,43 +90,6 @@ </setter> </property> - <method name="buildLibraryMenu"> - <body><![CDATA[ - var menulist = this.id('libraryMenu'); - var menupopup = menulist.firstChild; - - while (menupopup.hasChildNodes()) { - menupopup.removeChild(menupopup.firstChild); - } - - var libraryID = this.searchRef.libraryID; - var libraryIndex = 0; - - var libraries = Zotero.Libraries.getAll(); - var selectedIndex = 0; - var i = 0; - for (let library of libraries) { - let menuitem = document.createElement('menuitem'); - menuitem.setAttribute('label', library.name); - menuitem.setAttribute('libraryID', library.libraryID); - menupopup.appendChild(menuitem); - if (library.libraryID == libraryID) { - selectedIndex = i; - } - i++ - } - - menulist.appendChild(menupopup); - menulist.selectedIndex = selectedIndex; - - if (this.searchRef.id) { - this.id('libraryMenu').disabled = true; - } - - this.updateLibrary(); - ]]></body> - </method> - <method name="addCondition"> <parameter name="ref"/> <body> @@ -170,7 +142,7 @@ <method name="updateLibrary"> <body><![CDATA[ var menu = this.id('libraryMenu'); - var libraryID = parseInt(menu.selectedItem.getAttribute('libraryID')); + var libraryID = parseInt(menu.selectedItem.value); if (this.onLibraryChange) { this.onLibraryChange(libraryID); diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js @@ -986,7 +986,52 @@ Zotero.Utilities.Internal = { return parts.join('-'); }, - + + buildLibraryMenu: function (menulist, libraries, selectedLibraryID) { + var menupopup = menulist.firstChild; + while (menupopup.hasChildNodes()) { + menupopup.removeChild(menupopup.firstChild); + } + var selectedIndex = 0; + var i = 0; + for (let library of libraries) { + let menuitem = menulist.ownerDocument.createElement('menuitem'); + menuitem.value = library.libraryID; + menuitem.setAttribute('label', library.name); + menupopup.appendChild(menuitem); + if (library.libraryID == selectedLibraryID) { + selectedIndex = i; + } + i++; + } + + menulist.appendChild(menupopup); + menulist.selectedIndex = selectedIndex; + }, + + + buildLibraryMenuHTML: function (select, libraries, selectedLibraryID) { + var namespaceURI = 'http://www.w3.org/1999/xhtml'; + while (select.hasChildNodes()) { + select.removeChild(select.firstChild); + } + var selectedIndex = 0; + var i = 0; + for (let library of libraries) { + let option = select.ownerDocument.createElementNS(namespaceURI, 'option'); + option.setAttribute('value', library.libraryID); + option.setAttribute('data-editable', library.editable ? 'true' : 'false'); + option.setAttribute('data-filesEditable', library.filesEditable ? 'true' : 'false'); + option.textContent = library.name; + select.appendChild(option); + if (library.libraryID == selectedLibraryID) { + option.setAttribute('selected', 'selected'); + } + i++; + } + }, + + /** * Create a libraryOrCollection DOM tree to place in <menupopup> element. * If has no children, returns a <menuitem> element, otherwise <menu>. diff --git a/test/tests/advancedSearchTest.js b/test/tests/advancedSearchTest.js @@ -202,7 +202,7 @@ describe("Advanced Search", function () { for (let i = 0; i < libraryMenu.itemCount; i++) { let menuitem = libraryMenu.getItemAtIndex(i); // Switch to group library - if (menuitem.getAttribute('libraryID') == groupLibraryID) { + if (menuitem.value == groupLibraryID) { menuitem.click(); break; }