- add ZU.deepCopy()

- fix attachment export/import
- make Translator return a copy of displayOptions or configOptions objects
- adjust file export functions for above change
This commit is contained in:
Simon Kornblith 2010-11-07 03:13:58 +00:00
parent 983673e354
commit 352158b48c
6 changed files with 67 additions and 39 deletions

View file

@ -194,16 +194,17 @@ var Zotero_File_Interface_Export = new function() {
// set options on selected translator and generate optionString // set options on selected translator and generate optionString
var optionString = ""; var optionString = "";
var optionsAvailable = window.arguments[0].selectedTranslator.displayOptions; var optionsAvailable = window.arguments[0].selectedTranslator.displayOptions;
var displayOptions = window.arguments[0].displayOptions = {};
for(var option in optionsAvailable) { for(var option in optionsAvailable) {
var defValue = optionsAvailable[option]; var defValue = optionsAvailable[option];
var element = document.getElementById(OPTION_PREFIX+option); var element = document.getElementById(OPTION_PREFIX+option);
if(option == "exportCharset") { if(option == "exportCharset") {
if(_charsets) { if(_charsets) {
optionsAvailable[option] = element.selectedItem.value; displayOptions[option] = element.selectedItem.value;
} }
} else if(typeof(defValue) == "boolean") { } else if(typeof(defValue) == "boolean") {
optionsAvailable[option] = !!element.checked; displayOptions[option] = !!element.checked;
} }
} }

View file

@ -58,7 +58,7 @@ Zotero_File_Exporter.prototype.save = function() {
fp.init(window, Zotero.getString("fileInterface.export"), nsIFilePicker.modeSave); fp.init(window, Zotero.getString("fileInterface.export"), nsIFilePicker.modeSave);
// set file name and extension // 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 // if the result will be a folder, don't append any extension or use
// filters // filters
fp.defaultString = this.name; fp.defaultString = this.name;
@ -80,6 +80,7 @@ Zotero_File_Exporter.prototype.save = function() {
translation.setLocation(fp.file); translation.setLocation(fp.file);
translation.setTranslator(io.selectedTranslator); translation.setTranslator(io.selectedTranslator);
translation.setDisplayOptions(io.displayOptions);
translation.setHandler("done", this._exportDone); translation.setHandler("done", this._exportDone);
Zotero.UnresponsiveScriptIndicator.disable(); Zotero.UnresponsiveScriptIndicator.disable();
Zotero_File_Interface.Progress.show( Zotero_File_Interface.Progress.show(
@ -259,7 +260,8 @@ var Zotero_File_Interface = new function() {
function _importTranslatorsAvailable(translation, translators) { function _importTranslatorsAvailable(translation, translators) {
if(translators.length) { if(translators.length) {
if(translation.location instanceof Components.interfaces.nsIFile) { 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(); var allCollections = Zotero.getCollections();
for(var i=0; i<allCollections.length; i++) { for(var i=0; i<allCollections.length; i++) {
if(allCollections[i].name == collectionName) { if(allCollections[i].name == collectionName) {

View file

@ -74,43 +74,46 @@ Zotero.Translate.ItemSaver.ATTACHMENT_MODE_FILE = 2;
Zotero.Translate.ItemSaver.prototype = { Zotero.Translate.ItemSaver.prototype = {
"saveItem":function(item) { "saveItem":function(item) {
// Get typeID, defaulting to "webpage" // Get typeID, defaulting to "webpage"
var newItem;
var type = (item.itemType ? item.itemType : "webpage"); var type = (item.itemType ? item.itemType : "webpage");
if(type == "note") { // handle notes differently if(type == "note") { // handle notes differently
var newItem = new Zotero.Item('note'); newItem = new Zotero.Item('note');
newItem.libraryID = this._libraryID; newItem.libraryID = this._libraryID;
newItem.setNote(item.note); newItem.setNote(item.note);
var myID = newItem.save(); var myID = newItem.save();
var newItem = Zotero.Items.get(myID); newItem = Zotero.Items.get(myID);
} else { } else {
if(type == "attachment") { // handle attachments differently if(type == "attachment") { // handle attachments differently
var newItem = this._saveAttachment(item); newItem = this._saveAttachment(item);
var myID = newItem.id;
} else { } else {
var typeID = Zotero.ItemTypes.getID(type); var typeID = Zotero.ItemTypes.getID(type);
var newItem = new Zotero.Item(typeID); newItem = new Zotero.Item(typeID);
newItem._libraryID = this._libraryID; newItem._libraryID = this._libraryID;
}
this._saveFields(item, newItem); this._saveFields(item, newItem);
// handle creators // handle creators
if(item.creators) { if(item.creators) {
this._saveCreators(item, newItem); this._saveCreators(item, newItem);
} }
// save item // save item
var myID = newItem.save(); var myID = newItem.save();
newItem = Zotero.Items.get(myID); newItem = Zotero.Items.get(myID);
// handle notes // handle notes
if(item.notes) { if(item.notes) {
this._saveNotes(item, myID); this._saveNotes(item, myID);
} }
// handle attachments // handle attachments
if(item.attachments) { if(item.attachments) {
for(var i=0; i<item.attachments.length; i++) { for(var i=0; i<item.attachments.length; i++) {
this._saveAttachment(item.attachments[i], myID); 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.save();
newItem = Zotero.Items.get(myID);
return newItem; return newItem;
}, },
@ -539,11 +541,11 @@ Zotero.Translate.ItemGetter.prototype = {
} }
}, },
"exportFiles":function(dir) { "exportFiles":function(dir, extension) {
// generate directory // generate directory
var directory = Components.classes["@mozilla.org/file/local;1"]. var directory = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile); createInstance(Components.interfaces.nsILocalFile);
directory.initWithFile(this.location.parent); directory.initWithFile(dir.parent);
// delete this file if it exists // delete this file if it exists
if(dir.exists()) { if(dir.exists()) {
@ -551,7 +553,7 @@ Zotero.Translate.ItemGetter.prototype = {
} }
// get name // get name
var name = this.location.leafName; var name = dir.leafName;
directory.append(name); directory.append(name);
// create directory // create directory
@ -562,7 +564,7 @@ Zotero.Translate.ItemGetter.prototype = {
var location = Components.classes["@mozilla.org/file/local;1"]. var location = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile); createInstance(Components.interfaces.nsILocalFile);
location.initWithFile(directory); location.initWithFile(directory);
location.append(name+"."+this.translator[0].target); location.append(name+"."+extension);
// create files directory // create files directory
this._exportFileDirectory = Components.classes["@mozilla.org/file/local;1"]. this._exportFileDirectory = Components.classes["@mozilla.org/file/local;1"].
@ -683,8 +685,7 @@ Zotero.Translate.ItemGetter.prototype = {
} else { } else {
var returnItemArray = this._itemToArray(returnItem); var returnItemArray = this._itemToArray(returnItem);
// get attachments, although only urls will be passed if exportFileData // get attachments, although only urls will be passed if exportFileData is off
// is off
returnItemArray.attachments = new Array(); returnItemArray.attachments = new Array();
var attachments = returnItem.getAttachments(); var attachments = returnItem.getAttachments();
for each(var attachmentID in attachments) { for each(var attachmentID in attachments) {

View file

@ -1165,7 +1165,7 @@ Zotero.Translate.Export.prototype.setTranslator = function(translator) {
* Sets translator display options. you can also pass a translator (not ID) to * Sets translator display options. you can also pass a translator (not ID) to
* setTranslator that includes a displayOptions argument * setTranslator that includes a displayOptions argument
*/ */
Zotero.Translate.prototype.setDisplayOptions = function(displayOptions) { Zotero.Translate.Export.prototype.setDisplayOptions = function(displayOptions) {
this._displayOptions = displayOptions; this._displayOptions = displayOptions;
} }
@ -1202,7 +1202,7 @@ Zotero.Translate.Export.prototype._prepareTranslation = function(libraryID, save
// export file data, if requested // export file data, if requested
if(this._displayOptions["exportFileData"]) { if(this._displayOptions["exportFileData"]) {
this.location = this._itemGetter.exportFiles(this.location); this.location = this._itemGetter.exportFiles(this.location, this.translator[0].target);
} }
// initialize IO // initialize IO

View file

@ -356,8 +356,8 @@ Zotero.Translator = function(file, json, code) {
return; return;
} }
this.configOptions = info["configOptions"] ? info["configOptions"] : {}; this._configOptions = info["configOptions"] ? info["configOptions"] : {};
this.displayOptions = info["displayOptions"] ? info["displayOptions"] : {}; this._displayOptions = info["displayOptions"] ? info["displayOptions"] : {};
this.browserSupport = info["browserSupport"] ? info["browserSupport"] : "g"; this.browserSupport = info["browserSupport"] ? info["browserSupport"] : "g";
if(this.translatorType & TRANSLATOR_TYPES["import"]) { if(this.translatorType & TRANSLATOR_TYPES["import"]) {
@ -390,6 +390,13 @@ Zotero.Translator = function(file, json, code) {
if(!json) cStream.close(); 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 * Log a translator-related error
* @param {String} message The error message * @param {String} message The error message

View file

@ -506,6 +506,23 @@ Zotero.Utilities = {
nextSet(); nextSet();
}, },
/**
* 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 * Tests if an item type exists
* *