From f795240bbfc9556e2f3940be658872bb1ff32471 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 11 Mar 2016 07:34:57 -0500 Subject: [PATCH] Fix display of Duplicate/Unfiled Items rows --- .../zotero/xpcom/collectionTreeView.js | 41 ++++++++++--------- chrome/content/zotero/zoteroPane.js | 21 ++++------ test/tests/collectionTreeViewTest.js | 22 ++++++++++ 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js index fc35fb239..015d9763e 100644 --- a/chrome/content/zotero/xpcom/collectionTreeView.js +++ b/chrome/content/zotero/xpcom/collectionTreeView.js @@ -156,25 +156,28 @@ Zotero.CollectionTreeView.prototype.refresh = Zotero.Promise.coroutine(function* this._containerState = {}; } - if (this.hideSources.indexOf('duplicates') == -1) { - try { - this._duplicateLibraries = Zotero.Prefs.get('duplicateLibraries').split(',').map(function (val) parseInt(val)); - } - catch (e) { - // Add to personal library by default - Zotero.Prefs.set('duplicateLibraries', '0'); - this._duplicateLibraries = [0]; - } - } + var userLibraryID = Zotero.Libraries.userLibraryID; - try { - this._unfiledLibraries = Zotero.Prefs.get('unfiledLibraries').split(',').map(function (val) parseInt(val)); - } - catch (e) { - // Add to personal library by default - Zotero.Prefs.set('unfiledLibraries', '0'); - this._unfiledLibraries = [0]; + var readPref = function (pref) { + let ids = Zotero.Prefs.get(pref); + if (ids === "") { + this["_" + pref] = []; + } + else { + if (ids === undefined || typeof ids != 'string') { + ids = "" + userLibraryID; + Zotero.Prefs.set(pref, "" + userLibraryID); + } + this["_" + pref] = ids.split(',') + // Convert old id and convert to int + .map(id => id === "0" ? userLibraryID : parseInt(id)); + } + }.bind(this); + + if (this.hideSources.indexOf('duplicates') == -1) { + readPref('duplicateLibraries'); } + readPref('unfiledLibraries'); var oldCount = this.rowCount || 0; var newRows = []; @@ -916,10 +919,10 @@ Zotero.CollectionTreeView.prototype.selectByID = Zotero.Promise.coroutine(functi break; } - if (!found) { + var row = this._rowMap[type + id]; + if (!row) { return false; } - var row = this._rowMap[type + id]; this._treebox.ensureRowIsVisible(row); yield this.selectWait(row); diff --git a/chrome/content/zotero/zoteroPane.js b/chrome/content/zotero/zoteroPane.js index 90df62fba..914e8211b 100644 --- a/chrome/content/zotero/zoteroPane.js +++ b/chrome/content/zotero/zoteroPane.js @@ -860,7 +860,7 @@ var ZoteroPane = new function() }); - this.setVirtual = function (libraryID, mode, show) { + this.setVirtual = Zotero.Promise.coroutine(function* (libraryID, mode, show) { switch (mode) { case 'duplicates': var prefKey = 'duplicateLibraries'; @@ -873,7 +873,7 @@ var ZoteroPane = new function() break; default: - throw ("Invalid virtual mode '" + mode + "' in ZoteroPane.setVirtual()"); + throw new Error("Invalid virtual mode '" + mode + "'"); } try { @@ -883,10 +883,6 @@ var ZoteroPane = new function() var ids = []; } - if (!libraryID) { - libraryID = Zotero.Libraries.userLibraryID; - } - var newids = []; for (let i = 0; i < ids.length; i++) { let id = ids[i]; @@ -898,8 +894,8 @@ var ZoteroPane = new function() if (id == libraryID && !show) { continue; } - // Remove libraryIDs that no longer exist - if (id != 0 && !Zotero.Libraries.exists(id)) { + // Remove libraries that no longer exist + if (!Zotero.Libraries.exists(id)) { continue; } newids.push(id); @@ -914,10 +910,10 @@ var ZoteroPane = new function() Zotero.Prefs.set(prefKey, newids.join()); - this.collectionsView.refresh(); + yield this.collectionsView.refresh(); // If group is closed, open it - this.collectionsView.selectLibrary(libraryID); + yield this.collectionsView.selectLibrary(libraryID); row = this.collectionsView.selection.currentIndex; if (!this.collectionsView.isContainerOpen(row)) { this.collectionsView.toggleOpenState(row); @@ -925,10 +921,9 @@ var ZoteroPane = new function() // Select new row if (show) { - Zotero.Prefs.set('lastViewedFolder', lastViewedFolderID); - this.collectionsView.selectByID(lastViewedFolderID); // async + yield this.collectionsView.selectByID(lastViewedFolderID); } - } + }); this.openLookupWindow = Zotero.Promise.coroutine(function* () { diff --git a/test/tests/collectionTreeViewTest.js b/test/tests/collectionTreeViewTest.js index f8a77e41b..51ae7dd51 100644 --- a/test/tests/collectionTreeViewTest.js +++ b/test/tests/collectionTreeViewTest.js @@ -16,6 +16,28 @@ describe("Zotero.CollectionTreeView", function() { win.close(); }); + describe("#refresh()", function () { + it("should show Duplicate Items and Unfiled Items in My Library by default", function* () { + Zotero.Prefs.clear('duplicateLibraries'); + Zotero.Prefs.clear('unfiledLibraries'); + yield cv.refresh(); + yield assert.eventually.ok(cv.selectByID("D" + userLibraryID)); + yield assert.eventually.ok(cv.selectByID("U" + userLibraryID)); + assert.equal(Zotero.Prefs.get('duplicateLibraries'), "" + userLibraryID); + assert.equal(Zotero.Prefs.get('unfiledLibraries'), "" + userLibraryID); + }); + + it("shouldn't show Duplicate Items and Unfiled Items if hidden", function* () { + Zotero.Prefs.set('duplicateLibraries', ""); + Zotero.Prefs.set('unfiledLibraries', ""); + yield cv.refresh(); + yield assert.eventually.notOk(cv.selectByID("D" + userLibraryID)); + yield assert.eventually.notOk(cv.selectByID("U" + userLibraryID)); + assert.strictEqual(Zotero.Prefs.get('duplicateLibraries'), ""); + assert.strictEqual(Zotero.Prefs.get('unfiledLibraries'), ""); + }); + }); + describe("collapse/expand", function () { it("should close and open My Library repeatedly", function* () { var libraryID = Zotero.Libraries.userLibraryID;