From 2c18968c3ac79d7b99ac5d9970c3424597d90a96 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 17 May 2016 13:40:47 -0400 Subject: [PATCH] Fix Quick Format dialog for search results in unloaded libraries Since the search might return items in libraries that haven't yet been loaded, we have to get those items asynchronously and load their data explicitly. Need to test this with a large document to make sure it still properly populates the list with search results and then adds matching cited items. --- .../content/zotero/integration/quickFormat.js | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/chrome/content/zotero/integration/quickFormat.js b/chrome/content/zotero/integration/quickFormat.js index 5e0ceb092a..e86ca834c2 100644 --- a/chrome/content/zotero/integration/quickFormat.js +++ b/chrome/content/zotero/integration/quickFormat.js @@ -279,11 +279,15 @@ var Zotero_QuickFormat = new function () { if(haveConditions) { var searchResultIDs = (haveConditions ? (yield s.search()) : []); + // Show items list without cited items to start + yield _updateItemList(false, false, str, searchResultIDs); + // Check to see which search results match items already in the document var citedItems, completed = false, isAsync = false; - // Save current search so that when we get items, we know whether it's too late to + // Save current search time so that when we get items, we know whether it's too late to // process them or not var lastSearchTime = currentSearchTime = Date.now(); + // This may or may not be synchronous io.getItems().then(function(citedItems) { // Don't do anything if panel is already closed if(isAsync && @@ -322,13 +326,11 @@ var Zotero_QuickFormat = new function () { } _updateItemList(citedItems, citedItemsMatchingSearch, str, searchResultIDs, isAsync); - }).done(); + }); if(!completed) { // We are going to have to wait until items have been retrieved from the document. - // Until then, show item list without cited items. Zotero.debug("Getting cited items asynchronously"); - _updateItemList(false, false, str, searchResultIDs); isAsync = true; } else { Zotero.debug("Got cited items synchronously"); @@ -356,7 +358,8 @@ var Zotero_QuickFormat = new function () { /** * Updates the item list */ - function _updateItemList(citedItems, citedItemsMatchingSearch, searchString, searchResultIDs, preserveSelection) { + var _updateItemList = Zotero.Promise.coroutine(function* (citedItems, citedItemsMatchingSearch, + searchString, searchResultIDs, preserveSelection) { var selectedIndex = 1, previousItemID; // Do this so we can preserve the selected item after cited items have been loaded @@ -412,7 +415,10 @@ var Zotero_QuickFormat = new function () { } if(searchResultIDs.length && (!citedItemsMatchingSearch || citedItemsMatchingSearch.length < 50)) { - var items = Zotero.Items.get(searchResultIDs); + // Search results might be in an unloaded library, so get items asynchronously and load + // necessary data + var items = yield Zotero.Items.getAsync(searchResultIDs); + yield Zotero.Items.loadDataTypes(items, ['itemData', 'creators']); searchString = searchString.toLowerCase(); var collation = Zotero.getLocaleCollation(); @@ -489,7 +495,7 @@ var Zotero_QuickFormat = new function () { referenceBox.selectedIndex = selectedIndex; referenceBox.ensureIndexIsVisible(selectedIndex); } - } + }); /** * Builds a string describing an item. We avoid CSL here for speed.