- 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
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;
}
}

View file

@ -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) {

View file

@ -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);
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 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) {

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
* 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

View file

@ -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

View file

@ -506,6 +506,23 @@ Zotero.Utilities = {
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
*