Don't require parent item to be loaded when setting item parent

This commit is contained in:
Dan Stillman 2015-04-25 03:12:19 -04:00
parent 6126a49fea
commit 8ff258fe03

View file

@ -1338,24 +1338,24 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
} }
// Parent item // Parent item
let parentItem = this.parentKey; var parentItemKey = this.parentKey;
parentItem = parentItem ? this.ObjectsClass.getByLibraryAndKey(this.libraryID, parentItem) : null; var parentItemID = this.ObjectsClass.getIDFromLibraryAndKey(this.libraryID, parentItemKey) || null;
if (this._changed.parentKey) { if (this._changed.parentKey) {
if (isNew) { if (isNew) {
if (!parentItem) { if (!parentItemID) {
// TODO: clear caches? // TODO: clear caches?
let msg = this._parentKey + " is not a valid item key"; let msg = parentItemKey + " is not a valid item key";
throw new Zotero.Error(msg, "MISSING_OBJECT"); throw new Zotero.Error(msg, "MISSING_OBJECT");
} }
let newParentItemNotifierData = {}; let newParentItemNotifierData = {};
//newParentItemNotifierData[newParentItem.id] = {}; //newParentItemNotifierData[newParentItem.id] = {};
Zotero.Notifier.trigger('modify', 'item', parentItem.id, newParentItemNotifierData); Zotero.Notifier.trigger('modify', 'item', parentItemID, newParentItemNotifierData);
switch (Zotero.ItemTypes.getName(itemTypeID)) { switch (Zotero.ItemTypes.getName(itemTypeID)) {
case 'note': case 'note':
case 'attachment': case 'attachment':
reloadParentChildItems[parentItem.id] = true; reloadParentChildItems[parentItemID] = true;
break; break;
} }
} }
@ -1363,25 +1363,25 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
let type = Zotero.ItemTypes.getName(itemTypeID); let type = Zotero.ItemTypes.getName(itemTypeID);
let Type = type[0].toUpperCase() + type.substr(1); let Type = type[0].toUpperCase() + type.substr(1);
if (this._parentKey) { if (parentItemKey) {
if (!parentItem) { if (!parentItemID) {
// TODO: clear caches // TODO: clear caches
let msg = "Cannot set source to invalid item " + this._parentKey; let msg = "Cannot set source to invalid item " + parentItemKey;
throw new Zotero.Error(msg, "MISSING_OBJECT"); throw new Zotero.Error(msg, "MISSING_OBJECT");
} }
let newParentItemNotifierData = {}; let newParentItemNotifierData = {};
//newParentItemNotifierData[newParentItem.id] = {}; //newParentItemNotifierData[newParentItem.id] = {};
Zotero.Notifier.trigger('modify', 'item', parentItem.id, newParentItemNotifierData); Zotero.Notifier.trigger('modify', 'item', parentItemID, newParentItemNotifierData);
} }
var oldParentKey = this._previousData.parentKey; let oldParentKey = this._previousData.parentKey;
if (oldParentKey) { if (oldParentKey) {
var oldParentItem = this.ObjectsClass.getByLibraryAndKey(this.libraryID, oldParentKey); let oldParentItemID = this.ObjectsClass.getIDFromLibraryAndKey(this.libraryID, oldParentKey);
if (oldParentItem) { if (oldParentItemID) {
let oldParentItemNotifierData = {}; let oldParentItemNotifierData = {};
//oldParentItemNotifierData[oldParentItem.id] = {}; //oldParentItemNotifierData[oldParentItemID] = {};
Zotero.Notifier.trigger('modify', 'item', oldParentItem.id, oldParentItemNotifierData); Zotero.Notifier.trigger('modify', 'item', oldParentItemID, oldParentItemNotifierData);
} }
else { else {
Zotero.debug("Old source item " + oldParentKey Zotero.debug("Old source item " + oldParentKey
@ -1394,7 +1394,10 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
if (!oldParentKey) { if (!oldParentKey) {
let sql = "SELECT collectionID FROM collectionItems WHERE itemID=?"; let sql = "SELECT collectionID FROM collectionItems WHERE itemID=?";
let changedCollections = yield Zotero.DB.columnQueryAsync(sql, this.id); let changedCollections = yield Zotero.DB.columnQueryAsync(sql, this.id);
if (changedCollections) { if (changedCollections.length) {
let parentItem = yield this.ObjectsClass.getByLibraryAndKeyAsync(
this.libraryID, oldParentKey
)
for (let i=0; i<changedCollections.length; i++) { for (let i=0; i<changedCollections.length; i++) {
yield parentItem.loadCollections(); yield parentItem.loadCollections();
parentItem.addToCollection(changedCollections[i]); parentItem.addToCollection(changedCollections[i]);
@ -1407,7 +1410,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
changedCollections[i] + '-' + this.id changedCollections[i] + '-' + this.id
); );
} }
parentItem.save({ yield parentItem.save({
skipDateModifiedUpdate: true skipDateModifiedUpdate: true
}); });
} }
@ -1416,18 +1419,18 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
// Update DB, if not a note or attachment we're changing below // Update DB, if not a note or attachment we're changing below
if (!this._changed.attachmentData && if (!this._changed.attachmentData &&
(!this._changed.note || !this.isNote())) { (!this._changed.note || !this.isNote())) {
var sql = "UPDATE item" + Type + "s SET parentItemID=? " let sql = "UPDATE item" + Type + "s SET parentItemID=? "
+ "WHERE itemID=?"; + "WHERE itemID=?";
var bindParams = [parentItem ? parentItem.id : null, this.id]; let bindParams = [parentItemID, this.id];
yield Zotero.DB.queryAsync(sql, bindParams); yield Zotero.DB.queryAsync(sql, bindParams);
} }
// Update the counts of the previous and new sources // Update the counts of the previous and new sources
if (oldParentItem) { if (oldParentItemID) {
reloadParentChildItems[oldParentItem.id] = true; reloadParentChildItems[oldParentItemID] = true;
} }
if (parentItem) { if (parentItemID) {
reloadParentChildItems[parentItem.id] = true; reloadParentChildItems[parentItemID] = true;
} }
} }
} }
@ -1458,8 +1461,8 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
Zotero.Notifier.trigger('trash', 'item', this.id); Zotero.Notifier.trigger('trash', 'item', this.id);
} }
if (parentItem) { if (parentItemID) {
reloadParentChildItems[parentItem.id] = true; reloadParentChildItems[parentItemID] = true;
} }
} }
@ -1497,8 +1500,8 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
} }
yield Zotero.DB.queryAsync(sql, params); yield Zotero.DB.queryAsync(sql, params);
if (parentItem) { if (parentItemID) {
reloadParentChildItems[parentItem.id] = true; reloadParentChildItems[parentItemID] = true;
} }
} }
@ -1507,15 +1510,14 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
// //
if (!isNew) { if (!isNew) {
// If attachment title changes, update parent attachments // If attachment title changes, update parent attachments
if (this._changed.itemData && this._changed.itemData[110] && this.isAttachment() && parentItem) { if (this._changed.itemData && this._changed.itemData[110] && this.isAttachment() && parentItemID) {
reloadParentChildItems[parentItem.id] = true; reloadParentChildItems[parentItemID] = true;
} }
} }
if (this.isAttachment() || this._changed.attachmentData) { if (this.isAttachment() || this._changed.attachmentData) {
let sql = "REPLACE INTO itemAttachments (itemID, parentItemID, linkMode, " let sql = "REPLACE INTO itemAttachments (itemID, parentItemID, linkMode, "
+ "contentType, charsetID, path, syncState) VALUES (?,?,?,?,?,?,?)"; + "contentType, charsetID, path, syncState) VALUES (?,?,?,?,?,?,?)";
let parent = this.parentID;
let linkMode = this.attachmentLinkMode; let linkMode = this.attachmentLinkMode;
let contentType = this.attachmentContentType; let contentType = this.attachmentContentType;
let charsetID = yield Zotero.CharacterSets.add(this.attachmentCharset); let charsetID = yield Zotero.CharacterSets.add(this.attachmentCharset);
@ -1538,7 +1540,7 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
let params = [ let params = [
itemID, itemID,
parent ? parent : null, parentItemID,
{ int: linkMode }, { int: linkMode },
contentType ? { string: contentType } : null, contentType ? { string: contentType } : null,
charsetID ? { int: charsetID } : null, charsetID ? { int: charsetID } : null,
@ -1548,8 +1550,8 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
yield Zotero.DB.queryAsync(sql, params); yield Zotero.DB.queryAsync(sql, params);
// Clear cached child attachments of the parent // Clear cached child attachments of the parent
if (!isNew && parentItem) { if (!isNew && parentItemID) {
reloadParentChildItems[parentItem.id] = true; reloadParentChildItems[parentItemID] = true;
} }
} }