www

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

commit 352158b48ca28233a56ed564ea7e5dc3b4fee690
parent 983673e354615f3393e1fd4ff77e788d660ba731
Author: Simon Kornblith <simon@simonster.com>
Date:   Sun,  7 Nov 2010 03:13:58 +0000

- add ZU.deepCopy()
- fix attachment export/import
- make Translator return a copy of displayOptions or configOptions objects
- adjust file export functions for above change


Diffstat:
Mchrome/content/zotero/exportOptions.js | 5+++--
Mchrome/content/zotero/fileInterface.js | 6++++--
Mchrome/content/zotero/xpcom/translation/item_local.js | 63++++++++++++++++++++++++++++++++-------------------------------
Mchrome/content/zotero/xpcom/translation/translate.js | 4++--
Mchrome/content/zotero/xpcom/translation/translator.js | 11+++++++++--
Mchrome/content/zotero/xpcom/utilities.js | 17+++++++++++++++++
6 files changed, 67 insertions(+), 39 deletions(-)

diff --git a/chrome/content/zotero/exportOptions.js b/chrome/content/zotero/exportOptions.js @@ -194,16 +194,17 @@ var Zotero_File_Interface_Export = new function() { // set options on selected translator and generate optionString var optionString = ""; var optionsAvailable = window.arguments[0].selectedTranslator.displayOptions; + var displayOptions = window.arguments[0].displayOptions = {}; for(var option in optionsAvailable) { var defValue = optionsAvailable[option]; var element = document.getElementById(OPTION_PREFIX+option); if(option == "exportCharset") { if(_charsets) { - optionsAvailable[option] = element.selectedItem.value; + displayOptions[option] = element.selectedItem.value; } } else if(typeof(defValue) == "boolean") { - optionsAvailable[option] = !!element.checked; + displayOptions[option] = !!element.checked; } } diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js @@ -58,7 +58,7 @@ Zotero_File_Exporter.prototype.save = function() { fp.init(window, Zotero.getString("fileInterface.export"), nsIFilePicker.modeSave); // set file name and extension - if(io.selectedTranslator.displayOptions.exportFileData) { + if(io.displayOptions.exportFileData) { // if the result will be a folder, don't append any extension or use // filters fp.defaultString = this.name; @@ -80,6 +80,7 @@ Zotero_File_Exporter.prototype.save = function() { translation.setLocation(fp.file); translation.setTranslator(io.selectedTranslator); + translation.setDisplayOptions(io.displayOptions); translation.setHandler("done", this._exportDone); Zotero.UnresponsiveScriptIndicator.disable(); Zotero_File_Interface.Progress.show( @@ -259,7 +260,8 @@ var Zotero_File_Interface = new function() { function _importTranslatorsAvailable(translation, translators) { if(translators.length) { if(translation.location instanceof Components.interfaces.nsIFile) { - var collectionName = translation.location.leafName.substr(0, translation.location.leafName.lastIndexOf(".")); + var collectionName = (translation.location.isDirectory() ? translation.location.leafName + : translation.location.leafName.substr(0, translation.location.leafName.lastIndexOf("."))); var allCollections = Zotero.getCollections(); for(var i=0; i<allCollections.length; i++) { if(allCollections[i].name == collectionName) { diff --git a/chrome/content/zotero/xpcom/translation/item_local.js b/chrome/content/zotero/xpcom/translation/item_local.js @@ -74,43 +74,46 @@ Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE = 2; Zotero.Translate.ItemSaver.prototype = { "saveItem":function(item) { // Get typeID, defaulting to "webpage" + var newItem; var type = (item.itemType ? item.itemType : "webpage"); if(type == "note") { // handle notes differently - var newItem = new Zotero.Item('note'); + newItem = new Zotero.Item('note'); newItem.libraryID = this._libraryID; newItem.setNote(item.note); var myID = newItem.save(); - var newItem = Zotero.Items.get(myID); + newItem = Zotero.Items.get(myID); } else { if(type == "attachment") { // handle attachments differently - var newItem = this._saveAttachment(item); + newItem = this._saveAttachment(item); + var myID = newItem.id; } else { var typeID = Zotero.ItemTypes.getID(type); - var newItem = new Zotero.Item(typeID); + newItem = new Zotero.Item(typeID); newItem._libraryID = this._libraryID; - } - - this._saveFields(item, newItem); - - // handle creators - if(item.creators) { - this._saveCreators(item, newItem); - } - - // save item - var myID = newItem.save(); - newItem = Zotero.Items.get(myID); - // handle notes - if(item.notes) { - this._saveNotes(item, myID); - } + this._saveFields(item, newItem); + + // handle creators + if(item.creators) { + this._saveCreators(item, newItem); + } + + // save item + var myID = newItem.save(); + newItem = Zotero.Items.get(myID); + + // handle notes + if(item.notes) { + this._saveNotes(item, myID); + } - // handle attachments - if(item.attachments) { - for(var i=0; i<item.attachments.length; i++) { - this._saveAttachment(item.attachments[i], myID); + // handle attachments + if(item.attachments) { + for(var i=0; i<item.attachments.length; i++) { + var newAttachment = this._saveAttachment(item.attachments[i], myID); + if(newAttachment) this._saveTags(item.attachments[i], newAttachment); + } } } } @@ -267,7 +270,6 @@ Zotero.Translate.ItemSaver.prototype = { } newItem.save(); - newItem = Zotero.Items.get(myID); return newItem; }, @@ -539,11 +541,11 @@ Zotero.Translate.ItemGetter.prototype = { } }, - "exportFiles":function(dir) { + "exportFiles":function(dir, extension) { // generate directory var directory = Components.classes["@mozilla.org/file/local;1"]. createInstance(Components.interfaces.nsILocalFile); - directory.initWithFile(this.location.parent); + directory.initWithFile(dir.parent); // delete this file if it exists if(dir.exists()) { @@ -551,7 +553,7 @@ Zotero.Translate.ItemGetter.prototype = { } // get name - var name = this.location.leafName; + var name = dir.leafName; directory.append(name); // create directory @@ -562,7 +564,7 @@ Zotero.Translate.ItemGetter.prototype = { var location = Components.classes["@mozilla.org/file/local;1"]. createInstance(Components.interfaces.nsILocalFile); location.initWithFile(directory); - location.append(name+"."+this.translator[0].target); + location.append(name+"."+extension); // create files directory this._exportFileDirectory = Components.classes["@mozilla.org/file/local;1"]. @@ -683,8 +685,7 @@ Zotero.Translate.ItemGetter.prototype = { } else { var returnItemArray = this._itemToArray(returnItem); - // get attachments, although only urls will be passed if exportFileData - // is off + // get attachments, although only urls will be passed if exportFileData is off returnItemArray.attachments = new Array(); var attachments = returnItem.getAttachments(); for each(var attachmentID in attachments) { diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js @@ -1165,7 +1165,7 @@ Zotero.Translate.Export.prototype.setTranslator = function(translator) { * Sets translator display options. you can also pass a translator (not ID) to * setTranslator that includes a displayOptions argument */ -Zotero.Translate.prototype.setDisplayOptions = function(displayOptions) { +Zotero.Translate.Export.prototype.setDisplayOptions = function(displayOptions) { this._displayOptions = displayOptions; } @@ -1202,7 +1202,7 @@ Zotero.Translate.Export.prototype._prepareTranslation = function(libraryID, save // export file data, if requested if(this._displayOptions["exportFileData"]) { - this.location = this._itemGetter.exportFiles(this.location); + this.location = this._itemGetter.exportFiles(this.location, this.translator[0].target); } // initialize IO diff --git a/chrome/content/zotero/xpcom/translation/translator.js b/chrome/content/zotero/xpcom/translation/translator.js @@ -356,8 +356,8 @@ Zotero.Translator = function(file, json, code) { return; } - this.configOptions = info["configOptions"] ? info["configOptions"] : {}; - this.displayOptions = info["displayOptions"] ? info["displayOptions"] : {}; + this._configOptions = info["configOptions"] ? info["configOptions"] : {}; + this._displayOptions = info["displayOptions"] ? info["displayOptions"] : {}; this.browserSupport = info["browserSupport"] ? info["browserSupport"] : "g"; if(this.translatorType & TRANSLATOR_TYPES["import"]) { @@ -390,6 +390,13 @@ Zotero.Translator = function(file, json, code) { if(!json) cStream.close(); } +Zotero.Translator.prototype.__defineGetter__("displayOptions", function() { + return Zotero.Utilities.deepCopy(this._displayOptions); +}); +Zotero.Translator.prototype.__defineGetter__("configOptions", function() { + return Zotero.Utilities.deepCopy(this._configOptions); +}); + /** * Log a translator-related error * @param {String} message The error message diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js @@ -507,6 +507,23 @@ Zotero.Utilities = { }, /** + * Performs a deep copy of a JavaScript object + * @param {Object} obj + * @return {Object} + */ + "deepCopy":function(obj) { + var obj2 = {}; + for(var i in obj) { + if(typeof obj[i] === "object") { + obj2[i] = Zotero.Utilities.deepCopy(obj[i]); + } else { + obj2[i] = obj[i]; + } + } + return obj2; + }, + + /** * Tests if an item type exists * * @param {String} type Item type