diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js index 9b2dabb89b..a7a9b7ce52 100644 --- a/chrome/content/zotero/xpcom/data/dataObject.js +++ b/chrome/content/zotero/xpcom/data/dataObject.js @@ -55,17 +55,17 @@ Zotero.DataObject = function () { Zotero.DataObject.prototype._objectType = 'dataObject'; Zotero.DataObject.prototype._dataTypes = []; -Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'objectType', { +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject.prototype, 'objectType', { get: function() this._objectType }); -Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'libraryKey', { +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject.prototype, 'libraryKey', { get: function() this._libraryID + "/" + this._key }); -Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'parentKey', { +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject.prototype, 'parentKey', { get: function() this._parentKey, set: function(v) this._setParentKey(v) }); -Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'parentID', { +Zotero.Utilities.Internal.defineProperty(Zotero.DataObject.prototype, 'parentID', { get: function() this._getParentID(), set: function(v) this._setParentID(v) }); diff --git a/chrome/content/zotero/xpcom/data/libraries.js b/chrome/content/zotero/xpcom/data/libraries.js index 15659af806..8234f2083d 100644 --- a/chrome/content/zotero/xpcom/data/libraries.js +++ b/chrome/content/zotero/xpcom/data/libraries.js @@ -24,7 +24,18 @@ */ Zotero.Libraries = new function () { - var _libraryData = {}; + let _libraryData = {}, + _userLibraryID, + _libraryDataLoaded = false; + + Zotero.Utilities.Internal.defineProperty(this, 'userLibraryID', { + get: function() { + if (!_libraryDataLoaded) { + throw new Error("Library data not yet loaded"); + } + return _userLibraryID; + } + }); this.init = Zotero.Promise.coroutine(function* () { // Library data @@ -36,25 +47,28 @@ Zotero.Libraries = new function () { type: row.libraryType, version: row.version }; + if (row.libraryType == 'user') { + _userLibraryID = row.libraryID; + } } + _libraryDataLoaded = true; }); - this.exists = function (libraryID) { - // Until there are other library types, this can just check groups, - // which already preload ids at startup - try { - return !!Zotero.Groups.getGroupIDFromLibraryID(libraryID); - } - catch (e) { - if (e.getMessage().indexOf("does not exist") != -1) { - return false; - } - throw e; - } + function _getLibraryDataFromDB (libraryID) { + var sql = "SELECT * FROM libraries WHERE libraryID=?"; + return Zotero.DB.queryAsync(sql, [libraryID]) + .then(function(rows) { + return rows[0]; + }); } - this.add = function (libraryID, type) { + this.exists = function (libraryID) { + return _libraryData[libraryID] !== undefined; + } + + + this.add = Zotero.Promise.coroutine(function* (libraryID, type) { switch (type) { case 'group': break; @@ -64,9 +78,16 @@ Zotero.Libraries = new function () { } var sql = "INSERT INTO libraries (libraryID, libraryType) VALUES (?, ?)"; - Zotero.DB.query(sql, [libraryID, type]); - } - + yield Zotero.DB.queryAsync(sql, [libraryID, type]); + // Re-fetch from DB to get auto-filled defaults + var newData = yield _getLibraryDataFromDB(libraryID); + _libraryData[newData.libraryID] = { + type: newData.libraryType, + version: newData.version + }; + + return newData; + }); this.dbLibraryID = function (libraryID) { return (libraryID == Zotero.Users.getCurrentLibraryID()) ? 0 : libraryID; @@ -74,12 +95,10 @@ Zotero.Libraries = new function () { this.getName = function (libraryID) { - if (!libraryID) { - return Zotero.getString('pane.collections.library'); - } - var type = this.getType(libraryID); switch (type) { + case 'user': + return Zotero.getString('pane.collections.library'); case 'group': var groupID = Zotero.Groups.getGroupIDFromLibraryID(libraryID); var group = Zotero.Groups.get(groupID); @@ -92,10 +111,10 @@ Zotero.Libraries = new function () { this.getType = function (libraryID) { - if (this.dbLibraryID(libraryID) === 0) { + if (libraryID === Zotero.Libraries.userLibraryID) { return 'user'; } - if (!_libraryData[libraryID]) { + if (!this.exists(libraryID)) { throw new Error("Library data not loaded for library " + libraryID); } return _libraryData[libraryID].type; @@ -106,7 +125,7 @@ Zotero.Libraries = new function () { * @return {Integer} */ this.getVersion = function (libraryID) { - if (!_libraryData[libraryID]) { + if (!this.exists(libraryID)) { throw new Error("Library data not loaded for library " + libraryID); } return _libraryData[libraryID].version; @@ -122,7 +141,7 @@ Zotero.Libraries = new function () { version = parseInt(version); var sql = "UPDATE libraries SET version=? WHERE libraryID=?"; yield Zotero.DB.queryAsync(sql, [version, libraryID]); - _libraryData[libraryID] = version; + _libraryData[libraryID].version = version; }); diff --git a/chrome/content/zotero/xpcom/utilities_internal.js b/chrome/content/zotero/xpcom/utilities_internal.js index 18f0ae1f15..f88c08f752 100644 --- a/chrome/content/zotero/xpcom/utilities_internal.js +++ b/chrome/content/zotero/xpcom/utilities_internal.js @@ -496,7 +496,7 @@ Zotero.Utilities.Internal = { }, /** - * Defines property on the object's prototype. + * Defines property on the object * More compact way to do Object.defineProperty * * @param {Object} obj Target object @@ -510,7 +510,7 @@ Zotero.Utilities.Internal = { if (!desc.hasOwnProperty(p)) continue; d[p] = desc[p]; } - Object.defineProperty(obj.prototype, prop, d); + Object.defineProperty(obj, prop, d); } }