Various fixes for Zotero.Item

This commit is contained in:
Aurimas Vinckevicius 2014-11-14 04:35:26 -06:00
parent 83187eaa35
commit b83bc40426
2 changed files with 32 additions and 24 deletions

View file

@ -885,7 +885,7 @@ Zotero.Collection.prototype.loadChildCollections = Zotero.Promise.coroutine(func
this._childCollections = []; this._childCollections = [];
if (ids) { if (ids.length) {
for each(var id in ids) { for each(var id in ids) {
var col = yield this.ObjectsClass.getAsync(id); var col = yield this.ObjectsClass.getAsync(id);
if (!col) { if (!col) {

View file

@ -69,9 +69,9 @@ Zotero.Item = function(itemTypeOrID) {
this._attachments = null; this._attachments = null;
this._notes = null; this._notes = null;
this._tags = {}; this._tags = [];
this._collections = {}; this._collections = [];
this._relations = {}; this._relations = [];
this._bestAttachmentState = null; this._bestAttachmentState = null;
this._fileExists = null; this._fileExists = null;
@ -291,13 +291,13 @@ Zotero.Item.prototype.getField = function(field, unformatted, includeBaseMapped)
* @param {Boolean} asNames * @param {Boolean} asNames
* @return {Integer{}|String[]} * @return {Integer{}|String[]}
*/ */
Zotero.Item.prototype.getUsedFields = Zotero.Promise.coroutine(function* (asNames) { Zotero.Item.prototype.getUsedFields = function(asNames) {
this._requireData('itemData'); this._requireData('itemData');
return Object.keys(this._itemData) return Object.keys(this._itemData)
.filter(id => this._itemData[id] !== false) .filter(id => this._itemData[id] !== false && this._itemData[id] !== null)
.map(id => asNames ? Zotero.ItemFields.getName(id) : parseInt(id)); .map(id => asNames ? Zotero.ItemFields.getName(id) : parseInt(id));
}); };
@ -1325,7 +1325,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
continue; continue;
} }
let previousCreatorID = this._previousData.creators[orderIndex] let previousCreatorID = !isNew && this._previousData.creators[orderIndex]
? this._previousData.creators[orderIndex].id ? this._previousData.creators[orderIndex].id
: false; : false;
let newCreatorID = yield Zotero.Creators.getIDFromData(creatorData, true); let newCreatorID = yield Zotero.Creators.getIDFromData(creatorData, true);
@ -1603,7 +1603,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
// Collections // Collections
if (this._changed.collections) { if (this._changed.collections) {
let oldCollections = this._previousData.collections; let oldCollections = this._previousData.collections || [];
let newCollections = this._collections; let newCollections = this._collections;
let toAdd = Zotero.Utilities.arrayDiff(newCollections, oldCollections); let toAdd = Zotero.Utilities.arrayDiff(newCollections, oldCollections);
@ -3766,6 +3766,7 @@ Zotero.Item.prototype.clone = function(libraryID, skipTags) {
var sameLibrary = libraryID == this.libraryID; var sameLibrary = libraryID == this.libraryID;
var newItem = new Zotero.Item; var newItem = new Zotero.Item;
newItem.libraryID = libraryID;
newItem.setType(this.itemTypeID); newItem.setType(this.itemTypeID);
var fieldIDs = this.getUsedFields(); var fieldIDs = this.getUsedFields();
@ -3950,7 +3951,7 @@ Zotero.Item.prototype.fromJSON = function (json) {
case 'dateAdded': case 'dateAdded':
case 'dateModified': case 'dateModified':
item[field] = val; this['_'+field] = val;
break; break;
case 'tags': case 'tags':
@ -3999,8 +4000,9 @@ Zotero.Item.prototype.fromJSON = function (json) {
if (!changedFields[field] && if (!changedFields[field] &&
// Invalid fields will already have been cleared by the type change // Invalid fields will already have been cleared by the type change
Zotero.ItemFields.isValidForType( Zotero.ItemFields.isValidForType(
Zotero.ItemFields.getID(field), data.itemTypeID Zotero.ItemFields.getID(field), this.itemTypeID
)) { )
) {
this.setField(field, false); this.setField(field, false);
} }
} }
@ -4009,18 +4011,17 @@ Zotero.Item.prototype.fromJSON = function (json) {
this.deleted = !!json.deleted; this.deleted = !!json.deleted;
// Creators // Creators
var numCreators = 0; let pos = 0;
if (json.creators) { if (json.creators) {
for each (let creator in json.creators) { while (pos<json.creators.length) {
this.setCreator(pos, creator); this.setCreator(pos, json.creators[pos]);
numCreators++; pos++;
} }
} }
// Remove item's remaining creators not in JSON // Remove item's remaining creators not in JSON
var rem = this.numCreators() - numCreators; while (pos < this.numCreators()) {
for (let j = 0; j < rem; j++) {
// Keep removing last creator // Keep removing last creator
this.removeCreator(numCreators); this.removeCreator(this.numCreators() - 1);
} }
// Both notes and attachments might have parents and notes // Both notes and attachments might have parents and notes
@ -4257,21 +4258,22 @@ Zotero.Item.prototype.loadItemData = Zotero.Promise.coroutine(function* (reload)
this._loaded.itemData = true; this._loaded.itemData = true;
this._clearChanged('itemData'); this._clearChanged('itemData');
this.loadDisplayTitle(reload); yield this.loadDisplayTitle(reload);
}); });
Zotero.Item.prototype.loadNote = Zotero.Promise.coroutine(function* (reload) { Zotero.Item.prototype.loadNote = Zotero.Promise.coroutine(function* (reload) {
Zotero.debug("Loading note data for item " + this.libraryKey);
if (this._loaded.note && !reload) { if (this._loaded.note && !reload) {
return; return;
} }
if (!this.isNote() && !this.isAttachment()) { if (!this.isNote() && !this.isAttachment()) {
throw new Error("Can only load note for note or attachment item"); Zotero.debug("Can only load note for note or attachment item");
return;
} }
Zotero.debug("Loading note data for item " + this.libraryKey);
var sql = "SELECT note FROM itemNotes WHERE itemID=?"; var sql = "SELECT note FROM itemNotes WHERE itemID=?";
var row = yield Zotero.DB.rowQueryAsync(sql, this.id); var row = yield Zotero.DB.rowQueryAsync(sql, this.id);
if (row) { if (row) {
@ -4462,6 +4464,12 @@ Zotero.Item.prototype.loadChildItems = Zotero.Promise.coroutine(function* (reloa
return; return;
} }
if (this.isNote() || this.isAttachment()) {
Zotero.debug("Can only load child items for regular item");
return;
}
// Attachments // Attachments
this._attachments = { this._attachments = {
rows: null, rows: null,
@ -4722,7 +4730,7 @@ Zotero.Item.prototype._getOldCreators = function () {
Zotero.Item.prototype._disabledCheck = function () { Zotero.Item.prototype._disabledCheck = function () {
if (this._disabled) { if (this._disabled) {
var msg = "New Zotero.Item objects shouldn't be accessed after save -- use Zotero.Items.get()"; var msg = "New Zotero.Item objects shouldn't be accessed after save -- use Zotero.Items.get()";
Zotero.debug(msg, 2); Zotero.debug(msg, 2, true);
Components.utils.reportError(msg); Components.utils.reportError(msg);
} }
} }