Don't count on 0 being the user libraryID in Zotero.Libraries

Some other minor tweaks
This commit is contained in:
Aurimas Vinckevicius 2014-10-29 19:21:36 -05:00
parent bc8a340c30
commit 3f85ee73b3
3 changed files with 50 additions and 31 deletions

View file

@ -55,17 +55,17 @@ Zotero.DataObject = function () {
Zotero.DataObject.prototype._objectType = 'dataObject'; Zotero.DataObject.prototype._objectType = 'dataObject';
Zotero.DataObject.prototype._dataTypes = []; Zotero.DataObject.prototype._dataTypes = [];
Zotero.Utilities.Internal.defineProperty(Zotero.DataObject, 'objectType', { Zotero.Utilities.Internal.defineProperty(Zotero.DataObject.prototype, 'objectType', {
get: function() this._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 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, get: function() this._parentKey,
set: function(v) this._setParentKey(v) 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(), get: function() this._getParentID(),
set: function(v) this._setParentID(v) set: function(v) this._setParentID(v)
}); });

View file

@ -24,7 +24,18 @@
*/ */
Zotero.Libraries = new function () { 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* () { this.init = Zotero.Promise.coroutine(function* () {
// Library data // Library data
@ -36,25 +47,28 @@ Zotero.Libraries = new function () {
type: row.libraryType, type: row.libraryType,
version: row.version version: row.version
}; };
if (row.libraryType == 'user') {
_userLibraryID = row.libraryID;
}
} }
_libraryDataLoaded = true;
}); });
this.exists = function (libraryID) { function _getLibraryDataFromDB (libraryID) {
// Until there are other library types, this can just check groups, var sql = "SELECT * FROM libraries WHERE libraryID=?";
// which already preload ids at startup return Zotero.DB.queryAsync(sql, [libraryID])
try { .then(function(rows) {
return !!Zotero.Groups.getGroupIDFromLibraryID(libraryID); return rows[0];
} });
catch (e) {
if (e.getMessage().indexOf("does not exist") != -1) {
return false;
}
throw e;
}
} }
this.add = function (libraryID, type) { this.exists = function (libraryID) {
return _libraryData[libraryID] !== undefined;
}
this.add = Zotero.Promise.coroutine(function* (libraryID, type) {
switch (type) { switch (type) {
case 'group': case 'group':
break; break;
@ -64,9 +78,16 @@ Zotero.Libraries = new function () {
} }
var sql = "INSERT INTO libraries (libraryID, libraryType) VALUES (?, ?)"; 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) { this.dbLibraryID = function (libraryID) {
return (libraryID == Zotero.Users.getCurrentLibraryID()) ? 0 : libraryID; return (libraryID == Zotero.Users.getCurrentLibraryID()) ? 0 : libraryID;
@ -74,12 +95,10 @@ Zotero.Libraries = new function () {
this.getName = function (libraryID) { this.getName = function (libraryID) {
if (!libraryID) {
return Zotero.getString('pane.collections.library');
}
var type = this.getType(libraryID); var type = this.getType(libraryID);
switch (type) { switch (type) {
case 'user':
return Zotero.getString('pane.collections.library');
case 'group': case 'group':
var groupID = Zotero.Groups.getGroupIDFromLibraryID(libraryID); var groupID = Zotero.Groups.getGroupIDFromLibraryID(libraryID);
var group = Zotero.Groups.get(groupID); var group = Zotero.Groups.get(groupID);
@ -92,10 +111,10 @@ Zotero.Libraries = new function () {
this.getType = function (libraryID) { this.getType = function (libraryID) {
if (this.dbLibraryID(libraryID) === 0) { if (libraryID === Zotero.Libraries.userLibraryID) {
return 'user'; return 'user';
} }
if (!_libraryData[libraryID]) { if (!this.exists(libraryID)) {
throw new Error("Library data not loaded for library " + libraryID); throw new Error("Library data not loaded for library " + libraryID);
} }
return _libraryData[libraryID].type; return _libraryData[libraryID].type;
@ -106,7 +125,7 @@ Zotero.Libraries = new function () {
* @return {Integer} * @return {Integer}
*/ */
this.getVersion = function (libraryID) { this.getVersion = function (libraryID) {
if (!_libraryData[libraryID]) { if (!this.exists(libraryID)) {
throw new Error("Library data not loaded for library " + libraryID); throw new Error("Library data not loaded for library " + libraryID);
} }
return _libraryData[libraryID].version; return _libraryData[libraryID].version;
@ -122,7 +141,7 @@ Zotero.Libraries = new function () {
version = parseInt(version); version = parseInt(version);
var sql = "UPDATE libraries SET version=? WHERE libraryID=?"; var sql = "UPDATE libraries SET version=? WHERE libraryID=?";
yield Zotero.DB.queryAsync(sql, [version, libraryID]); yield Zotero.DB.queryAsync(sql, [version, libraryID]);
_libraryData[libraryID] = version; _libraryData[libraryID].version = version;
}); });

View file

@ -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 * More compact way to do Object.defineProperty
* *
* @param {Object} obj Target object * @param {Object} obj Target object
@ -510,7 +510,7 @@ Zotero.Utilities.Internal = {
if (!desc.hasOwnProperty(p)) continue; if (!desc.hasOwnProperty(p)) continue;
d[p] = desc[p]; d[p] = desc[p];
} }
Object.defineProperty(obj.prototype, prop, d); Object.defineProperty(obj, prop, d);
} }
} }