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.
This commit is contained in:
Dan Stillman 2017-12-08 00:01:24 -05:00
parent 36436d0b43
commit 045f1fbb7e
3 changed files with 58 additions and 41 deletions

View file

@ -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);

View file

@ -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>.

View file

@ -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;
}