www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

commit 1ddb38036eba228d1577968fcbef54a42e9720ff
parent f7c1c56d7d9117e8c4c1dcac40e9518e2462cd39
Author: Adomas VenĨkauskas <adomas.ven@gmail.com>
Date:   Thu,  6 Jul 2017 13:37:44 +0300

Moving Utilities.itemToServerJSON() to the connector codebase

See Zotero.Item.toJSON() for similar functionality

Diffstat:
Mchrome/content/zotero/xpcom/utilities.js | 132-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 132 deletions(-)

diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js @@ -1520,138 +1520,6 @@ Zotero.Utilities = { }, /** - * Converts an item from toArray() format to an array of items in - * the content=json format used by the server - */ - "itemToServerJSON":function(item) { - var newItem = { - "itemKey":Zotero.Utilities.generateObjectKey(), - "itemVersion":0 - }, - newItems = [newItem]; - - var typeID = Zotero.ItemTypes.getID(item.itemType); - if(!typeID) { - Zotero.debug("itemToServerJSON: Invalid itemType "+item.itemType+"; using webpage"); - item.itemType = "webpage"; - typeID = Zotero.ItemTypes.getID(item.itemType); - } - - var fieldID, itemFieldID; - for(var field in item) { - if(field === "complete" || field === "itemID" || field === "attachments" - || field === "seeAlso") continue; - - var val = item[field]; - - if(field === "itemType") { - newItem[field] = val; - } else if(field === "creators") { - // normalize creators - var n = val.length; - var newCreators = newItem.creators = []; - for(var j=0; j<n; j++) { - var creator = val[j]; - - if(!creator.firstName && !creator.lastName) { - Zotero.debug("itemToServerJSON: Silently dropping empty creator"); - continue; - } - - // Single-field mode - if (!creator.firstName || (creator.fieldMode && creator.fieldMode == 1)) { - var newCreator = { - name: creator.lastName - }; - } - // Two-field mode - else { - var newCreator = { - firstName: creator.firstName, - lastName: creator.lastName - }; - } - - // ensure creatorType is present and valid - if(creator.creatorType) { - if(Zotero.CreatorTypes.getID(creator.creatorType)) { - newCreator.creatorType = creator.creatorType; - } else { - Zotero.debug("itemToServerJSON: Invalid creator type "+creator.creatorType+"; falling back to author"); - } - } - if(!newCreator.creatorType) newCreator.creatorType = "author"; - - newCreators.push(newCreator); - } - } else if(field === "tags") { - // normalize tags - var n = val.length; - var newTags = newItem.tags = []; - for(var j=0; j<n; j++) { - var tag = val[j]; - if(typeof tag === "object") { - if(tag.tag) { - tag = tag.tag; - } else if(tag.name) { - tag = tag.name; - } else { - Zotero.debug("itemToServerJSON: Discarded invalid tag"); - continue; - } - } else if(tag === "") { - continue; - } - newTags.push({"tag":tag.toString(), "type":1}); - } - } else if(field === "notes") { - // normalize notes - var n = val.length; - for(var j=0; j<n; j++) { - var note = val[j]; - if(typeof note === "object") { - if(!note.note) { - Zotero.debug("itemToServerJSON: Discarded invalid note"); - continue; - } - note = note.note; - } - newItems.push({"itemType":"note", "parentItem":newItem.itemKey, - "note":note.toString()}); - } - } else if((fieldID = Zotero.ItemFields.getID(field))) { - // if content is not a string, either stringify it or delete it - if(typeof val !== "string") { - if(val || val === 0) { - val = val.toString(); - } else { - continue; - } - } - - // map from base field if possible - if((itemFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(typeID, fieldID))) { - var fieldName = Zotero.ItemFields.getName(itemFieldID); - // Only map if item field does not exist - if(fieldName !== field && !newItem[fieldName]) newItem[fieldName] = val; - continue; // already know this is valid - } - - // if field is valid for this type, set field - if(Zotero.ItemFields.isValidForType(fieldID, typeID)) { - newItem[field] = val; - } else { - Zotero.debug("itemToServerJSON: Discarded field "+field+": field not valid for type "+item.itemType, 3); - } - } else { - Zotero.debug("itemToServerJSON: Discarded unknown field "+field, 3); - } - } - - return newItems; - }, - - /** * Converts an item from toArray() format to citeproc-js JSON * @param {Zotero.Item} zoteroItem * @return {Object|Promise<Object>} A CSL item, or a promise for a CSL item if a Zotero.Item