diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js index e56fad337c..008970335f 100644 --- a/chrome/content/zotero/xpcom/collectionTreeView.js +++ b/chrome/content/zotero/xpcom/collectionTreeView.js @@ -1767,6 +1767,23 @@ Zotero.CollectionTreeView.prototype.cycleHeader = function(column) { } /// //////////////////////////////////////////////////////////////////////////////// +Zotero.ItemGroupCache = { + "lastItemGroup":null, + "lastTempTable":null, + "lastSearch":null, + "lastResults":null, + + "clear":function() { + this.lastItemGroup = null; + this.lastSearch = null; + this.lastTempTable = null; + if(this.lastTempTable) { + Zotero.DB.query("DROP TABLE "+this.lastTempTable); + } + this.lastResults = null; + } +}; + Zotero.ItemGroup = function(type, ref) { this.type = type; @@ -1947,24 +1964,8 @@ Zotero.ItemGroup.prototype.getItems = function() return []; } - var s = this.getSearchObject(); - - // FIXME: Hack to exclude group libraries for now - if (this.isSearch()) { - var currentLibraryID = this.ref.libraryID; - if (currentLibraryID) { - s.addCondition('libraryID', 'is', currentLibraryID); - } - else { - var groups = Zotero.Groups.getAll(); - for each(var group in groups) { - s.addCondition('libraryID', 'isNot', group.libraryID); - } - } - } - try { - var ids = s.search(); + var ids = this.getSearchResults(); } catch (e) { Zotero.DB.rollbackAllTransactions(); @@ -1975,6 +1976,40 @@ Zotero.ItemGroup.prototype.getItems = function() return Zotero.Items.get(ids); } +Zotero.ItemGroup.prototype.getSearchResults = function(asTempTable) { + if(Zotero.ItemGroupCache.lastItemGroup !== this) { + Zotero.ItemGroupCache.clear(); + } + + if(!Zotero.ItemGroupCache.lastResults) { + var s = this.getSearchObject(); + + // FIXME: Hack to exclude group libraries for now + if (this.isSearch()) { + var currentLibraryID = this.ref.libraryID; + if (currentLibraryID) { + s.addCondition('libraryID', 'is', currentLibraryID); + } + else { + var groups = Zotero.Groups.getAll(); + for each(var group in groups) { + s.addCondition('libraryID', 'isNot', group.libraryID); + } + } + } + + Zotero.ItemGroupCache.lastResults = s.search(); + Zotero.ItemGroupCache.lastItemGroup = this; + } + + if(asTempTable) { + if(!Zotero.ItemGroupCache.lastTempTable) { + Zotero.ItemGroupCache.lastTempTable = Zotero.Search.idsToTempTable(Zotero.ItemGroupCache.lastResults); + } + return Zotero.ItemGroupCache.lastTempTable; + } + return Zotero.ItemGroupCache.lastResults; +} /* * Returns the search object for the currently display @@ -1982,6 +2017,14 @@ Zotero.ItemGroup.prototype.getItems = function() * This accounts for the collection, saved search, quicksearch, tags, etc. */ Zotero.ItemGroup.prototype.getSearchObject = function() { + if(Zotero.ItemGroupCache.lastItemGroup !== this) { + Zotero.ItemGroupCache.clear(); + } + + if(Zotero.ItemGroupCache.lastSearch) { + return Zotero.ItemGroupCache.lastSearch; + } + var includeScopeChildren = false; // Create/load the inner search @@ -2040,6 +2083,8 @@ Zotero.ItemGroup.prototype.getSearchObject = function() { } } + Zotero.ItemGroupCache.lastItemGroup = this; + Zotero.ItemGroupCache.lastSearch = s2; return s2; } @@ -2060,18 +2105,22 @@ Zotero.ItemGroup.prototype.getChildTags = function() { return false; } - var s = this.getSearchObject(); - return Zotero.Tags.getAllWithinSearch(s); + return Zotero.Tags.getAllWithinSearch(this.getSearchObject(), + undefined, this.getSearchResults(true)); } Zotero.ItemGroup.prototype.setSearch = function(searchText) { + if(searchText !== this.searchText) { + Zotero.ItemGroupCache.clear(); + } this.searchText = searchText; } Zotero.ItemGroup.prototype.setTags = function(tags) { + Zotero.ItemGroupCache.clear(); this.tags = tags; } diff --git a/chrome/content/zotero/xpcom/data/tags.js b/chrome/content/zotero/xpcom/data/tags.js index 6e0c94b498..9d82a20610 100644 --- a/chrome/content/zotero/xpcom/data/tags.js +++ b/chrome/content/zotero/xpcom/data/tags.js @@ -199,23 +199,25 @@ Zotero.Tags = new function() { * * _types_ is an optional array of tag types to fetch */ - function getAllWithinSearch(search, types) { + function getAllWithinSearch(search, types, tmpTable) { // Save search results to temporary table - try { - var tmpTable = search.search(true); - } - catch (e) { - if (typeof e == 'string' - && e.match(/Saved search [0-9]+ does not exist/)) { - Zotero.DB.rollbackTransaction(); - Zotero.debug(e, 2); + if(!tmpTable) { + try { + var tmpTable = search.search(true); } - else { - throw (e); + catch (e) { + if (typeof e == 'string' + && e.match(/Saved search [0-9]+ does not exist/)) { + Zotero.DB.rollbackTransaction(); + Zotero.debug(e, 2); + } + else { + throw (e); + } + } + if (!tmpTable) { + return {}; } - } - if (!tmpTable) { - return {}; } var sql = "SELECT DISTINCT tagID, name, type FROM itemTags " @@ -226,7 +228,9 @@ Zotero.Tags = new function() { } var tags = Zotero.DB.query(sql); - Zotero.DB.query("DROP TABLE " + tmpTable); + if(!tmpTable) { + Zotero.DB.query("DROP TABLE " + tmpTable); + } if (!tags) { return {}; diff --git a/chrome/content/zotero/xpcom/itemTreeView.js b/chrome/content/zotero/xpcom/itemTreeView.js index 4d7a4e4101..451fe22fdb 100644 --- a/chrome/content/zotero/xpcom/itemTreeView.js +++ b/chrome/content/zotero/xpcom/itemTreeView.js @@ -63,6 +63,7 @@ Zotero.ItemTreeView.prototype.addCallback = function(callback) { Zotero.ItemTreeView.prototype._runCallbacks = function() { for each(var cb in this._callbacks) { + this.showLoadingMessageIfNecessary(); cb(); } } diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js index 09b1c4b23f..95b388ed7a 100644 --- a/chrome/content/zotero/xpcom/search.js +++ b/chrome/content/zotero/xpcom/search.js @@ -651,7 +651,7 @@ Zotero.Search.prototype.search = function(asTempTable){ return false; } - var tmpTable = this._idsToTempTable(ids); + var tmpTable = Zotero.Search.idsToTempTable(ids); } // Otherwise, just copy to temp table directly else { @@ -720,7 +720,7 @@ Zotero.Search.prototype.search = function(asTempTable){ if (joinMode == 'any') { if (!tmpTable) { Zotero.DB.beginTransaction(); - var tmpTable = this._idsToTempTable(ids); + var tmpTable = Zotero.Search.idsToTempTable(ids); } var sql = "SELECT itemID FROM items WHERE " @@ -839,7 +839,7 @@ Zotero.Search.prototype.search = function(asTempTable){ if (this.hasPostSearchFilter() && (includeParentsAndChildren || includeParents || includeChildren)) { Zotero.DB.beginTransaction(); - var tmpTable = this._idsToTempTable(ids); + var tmpTable = Zotero.Search.idsToTempTable(ids); if (includeParentsAndChildren || includeParents) { //Zotero.debug("Adding parent items to result set"); @@ -890,7 +890,8 @@ Zotero.Search.prototype.search = function(asTempTable){ } if (asTempTable) { - return this._idsToTempTable(ids); + var table = Zotero.Search.idsToTempTable(ids); + return table; } return ids; @@ -951,7 +952,7 @@ Zotero.Search.prototype._prepFieldChange = function (field) { /* * Batch insert */ -Zotero.Search.prototype._idsToTempTable = function (ids) { +Zotero.Search.idsToTempTable = function (ids) { var tmpTable = "tmpSearchResults_" + Zotero.randomString(8); Zotero.DB.beginTransaction();