- Add ability to pass a callback to Zotero.getTranslators(). AFAIK, this is only used by COinS.
- Don't try to do RPC on non-web translators - Remove Zotero.Text.titleCase from zotero.js
This commit is contained in:
parent
3dcb45d6ac
commit
bd165f60bc
2 changed files with 40 additions and 69 deletions
|
@ -210,7 +210,25 @@ Zotero.Translate.Sandbox = {
|
|||
throw new Error("Translator "+translate.translator[0].translatorID+" attempted to call invalid translatorID "+arg);
|
||||
}
|
||||
};
|
||||
safeTranslator.getTranslators = function() { return translation.getTranslators() };
|
||||
|
||||
safeTranslator.getTranslators = function(callback) {
|
||||
if(callback) {
|
||||
translate.incrementAsyncProcesses();
|
||||
translation.clearHandlers("translators");
|
||||
translation.setHandler("translators", function(obj, translators) {
|
||||
translate.decrementAsyncProcesses();
|
||||
callback(translators);
|
||||
});
|
||||
translation.getTranslators();
|
||||
} else if(Zotero.isConnector) {
|
||||
throw new Error("Translator must pass a callback to getTranslatorObject() to "+
|
||||
"operate in this translation environment.");
|
||||
} else {
|
||||
Zotero.debug("Translate: COMPAT WARNING: Translator must pass a callback to getTranslators() to operate in connector");
|
||||
return translation.getTranslators();
|
||||
}
|
||||
};
|
||||
|
||||
var doneHandlerSet = false;
|
||||
safeTranslator.translate = function() {
|
||||
translate.incrementAsyncProcesses();
|
||||
|
@ -221,9 +239,14 @@ Zotero.Translate.Sandbox = {
|
|||
}
|
||||
return translation.translate(false);
|
||||
};
|
||||
// TODO
|
||||
|
||||
safeTranslator.getTranslatorObject = function(callback) {
|
||||
if(callback) translate.incrementAsyncProcesses();
|
||||
if(callback) {
|
||||
translate.incrementAsyncProcesses();
|
||||
} else {
|
||||
Zotero.debug("Translate: COMPAT WARNING: Translator must pass a callback to getTranslatorObject() to operate in connector");
|
||||
}
|
||||
|
||||
var haveTranslatorFunction = function(translator) {
|
||||
translation.translator[0] = translator;
|
||||
if(!Zotero._loadTranslator(translator)) throw new Error("Translator could not be loaded");
|
||||
|
@ -262,7 +285,7 @@ Zotero.Translate.Sandbox = {
|
|||
return translation._sandboxManager.sandbox;
|
||||
} else {
|
||||
if(Zotero.isConnector && !callback) {
|
||||
throw new Error("Translator must accept a callback to getTranslatorObject() to "+
|
||||
throw new Error("Translator must pass a callback to getTranslatorObject() to "+
|
||||
"operate in this translation environment.");
|
||||
}
|
||||
|
||||
|
@ -365,7 +388,7 @@ Zotero.Translate.Sandbox = {
|
|||
if(haveAsyncHandler) translate.incrementAsyncProcesses();
|
||||
return false;
|
||||
} else {
|
||||
translate._debug("WARNING: No callback was provided for "+
|
||||
translate._debug("Translate: COMPAT WARNING: No callback was provided for "+
|
||||
"Zotero.selectItems(). When executed outside of Firefox, a selectItems() call "+
|
||||
"will require that this translator to be called multiple times.", 1);
|
||||
|
||||
|
@ -678,8 +701,12 @@ Zotero.Translate.Base.prototype = {
|
|||
*/
|
||||
"incrementAsyncProcesses":function() {
|
||||
this._runningAsyncProcesses++;
|
||||
Zotero.debug("Translate: Incremented asynchronous processes to "+this._runningAsyncProcesses, 4);
|
||||
if(this._parentTranslator) this._parentTranslator.incrementAsyncProcesses();
|
||||
if(this._parentTranslator) {
|
||||
this._parentTranslator.incrementAsyncProcesses();
|
||||
} else {
|
||||
Zotero.debug("Translate: Incremented asynchronous processes to "+this._runningAsyncProcesses, 4);
|
||||
//Zotero.debug((new Error()).stack);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -687,7 +714,10 @@ Zotero.Translate.Base.prototype = {
|
|||
*/
|
||||
"decrementAsyncProcesses":function(by) {
|
||||
this._runningAsyncProcesses -= (by ? by : 1);
|
||||
Zotero.debug("Translate: Decremented asynchronous processes to "+this._runningAsyncProcesses, 4);
|
||||
if(!this._parentTranslator) {
|
||||
Zotero.debug("Translate: Decremented asynchronous processes to "+this._runningAsyncProcesses, 4);
|
||||
//Zotero.debug((new Error()).stack);
|
||||
}
|
||||
if(this._runningAsyncProcesses === 0) {
|
||||
this.complete();
|
||||
}
|
||||
|
@ -784,12 +814,12 @@ Zotero.Translate.Base.prototype = {
|
|||
var translator = allPotentialTranslators[i];
|
||||
if(translator.runMode === Zotero.Translator.RUN_MODE_IN_BROWSER) {
|
||||
this._potentialTranslators.push(translator);
|
||||
} else {
|
||||
} else if(this instanceof Zotero.Translate.Web) {
|
||||
this._waitingForRPC = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(this._waitingForRPC && this instanceof Zotero.Translate.Web) {
|
||||
if(this._waitingForRPC) {
|
||||
var me = this;
|
||||
Zotero.Connector.callMethod("detect", {"uri":this.location.toString(),
|
||||
"cookie":this.document.cookie,
|
||||
|
|
|
@ -2242,65 +2242,6 @@ Zotero.Hash.prototype.has = function(in_key){
|
|||
return typeof(this.items[in_key]) != 'undefined';
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton for common text formatting routines
|
||||
**/
|
||||
Zotero.Text = new function() {
|
||||
this.titleCase = titleCase;
|
||||
|
||||
var skipWords = ["but", "or", "yet", "so", "for", "and", "nor", "a", "an",
|
||||
"the", "at", "by", "from", "in", "into", "of", "on", "to", "with", "up",
|
||||
"down", "as"];
|
||||
// this may only match a single character
|
||||
var delimiterRegexp = /([ \/\-–—])/;
|
||||
|
||||
function titleCase(string) {
|
||||
if (!string) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// split words
|
||||
var words = string.split(delimiterRegexp);
|
||||
var isUpperCase = string.toUpperCase() == string;
|
||||
|
||||
var newString = "";
|
||||
var delimiterOffset = words[0].length;
|
||||
var lastWordIndex = words.length-1;
|
||||
var previousWordIndex = -1;
|
||||
for(var i=0; i<=lastWordIndex; i++) {
|
||||
// only do manipulation if not a delimiter character
|
||||
if(words[i].length != 0 && (words[i].length != 1 || !delimiterRegexp.test(words[i]))) {
|
||||
var upperCaseVariant = words[i].toUpperCase();
|
||||
var lowerCaseVariant = words[i].toLowerCase();
|
||||
|
||||
// only use if word does not already possess some capitalization
|
||||
if(isUpperCase || words[i] == lowerCaseVariant) {
|
||||
if(
|
||||
// a skip word
|
||||
skipWords.indexOf(lowerCaseVariant.replace(/[^a-zA-Z]+/, "")) != -1
|
||||
// not first or last word
|
||||
&& i != 0 && i != lastWordIndex
|
||||
// does not follow a colon
|
||||
&& (previousWordIndex == -1 || words[previousWordIndex][words[previousWordIndex].length-1] != ":")
|
||||
) {
|
||||
words[i] = lowerCaseVariant;
|
||||
} else {
|
||||
// this is not a skip word or comes after a colon;
|
||||
// we must capitalize
|
||||
words[i] = upperCaseVariant[0] + lowerCaseVariant.substr(1);
|
||||
}
|
||||
}
|
||||
|
||||
previousWordIndex = i;
|
||||
}
|
||||
|
||||
newString += words[i];
|
||||
}
|
||||
|
||||
return newString;
|
||||
}
|
||||
}
|
||||
|
||||
Zotero.DragDrop = {
|
||||
currentDataTransfer: null,
|
||||
|
||||
|
|
Loading…
Reference in a new issue