From adab8e45a7e74a51ec449a12a8ca70741845436c Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Fri, 14 Nov 2014 04:08:49 -0600 Subject: [PATCH] Additional tweaks to DataObject & descendents to support inheritance --- .../content/zotero/xpcom/data/collection.js | 80 ++++++++++--------- .../content/zotero/xpcom/data/dataObject.js | 9 ++- chrome/content/zotero/xpcom/data/item.js | 73 +++++++++-------- chrome/content/zotero/xpcom/search.js | 3 +- 4 files changed, 88 insertions(+), 77 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index 1ae5aa4b1e..a97a7937d5 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -37,17 +37,18 @@ Zotero.Collection = function() { this._childItems = []; } -Zotero.Collection._super = Zotero.DataObject; -Zotero.Collection.prototype = Object.create(Zotero.Collection._super.prototype); -Zotero.Collection.constructor = Zotero.Collection; +Zotero.extendClass(Zotero.DataObject, Zotero.Collection); Zotero.Collection.prototype._objectType = 'collection'; Zotero.Collection.prototype._dataTypes = Zotero.Collection._super.prototype._dataTypes.concat([ - 'primaryData', 'childCollections', 'childItems' ]); +Zotero.defineProperty(Zotero.Collection.prototype, 'ChildObjects', { + get: function() Zotero.Items +}); + Zotero.defineProperty(Zotero.Collection.prototype, 'id', { get: function() this._get('id'), set: function(val) this._set('id', val) @@ -136,7 +137,7 @@ Zotero.Collection.prototype.loadPrimaryData = Zotero.Promise.coroutine(function* var key = this._key; var libraryID = this._libraryID; - var sql = Zotero.Collections.getPrimaryDataSQL(); + var sql = this.ObjectsClass.getPrimaryDataSQL(); if (id) { sql += " AND O.collectionID=?"; var params = id; @@ -164,7 +165,7 @@ Zotero.Collection.prototype.loadPrimaryData = Zotero.Promise.coroutine(function* Zotero.Collection.prototype.loadFromRow = function(row) { Zotero.debug("Loading collection from row"); - for each(let col in Zotero.Collections.primaryFields) { + for each(let col in this.ObjectsClass.primaryFields) { if (row[col] === undefined) { Zotero.debug('Skipping missing collection field ' + col); } @@ -284,21 +285,12 @@ Zotero.Collection.prototype._initSave = Zotero.Promise.coroutine(function* (env) throw new Error('Collection name is empty'); } - return Zotero.Collection._super.prototype._initSave.apply(this, arguments); -}); - -Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) { - var isNew = env.isNew; - - var collectionID = env.id = this._id = this.id ? this.id : yield Zotero.ID.get('collections'); - var libraryID = env.libraryID = this.libraryID; - var key = env.key = this._key = this.key ? this.key : this._generateKey(); + var proceed = yield Zotero.Collection._super.prototype._initSave.apply(this, arguments); + if (!proceed) return false; - Zotero.debug("Saving collection " + this.id); - - // Verify parent + // Verify parent if (this._parentKey) { - let newParent = Zotero.Collections.getByLibraryAndKey( + let newParent = this.ObjectsClass.getByLibraryAndKey( this.libraryID, this._parentKey ); @@ -314,12 +306,24 @@ Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) throw ('Cannot move collection "' + this.name + '" into one of its own descendents'); } - var parent = newParent.id; + env.parent = newParent.id; } else { - var parent = null; + env.parent = null; } + return true; +}); + +Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) { + var isNew = env.isNew; + + var collectionID = env.id = this._id = this.id ? this.id : yield Zotero.ID.get('collections'); + var libraryID = env.libraryID = this.libraryID; + var key = env.key = this._key = this.key ? this.key : this._generateKey(); + + Zotero.debug("Saving collection " + this.id); + var columns = [ 'collectionID', 'collectionName', @@ -333,7 +337,7 @@ Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) var sqlValues = [ collectionID ? { int: collectionID } : null, { string: this.name }, - parent ? parent : null, + env.parent ? env.parent : null, Zotero.DB.transactionDateTime, this.libraryID ? this.libraryID : 0, key, @@ -362,12 +366,12 @@ Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) if (this._changed.parentKey) { var parentIDs = []; if (this.id && this._previousData.parentKey) { - parentIDs.push(Zotero.Collections.getIDFromLibraryAndKey( + parentIDs.push(this.ObjectsClass.getIDFromLibraryAndKey( this.libraryID, this._previousData.parentKey )); } if (this.parentKey) { - parentIDs.push(Zotero.Collections.getIDFromLibraryAndKey( + parentIDs.push(this.ObjectsClass.getIDFromLibraryAndKey( this.libraryID, this.parentKey )); } @@ -380,7 +384,7 @@ Zotero.Collection.prototype._saveData = Zotero.Promise.coroutine(function* (env) Zotero.Collection.prototype._finalizeSave = Zotero.Promise.coroutine(function* (env) { var isNew = env.isNew; - if (isNew && this.libraryID) { + if (isNew && Zotero.Libraries.isGroupLibrary(this.libraryID)) { var groupID = Zotero.Groups.getGroupIDFromLibraryID(this.libraryID); var group = Zotero.Groups.get(groupID); group.clearCollectionCache(); @@ -395,7 +399,7 @@ Zotero.Collection.prototype._finalizeSave = Zotero.Promise.coroutine(function* ( // Invalidate cached child collections if (env.parentIDs) { - Zotero.Collections.refreshChildCollections(env.parentIDs); + this.ObjectsClass.refreshChildCollections(env.parentIDs); } // New collections have to be reloaded via Zotero.Collections.get(), so mark them as disabled @@ -446,7 +450,7 @@ Zotero.Collection.prototype.addItems = Zotero.Promise.coroutine(function* (itemI continue; } - let item = yield Zotero.Items.getAsync(itemID); + let item = yield this.ChildObjects.getAsync(itemID); yield item.loadCollections(); item.addToCollection(this.id); yield item.save({ @@ -493,7 +497,7 @@ Zotero.Collection.prototype.removeItems = Zotero.Promise.coroutine(function* (it continue; } - let item = yield Zotero.Items.getAsync(itemID); + let item = yield this.ChildObjects.getAsync(itemID); yield item.loadCollections(); item.removeFromCollection(this.id); yield item.save({ @@ -545,7 +549,7 @@ Zotero.Collection.prototype.diff = function (collection, includeMatches) { var diff = []; var thisData = this.serialize(); var otherData = collection.serialize(); - var numDiffs = Zotero.Collections.diff(thisData, otherData, diff, includeMatches); + var numDiffs = this.ObjectsClass.diff(thisData, otherData, diff, includeMatches); // For the moment, just compare children and increase numDiffs if any differences var d1 = Zotero.Utilities.arrayDiff( @@ -605,7 +609,7 @@ Zotero.Collection.prototype.clone = function (includePrimary, newCollection) { var sameLibrary = newCollection.libraryID == this.libraryID; } else { - var newCollection = new Zotero.Collection; + var newCollection = new this.constructor; var sameLibrary = true; if (includePrimary) { @@ -641,7 +645,7 @@ Zotero.Collection.prototype.erase = function(deleteItems) { // Descendent collections if (descendents[i].type == 'collection') { collections.push(descendents[i].id); - var c = yield Zotero.Collections.getAsync(descendents[i].id); + var c = yield this.ObjectsClass.getAsync(descendents[i].id); if (c) { notifierData[c.id] = { old: c.toJSON() }; } @@ -655,7 +659,7 @@ Zotero.Collection.prototype.erase = function(deleteItems) { } } if (del.length) { - yield Zotero.Items.trash(del); + yield this.ChildObjects.trash(del); } // Remove relations @@ -678,9 +682,9 @@ Zotero.Collection.prototype.erase = function(deleteItems) { // TODO: Update member items }.bind(this)) - .then(function () { + .then(() => { // Clear deleted collection from internal memory - Zotero.Collections.unload(collections); + this.ObjectsClass.unload(collections); //return Zotero.Collections.reloadAll(); }) .then(function () { @@ -795,7 +799,7 @@ Zotero.Collection.prototype.getChildren = Zotero.Promise.coroutine(function* (re } if (recursive) { - let child = yield Zotero.Collections.getAsync(children[i].id); + let child = yield this.ObjectsClass.getAsync(children[i].id); let descendents = yield child.getChildren( true, nested, type, includeDeletedItems, level+1 ); @@ -851,7 +855,7 @@ Zotero.Collection.prototype.addLinkedCollection = Zotero.Promise.coroutine(funct var predicate = Zotero.Relations.linkedObjectPredicate; if ((yield Zotero.Relations.getByURIs(url1, predicate, url2)).length || (yield Zotero.Relations.getByURIs(url2, predicate, url1)).length) { - Zotero.debug("Collections " + this.key + " and " + collection.key + " are already linked"); + Zotero.debug(this._ObjectTypePlural + " " + this.key + " and " + collection.key + " are already linked"); return false; } @@ -883,7 +887,7 @@ Zotero.Collection.prototype.loadChildCollections = Zotero.Promise.coroutine(func if (ids) { for each(var id in ids) { - var col = yield Zotero.Collections.getAsync(id); + var col = yield this.ObjectsClass.getAsync(id); if (!col) { throw new Error('Collection ' + id + ' not found'); } @@ -923,7 +927,7 @@ Zotero.Collection.prototype.loadChildItems = Zotero.Promise.coroutine(function* this._childItems = []; if (ids) { - var items = yield Zotero.Items.getAsync(ids) + var items = yield this.ChildObjects.getAsync(ids) if (items) { this._childItems = items; } diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js index 271b03b147..f2b86ad8ae 100644 --- a/chrome/content/zotero/xpcom/data/dataObject.js +++ b/chrome/content/zotero/xpcom/data/dataObject.js @@ -34,6 +34,8 @@ Zotero.DataObject = function () { let objectType = this._objectType; this._ObjectType = objectType[0].toUpperCase() + objectType.substr(1); this._objectTypePlural = Zotero.DataObjectUtilities.getObjectTypePlural(objectType); + this._ObjectTypePlural = this._objectTypePlural[0].toUpperCase() + this._objectTypePlural.substr(1); + this._ObjectsClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(objectType); this._id = null; this._libraryID = null; @@ -53,7 +55,7 @@ Zotero.DataObject = function () { }; Zotero.DataObject.prototype._objectType = 'dataObject'; -Zotero.DataObject.prototype._dataTypes = []; +Zotero.DataObject.prototype._dataTypes = ['primaryData']; Zotero.defineProperty(Zotero.DataObject.prototype, 'objectType', { get: function() this._objectType @@ -64,6 +66,9 @@ Zotero.defineProperty(Zotero.DataObject.prototype, 'id', { Zotero.defineProperty(Zotero.DataObject.prototype, 'libraryID', { get: function() this._libraryID }); +Zotero.defineProperty(Zotero.DataObject.prototype, 'key', { + get: function() this._key +}); Zotero.defineProperty(Zotero.DataObject.prototype, 'libraryKey', { get: function() this._libraryID + "/" + this._key }); @@ -77,7 +82,7 @@ Zotero.defineProperty(Zotero.DataObject.prototype, 'parentID', { }); Zotero.defineProperty(Zotero.DataObject.prototype, 'ObjectsClass', { - get: function() Zotero.DataObjectUtilities.getObjectsClassForObjectType(this.objectType) + get: function() this._ObjectsClass }); diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 2a1e2fdc54..6fd8687e5f 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -92,8 +92,11 @@ Zotero.Item = function(itemTypeOrID) { Zotero.extendClass(Zotero.DataObject, Zotero.Item); Zotero.Item.prototype._objectType = 'item'; +Zotero.defineProperty(Zotero.Item.prototype, 'ContainerObjectsClass', { + get: function() Zotero.Collections +}); + Zotero.Item.prototype._dataTypes = Zotero.Item._super.prototype._dataTypes.concat([ - 'primaryData', 'itemData', 'note', 'creators', @@ -173,7 +176,7 @@ Zotero.Item.prototype.getType = function() { Zotero.Item.prototype.isPrimaryField = function (fieldName) { Zotero.debug("Zotero.Item.isPrimaryField() is deprecated -- use Zotero.Items.isPrimaryField()"); - return Zotero.Items.isPrimaryField(fieldName); + return this.ObjectsClass.isPrimaryField(fieldName); } Zotero.Item.prototype._get = function (fieldName) { @@ -228,7 +231,7 @@ Zotero.Item.prototype.getField = function(field, unformatted, includeBaseMapped) } else if (creators.length > 3) { return creatorsData[0].lastName + " " + Zotero.getString('general.etAl'); } - } else if (field === 'id' || Zotero.Items.isPrimaryField(field)) { + } else if (field === 'id' || this.ObjectsClass.isPrimaryField(field)) { var privField = '_' + field; //Zotero.debug('Returning ' + (this[privField] ? this[privField] : '') + ' (typeof ' + typeof this[privField] + ')'); return this[privField]; @@ -313,12 +316,12 @@ Zotero.Item.prototype.loadPrimaryData = Zotero.Promise.coroutine(function* (relo } var columns = [], join = [], where = []; - var primaryFields = Zotero.Items.primaryFields; + var primaryFields = this.ObjectsClass.primaryFields; for (let i=0; i { + var aTitle = this.ObjectsClass.getSortTitle(a.title); + var bTitle = this.ObjectsClass.getSortTitle(b.title); return collation.compareString(1, aTitle, bTitle); }); } @@ -2461,7 +2464,7 @@ Zotero.Item.prototype._updateAttachmentStates = function (exists) { } try { - var item = Zotero.Items.getByLibraryAndKey(this.libraryID, parentKey); + var item = this.ObjectsClass.getByLibraryAndKey(this.libraryID, parentKey); } catch (e) { if (e instanceof Zotero.Exception.UnloadedDataException) { @@ -3121,7 +3124,7 @@ Zotero.Item.prototype.getBestAttachments = Zotero.Promise.coroutine(function* () + "AND IA.itemID NOT IN (SELECT itemID FROM deletedItems) " + "ORDER BY contentType='application/pdf' DESC, value=? DESC, dateAdded ASC"; var itemIDs = yield Zotero.DB.columnQueryAsync(sql, [this.id, Zotero.Attachments.LINK_MODE_LINKED_URL, url]); - return Zotero.Items.get(itemIDs); + return this.ObjectsClass.get(itemIDs); }); @@ -3377,7 +3380,7 @@ Zotero.Item.prototype.setCollections = function (collectionIDsOrKeys) { var collectionIDs = collectionIDsOrKeys.map(function (val) { return parseInt(val) == val ? parseInt(val) - : Zotero.Collections.getIDFromLibraryAndKey(this.libraryID, val); + : this.ContainerObjectsClass.getIDFromLibraryAndKey(this.libraryID, val); }.bind(this)); collectionIDs = Zotero.Utilities.arrayUnique(collectionIDs); @@ -3402,7 +3405,7 @@ Zotero.Item.prototype.setCollections = function (collectionIDsOrKeys) { Zotero.Item.prototype.addToCollection = function (collectionIDOrKey) { var collectionID = parseInt(collectionIDOrKey) == collectionIDOrKey ? parseInt(collectionIDOrKey) - : Zotero.Collections.getIDFromLibraryAndKey(this.libraryID, collectionIDOrKey) + : this.ContainerObjectsClass.getIDFromLibraryAndKey(this.libraryID, collectionIDOrKey) if (!collectionID) { throw new Error("Invalid collection '" + collectionIDOrKey + "'"); @@ -3427,7 +3430,7 @@ Zotero.Item.prototype.addToCollection = function (collectionIDOrKey) { Zotero.Item.prototype.removeFromCollection = function (collectionIDOrKey) { var collectionID = parseInt(collectionIDOrKey) == collectionIDOrKey ? parseInt(collectionIDOrKey) - : Zotero.Collections.getIDFromLibraryAndKey(this.libraryID, collectionIDOrKey) + : this.ContainerObjectsClass.getIDFromLibraryAndKey(this.libraryID, collectionIDOrKey) if (!collectionID) { throw new Error("Invalid collection '" + collectionIDOrKey + "'"); @@ -3576,7 +3579,7 @@ Zotero.Item.prototype.diff = function (item, includeMatches, ignoreFields) { var thisData = this.serialize(); var otherData = item.serialize(); - var numDiffs = Zotero.Items.diff(thisData, otherData, diff, includeMatches); + var numDiffs = this.ObjectsClass.diff(thisData, otherData, diff, includeMatches); diff[0].creators = []; diff[1].creators = []; @@ -3712,7 +3715,7 @@ Zotero.Item.prototype.multiDiff = Zotero.Promise.coroutine(function* (otherItems let otherItem = otherItems[i]; let diff = []; let otherData = yield otherItem.toJSON(); - let numDiffs = Zotero.Items.diff(thisData, otherData, diff); + let numDiffs = this.ObjectsClass.diff(thisData, otherData, diff); if (numDiffs) { for (let field in diff[1]) { @@ -3846,13 +3849,13 @@ Zotero.Item.prototype.erase = Zotero.Promise.coroutine(function* () { var parentCollectionIDs = this.collections; if (parentCollectionIDs) { for (var i=0; i