From 15722e5022b8aa2e9aebc174e0e98a2de4c49f15 Mon Sep 17 00:00:00 2001 From: Aurimas Vinckevicius Date: Thu, 28 Aug 2014 18:15:43 -0500 Subject: [PATCH] Allow calling Zotero.Translate.*.translate without setting translator first. This simply means that detection code will be run first. Attempting this with Export translators will fail, because trying to detect a translator does not make sense in this case. --- .../zotero/xpcom/translation/translate.js | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js index 38976250f0..abc3252e06 100644 --- a/chrome/content/zotero/xpcom/translation/translate.js +++ b/chrome/content/zotero/xpcom/translation/translate.js @@ -1112,13 +1112,23 @@ Zotero.Translate.Base.prototype = { * @param {Boolean} [saveAttachments=true] Exclude attachments (e.g., snapshots) on import */ "translate":function(libraryID, saveAttachments) { // initialize properties specific to each translation - this._currentState = "translate"; - if(!this.translator || !this.translator.length) { - this.complete(false, new Error("No translator specified")); + var args = arguments; + Zotero.debug("Translate: translate called without specifying a translator. Running detection first."); + this.setHandler('translators', function(me, translators) { + if(!translators.length) { + me.complete(false, "Could not find an appropriate translator"); + } else { + me.setTranslator(translators); + Zotero.Translate.Base.prototype.translate.apply(me, args); + } + }); + this.getTranslators(); return; } + this._currentState = "translate"; + this._libraryID = libraryID; this._saveAttachments = saveAttachments === undefined || saveAttachments; this._savingAttachments = []; @@ -2158,6 +2168,19 @@ Zotero.Translate.Export.prototype._prepareTranslation = function() { this._sandboxManager.importObject(this._io); } +/** + * Overload Zotero.Translate.Base#translate to make sure that + * Zotero.Translate.Export#translate is not called without setting a + * translator first. Doesn't make sense to run detection for export. + */ +Zotero.Translate.Export.prototype.translate = function() { + if(!this.translator || !this.translator.length) { + this.complete(false, new Error("Export translation initiated without setting a translator")); + } else { + Zotero.Translate.Base.prototype.translate.apply(this, arguments); + } +}; + /** * Return the progress of the import operation, or null if progress cannot be determined */