commit 5100cd31edd6041bbb2ef95ecf4cb783c8d08a9e
parent 99152d78e5cb1d6d4be2ea2bf0b6acc77ce88545
Author: Dan Stillman <dstillman@zotero.org>
Date: Mon, 26 Dec 2016 18:44:39 -0500
Add collectionTreeView::selectItem()
This moves most selection logic from ZoteroPane.selectItem() into
collectionTreeView::selectItem() so that it can be used in the
edit-citation dialog.
Unlike itemTreeView::selectItem(), which only selects within a given
items tree, this function automatically switches to a library root if
necessary. ZoteroPane.selectItem() remains and does a little bit extra
(unminimizing Zotero, focusing the items pane) in addition to calling
collectionTreeView::selectItem().
Diffstat:
3 files changed, 69 insertions(+), 48 deletions(-)
diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js
@@ -1147,6 +1147,69 @@ Zotero.CollectionTreeView.prototype.selectTrash = Zotero.Promise.coroutine(funct
});
+/**
+ * Find item in current collection, or, if not there, in a library root, and select it
+ *
+ * @param {Integer} itemID
+ * @param {Boolean} [inLibraryRoot=false] - Always show in library root
+ * @param {Boolean} [expand=false] - Open item if it's a container
+ * @return {Boolean} - True if item was found, false if not
+ */
+Zotero.CollectionTreeView.prototype.selectItem = Zotero.Promise.coroutine(function* (itemID, inLibraryRoot, expand) {
+ if (!itemID) {
+ return false;
+ }
+
+ var item = yield Zotero.Items.getAsync(itemID);
+ if (!item) {
+ return false;
+ }
+
+ var deferred = Zotero.Promise.defer();
+ this.addEventListener('load', () => deferred.resolve());
+ yield deferred.promise;
+
+ var currentLibraryID = this.getSelectedLibraryID();
+ // If in a different library
+ if (item.libraryID != currentLibraryID) {
+ Zotero.debug("Library ID differs; switching library");
+ yield this.selectLibrary(item.libraryID);
+ }
+ // Force switch to library view
+ else if (!this.selectedTreeRow.isLibrary() && inLibraryRoot) {
+ Zotero.debug("Told to select in library; switching to library");
+ yield cv.selectLibrary(item.libraryID);
+ }
+
+ var itemsView = this.selectedTreeRow.itemTreeView;
+
+ deferred = Zotero.Promise.defer();
+ itemsView.addEventListener('load', () => deferred.resolve());
+ yield deferred.promise;
+
+ var selected = yield itemsView.selectItem(itemID, expand);
+ if (selected) {
+ return true;
+ }
+
+ if (item.deleted) {
+ Zotero.debug("Item is deleted; switching to trash");
+ yield this.selectTrash(item.libraryID);
+ }
+ else {
+ Zotero.debug("Item was not selected; switching to library");
+ yield this.selectLibrary(item.libraryID);
+ }
+
+ itemsView = this.selectedTreeRow.itemTreeView;
+ deferred = Zotero.Promise.defer();
+ itemsView.addEventListener('load', () => deferred.resolve());
+ yield deferred.promise;
+
+ return itemsView.selectItem(itemID, expand);
+});
+
+
/*
* Delete the selection
*/
diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js
@@ -40,6 +40,7 @@ Zotero.ItemTreeView = function (collectionTreeRow, sourcesOnly) {
this.wrappedJSObject = this;
this.rowCount = 0;
this.collectionTreeRow = collectionTreeRow;
+ collectionTreeRow.itemTreeView = this;
this._skipKeypress = false;
diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js
@@ -2201,14 +2201,7 @@ var ZoteroPane = new function()
});
- /*
- * Select item in current collection or, if not there, in Library
- *
- * If _inLibrary_, force switch to Library
- * If _expand_, open item if it's a container
- */
- this.selectItem = Zotero.Promise.coroutine(function* (itemID, inLibrary, expand)
- {
+ this.selectItem = Zotero.Promise.coroutine(function* (itemID, inLibraryRoot, expand) {
if (!itemID) {
return false;
}
@@ -2227,47 +2220,11 @@ var ZoteroPane = new function()
throw new Error("Collections view not loaded");
}
- var cv = this.collectionsView;
-
- var deferred = Zotero.Promise.defer();
- cv.addEventListener('load', () => deferred.resolve());
- yield deferred.promise;
-
- var currentLibraryID = this.getSelectedLibraryID();
- // If in a different library
- if (item.libraryID != currentLibraryID) {
- Zotero.debug("Library ID differs; switching library");
- yield cv.selectLibrary(item.libraryID);
- }
- // Force switch to library view
- else if (!cv.selectedTreeRow.isLibrary() && inLibrary) {
- Zotero.debug("Told to select in library; switching to library");
- yield cv.selectLibrary(item.libraryID);
- }
-
- deferred = Zotero.Promise.defer();
- this.itemsView.addEventListener('load', () => deferred.resolve());
- yield deferred.promise;
-
- // Focus the items column before selecting the item.
- document.getElementById('zotero-items-tree').focus();
+ var found = yield this.collectionsView.selectItem(itemID, inLibraryRoot, expand);
- var selected = yield this.itemsView.selectItem(itemID, expand);
- if (!selected) {
- if (item.deleted) {
- Zotero.debug("Item is deleted; switching to trash");
- yield cv.selectTrash(item.libraryID);
- }
- else {
- Zotero.debug("Item was not selected; switching to library");
- yield cv.selectLibrary(item.libraryID);
- }
-
- deferred = Zotero.Promise.defer();
- this.itemsView.addEventListener('load', () => deferred.resolve());
- yield deferred.promise;
-
- yield this.itemsView.selectItem(itemID, expand);
+ // Focus the items pane
+ if (found) {
+ document.getElementById('zotero-items-tree').focus();
}
// open Zotero pane