commit 32abbe7c2550162e42a9398fe96c3021cac74302
parent f0b44b20474c4b04368e59b964694a11233aa431
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 22 May 2015 04:49:02 -0400
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.
Diffstat:
7 files changed, 61 insertions(+), 49 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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.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.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.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<rows.length; i++) {
- let row = rows[i];
- _groupIDsByLibraryID[row.libraryID] = row.groupID;
- _libraryIDsByGroupID[row.groupID] = row.libraryID;
- }
- }.bind(this));
+ this.register = function (group) {
+ _libraryIDsByGroupID[group.id] = group.libraryID;
+ _groupIDsByLibraryID[group.libraryID] = group.id;
+ _cache[group.id] = group;
+ }
+
+
+ this.unregister = function (id) {
+ var libraryID = _libraryIDsByGroupID[groupID];
+ delete _groupIDsByLibraryID[libraryID];
+ delete _libraryIDsByGroupID[groupID];
+ delete _cache[id];
}
+
+
+ var _load = Zotero.Promise.coroutine(function* () {
+ var sql = "SELECT libraryID, groupID FROM groups";
+ var rows = yield Zotero.DB.queryAsync(sql)
+ for (let i=0; i<rows.length; i++) {
+ let row = rows[i];
+ _groupIDsByLibraryID[row.libraryID] = row.groupID;
+ _libraryIDsByGroupID[row.groupID] = row.libraryID;
+ let group = new Zotero.Group;
+ group.id = row.groupID;
+ yield group.load();
+ _cache[row.groupID] = group;
+ }
+ });
}
diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js
@@ -604,7 +604,7 @@ Components.utils.import("resource://gre/modules/osfile.jsm");
Zotero.Collections.init();
Zotero.Items.init();
yield Zotero.Searches.init();
- Zotero.Groups.init();
+ yield Zotero.Groups.init();
yield Zotero.QuickCopy.init();
diff --git a/components/zotero-protocol-handler.js b/components/zotero-protocol-handler.js
@@ -596,7 +596,7 @@ function ZoteroProtocolHandler() {
// FIXME: Hack to exclude group libraries for now
var search = new Zotero.Search();
search.setScope(s);
- var groups = yield Zotero.Groups.getAll();
+ var groups = Zotero.Groups.getAll();
for each(var group in groups) {
search.addCondition('libraryID', 'isNot', group.libraryID);
}