diff --git a/chrome/content/zotero/xpcom/data/library.js b/chrome/content/zotero/xpcom/data/library.js index ac0fcdf7b..60edb9b85 100644 --- a/chrome/content/zotero/xpcom/data/library.js +++ b/chrome/content/zotero/xpcom/data/library.js @@ -102,12 +102,34 @@ Zotero.defineProperty(Zotero.Library.prototype, 'libraryID', { Zotero.defineProperty(Zotero.Library.prototype, 'id', { get: function() this.libraryID, set: function(val) this.libraryID = val -}); +}); Zotero.defineProperty(Zotero.Library.prototype, 'libraryType', { get: function() this._get('_libraryType'), set: function(v) this._set('_libraryType', v) }); +/** + * Get the library-type-specific id for the library (e.g., userID for user library, + * groupID for group library) + * + * @property + */ +Zotero.defineProperty(Zotero.Library.prototype, 'libraryTypeID', { + get: function () { + switch (this._libraryType) { + case 'user': + case 'publications': + return Zotero.Users.getCurrentUserID(); + + case 'group': + return Zotero.Groups.getGroupIDFromLibraryID(this._libraryID); + + default: + throw new Error(`Cannot return library type id for ${this._libraryType} library`); + } + } +}); + Zotero.defineProperty(Zotero.Library.prototype, 'libraryVersion', { get: function() this._get('_libraryVersion'), set: function(v) this._set('_libraryVersion', v) diff --git a/test/tests/libraryTest.js b/test/tests/libraryTest.js index 79eca29aa..548e69ad0 100644 --- a/test/tests/libraryTest.js +++ b/test/tests/libraryTest.js @@ -34,6 +34,14 @@ describe("Zotero.Library", function() { }); }); + describe("#libraryTypeID", function () { + it("should return a group id for a group", function* () { + let library = yield createGroup(); + assert.typeOf(library.libraryTypeID, 'number'); + assert.equal(library.libraryTypeID, library.groupID); + }) + }) + describe("#libraryVersion", function() { it("should be settable to increasing values", function() { let library = new Zotero.Library();