From 8a4c8d2be050575002a5bc74d37b43a921aa44fa Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 14 Dec 2016 01:57:08 -0500 Subject: [PATCH] Classic citation dialog fixes - Fix loading for libraries that haven't yet been loaded (including if the Zotero pane hasn't yet been opened) - Show a loading message over the items tree - Fix item selection when editing citation (except if library data hasn't yet been loaded, but that doesn't happen in Standalone) --- .../zotero/integration/addCitationDialog.js | 38 +- .../zotero/integration/addCitationDialog.xul | 340 +++++++++--------- chrome/content/zotero/selectItemsDialog.js | 69 +++- 3 files changed, 243 insertions(+), 204 deletions(-) diff --git a/chrome/content/zotero/integration/addCitationDialog.js b/chrome/content/zotero/integration/addCitationDialog.js index f153b0e3c4..1aef608d4b 100644 --- a/chrome/content/zotero/integration/addCitationDialog.js +++ b/chrome/content/zotero/integration/addCitationDialog.js @@ -52,7 +52,6 @@ var Zotero_Citation_Dialog = new function () { var serial_number; var io; - this.load = load; this.toggleMultipleSources = toggleMultipleSources; this.toggleEditor = toggleEditor; this.treeItemSelected = treeItemSelected; @@ -70,7 +69,7 @@ var Zotero_Citation_Dialog = new function () { /* * initialize add citation dialog */ - function load() { + this.load = Zotero.Promise.coroutine(function* () { // make sure we are visible window.setTimeout(function() { var screenX = window.screenX; @@ -129,7 +128,7 @@ var Zotero_Citation_Dialog = new function () { menu.selectedIndex = 0; // load (from selectItemsDialog.js) - doLoad(); + yield doLoad(); // if we already have a citation, load data from it document.getElementById('editor').format = "RTF"; @@ -139,20 +138,12 @@ var Zotero_Citation_Dialog = new function () { toggleMultipleSources(false); _suppressNextTreeSelect = true; - // If we're in a different library, switch libraries + // DEBUG: When editing a citation before the library data has been loaded (i.e., in + // Firefox before the pane has been opened), this is the citation id, not the item id, + // and this fails. It works on subsequent attempts. Since this won't happen in + // Standalone, we can ignore. var id = io.citation.citationItems[0].id; - var collectionTreeRow = collectionsView.selectedTreeRow; - var item = Zotero.Items.get(id); - if(item.libraryID != collectionTreeRow.ref.libraryID) { - collectionsView.selectLibrary(item.libraryID); - } - var selected = itemsView.selectItem(id); - if(!selected) { - // If item wasn't found in current view, select library root - // and try again (in case we were in a collection of correct library) - collectionsView.selectLibrary(item.libraryID); - itemsView.selectItem(id); - } + var selected = yield itemsView.selectItem(id); for(var box in _preserveData) { var property = _preserveData[box][0]; @@ -209,7 +200,7 @@ var Zotero_Citation_Dialog = new function () { } else { toggleMultipleSources(false); } - } + }); /* * turn on/off multiple sources item list @@ -652,7 +643,7 @@ var Zotero_Citation_Dialog = new function () { /* * Controls whether the accept (OK) button should be enabled */ - function _updateAccept(status) { + function _updateAccept() { if(_multipleSourcesOn) { _acceptButton.disabled = !_citationList.getRowCount(); // To prevent accidental data loss, do not allow change to @@ -664,9 +655,14 @@ var Zotero_Citation_Dialog = new function () { _multipleSourceButton.disabled = false; } } else { - collectionsView.addEventListener('load', () => { - _acceptButton.disabled = !itemsView.getSelectedItems().length; - }); + collectionsView.addEventListener('load', Zotero.Promise.coroutine(function* () { + if (itemsView) { + var deferred = Zotero.Promise.defer(); + itemsView.addEventListener('load', () => deferred.resolve()); + yield deferred.promise; + _acceptButton.disabled = !itemsView.getSelectedItems().length; + } + })); } } diff --git a/chrome/content/zotero/integration/addCitationDialog.xul b/chrome/content/zotero/integration/addCitationDialog.xul index 7e4bab6d0c..980eb8093b 100644 --- a/chrome/content/zotero/integration/addCitationDialog.xul +++ b/chrome/content/zotero/integration/addCitationDialog.xul @@ -74,173 +74,179 @@ - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/chrome/content/zotero/selectItemsDialog.js b/chrome/content/zotero/selectItemsDialog.js index a8335a5c8c..f92a6dfcea 100644 --- a/chrome/content/zotero/selectItemsDialog.js +++ b/chrome/content/zotero/selectItemsDialog.js @@ -26,6 +26,7 @@ var itemsView; var collectionsView; var io; +var connectionSelectedDeferred; /* * window takes two arguments: @@ -42,8 +43,9 @@ var doLoad = Zotero.Promise.coroutine(function* () { if(io.addBorder) document.getElementsByTagName("dialog")[0].style.border = "1px solid black"; if(io.singleSelection) document.getElementById("zotero-items-tree").setAttribute("seltype", "single"); + setItemsPaneMessage(Zotero.getString('pane.items.loading')); + collectionsView = new Zotero.CollectionTreeView(); - // Don't show Commons when citing collectionsView.hideSources = ['duplicates', 'trash', 'feeds']; document.getElementById('zotero-collections-tree').view = collectionsView; @@ -51,7 +53,12 @@ var doLoad = Zotero.Promise.coroutine(function* () { collectionsView.addEventListener('load', () => deferred.resolve()); yield deferred.promise; - if(io.select) itemsView.selectItem(io.select); + connectionSelectedDeferred = Zotero.Promise.defer(); + yield connectionSelectedDeferred.promise; + + if (io.select) { + yield itemsView.selectItem(io.select); + } Zotero.updateQuickSearchBox(document); }); @@ -63,30 +70,37 @@ function doUnload() itemsView.unregister(); } -function onCollectionSelected() +var onCollectionSelected = Zotero.Promise.coroutine(function* () { if(itemsView) itemsView.unregister(); if(collectionsView.selection.count == 1 && collectionsView.selection.currentIndex != -1) { - var collection = collectionsView.getRow(collectionsView.selection.currentIndex); - collection.setSearch(''); + var collectionTreeRow = collectionsView.getRow(collectionsView.selection.currentIndex); + collectionTreeRow.setSearch(''); + Zotero.Prefs.set('lastViewedFolder', collectionTreeRow.id); - itemsView = new Zotero.ItemTreeView(collection, (window.arguments[1] ? true : false)); + // Load library data if necessary + var library = Zotero.Libraries.get(collectionTreeRow.ref.libraryID); + if (!library.getDataLoaded('item')) { + Zotero.debug("Waiting for items to load for library " + library.libraryID); + setItemsPaneMessage(Zotero.getString('pane.items.loading')); + yield library.waitForDataLoad('item'); + } + + // Create items list and wait for it to load + itemsView = new Zotero.ItemTreeView(collectionTreeRow, (window.arguments[1] ? true : false)); document.getElementById('zotero-items-tree').view = itemsView; + var deferred = Zotero.Promise.defer(); + itemsView.addEventListener('load', () => deferred.resolve()); + yield deferred.promise; - if (collection.isLibrary()) { - Zotero.Prefs.set('lastViewedFolder', 'L'); - } - if (collection.isCollection()) { - Zotero.Prefs.set('lastViewedFolder', 'C' + collection.ref.getID()); - } - else if (collection.isSearch()) { - Zotero.Prefs.set('lastViewedFolder', 'S' + collection.ref.id); - } + clearItemsPaneMessage(); + + connectionSelectedDeferred.resolve(); } -} +}); function onSearch() { @@ -102,6 +116,29 @@ function onItemSelected() } +function setItemsPaneMessage(content) { + var elem = document.getElementById('zotero-items-pane-message-box'); + elem.textContent = ''; + if (typeof content == 'string') { + let contentParts = content.split("\n\n"); + for (let part of contentParts) { + var desc = document.createElement('description'); + desc.appendChild(document.createTextNode(part)); + elem.appendChild(desc); + } + } + else { + elem.appendChild(content); + } + document.getElementById('zotero-items-pane-content').selectedIndex = 1; +} + + +function clearItemsPaneMessage() { + var box = document.getElementById('zotero-items-pane-message-box'); + document.getElementById('zotero-items-pane-content').selectedIndex = 0; +} + function doAccept() { io.dataOut = itemsView.getSelectedItems(true);