From 32abbe7c2550162e42a9398fe96c3021cac74302 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Fri, 22 May 2015 04:49:02 -0400 Subject: [PATCH] Load groups at startup, and make Zotero.Groups functions synchronous Groups were already being loaded for the collections list, so we might as well just store them initially and let Zotero.Libraries.getName() be a synchronous call. --- chrome/content/zotero/advancedSearch.js | 6 -- .../content/zotero/bindings/zoterosearch.xml | 5 +- .../zotero/xpcom/collectionTreeView.js | 2 +- chrome/content/zotero/xpcom/data/group.js | 5 +- chrome/content/zotero/xpcom/data/groups.js | 88 +++++++++++-------- chrome/content/zotero/xpcom/zotero.js | 2 +- components/zotero-protocol-handler.js | 2 +- 7 files changed, 61 insertions(+), 49 deletions(-) diff --git a/chrome/content/zotero/advancedSearch.js b/chrome/content/zotero/advancedSearch.js index c549812397..2f2e2c9173 100644 --- a/chrome/content/zotero/advancedSearch.js +++ b/chrome/content/zotero/advancedSearch.js @@ -48,12 +48,6 @@ var ZoteroAdvancedSearch = new function() { io.dataIn.search.loadPrimaryData() .then(function () { - return Zotero.Groups.getAll(); - }) - .then(function (groups) { - // Since the search box can be used as a modal dialog, which can't use promises, - // it expects groups to be passed in. - _searchBox.groups = groups; _searchBox.search = io.dataIn.search; }); } diff --git a/chrome/content/zotero/bindings/zoterosearch.xml b/chrome/content/zotero/bindings/zoterosearch.xml index 8203bbbe11..a2244b8f52 100644 --- a/chrome/content/zotero/bindings/zoterosearch.xml +++ b/chrome/content/zotero/bindings/zoterosearch.xml @@ -95,8 +95,9 @@ menupopup.appendChild(menuitem); // Add groups - for (let i = 0; i < this.groups.length; i++) { - let group = this.groups[i]; + var groups = Zotero.Groups.getAll(); + for (let i = 0; i < groups.length; i++) { + let group = groups[i]; let menuitem = document.createElement('menuitem'); menuitem.setAttribute('label', group.name); menuitem.setAttribute('libraryID', group.libraryID); diff --git a/chrome/content/zotero/xpcom/collectionTreeView.js b/chrome/content/zotero/xpcom/collectionTreeView.js index 77e6dc9dea..4328e9a65f 100644 --- a/chrome/content/zotero/xpcom/collectionTreeView.js +++ b/chrome/content/zotero/xpcom/collectionTreeView.js @@ -200,7 +200,7 @@ Zotero.CollectionTreeView.prototype.refresh = Zotero.Promise.coroutine(function* ); // Add groups - var groups = yield Zotero.Groups.getAll(); + var groups = Zotero.Groups.getAll(); if (groups.length) { this._addRowToArray( newRows, diff --git a/chrome/content/zotero/xpcom/data/group.js b/chrome/content/zotero/xpcom/data/group.js index 6b71af9499..8d2fcfd6e2 100644 --- a/chrome/content/zotero/xpcom/data/group.js +++ b/chrome/content/zotero/xpcom/data/group.js @@ -287,7 +287,7 @@ Zotero.Group.prototype.save = function () { throw (e); } - //Zotero.Groups.reload(this.id); + Zotero.Groups.register(this); Zotero.Notifier.trigger('add', 'group', this.id); } @@ -369,6 +369,7 @@ Zotero.Group.prototype.erase = Zotero.Promise.coroutine(function* () { Zotero.Notifier.enable(); } + Zotero.Groups.unregister(this.id); Zotero.Notifier.trigger('delete', 'group', this.id, notifierData); }); @@ -391,7 +392,7 @@ Zotero.Group.prototype.serialize = function() { Zotero.Group.prototype._requireLoad = function () { - if (!this._loaded) { + if (!this._loaded && Zotero.Groups.exists(this.id)) { throw new Error("Group has not been loaded"); } } diff --git a/chrome/content/zotero/xpcom/data/groups.js b/chrome/content/zotero/xpcom/data/groups.js index 000095a933..286ef13a8d 100644 --- a/chrome/content/zotero/xpcom/data/groups.js +++ b/chrome/content/zotero/xpcom/data/groups.js @@ -27,39 +27,38 @@ Zotero.Groups = new function () { this.__defineGetter__('addGroupURL', function () ZOTERO_CONFIG.WWW_BASE_URL + 'groups/new/'); + var _cache = {}; var _groupIDsByLibraryID = {}; var _libraryIDsByGroupID = {}; - this.init = function () { - _loadIDs(); + this.init = Zotero.Promise.coroutine(function* () { + yield _load(); + }); + + /** + * @param {Integer} id - Group id + * @return {Zotero.Group} + */ + this.get = function (id) { + if (!id) throw new Error("groupID not provided"); + return _cache[id] ? _cache[id] : false; } - this.get = Zotero.Promise.coroutine(function* (id) { - if (!id) { - throw new Error("groupID not provided"); - } - var group = new Zotero.Group; - group.id = id; - if (!(yield group.load())) { - return false; - } - return group; - }); - - this.getAll = Zotero.Promise.coroutine(function* () { - var groups = []; - var sql = "SELECT groupID FROM groups ORDER BY name COLLATE locale"; - var groupIDs = yield Zotero.DB.columnQueryAsync(sql); - if (!groupIDs.length) { - return groups; - } - for each(var groupID in groupIDs) { - groups.push(this.get(groupID)); - } - return Zotero.Promise.all(groups); - }); + /** + * Get all groups, sorted by name + * + * @return {Zotero.Group[]} + */ + this.getAll = function () { + var groups = [for (id of Object.keys(_cache)) _cache[id]]; + var collation = Zotero.getLocaleCollation(); + groups.sort(function(a, b) { + return collation.compareString(1, a.name, b.name); + }); + return groups; + } this.getByLibraryID = function (libraryID) { @@ -91,15 +90,32 @@ Zotero.Groups = new function () { } - function _loadIDs() { - var sql = "SELECT libraryID, groupID FROM groups"; - return Zotero.DB.queryAsync(sql) - .then(function (rows) { - for (let i=0; i