Advertisement
salmancreation

bijoyunicode.js

Feb 13th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Unicode unijoy Parser for writing in webpages
  3.  * This script helps to write unicode bangla using unijoy (or bijoy) keyboard mapping
  4.  *
  5.  * @name Unijoy Unicode Parser
  6.  * @version 1.0 [Date 26th August, 2006]
  7.  * @author Hasin Hayder. Visit My Homepage at http://hasin.wordpress.com
  8.  * @license LGPL
  9.  */
  10.  
  11. /**
  12.  * This script is released under Lesser GNU Public License [LGPL]
  13.  * which implies that you are free to use this script in your
  14.  * web applications without any problem. No warranty ensured. If you like
  15.  * this script, Please acknowledge by keeping a link to my website
  16.  * http://hasin.wordpress.com in the page where you use this script.
  17.  */
  18.  
  19. // Set of Characters
  20. var activeta; //active text area
  21. var unijoy=new Array();
  22.  
  23. // unijoy bangla equivalents
  24. unijoy['j'] = "\u0995"; // ko
  25.  
  26. unijoy['d']='\u09BF'; // hrossho i kar
  27. unijoy['gd']='\u0987'; // hrossho i
  28. unijoy['D']='\u09C0'; // dirgho i kar
  29. unijoy['gD']='\u0988'; // dirgho i
  30. unijoy['c']='\u09C7'; // e kar
  31. unijoy['gc'] = '\u098F'; // E
  32. unijoy['gs'] = '\u0989'; // hrossho u
  33. unijoy['s'] = '\u09C1'; // hrossho u kar
  34. unijoy['S'] = '\u09C2'; // dirgho u kar
  35. unijoy['gS'] = '\u098A'; // dirgho u
  36. unijoy['v']='\u09B0'; // ro
  37. unijoy['a']='\u098B'; // wri
  38. unijoy['f']='\u09BE'; // a kar
  39. unijoy['gf'] = '\u0986'; //shore a
  40. unijoy['F']='\u0985'; // shore ao
  41. //unijoy['ao']='\u0985'; // shore o
  42. unijoy['n']='\u09B8'; // dontyo so
  43. unijoy['t']='\u099f'; // to
  44. unijoy['J'] = '\u0996'; // Kho
  45.  
  46. //unijoy['kh'] = '\u0996'; // kho
  47.  
  48. unijoy['b']='\u09A8'; // dontyo no
  49. unijoy['B']='\u09A3'; // murdhonyo no
  50. unijoy['k']='\u09A4'; // tto
  51. unijoy['K']='\u09A5'; // ttho
  52.  
  53. unijoy['e']='\u09A1'; // ddo
  54. unijoy['E']='\u09A2'; // ddho
  55.  
  56. unijoy['h']='\u09AC'; // bo
  57. unijoy['H']='\u09AD'; // bho
  58. //unijoy['v']='\u09AD'; // bho
  59. //unijoy['rh']='o';  // doye bindu ro
  60. unijoy['p']='\u09DC';    // doye bindu ro
  61. unijoy['P']='\u09DD';    // dhoye bindu ro
  62. unijoy['o']='\u0997';   // go
  63. unijoy['O']='\u0998';   // gho
  64.  
  65. //unijoy['gh']='\u0998'; // gho
  66.  
  67. unijoy['i']='\u09B9';   // ho
  68. unijoy['I']='\u099E';   // yo
  69. unijoy['u']='\u099C';   // borgio jo
  70. unijoy['U']='\u099D'; // jho
  71. //unijoy['jh']='\u099D'; // jho
  72. unijoy['y']='\u099A'; //  cho
  73. unijoy['Y']='\u099B'; // cho
  74. //unijoy['C']='\u099B'; // ccho
  75. unijoy['T']='\u09A0'; // tho
  76. unijoy['r']='\u09AA'; // po
  77. unijoy['R']='\u09AB'; // fo
  78. //unijoy['ph']='\u09AB'; // fo
  79. unijoy['l']='\u09A6'; // do
  80. unijoy['L']='\u09A7'; // dho
  81.  
  82. unijoy['w']='\u09AF';// ontoshyo zo
  83. unijoy['W']='\u09DF';   // ontostho yo
  84. unijoy['q']='\u0999';   // Uma
  85. unijoy['Q']='\u0982';   // uniswor
  86. unijoy['V']='\u09B2';   // lo
  87. unijoy['m']='\u09AE';   // mo
  88. unijoy['M']='\u09B6';   // talobyo sho
  89. unijoy['N']='\u09B7'; // mordhonyo sho
  90. unijoy['gx']= '\u0993';//'\u09CB'; // o
  91. unijoy['X']='\u09CC'; // ou kar
  92. unijoy['gX']='\u0994'; // OU
  93. //unijoy['Ou']='\u0994'; // OU
  94. unijoy['gC']='\u0990'; // Oi
  95. //unijoy['\\']='\u09CE'; // tto
  96. //unijoy['|']='\u0983'; // bisworgo
  97. unijoy["G"] ="\u0964"; // dari
  98. //unijoy[".."] = "."; // fullstop
  99. unijoy['g'] = ' ';//'\u09CD' + '\u200c'; // hosonto
  100. unijoy['&'] = '\u0981'; // chondrobindu
  101. unijoy['Z'] ='\u09CD'+'\u09AF'; // jo fola
  102. unijoy['gh'] ='\u09CD'+ '\u09AC'; // bo fola
  103. unijoy['ga'] ='\u098B';// wri kar
  104. unijoy['a'] ='\u09C3'; // wri
  105. //unijoy['k'] ="\u0995"  + '\u09CD'+ '\u09B8';
  106. unijoy['rZ'] = unijoy['r']+ '\u200c'+ '\u09CD'+'\u09AF';
  107. unijoy['z'] =  '\u09CD'+ unijoy['v'];
  108. unijoy['x'] = '\u09CB';
  109. unijoy['C'] = '\u09C8'; //Oi Kar
  110. unijoy['\\'] = '\u0983';
  111. unijoy['|']='\u09CE'; // bisworgo
  112.  
  113.  
  114.  
  115.  
  116. var carry = '';  //This variable stores each keystrokes
  117. var old_len =0; //This stores length parsed bangla charcter
  118. var ctrlPressed=false;
  119. var first_letter = false;
  120. var lastInserted;
  121.  
  122. isIE=document.all? 1:0;
  123. var switched=false;
  124.  
  125. function checkKeyDown(ev)
  126. {
  127.     //just track the control key
  128.     var e = (window.event) ? event.keyCode : ev.which;
  129.     if (e=='17')
  130.     {
  131.         ctrlPressed = true;
  132.     }
  133. }
  134.  
  135. function checkKeyUp(ev)
  136. {
  137.     //just track the control key
  138.     var e = (window.event) ? event.keyCode : ev.which;
  139.     if (e=='17')
  140.     {
  141.         ctrlPressed = false;
  142.         //alert(ctrlPressed);
  143.     }
  144.  
  145. }
  146.  
  147.  
  148. function parseunijoy(evnt)
  149. {
  150.     // main unijoy parser
  151.     var t = document.getElementById(activeta); // the active text area
  152.     var e = (window.event) ? event.keyCode : evnt.which; // get the keycode
  153.  
  154.     if (e=='113')
  155.     {
  156.         //switch the keyboard mode
  157.         if(ctrlPressed){
  158.             switched = !switched;
  159.             //alert("H"+switched);
  160.             return true;
  161.         }
  162.     }
  163.  
  164.     if (switched) return true;
  165.    
  166.     if(ctrlPressed)
  167.     {
  168.         // user is pressing control, so leave the parsing
  169.         e=0;
  170.     }
  171.  
  172.     var char_e = String.fromCharCode(e); // get the character equivalent to this keycode
  173.    
  174.     if(e==8 || e==32)
  175.     {
  176.         // if space is pressed we have to clear the carry. otherwise there will be some malformed conjunctions
  177.         carry = " ";   
  178.         old_len = 1;
  179.         return;
  180.     }
  181.  
  182.     lastcarry = carry;
  183.     carry += "" + char_e;    //append the current character pressed to the carry
  184.    
  185.     bangla = parseunijoyCarry(carry); // get the combined equivalent
  186.     tempBangla = parseunijoyCarry(char_e); // get the single equivalent
  187.  
  188.     if (tempBangla == ".." || bangla == "..") //that means it has sibling
  189.     {
  190.         return false;
  191.     }
  192.     if (char_e=="g")
  193.     {
  194.         if(carry=="gg")
  195.         {
  196.             // check if it is a plus sign
  197.             insertConjunction('\u09CD' + '\u200c',old_len);
  198.             old_len=1;
  199.             return false;
  200.         }  
  201.         //otherwise this is a simple joiner
  202.         insertAtCursor("\u09CD");old_len = 1;
  203.         carry="g";
  204.         return false;
  205.     }
  206.  
  207.     else if(old_len==0) //first character
  208.     {
  209.         // this is first time someone press a character
  210.         insertConjunction(bangla,1);
  211.         old_len=1;
  212.         return false;
  213.        
  214.     }
  215.  
  216.     else if(char_e=="A")
  217.     {
  218.         //process old style ref
  219.         newChar = unijoy['v']+ '\u09CD' + lastInserted;
  220.         insertConjunction(newChar, lastInserted.length );
  221.         old_len = lastInserted.length;
  222.         return false;
  223.     }
  224.  
  225.    
  226.     else if((bangla == "" && tempBangla !="")) //that means it has no joint equivalent
  227.     {
  228.        
  229.         // there is no joint equivalent - so show the single equivalent.
  230.         bangla = tempBangla;
  231.         if (bangla=="")
  232.         {
  233.             // there is no available equivalent - leave as is
  234.             carry ="";
  235.             return;
  236.         }
  237.        
  238.         else
  239.         {
  240.             // found one equivalent
  241.             carry = char_e;
  242.             insertAtCursor(bangla);
  243.             old_len = bangla.length;
  244.             return false;
  245.         }
  246.     }
  247.     else if(bangla!="")//joint equivalent found
  248.     {
  249.         // we have found some joint equivalent process it
  250.        
  251.         insertConjunction(bangla, old_len);
  252.         old_len = bangla.length;
  253.         return false;
  254.     }
  255. }
  256.  
  257.     function parseunijoyCarry(code)
  258.     {
  259.     //this function just returns a bangla equivalent for a given keystroke
  260.     //or a conjunction
  261.     //just read the array - if found then return the bangla eq.
  262.     //otherwise return a null value
  263.         if (!unijoy[code])  //Oh my god :-( no bangla equivalent for this keystroke
  264.  
  265.         {
  266.             return ''; //return a null value
  267.         }
  268.         else
  269.         {
  270.             return ( unijoy[code]);  //voila - we've found bangla equivalent
  271.         }
  272.  
  273.     }
  274.  
  275.  
  276. function insertAtCursor(myValue) {
  277.     /**
  278.      * this function inserts a character at the current cursor position in a text area
  279.      * many thanks to alex king and phpMyAdmin for this cool function
  280.      *
  281.      * This function is originally found in phpMyAdmin package and modified by Hasin Hayder to meet the requirement
  282.      */
  283.     lastInserted = myValue;
  284.     var myField = document.getElementById(activeta);
  285.     if (document.selection) {
  286.         //alert("hello2");
  287.         myField.focus();
  288.         sel = document.selection.createRange();
  289.         sel.text = myValue;
  290.         sel.collapse(true);
  291.         sel.select();
  292.     }
  293.     //MOZILLA/NETSCAPE support
  294.     else if (myField.selectionStart || myField.selectionStart == 0) {
  295.        
  296.         var startPos = myField.selectionStart;
  297.         var endPos = myField.selectionEnd;
  298.         var scrollTop = myField.scrollTop;
  299.         startPos = (startPos == -1 ? myField.value.length : startPos );
  300.         myField.value = myField.value.substring(0, startPos)
  301.         + myValue
  302.         + myField.value.substring(endPos, myField.value.length);
  303.         myField.focus();
  304.         myField.selectionStart = startPos + myValue.length;
  305.         myField.selectionEnd = startPos + myValue.length;
  306.         myField.scrollTop = scrollTop;
  307.     } else {
  308.         var scrollTop = myField.scrollTop;
  309.         myField.value += myValue;
  310.         myField.focus();
  311.         myField.scrollTop = scrollTop;
  312.     }
  313. }
  314.  
  315. function insertConjunction(myValue, len) {
  316.     /**
  317.      * this function inserts a conjunction and removes previous single character at the current cursor position in a text area
  318.      *
  319.      * This function is derived from the original one found in phpMyAdmin package and modified by Hasin to meet our need
  320.      */
  321.     //alert(len);
  322.     lastInserted = myValue;
  323.     var myField = document.getElementById(activeta);
  324.     if (document.selection) {
  325.         myField.focus();
  326.         sel = document.selection.createRange();
  327.         if (myField.value.length >= len){  // here is that first conjunction bug in IE, if you use the > operator
  328.             sel.moveStart('character', -1*(len));  
  329.             //sel.moveEnd('character',-1*(len-1));
  330.         }
  331.         sel.text = myValue;
  332.         sel.collapse(true);
  333.         sel.select();
  334.     }
  335.     //MOZILLA/NETSCAPE support
  336.     else if (myField.selectionStart || myField.selectionStart == 0) {
  337.         myField.focus();
  338.         var startPos = myField.selectionStart-len;
  339.         var endPos = myField.selectionEnd;
  340.         var scrollTop = myField.scrollTop;
  341.         startPos = (startPos == -1 ? myField.value.length : startPos );
  342.         myField.value = myField.value.substring(0, startPos)
  343.         + myValue
  344.         + myField.value.substring(endPos, myField.value.length);
  345.         myField.focus();
  346.         myField.selectionStart = startPos + myValue.length;
  347.         myField.selectionEnd = startPos + myValue.length;
  348.         myField.scrollTop = scrollTop;
  349.     } else {
  350.         var scrollTop = myField.scrollTop;
  351.         myField.value += myValue;
  352.         myField.focus();
  353.         myField.scrollTop = scrollTop;
  354.     }
  355.     //document.getElementById("len").innerHTML = len;
  356. }
  357.  
  358. function makeUnijoyEditor(textAreaId)
  359. {
  360.     activeTextAreaInstance = document.getElementById(textAreaId);
  361.     activeTextAreaInstance.onkeypress = parseunijoy;
  362.     activeTextAreaInstance.onkeydown = checkKeyDown;
  363.     activeTextAreaInstance.onkeyup = checkKeyUp;
  364.     activeTextAreaInstance.onfocus = function(){activeta=textAreaId;};
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement