0 && previousIsDouble == 0) {
//attempt double conversion ONLY if previous wasn't double.
doubleConversion = Transliteration.convert(text1.substr(i-1,2));
decal += doubleConversion.length-2
}
if (doubleConversion == "") {
var simpleConversion = Transliteration.convert(text1.charAt(i));
if (simpleConversion =="") {
text2 += text1.charAt(i);
} else {
text2 += simpleConversion;
decal += simpleConversion.length-1;
}
previousIsDouble = 0;
} else {
text2 = text2.substr(0,text2.length-1)+doubleConversion;
previousIsDouble = 1;
}
} else {
// Do not convert, keep as is
text2 = text2+text1.charAt(i);
}
}
return text2;
}
function convertWholeTextToMainTextArea() {
var main = document.getElementById("mainText")
var type = document.getElementById("typeText")
main.value = convertWholeText(type.value);
if (main.selectionStart || main.selectionStart == '0') {
//CURSOR_POSITION_IN_TYPING_FIELD = main.selectionEnd;
}
}
// Transliteration class
function Transliteration(keyName) {
this.direction = "ltr";
this.keyName = keyName;
this.fullName = "LATIN"
this.shortName = "LAT"
this.longName = "Latin (no transliteration)"
this.cols = 60;
this.rows = 15;
this.fontSize = "1.2em";
this.trans = new Object();
//save new translit in savedTranslits static table
Transliteration.savedTranslits[keyName] = this; //As always in JS, you save a REFERENCE!
//i.e. once saved you can still modify it!
if(!Transliteration.current) {
Transliteration.current = this; //Defines the translit we're currently using
}
if(!Transliteration.defaullt) {
Transliteration.defaullt = this; //Defines the current transliteration that you revert to when toggling #
}
if(!Transliteration.previous) {
Transliteration.previous = this; //Defines previous transliteration that you revert to when toggling #
}
this.convertedChar = function(key) {
if (typeof(this.trans[key]) != "undefined") {
return this.trans[key]
}
return "";
}
this.define = function(string1, string2) { //add a conversion
this.trans[string1] = string2;
}
this.defineN = function(string1, num) { //add a conversion using a UNICODE number instead of letter
var string2 = String.fromCharCode(num);
if (string2 != "") this.define(string1,string2);
else alasert("Error trying to defineN("+string1+","+num+"): this number is not a UNICODE character!");
}
this.defineAllCorrespondingCapitals = function() {
//takes All previously defined non-capitals and defines the capitals
//use: define all smalls, then run this, then define special chars which haven't a conversion
//it is a bit systematic, unsubtle!
for (i in this.trans) {
this.define(i.toUpperCase(),this.trans[i].toUpperCase());
}
}
this.HTMLTable = function() {
/* This function is very simple, it just writes an HTML table showing the correspondances.
I am not sure it should be there, as I prefer my Transliteration class to be browser-indept.
*/
var line1 = "";
var line2 = "";
var previousAssoc = -1;
with(this) {
myLoop: for (var assoc in trans) {
// avoid to repeat associations for CAPITALS when they are obvious
upperCaseThenConvert = trans[assoc.toUpperCase()];
convertThenUpperCase = trans[assoc].toUpperCase()
lowerCaseThenConvert = trans[assoc.toLowerCase()];
convertThenLowerCase = trans[assoc].toLowerCase()
//if the two are equal BUT you have a capital,
//THEN, you can skip (it is obvious)
//if (assoc == "aa" || assoc == "AA")
//alert(assoc+" -> "+trans[assoc]+"\nu,c: "+upperCaseThenConvert+"\nc,u: "+convertThenUpperCase);
if(convertThenUpperCase == upperCaseThenConvert
&& convertThenLowerCase == lowerCaseThenConvert
&& assoc.toLowerCase() != assoc ) { //it contains capitals
// you have something of the type: X --> Y and x --> y, you don't want to repeat yourself
continue myLoop;
}
if(lowerCaseThenConvert == trans[assoc] & assoc.toLowerCase() != assoc) {
// you have something of the type jj --> a and JJ --> a (or Jj --> a) keep only smallcase
continue myLoop;
}
if(trans[assoc] != previousAssoc) {
//create new column
if(line2 != "") line2+="";
line2 += ""+assoc;
if(line1 != "") line1+=" | ";
line1+= ""+trans[assoc];
} else {
//write other possible key combination in the same column
line2 +=" "+assoc
}
previousAssoc = trans[assoc];
}
if (line1 == "") line1=" | No conversion made ";
return "\n"
+""+line1+" \n"+line2+" \n ";
}
}
}
// Now the "static" properties and methods
Transliteration.savedTranslits = new Object(); //a table of all current transliterations
Transliteration.convertedChar = function(translitName, string1) {
return Transliteration.savedTranslits[translitName].convertedChar(string1);
}
Transliteration.convert = function(string1) {
return Transliteration.current.convertedChar(string1);
}
Transliteration.choose = function(translitName) {
//set current transliteration
if(typeof(Transliteration.savedTranslits[translitName]) != "undefined") {
//retain Previous transliteration name if you actually change
if (translitName != Transliteration.current.keyName) {
Transliteration.previous = Transliteration.savedTranslits[Transliteration.current.keyName];
}
//choose new current transliteration
Transliteration.current = Transliteration.savedTranslits[translitName];
//alert("Previous: "+Transliteration.previous.keyName
// +"Current: "+Transliteration.current.keyName);
} else {
alert("Error choosing transliteration: "+translitName+" does not exist!");
}
}
Transliteration.setdefaullt = function(translitName) {
//set current transliteration
if(typeof(Transliteration.savedTranslits[translitName]) != "undefined") {
Transliteration.defaullt = Transliteration.savedTranslits[translitName];
} else {
alert("Error choosing transliteration: "+translitName+" does not exist!");
}
}
Transliteration.HTMLTable = function() {
return Transliteration.current.HTMLTable();
}
//end of class definition
defaulltTranslit = new Transliteration("latin"); //automatically adds it to Transliteration.savedTranslits
Transliteration.choose("latin"); //this was done implicitly because defaullt was the only one
Transliteration.setdefaullt("latin");
|