commit 5635fec4e3ccf9c84556216518d44031cd525767
parent 9f8510c821cf7240efcb538883b7db683c0e7e2a
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 8 May 2015 13:26:11 -0400
Various creator-saving fixes
Diffstat:
3 files changed, 55 insertions(+), 4 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/creators.js b/chrome/content/zotero/xpcom/data/creators.js
@@ -175,6 +175,7 @@ Zotero.Creators = new function() {
switch (field) {
case 'firstName':
case 'lastName':
+ if (val === undefined) continue;
cleanedData[field] = val.trim().normalize();
break;
@@ -184,9 +185,9 @@ Zotero.Creators = new function() {
}
}
- // Handle API JSON format
+ // Handle API JSON .name
if (data.name !== undefined) {
- cleanedData.lastName = data.name.trim();
+ cleanedData.lastName = data.name.trim().normalize();
cleanedData.fieldMode = 1;
}
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -974,6 +974,11 @@ Zotero.Item.prototype.getCreatorsJSON = function () {
* </ul>
*/
Zotero.Item.prototype.setCreator = function (orderIndex, data) {
+ var itemTypeID = this._itemTypeID;
+ if (!itemTypeID) {
+ throw new Error('Item type must be set before setting creators');
+ }
+
this._requireData('creators');
data = Zotero.Creators.cleanData(data);
@@ -983,7 +988,6 @@ Zotero.Item.prototype.setCreator = function (orderIndex, data) {
}
// If creatorTypeID isn't valid for this type, use the primary type
- var itemTypeID = this._itemTypeID;
if (!data.creatorTypeID || !Zotero.CreatorTypes.isValidForItemType(data.creatorTypeID, itemTypeID)) {
var msg = "Creator type '" + Zotero.CreatorTypes.getName(data.creatorTypeID) + "' "
+ "isn't valid for " + Zotero.ItemTypes.getName(itemTypeID)
@@ -4022,7 +4026,6 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
case 'key':
case 'version':
case 'itemType':
- case 'creators':
case 'note':
// Use?
case 'md5':
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -242,6 +242,53 @@ describe("Zotero.Item", function () {
})
});
+ describe("#setCreators", function () {
+ it("should accept an array of creators in API JSON format", function* () {
+ var creators = [
+ {
+ firstName: "First",
+ lastName: "Last",
+ creatorType: "author"
+ },
+ {
+ name: "Test Name",
+ creatorType: "editor"
+ }
+ ];
+
+ var item = new Zotero.Item("journalArticle");
+ item.setCreators(creators);
+ var id = yield item.save();
+ item = yield Zotero.Items.getAsync(id);
+ yield item.loadCreators();
+ assert.sameDeepMembers(item.getCreatorsJSON(), creators);
+ })
+
+ it("should accept an array of creators in internal format", function* () {
+ var creators = [
+ {
+ firstName: "First",
+ lastName: "Last",
+ fieldMode: 0,
+ creatorTypeID: 1
+ },
+ {
+ firstName: "",
+ lastName: "Test Name",
+ fieldMode: 1,
+ creatorTypeID: 2
+ }
+ ];
+
+ var item = new Zotero.Item("journalArticle");
+ item.setCreators(creators);
+ var id = yield item.save();
+ item = yield Zotero.Items.getAsync(id);
+ yield item.loadCreators();
+ assert.sameDeepMembers(item.getCreators(), creators);
+ })
+ })
+
describe("#attachmentCharset", function () {
it("should get and set a value", function* () {
var charset = 'utf-8';