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().
This commit is contained in:
parent
99152d78e5
commit
5100cd31ed
3 changed files with 69 additions and 48 deletions
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -40,6 +40,7 @@ Zotero.ItemTreeView = function (collectionTreeRow, sourcesOnly) {
|
|||
this.wrappedJSObject = this;
|
||||
this.rowCount = 0;
|
||||
this.collectionTreeRow = collectionTreeRow;
|
||||
collectionTreeRow.itemTreeView = this;
|
||||
|
||||
this._skipKeypress = false;
|
||||
|
||||
|
|
|
@ -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 found = yield this.collectionsView.selectItem(itemID, inLibraryRoot, expand);
|
||||
|
||||
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 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
|
||||
|
|
Loading…
Reference in a new issue