Advertisement
baivong

mudim-0.9-r162-fork.js

May 21st, 2014
1,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*----------------------------------------------------------------------------
  2.  CHIM - CHuoi's Input Method
  3. ----------------------------------------------------------------------------
  4.  copyright         : (C) 2005, 2006, 2007 by Dao Hai Lam
  5.  http:/xvnkb.sf.net/chim
  6.  email             : daohailam<at>yahoo<dot>com
  7.  last modify       : Thu, 05 Jul 2007 23:07:22 +0700
  8.  version           : 0.9.3
  9. ----------------------------------------------------------------------------
  10.  Mudim - Mudzot's Input Method
  11.  (c)2008 by Mudzot
  12.  http:/code.google.com/p/mudim
  13.  email: mudzot<at>gmail.com
  14.  version: 0.8
  15.  date: 29.05.08
  16. ----------------------------------------------------------------------------
  17.  This program is free software; you can redistribute it and/or modify
  18.  it under the terms of the GNU General Public License as published by
  19.  the Free Software Foundation; either version 2 of the License, or
  20.  (at your option) any later version.
  21. -------------------------------------------------------------------------------
  22.  displayMode 2 by Zzbaivong <http://devs.forumvi.com>
  23. ----------------------------------------------------------------------------*/
  24. if (typeof (console) == 'undefined') {}
  25. CHIM = function () {
  26.     return this
  27. };
  28. Mudim = function () {
  29.     return this
  30. };
  31. Mudim.DISPLAY_ID = ['mudim-off', 'mudim-vni', 'mudim-telex', 'mudim-viqr', 'mudim-mix', 'mudim-auto'];
  32. Mudim.SPELLCHECK_ID = 'mudim-checkspell';
  33. Mudim.ACCENTRULE_ID = 'mudim-accentrule';
  34. CHIM.CHAR_A = 'A';
  35. CHIM.CHAR_a = 'a';
  36. CHIM.CHAR_E = 'E';
  37. CHIM.CHAR_e = 'e';
  38. CHIM.CHAR_U = 'U';
  39. CHIM.CHAR_u = 'u';
  40. CHIM.CHAR_G = 'G';
  41. CHIM.CHAR_g = 'g';
  42. CHIM.CHAR_Q = 'Q';
  43. CHIM.CHAR_q = 'q';
  44. CHIM.CHAR_y = 'y';
  45. CHIM.CHAR_Y = 'Y';
  46. CHIM.CHAR_i = 'i';
  47. CHIM.CHAR_I = 'I';
  48. CHIM.CHAR_0x80 = String.fromCharCode(0x80);
  49. CHIM.vowels = "AIUEOYaiueoy";
  50. CHIM.separators = " !@#$%^&*()_+=-{}[]|\\:\";'<>?,./~`\r\n\t";
  51. CHIM.off = 0;
  52. CHIM.buffer = [];
  53. CHIM.dirty = false;
  54. CHIM.CharIsUI = function (u) {
  55.     var n, UI = CHIM.UI;
  56.     u = u.charCodeAt(0);
  57.     for (n = 0; UI[n] != 0 && UI[n] != u; n++) {}
  58.     return UI[n] != 0 ? n : -1
  59. };
  60. CHIM.CharIsO = function (u) {
  61.     var n, O = CHIM.O;
  62.     u = u.charCodeAt(0);
  63.     for (n = 0; O[n] != 0 && O[n] != u; n++) {}
  64.     return O[n] != 0 ? n : -1
  65. };
  66. CHIM.CharPriorityCompare = function (u1, u2) {
  67.     var VN = CHIM.VN;
  68.     var n, i = -1,
  69.         j = -1,
  70.         u;
  71.     for (n = 0, u = u1.charCodeAt(0); VN[n] != 0 && VN[n] != u; n++) {}
  72.     if (VN[n] != 0) {
  73.         i = n
  74.     }
  75.     for (n = 0, u = u2.charCodeAt(0); VN[n] != 0 && VN[n] != u; n++) {}
  76.     if (VN[n]) {
  77.         j = n
  78.     }
  79.     return i - j
  80. };
  81. CHIM.SetCharAt = function (n, c) {
  82.     CHIM.buffer[n] = String.fromCharCode(c)
  83. };
  84. CHIM.Speller = function () {
  85.     return this
  86. };
  87. CHIM.Speller.enabled = true;
  88. CHIM.Speller.position = 0;
  89. CHIM.Speller.count = 0;
  90. CHIM.Speller.vowels = [];
  91. CHIM.Speller.lasts = [];
  92. CHIM.Speller.Toggle = function () {
  93.     CHIM.Speller.enabled = !CHIM.Speller.enabled;
  94.     Mudim.SetPreference()
  95. };
  96. CHIM.Speller.Set = function (position, key) {
  97.     CHIM.Speller.vowels[CHIM.Speller.count] = CHIM.Speller.position;
  98.     CHIM.Speller.lasts[CHIM.Speller.count++] = key;
  99.     CHIM.Speller.position = position
  100. };
  101. CHIM.Speller.Clear = function () {
  102.     CHIM.Speller.position = -1;
  103.     CHIM.Speller.count = 0
  104. };
  105. CHIM.Speller.Last = function () {
  106.     return CHIM.Speller.lasts[CHIM.Speller.count - 1]
  107. };
  108. Mudim.consonants = "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz";
  109. Mudim.spchk = "AIUEOYaiueoy|BDFJKLQSVWXZbdfjklqsvwxz|'`~?.^*+=";
  110. Mudim.vwchk = "|oa|uy|ue|oe|ou|ye|ua|uo|ai|ui|oi|au|iu|ia|eu|ie|ao|eo|ay|uu|io|yu|";
  111. Mudim.nvchk = "FfJjWwZz";
  112. Mudim.separators = "!@#$%^&*()_+=-{}[]|\\:\";'<>?,./~`";
  113. Mudim.tailConsonantsPattern = '|c|ch|p|t|m|n|ng|nh|';
  114. Mudim.CheckSpell = function (key, grp) {
  115.     var b = CHIM.buffer;
  116.     var len = b.length;
  117.     var n = key.toLowerCase();
  118.     if (CHIM.Speller.enabled && !Mudim.tempDisableSpellCheck) {
  119.         if (grp > 0 && CHIM.off == 0) {
  120.             if (Mudim.tailConsonants.length > 0) {
  121.                 var ecIdx = Mudim.tailConsonantsPattern.indexOf('|' + Mudim.tailConsonants + '|');
  122.                 if (ecIdx < 0) {
  123.                     CHIM.off = len;
  124.                     Mudim.tailConsonants = '';
  125.                     return true
  126.                 } else if (ecIdx < 9 && grp == 2) {
  127.                     var typeid = Mudim.GetMarkTypeID(n, 2);
  128.                     if (typeid != 0 && typeid != 1 && typeid != 5) {
  129.                         CHIM.off = len;
  130.                         Mudim.tailConsonants = '';
  131.                         return true
  132.                     }
  133.                 }
  134.             }
  135.             if (len == 2 && (b[1] == CHIM.CHAR_u || b[1] == CHIM.CHAR_U) && (b[0] == CHIM.CHAR_q || b[0] == CHIM.CHAR_Q) && (grp == 2 || (grp == 1 && Mudim.GetMarkTypeID(n, 1) == 1))) {
  136.                 CHIM.off = len;
  137.                 return CHIM.Append(len, c, key)
  138.             }
  139.         } else if (!CHIM.off) {
  140.             var kp = Mudim.spchk.indexOf(key);
  141.             if (len > 0) {
  142.                 var lkey = b[len - 1].toLowerCase()
  143.             }
  144.             if (len == 0) {
  145.                 if (Mudim.nvchk.indexOf(key) >= 0) {
  146.                     CHIM.off = -1
  147.                 } else if (kp >= 0 && kp < 12) {
  148.                     CHIM.Speller.Set(0, key)
  149.                 } else if (kp == 12 || kp > 37) {
  150.                     return
  151.                 } else {
  152.                     CHIM.Speller.Clear()
  153.                 }
  154.             } else if (kp == 12 || kp > 37) {
  155.                 CHIM.ClearBuffer();
  156.                 return
  157.             } else if (kp > 12) {
  158.                 CHIM.off = len
  159.             } else if (kp >= 0) {
  160.                 var i = 0;
  161.                 while (Mudim.consonants.indexOf(b[i]) >= 0) {
  162.                     i++
  163.                 }
  164.                 if (i > 0) {
  165.                     Mudim.headConsonants = b.slice(0, i).toString().replace(/,/g, '').toLowerCase()
  166.                 }
  167.                 if (CHIM.Speller.position < 0) {
  168.                     if (Mudim.headConsonants == 'q') {
  169.                         if (len == 1 && n != 'u') {
  170.                             CHIM.off = len
  171.                         } else if (b[1] == 'u' && n == 'u') {
  172.                             CHIM.off = len
  173.                         }
  174.                     } else if (lkey == 'p' && n != 'h') {
  175.                         CHIM.off = len
  176.                     } else if (lkey == 'k' && n != 'i' && n != 'e' && n != 'y') {
  177.                         CHIM.off = len
  178.                     } else if (Mudim.headConsonants == 'ngh' && n != 'i' && n != 'e') {
  179.                         CHIM.off = len
  180.                     } else {
  181.                         CHIM.Speller.Set(len, key);
  182.                         if (n == 'y') {
  183.                             if ('hklmst'.indexOf(lkey) < 0) {
  184.                                 CHIM.off = len
  185.                             }
  186.                         } else if (n == 'e' || n == 'i') {
  187.                             if (len > 1 && (lkey == 'g')) {
  188.                                 CHIM.off = len
  189.                             }
  190.                             if (lkey == 'c') {
  191.                                 CHIM.off = 1
  192.                             }
  193.                         }
  194.                     }
  195.                 } else if (len - CHIM.Speller.position > 1) {
  196.                     CHIM.off = len
  197.                 } else {
  198.                     var w = "|" + CHIM.Speller.Last().toLowerCase() + key.toLowerCase() + "|";
  199.                     var idx = Mudim.vwchk.indexOf(w);
  200.                     if (idx < 0) {
  201.                         CHIM.off = len
  202.                     } else if (idx < 18 && (Mudim.headConsonants == 'c' || Mudim.headConsonants == 'C')) {
  203.                         CHIM.off = len
  204.                     } else if (lkey == 'y' && Mudim.headConsonants == '' && n != 'e') {
  205.                         CHIM.off = len
  206.                     } else {
  207.                         CHIM.Speller.Set(len, key)
  208.                     }
  209.                 }
  210.             } else {
  211.                 switch (key) {
  212.                 case 'h':
  213.                 case 'H':
  214.                     if (lkey >= CHIM.CHAR_0x80 || "CGKNPTcgknpt".indexOf(lkey) < 0) {
  215.                         CHIM.off = len
  216.                     }
  217.                     break;
  218.                 case 'g':
  219.                 case 'G':
  220.                     if (lkey != 'n' && lkey != 'N') {
  221.                         CHIM.off = len
  222.                     }
  223.                     break;
  224.                 case 'r':
  225.                 case 'R':
  226.                     if (lkey != 't' && lkey != 'T') {
  227.                         CHIM.off = len
  228.                     }
  229.                     break;
  230.                 default:
  231.                     if (Mudim.consonants.indexOf(lkey) >= 0) {
  232.                         CHIM.off = len
  233.                     }
  234.                     break
  235.                 }
  236.             }
  237.         }
  238.         if (CHIM.off != 0) {
  239.             return true
  240.         }
  241.     }
  242.     return false
  243. };
  244. CHIM.Append = function (count, lastkey, key) {
  245.     if (Mudim.separators.indexOf(key) >= 0) {
  246.         CHIM.ClearBuffer();
  247.         return
  248.     }
  249.     Mudim.my = 'mu';
  250.     CHIM.buffer.push(key);
  251.     return Mudim.AdjustAccent(CHIM.modes[Mudim.method - 1][2].charAt(0))
  252. };
  253. CHIM.AddKey = function (key) {
  254.     var p = -1;
  255.     var i, j = -1;
  256.     var b, c = 0,
  257.         cc, l;
  258.     var count = CHIM.buffer.length;
  259.     var m = CHIM.modes[Mudim.method - 1],
  260.         n;
  261.     var v = null;
  262.     var autoModeProbe = false;
  263.     if (!count || CHIM.off != 0 || Mudim.tempOff) {
  264.         if (Mudim.CheckSpell(key, l)) {
  265.             return CHIM.Append(count, c, key)
  266.         }
  267.         return CHIM.Append(0, 0, key)
  268.     }
  269.     b = CHIM.buffer;
  270.     c = b[p = count - 1];
  271.     n = key.toLowerCase();
  272.     for (l = 1; l < m.length; l++)
  273.         if (m[l].indexOf(n) >= 0) {
  274.             break
  275.         }
  276.     if (l >= m.length) {
  277.         Mudim.CheckSpell(key, 0);
  278.         return CHIM.Append(count, c, key)
  279.     }
  280.     if (Mudim.method == 5) {
  281.         Mudim.method = Mudim.AutoDetectMode(n);
  282.         autoModeProbe = true
  283.     }
  284.     if ((p = Mudim.FindAccentPos(n)) < 0) {
  285.         if (autoModeProbe) {
  286.             Mudim.method = 5;
  287.             autoModeProbe = false
  288.         }
  289.         Mudim.CheckSpell(key, 0);
  290.         return CHIM.Append(count, c, key)
  291.     }
  292.     Mudim.lord = 'dz';
  293.     if (Mudim.CheckSpell(key, l)) {
  294.         if (autoModeProbe) {
  295.             Mudim.method = 5;
  296.             autoModeProbe = false
  297.         }
  298.         return CHIM.Append(count, c, key)
  299.     }
  300.     c = b[p];
  301.     var x = c.charCodeAt(0);
  302.     var found = false;
  303.     if (l == 1) {
  304.         m = m[0];
  305.         for (i = 0; !found && i < m.length; i++) {
  306.             var k = m[i];
  307.             if (k[0] == n) {
  308.                 for (i = 1; i < k.length; i++) {
  309.                     v = CHIM.vncode_1[k[i]];
  310.                     Mudim.AdjustAccent(n);
  311.                     x = b[p].charCodeAt(0);
  312.                     if (Mudim.GetMarkTypeID(n, 1) == 3) {
  313.                         p = 0;
  314.                         c = b[p];
  315.                         x = c.charCodeAt(0)
  316.                     }
  317.                     if (Mudim.PutMark(p, x, 1, v, n, true)) {
  318.                         if (p > 0 && Mudim.GetMarkTypeID(n, 1) == 1 && p < count - 1 && CHIM.CharIsO(b[p]) >= 0 && CHIM.CharIsUI(b[p - 1]) >= 0 && b[0] != CHIM.CHAR_q && b[0] != CHIM.CHAR_Q) {
  319.                             Mudim.PutMark(p - 1, b[p - 1].charCodeAt(0), 1, CHIM.vn_UW, n, false)
  320.                         }
  321.                         found = true;
  322.                         break
  323.                     }
  324.                 }
  325.                 break
  326.             }
  327.         }
  328.     } else {
  329.         for (i = 0; i < CHIM.vncode_2.length; i++) {
  330.             v = CHIM.vncode_2[i];
  331.             if (Mudim.PutMark(p, x, 2, v, n, true)) {
  332.                 found = true;
  333.                 break
  334.             }
  335.         }
  336.     } if (!found) {
  337.         Mudim.CheckSpell(key, 0);
  338.         if (autoModeProbe) {
  339.             Mudim.method = 5
  340.         }
  341.         autoModeProbe = false;
  342.         return CHIM.Append(count, c, key)
  343.     } else {
  344.         if (autoModeProbe) {
  345.             CHIM.SetDisplay()
  346.         }
  347.         autoModeProbe = false
  348.     } if (CHIM.off != 0) {
  349.         CHIM.buffer.push(key)
  350.     }
  351.     return p >= 0
  352. };
  353. CHIM.BackSpace = function () {
  354.     var count = CHIM.buffer.length;
  355.     if (count <= 0) {
  356.         CHIM.dirty = true
  357.     } else {
  358.         if (Mudim.accent[0] == count - 1) Mudim.ResetAccentInfo();
  359.         var i = CHIM.vn_OW.length - 1;
  360.         var code = CHIM.buffer[count - 1].charCodeAt(0);
  361.         while (i >= 0 && CHIM.vn_OW[i] != code) {
  362.             i--
  363.         }
  364.         if (i < 0) {
  365.             i = CHIM.vn_UW.length - 1;
  366.             while (i >= 0 && CHIM.vn_UW[i] != code) {
  367.                 i--
  368.             }
  369.         }
  370.         if (i >= 0 && (i % 2) == 1) {
  371.             Mudim.w--
  372.         }--count;
  373.         CHIM.buffer.pop();
  374.         if (count == CHIM.Speller.position) {
  375.             CHIM.Speller.position = CHIM.Speller.vowels[--CHIM.Speller.count]
  376.         }
  377.         if ((CHIM.off < 0 && !count) || (count <= CHIM.off)) {
  378.             CHIM.off = 0
  379.         }
  380.     }
  381. };
  382. CHIM.ClearBuffer = function () {
  383.     CHIM.off = 0;
  384.     Mudim.w = 0;
  385.     CHIM.Speller.Clear();
  386.     Mudim.ResetAccentInfo();
  387.     Mudim.tailConsonants = '';
  388.     Mudim.headConsonants = '';
  389.     Mudim.ctrlSerie = 0;
  390.     Mudim.shiftSerie = 0;
  391.     if (CHIM.buffer.length > 0) {
  392.         Mudim.tempOff = false;
  393.         Mudim.tempDisableSpellCheck = false
  394.     }
  395.     CHIM.buffer = []
  396. };
  397. CHIM.SetDisplay = function () {
  398.     if (typeof (Mudim.DISPLAY_ID) != "undefined" && Mudim.method < Mudim.DISPLAY_ID.length) {
  399.         var r;
  400.         for (var i = 0; i < 5; i++) {
  401.             r = document.getElementById(Mudim.DISPLAY_ID[i]);
  402.             if (r) {
  403.                 r.checked = false
  404.             }
  405.         }
  406.         r = document.getElementById(Mudim.DISPLAY_ID[Mudim.method]);
  407.         if (r) {
  408.             r.checked = true
  409.         }
  410.     }
  411.     if (typeof (Mudim.SPELLCHECK_ID) != "undefined") {
  412.         var r = document.getElementById(Mudim.SPELLCHECK_ID);
  413.         if (r) {
  414.             r.checked = CHIM.Speller.enabled
  415.         }
  416.     }
  417.     if (typeof (Mudim.ACCENTRULE_ID) != "undefined") {
  418.         var r = document.getElementById(Mudim.ACCENTRULE_ID);
  419.         if (r) {
  420.             r.checked = Mudim.newAccentRule
  421.         }
  422.     }
  423. };
  424. CHIM.SwitchMethod = function () {
  425.     CHIM.ClearBuffer();
  426.     Mudim.method = (++Mudim.method % 6);
  427.     CHIM.SetDisplay();
  428.     Mudim.SetPreference()
  429. };
  430. CHIM.SetMethod = function (m) {
  431.     CHIM.ClearBuffer();
  432.     Mudim.method = m;
  433.     CHIM.SetDisplay();
  434.     Mudim.SetPreference()
  435. };
  436. CHIM.Toggle = function () {
  437.     var p;
  438.     if (!(p = Mudim.Panel)) {
  439.         Mudim.InitPanel()
  440.     }
  441.     if (Mudim.method == 0) {
  442.         CHIM.SetMethod(Mudim.oldMethod)
  443.     } else {
  444.         Mudim.oldMethod = Mudim.method;
  445.         CHIM.SetMethod(0)
  446.     }
  447.     Mudim.SetPreference()
  448. };
  449. CHIM.GetTarget = function (e) {
  450.     var r;
  451.     if (e == null) {
  452.         e = window.event
  453.     }
  454.     if (e == null) {
  455.         return null
  456.     }
  457.     if (e.srcElement != null) {
  458.         r = e.srcElement
  459.     } else {
  460.         r = e.target;
  461.         while (r && r.nodeType != 1) r = r.parentNode
  462.     } if (r.tagName == 'BODY') {
  463.         r = r.parentNode
  464.     }
  465.     CHIM.peckable = r.tagName == 'HTML' || r.type == 'textarea' || r.type == 'text' || r.type == 'search';
  466.     return r
  467. };
  468. CHIM.GetCursorPosition = function (target) {
  469.     if (target == null || target.value == null || target.value.length == 0) {
  470.         return -1
  471.     }
  472.     if (typeof (target.selectionStart) != 'undefined') {
  473.         if (target.selectionStart < 0 || target.selectionStart > target.length || target.selectionEnd < 0 || target.selectionEnd > target.length || target.selectionEnd < target.selectionStart) {
  474.             return -1
  475.         }
  476.         return target.selectionStart
  477.     }
  478.     if (document.selection) {
  479.         var selection = document.selection.createRange();
  480.         var textRange = target.createTextRange();
  481.         if (textRange == null || selection == null || ((selection.text != "") && textRange.inRange(selection) == false)) {
  482.             return -1
  483.         }
  484.         if (selection.text == "") {
  485.             var index = 1;
  486.             if (target.tagName == "INPUT") {
  487.                 var contents = textRange.text;
  488.                 while (index < contents.length) {
  489.                     textRange.findText(contents.substring(index));
  490.                     if (textRange.boundingLeft == selection.boundingLeft) {
  491.                         break
  492.                     }
  493.                     index++
  494.                 }
  495.             } else if (target.tagName == "TEXTAREA") {
  496.                 var caret = document.selection.createRange().duplicate();
  497.                 index = target.value.length + 1;
  498.                 while (caret.parentElement() == target && caret.move("character", 1) == 1) {
  499.                     --index;
  500.                     if (target.value.charCodeAt(index) == 10) {
  501.                         index -= 1
  502.                     }
  503.                 }
  504.                 if (index == target.value.length + 1) {
  505.                     index = -1
  506.                 }
  507.             }
  508.             return index
  509.         }
  510.         return textRange.text.indexOf(selection.text)
  511.     }
  512. };
  513. CHIM.SetCursorPosition = function (target, p) {
  514.     if (p < 0) {
  515.         return
  516.     }
  517.     if (target.setSelectionRange) {
  518.         target.setSelectionRange(p, p)
  519.     } else if (target.createTextRange) {
  520.         var range = target.createTextRange();
  521.         range.collapse(true);
  522.         var i;
  523.         var dec = 0;
  524.         for (i = 0; i < p; i++) {
  525.             if ((target.value.charCodeAt(i) == 10) || (target.value.charCodeAt(i) == 13)) {
  526.                 if (dec == 0) {
  527.                     --p;
  528.                     dec = 1
  529.                 }
  530.             } else {
  531.                 dec = 0
  532.             }
  533.         }
  534.         range.moveStart('character', p);
  535.         range.moveEnd('character', 0);
  536.         range.select()
  537.     }
  538. };
  539. CHIM.UpdateBuffer = function (target) {
  540.     CHIM.ClearBuffer();
  541.     if (target.tagName != 'HTML') {
  542.         var separators = CHIM.separators;
  543.         var c = CHIM.GetCursorPosition(target) - 1;
  544.         if (c > 0) {
  545.             while (c >= 0 && separators.indexOf(target.value.charAt(c)) < 0) {
  546.                 CHIM.buffer.unshift(target.value.charAt(c));
  547.                 c = c - 1
  548.             }
  549.         }
  550.         Mudim.startWordOffset = c + 1
  551.     } else {
  552.         CHIM.buffer = CHIM.HTMLEditor.GetCurrentWord(target).split('')
  553.     }
  554.     CHIM.dirty = false
  555. };
  556. CHIM.VK_TAB = 9;
  557. CHIM.VK_BACKSPACE = 8;
  558. CHIM.VK_ENTER = 13;
  559. CHIM.VK_DELETE = 46;
  560. CHIM.VK_SPACE = 32;
  561. CHIM.VK_LIMIT = 128;
  562. CHIM.VK_LEFT_ARROW = 37;
  563. CHIM.VK_RIGHT_ARROW = 39;
  564. CHIM.VK_HOME = 36;
  565. CHIM.VK_END = 35;
  566. CHIM.VK_PAGE_UP = 33;
  567. CHIM.VK_PAGE_DOWN = 34;
  568. CHIM.VK_UP_ARROW = 38;
  569. CHIM.VK_DOWN_ARROW = 40;
  570. CHIM.VK_ONOFF = 120;
  571. CHIM.VK_ONOFF2 = 121;
  572. CHIM.VK_PANELTOGGLE = 119;
  573. CHIM.VK_CTRL = 17;
  574. CHIM.VK_SHIFT = 16;
  575. CHIM.VK_ALT = 18;
  576. CHIM.ProcessControlKey = function (keyCode, release) {
  577.     switch (keyCode) {
  578.     case CHIM.VK_TAB:
  579.     case CHIM.VK_ENTER:
  580.         CHIM.ClearBuffer();
  581.         break;
  582.     case CHIM.VK_BACKSPACE:
  583.         if (!release) {
  584.             CHIM.BackSpace()
  585.         }
  586.         break;
  587.     case CHIM.VK_DELETE:
  588.     case CHIM.VK_LEFT_ARROW:
  589.     case CHIM.VK_RIGHT_ARROW:
  590.     case CHIM.VK_HOME:
  591.     case CHIM.VK_END:
  592.     case CHIM.VK_PAGE_UP:
  593.     case CHIM.VK_PAGE_DOWN:
  594.     case CHIM.VK_UP_ARROW:
  595.     case CHIM.VK_DOWN_ARROW:
  596.         CHIM.dirty = true;
  597.         break
  598.     }
  599. };
  600. CHIM.IsHotkey = function (e, k) {
  601.     if (k == CHIM.VK_PANELTOGGLE) {
  602.         Mudim.TogglePanel();
  603.         return true
  604.     } else if (k == CHIM.VK_ONOFF || k == CHIM.VK_ONOFF2) {
  605.         CHIM.Toggle();
  606.         return true
  607.     }
  608.     return false
  609. };
  610. CHIM.HTMLEditor = function () {
  611.     return this
  612. };
  613. CHIM.HTMLEditor.GetRange = function (target) {
  614.     if (!target.parentNode.iframe) {
  615.         return
  616.     }
  617.     var win = target.parentNode.iframe.contentWindow;
  618.     return (!window.opera && document.all) ? win.document.selection.createRange() : win.getSelection().getRangeAt(0)
  619. };
  620. CHIM.HTMLEditor.GetCurrentWord = function (target) {
  621.     var range = CHIM.HTMLEditor.GetRange(target);
  622.     if (!range) {
  623.         return ''
  624.     }
  625.     if (!window.opera && document.all) {
  626.         while (range.moveStart('character', -1) == -1) {
  627.             if (CHIM.separators.indexOf(range.text.charAt(0)) >= 0) {
  628.                 range.moveStart('character', 1);
  629.                 break
  630.             }
  631.         }
  632.         return range.text
  633.     }
  634.     var word = '';
  635.     var s;
  636.     if (!(s = range.startContainer.nodeValue)) {
  637.         return ''
  638.     }
  639.     var c = range.startOffset - 1;
  640.     if (c > 0) {
  641.         while (c >= 0 && CHIM.separators.indexOf(s.charAt(c)) < 0 && s.charCodeAt(c) != 160) {
  642.             word = s.charAt(c) + word;
  643.             c = c - 1
  644.         }
  645.     }
  646.     return word
  647. };
  648. CHIM.HTMLEditor.Process = function (target, l) {
  649.     var sel = window.getSelection();
  650.     var range = CHIM.HTMLEditor.GetRange(target);
  651.     if (typeof (range) == 'undefined') {
  652.         return
  653.     }
  654.     var b = CHIM.buffer;
  655.     if (!window.opera && document.all) {
  656.         var x = -l;
  657.         range.moveStart('character', x);
  658.         range.moveEnd('character', x + b.length);
  659.         range.pasteHTML(b.toString().replace(/,/g, ''));
  660.         return
  661.     }
  662.     var container = range.startContainer;
  663.     var offset = range.startOffset;
  664.     var start = offset - l;
  665.     container.nodeValue = container.nodeValue.substring(0, start) + b.toString().replace(/,/g, '') + container.nodeValue.substring(start + l);
  666.     if (l < b.length) {
  667.         offset++
  668.     }
  669.     range.setEnd(container, offset);
  670.     range.setStart(container, offset);
  671.     sel.removeAllRanges();
  672.     sel.addRange(range)
  673. };
  674. CHIM.Freeze = function (target) {
  675.     var ign = Mudim.IGNORE_ID;
  676.     if (ign.length > 0) {
  677.         for (var i = 0; i < ign.length; i++) {
  678.             if (target.id == ign[i]) {
  679.                 return true
  680.             }
  681.         }
  682.     }
  683.     return false
  684. };
  685. CHIM.KeyHandler = function (e) {
  686.     if (e == null) {
  687.         e = window.event
  688.     }
  689.     if (e.isHandled == true) {
  690.         return
  691.     }
  692.     e.isHandled = true;
  693.     var keyCode = e.keyCode;
  694.     if (keyCode == 0) {
  695.         keyCode = e.charCode
  696.     }
  697.     if (keyCode == 0) {
  698.         keyCode = e.which
  699.     }
  700.     if (Mudim.method == 0) {
  701.         return
  702.     }
  703.     var target = null;
  704.     if (!(target = CHIM.GetTarget(e)) || !CHIM.peckable || CHIM.Freeze(target)) {
  705.         return
  706.     }
  707.     if (e.ctrlKey || e.ctrlLeft || e.metaKey) {
  708.         if (keyCode == CHIM.VK_BACKSPACE || keyCode == CHIM.VK_LEFT_ARROW || keyCode == CHIM.VK_RIGHT_ARROW) {
  709.             CHIM.dirty = true
  710.         }
  711.         return
  712.     }
  713.     if (e.charCode == null || e.charCode != 0) {
  714.         var key = String.fromCharCode(keyCode);
  715.         if (keyCode == CHIM.VK_SPACE || keyCode == CHIM.VK_ENTER) {
  716.             CHIM.ClearBuffer()
  717.         } else if (keyCode > CHIM.VK_SPACE && keyCode < CHIM.VK_LIMIT) {
  718.             if (CHIM.dirty) {
  719.                 CHIM.UpdateBuffer(target)
  720.             }
  721.             var l = CHIM.buffer.length;
  722.             if (l == 0) {
  723.                 Mudim.startWordOffset = CHIM.GetCursorPosition(target)
  724.             }
  725.             if (Mudim.newTempDisableSpellCheckRequest) {
  726.                 CHIM.ClearBuffer();
  727.                 Mudim.startWordOffset = CHIM.GetCursorPosition(target);
  728.                 Mudim.newTempDisableSpellCheckRequest = false
  729.             }
  730.             if (CHIM.AddKey(key)) {
  731.                 if (e.stopPropagation) {
  732.                     e.stopPropagation()
  733.                 }
  734.                 if (e.preventDefault) {
  735.                     e.preventDefault()
  736.                 }
  737.                 e.cancelBubble = true;
  738.                 e.returnValue = false;
  739.                 Mudim.UpdateUI(target, l)
  740.             }
  741.         } else {
  742.             CHIM.dirty = true
  743.         }
  744.     } else {
  745.         CHIM.ProcessControlKey(keyCode, true)
  746.     }
  747. };
  748. CHIM.KeyUp = function (e) {
  749.     if (e == null) {
  750.         e = window.event
  751.     }
  752.     if (e.keyCode == CHIM.VK_SHIFT) {
  753.         if (Mudim.shiftSerie == 1) {
  754.             Mudim.tempOff = true;
  755.             Mudim.shiftSerie = 0
  756.         }
  757.     }
  758.     if (e.keyCode == CHIM.VK_CTRL) {
  759.         if (Mudim.ctrlSerie == 1) {
  760.             Mudim.tempDisableSpellCheck = true;
  761.             Mudim.ctrlSerie = 0;
  762.             Mudim.newTempDisableSpellCheckRequest = true
  763.         }
  764.     }
  765. };
  766. CHIM.KeyDown = function (e) {
  767.     var target = null;
  768.     if (e == null) {
  769.         e = window.event
  770.     }
  771.     if (CHIM.IsHotkey(e, e.keyCode)) {
  772.         return
  773.     }
  774.     if (e.altKey || e.altLeft) {
  775.         return
  776.     }
  777.     if (e.shiftKey || e.shiftLeft || e.metaKey) {
  778.         Mudim.shiftSerie |= 1;
  779.         if (e.keyCode != CHIM.VK_SHIFT) {
  780.             Mudim.shiftSerie |= 2
  781.         }
  782.         return
  783.     }
  784.     if (e.ctrlKey || e.ctrlLeft || e.metaKey) {
  785.         Mudim.ctrlSerie |= 1;
  786.         if (e.keyCode != CHIM.VK_CTRL) {
  787.             Mudim.ctrlSerie |= 2
  788.         }
  789.         return
  790.     }
  791.     if (!(target = CHIM.GetTarget(e)) || !CHIM.peckable || CHIM.Freeze(target)) {
  792.         return
  793.     }
  794.     var keyCode = e.keyCode;
  795.     if (keyCode == 0) {
  796.         keyCode = e.charCode
  797.     }
  798.     if (keyCode == 0) {
  799.         keyCode = e.which
  800.     }
  801.     CHIM.ProcessControlKey(keyCode, false)
  802. };
  803. CHIM.MouseDown = function (e) {
  804.     CHIM.Activate();
  805.     CHIM.dirty = true
  806. };
  807. CHIM.Attach = function (e, r) {
  808.     if (!e) {
  809.         return
  810.     }
  811.     if (!e.chim) {
  812.         if (!r) {
  813.             if (!window.opera && document.all) {
  814.                 e.attachEvent('onkeydown', CHIM.KeyDown);
  815.                 e.attachEvent('onkeyup', CHIM.KeyUp);
  816.                 e.attachEvent('onkeypress', CHIM.KeyHandler);
  817.                 e.attachEvent('onmousedown', CHIM.MouseDown)
  818.             } else {
  819.                 e.addEventListener('keydown', CHIM.KeyDown, false);
  820.                 e.addEventListener('keyup', CHIM.KeyUp, false);
  821.                 e.addEventListener('keypress', CHIM.KeyHandler, false);
  822.                 e.addEventListener('mousedown', CHIM.MouseDown, false)
  823.             }
  824.         } else {
  825.             e.onkeydown = CHIM.KeyDown;
  826.             e.onkeyup = CHIM.KeyUp;
  827.             e.onkeypress = CHIM.KeyHandler;
  828.             e.onmousedown = CHIM.MouseDown
  829.         }
  830.         e.chim = true
  831.     }
  832.     var f = e.getElementsByTagName('iframe');
  833.     for (var i = 0; i < f.length; i++) {
  834.         var doc = (!window.opera && document.all) ? f[i].contentWindow.document : f[i].contentDocument;
  835.         try {
  836.             doc.iframe = f[i];
  837.             CHIM.Attach(doc, false)
  838.         } catch (e) {}
  839.     }
  840.     var f = e.getElementsByTagName('frame');
  841.     for (var i = 0; i < f.length; i++) {
  842.         var doc = (!window.opera && document.all) ? f[i].contentWindow.document : f[i].contentDocument;
  843.         try {
  844.             doc.iframe = f[i];
  845.             CHIM.Attach(doc, false)
  846.         } catch (e) {}
  847.     }
  848. };
  849. CHIM.Activate = function () {
  850.     try {
  851.         CHIM.Attach(document, true);
  852.         CHIM.SetDisplay()
  853.     } catch (exc) {}
  854. };
  855. CHIM.vn_A0 = [65, 193, 192, 7842, 195, 7840];
  856. CHIM.vn_a0 = [97, 225, 224, 7843, 227, 7841];
  857. CHIM.vn_A6 = [194, 7844, 7846, 7848, 7850, 7852];
  858. CHIM.vn_a6 = [226, 7845, 7847, 7849, 7851, 7853];
  859. CHIM.vn_A8 = [258, 7854, 7856, 7858, 7860, 7862];
  860. CHIM.vn_a8 = [259, 7855, 7857, 7859, 7861, 7863];
  861. CHIM.vn_O0 = [79, 211, 210, 7886, 213, 7884];
  862. CHIM.vn_o0 = [111, 243, 242, 7887, 245, 7885];
  863. CHIM.vn_O6 = [212, 7888, 7890, 7892, 7894, 7896];
  864. CHIM.vn_o6 = [244, 7889, 7891, 7893, 7895, 7897];
  865. CHIM.vn_O7 = [416, 7898, 7900, 7902, 7904, 7906];
  866. CHIM.vn_o7 = [417, 7899, 7901, 7903, 7905, 7907];
  867. CHIM.vn_U0 = [85, 218, 217, 7910, 360, 7908];
  868. CHIM.vn_u0 = [117, 250, 249, 7911, 361, 7909];
  869. CHIM.vn_U7 = [431, 7912, 7914, 7916, 7918, 7920];
  870. CHIM.vn_u7 = [432, 7913, 7915, 7917, 7919, 7921];
  871. CHIM.vn_E0 = [69, 201, 200, 7866, 7868, 7864];
  872. CHIM.vn_e0 = [101, 233, 232, 7867, 7869, 7865];
  873. CHIM.vn_E6 = [202, 7870, 7872, 7874, 7876, 7878];
  874. CHIM.vn_e6 = [234, 7871, 7873, 7875, 7877, 7879];
  875. CHIM.vn_I0 = [73, 205, 204, 7880, 296, 7882];
  876. CHIM.vn_i0 = [105, 237, 236, 7881, 297, 7883];
  877. CHIM.vn_Y0 = [89, 221, 7922, 7926, 7928, 7924];
  878. CHIM.vn_y0 = [121, 253, 7923, 7927, 7929, 7925];
  879. CHIM.vncode_2 = [CHIM.vn_A0, CHIM.vn_a0, CHIM.vn_A6, CHIM.vn_a6, CHIM.vn_A8, CHIM.vn_a8, CHIM.vn_O0, CHIM.vn_o0, CHIM.vn_O6, CHIM.vn_o6, CHIM.vn_O7, CHIM.vn_o7, CHIM.vn_U0, CHIM.vn_u0, CHIM.vn_U7, CHIM.vn_u7, CHIM.vn_E0, CHIM.vn_e0, CHIM.vn_E6, CHIM.vn_e6, CHIM.vn_I0, CHIM.vn_i0, CHIM.vn_Y0, CHIM.vn_y0];
  880. CHIM.vn_AA = [65, 194, 193, 7844, 192, 7846, 7842, 7848, 195, 7850, 7840, 7852, 258, 194, 7854, 7844, 7856, 7846, 7858, 7848, 7860, 7850, 7862, 7852, 97, 226, 225, 7845, 224, 7847, 7843, 7849, 227, 7851, 7841, 7853, 259, 226, 7855, 7845, 7857, 7847, 7859, 7849, 7861, 7851, 7863, 7853];
  881. CHIM.vn_AW = [65, 258, 193, 7854, 192, 7856, 7842, 7858, 195, 7860, 7840, 7862, 194, 258, 7844, 7854, 7846, 7856, 7848, 7858, 7850, 7860, 7852, 7862, 97, 259, 225, 7855, 224, 7857, 7843, 7859, 227, 7861, 7841, 7863, 226, 259, 7845, 7855, 7847, 7857, 7849, 7859, 7851, 7861, 7853, 7863];
  882. CHIM.vn_OO = [79, 212, 211, 7888, 210, 7890, 7886, 7892, 213, 7894, 7884, 7896, 416, 212, 7898, 7888, 7900, 7900, 7902, 7892, 7904, 7894, 7906, 7896, 111, 244, 243, 7889, 242, 7891, 7887, 7893, 245, 7895, 7885, 7897, 417, 244, 7899, 7889, 7901, 7891, 7903, 7893, 7905, 7895, 7907, 7897];
  883. CHIM.vn_OW = [79, 416, 211, 7898, 210, 7900, 7886, 7902, 213, 7904, 7884, 7906, 212, 416, 7888, 7898, 7890, 7900, 7892, 7902, 7894, 7904, 7896, 7906, 111, 417, 243, 7899, 242, 7901, 7887, 7903, 245, 7905, 7885, 7907, 244, 417, 7889, 7899, 7891, 7901, 7893, 7903, 7895, 7905, 7897, 7907];
  884. CHIM.vn_UW = [85, 431, 218, 7912, 217, 7914, 7910, 7916, 360, 7918, 7908, 7920, 117, 432, 250, 7913, 249, 7915, 7911, 7917, 361, 7919, 7909, 7921];
  885. CHIM.vn_EE = [69, 202, 201, 7870, 200, 7872, 7866, 7874, 7868, 7876, 7864, 7878, 101, 234, 233, 7871, 232, 7873, 7867, 7875, 7869, 7877, 7865, 7879];
  886. CHIM.vn_DD = [68, 272, 100, 273];
  887. CHIM.vncode_1 = [CHIM.vn_AA, CHIM.vn_EE, CHIM.vn_OO, CHIM.vn_AW, CHIM.vn_OW, CHIM.vn_UW, CHIM.vn_DD];
  888. CHIM.modes = [
  889.     [
  890.         [
  891.             ['6', 0, 1, 2],
  892.             ['7', 4, 5],
  893.             ['8', 3],
  894.             ['9', 6]
  895.         ], '6789', '012345'
  896.     ],
  897.     [
  898.         [
  899.             ['a', 0],
  900.             ['e', 1],
  901.             ['o', 2],
  902.             ['w', 3, 4, 5],
  903.             ['d', 6]
  904.         ], 'ewoda', 'zsfrxj'
  905.     ],
  906.     [
  907.         [
  908.             ['^', 0, 1, 2],
  909.             ['+', 4, 5],
  910.             ['(', 3],
  911.             ['d', 6]
  912.         ], '^+(d', "='`?~."
  913.     ],
  914.     [
  915.         [
  916.             ['6', 0, 1, 2],
  917.             ['7', 4, 5],
  918.             ['8', 3],
  919.             ['9', 6],
  920.             ['a', 0],
  921.             ['e', 1],
  922.             ['o', 2],
  923.             ['w', 3, 4, 5],
  924.             ['d', 6]
  925.         ], '6789ewoda', "012345zsfrxj"
  926.     ],
  927.     [
  928.         [
  929.             ['6', 0, 1, 2],
  930.             ['7', 4, 5],
  931.             ['8', 3],
  932.             ['9', 6],
  933.             ['a', 0],
  934.             ['e', 1],
  935.             ['o', 2],
  936.             ['w', 3, 4, 5],
  937.             ['d', 6],
  938.             ['^', 0, 1, 2],
  939.             ['+', 4, 5],
  940.             ['(', 3],
  941.             ['d', 6]
  942.         ], '6789ewoda^+(d', "012345zsfrxj='`?~."
  943.     ]
  944. ];
  945. CHIM.UI = [85, 218, 217, 7910, 360, 7908, 117, 250, 249, 7911, 361, 7909, 431, 7912, 7914, 7916, 7918, 7920, 432, 7913, 7915, 7917, 7919, 7921, 73, 205, 204, 7880, 296, 7882, 105, 237, 236, 7881, 297, 7883, 0];
  946. CHIM.O = [79, 211, 210, 7886, 213, 7884, 111, 243, 242, 7887, 245, 7885, 212, 7888, 7890, 7892, 7894, 7896, 244, 7889, 7891, 7893, 7895, 7897, 416, 7898, 7900, 7902, 7904, 7906, 417, 7899, 7901, 7903, 7905, 7907, 0];
  947. CHIM.VN = [97, 65, 225, 193, 224, 192, 7843, 7842, 227, 195, 7841, 7840, 226, 194, 7845, 7844, 7847, 7846, 7849, 7848, 7851, 7850, 7853, 7852, 259, 258, 7855, 7854, 7857, 7856, 7859, 7858, 7861, 7860, 7863, 7862, 101, 69, 233, 201, 232, 200, 7867, 7866, 7869, 7868, 7865, 7864, 234, 202, 7871, 7870, 7873, 7872, 7875, 7874, 7877, 7876, 7879, 7878, 111, 79, 243, 211, 242, 210, 7887, 7886, 245, 213, 7885, 7884, 244, 212, 7889, 7888, 7891, 7890, 7893, 7892, 7895, 7894, 7897, 7896, 417, 416, 7899, 7898, 7901, 7900, 7903, 7902, 7905, 7904, 7907, 7906, 121, 89, 253, 221, 7923, 7922, 7927, 7926, 7929, 7928, 7925, 7924, 117, 85, 250, 218, 249, 217, 7911, 7910, 361, 360, 7909, 7908, 432, 431, 7913, 7912, 7915, 7914, 7917, 7916, 7919, 7918, 7921, 7920, 105, 73, 237, 205, 236, 204, 7881, 7880, 297, 296, 7883, 7882, 273, 272, 0];
  948. Mudim.UpdateUI = function (target, l) {
  949.     var b = CHIM.buffer;
  950.     if (target.tagName == 'HTML') {
  951.         CHIM.HTMLEditor.Process(target, l);
  952.         if (l < CHIM.buffer.length) {
  953.             return
  954.         }
  955.         return false
  956.     }
  957.     var start = Mudim.startWordOffset < 0 ? 0 : Mudim.startWordOffset;
  958.     var end = CHIM.GetCursorPosition(target);
  959.     var t = target.scrollTop;
  960.     target.value = target.value.substring(0, start) + b.toString().replace(/,/g, '') + target.value.substring(end);
  961.     CHIM.SetCursorPosition(target, start + b.length);
  962.     target.scrollTop = t
  963. };
  964. Mudim.FindAccentPos = function (nKey) {
  965.     var k = nKey.toLowerCase();
  966.     var m = CHIM.modes[Mudim.method - 1];
  967.     var b = CHIM.buffer;
  968.     var len = b.length;
  969.     var i, j, l, p, c;
  970.     if (!len || CHIM.off != 0) {
  971.         return -1
  972.     }
  973.     for (i = 1; i < m.length; i++)
  974.         if (m[i].indexOf(k) >= 0) {
  975.             break
  976.         }
  977.     p = len - 1;
  978.     Mudim.is = 'ot';
  979.     switch (l = i) {
  980.     case 1:
  981.         if (Mudim.GetMarkTypeID(k, 1) == 3) {
  982.             break
  983.         }
  984.     case 2:
  985.     default:
  986.         i = p;
  987.         while (i >= 0 && b[i] < CHIM.CHAR_0x80 && CHIM.vowels.indexOf(b[i]) < 0) i--;
  988.         if (i < 0) {
  989.             return -1
  990.         }
  991.         if (i < len - 1) {
  992.             Mudim.tailConsonants = b.slice(i + 1, len).toString().replace(/,/g, '').toLowerCase()
  993.         }
  994.         while (i - 1 >= 0 && (CHIM.vowels.indexOf(b[i - 1]) >= 0 || b[i - 1] > CHIM.CHAR_0x80) && CHIM.CharPriorityCompare(b[i - 1], b[i]) < 0) i--;
  995.         if (i == len - 1 && i - 1 >= 0 && (j = CHIM.CharIsUI(b[i - 1])) > 0) {
  996.             switch (b[i]) {
  997.             case CHIM.CHAR_a:
  998.             case CHIM.CHAR_A:
  999.                 if ((i - 2 < 0 || (j < 24 && b[i - 2] != CHIM.CHAR_q && b[i - 2] != CHIM.CHAR_Q) || (j >= 24 && b[i - 2] != CHIM.CHAR_g && b[i - 2] != CHIM.CHAR_G)) && (l == 2 || (l == 1 && Mudim.GetMarkTypeID(k, 1) == 1))) i--;
  1000.                 break;
  1001.             case CHIM.CHAR_u:
  1002.             case CHIM.CHAR_U:
  1003.                 if (i - 2 < 0 || (b[i - 2] != CHIM.CHAR_g && b[i - 2] != CHIM.CHAR_G)) i--;
  1004.                 break;
  1005.             case CHIM.CHAR_Y:
  1006.             case CHIM.CHAR_y:
  1007.                 if ((!Mudim.newAccentRule) && i - 2 >= 0 && b[i - 2] != CHIM.CHAR_q && b[i - 2] != CHIM.CHAR_Q) {
  1008.                     i--
  1009.                 }
  1010.                 break
  1011.             }
  1012.         }
  1013.         if (i == len - 1 && i - 1 >= 0 && CHIM.CharIsO(b[i - 1]) > 0) {
  1014.             switch (b[i]) {
  1015.             case CHIM.CHAR_a:
  1016.             case CHIM.CHAR_A:
  1017.                 if (!Mudim.newAccentRule && (l == 2 || (l == 1 && Mudim.GetMarkTypeID(k, 1) != 1))) i--;
  1018.                 break;
  1019.             case CHIM.CHAR_e:
  1020.             case CHIM.CHAR_E:
  1021.                 if (!Mudim.newAccentRule) i--;
  1022.                 break
  1023.             }
  1024.         }
  1025.         if (i == len - 2 && i - 1 >= 0) {
  1026.             var uipos = CHIM.CharIsUI(b[i]);
  1027.             if (uipos >= 0 && uipos < 24 & (b[i - 1] == CHIM.CHAR_q || b[i - 1] == CHIM.CHAR_Q)) {
  1028.                 i++
  1029.             }
  1030.         }
  1031.         p = i;
  1032.         break
  1033.     };
  1034.     if (Mudim.GetMarkTypeID(k, 1) == 3 && b[0] == 'd') {
  1035.         return 0
  1036.     }
  1037.     return p
  1038. };
  1039. Mudim.PutMark = function (pos, charCodeAtPos, group, subsTab, key, checkDouble) {
  1040.     var v = subsTab;
  1041.     var i;
  1042.     for (i = 0; i < v.length; i++) {
  1043.         if (v[i] == charCodeAtPos) {
  1044.             switch (group) {
  1045.             case 1:
  1046.                 if (Mudim.GetMarkTypeID(key, 1) == 1) {
  1047.                     Mudim.w++
  1048.                 }
  1049.                 if (i % 2 == 0) {
  1050.                     CHIM.SetCharAt(pos, v[i + 1])
  1051.                 } else {
  1052.                     CHIM.SetCharAt(pos, v[i - 1]);
  1053.                     if (checkDouble) {
  1054.                         CHIM.off = CHIM.buffer.length + 1
  1055.                     }
  1056.                 }
  1057.                 break;
  1058.             case 2:
  1059.                 var j = Mudim.GetMarkTypeID(key, 2);
  1060.                 if (j >= 0) {
  1061.                     if (j != i) {
  1062.                         CHIM.SetCharAt(pos, v[j]);
  1063.                         Mudim.accent = [pos, (CHIM.buffer[pos]).charCodeAt(0), v, key]
  1064.                     } else {
  1065.                         CHIM.SetCharAt(pos, v[0]);
  1066.                         Mudim.ResetAccentInfo();
  1067.                         if (checkDouble) {
  1068.                             CHIM.off = CHIM.buffer.length + 1
  1069.                         }
  1070.                     }
  1071.                 }
  1072.                 break
  1073.             }
  1074.             return true
  1075.         }
  1076.     }
  1077.     return false
  1078. };
  1079. Mudim.ResetAccentInfo = function () {
  1080.     Mudim.accent = [-1, 0, null, 'z']
  1081. };
  1082. Mudim.AdjustAccent = function (vk) {
  1083.     if (CHIM.off != 0) {
  1084.         return false
  1085.     }
  1086.     var p = Mudim.FindAccentPos(vk);
  1087.     var a = Mudim.accent;
  1088.     var b = CHIM.buffer;
  1089.     var v, i, j, c;
  1090.     if (p < 0) {
  1091.         return false
  1092.     }
  1093.     i = CHIM.vn_OW.length - 1;
  1094.     c = b[p].charCodeAt(0);
  1095.     while (i >= 0 && CHIM.vn_OW[i] != c) {
  1096.         i--
  1097.     }
  1098.     j = CHIM.vn_UW.length - 1;
  1099.     if (p > 0) {
  1100.         c = b[p - 1].charCodeAt(0);
  1101.         while (j >= 0 && CHIM.vn_UW[j] != c) {
  1102.             j--
  1103.         }
  1104.     } else {
  1105.         j = -1
  1106.     } if (p < b.length - 1 && p > 0 && i >= 0 && j >= 0) {
  1107.         if (Mudim.w == 1) {
  1108.             if (i % 2 == 0) {
  1109.                 Mudim.PutMark(p, b[p].charCodeAt(0), 1, CHIM.vn_OW, CHIM.modes[Mudim.method - 1][1].charAt(1), false);
  1110.                 if (b[0] == CHIM.CHAR_q || b[0] == CHIM.CHAR_Q) {
  1111.                     Mudim.PutMark(p - 1, b[p - 1].charCodeAt(0), 1, CHIM.vn_UW, CHIM.modes[Mudim.method - 1][1].charAt(1), false)
  1112.                 }
  1113.             } else {
  1114.                 if (b[0] != CHIM.CHAR_q && b[0] != CHIM.CHAR_Q) {
  1115.                     Mudim.PutMark(p - 1, b[p - 1].charCodeAt(0), 1, CHIM.vn_UW, CHIM.modes[Mudim.method - 1][1].charAt(1), false)
  1116.                 }
  1117.             }
  1118.             return true
  1119.         }
  1120.     }
  1121.     if (a[0] >= 0 && p > 0 && a[0] != p) {
  1122.         Mudim.PutMark(a[0], a[1], 2, a[2], a[3], false);
  1123.         for (i = 0; i < CHIM.vncode_2.length; i++) {
  1124.             v = CHIM.vncode_2[i];
  1125.             if (Mudim.PutMark(p, b[p].charCodeAt(0), 2, v, a[3], true)) {
  1126.                 break
  1127.             }
  1128.         }
  1129.         return true
  1130.     }
  1131.     return false
  1132. };
  1133. Mudim.GetMarkTypeID = function (key, group) {
  1134.     var m = CHIM.modes[Mudim.method - 1];
  1135.     if (Mudim.method != 4) {
  1136.         return m[group].indexOf(key)
  1137.     } else {
  1138.         var j = -1;
  1139.         for (var i = 0; i < 2; i++) {
  1140.             j = CHIM.modes[i][group].indexOf(key);
  1141.             if (j >= 0) {
  1142.                 return j
  1143.             }
  1144.         }
  1145.         return j
  1146.     }
  1147. };
  1148. Mudim.AutoDetectMode = function (c) {
  1149.     var gi;
  1150.     if ((gi = CHIM.modes[4][1].indexOf(c)) >= 0) {
  1151.         if (gi < 4) {
  1152.             return 1
  1153.         } else if (gi < 9) {
  1154.             return 2
  1155.         } else {
  1156.             return 3
  1157.         }
  1158.     } else if ((gi = CHIM.modes[4][2].indexOf(c)) >= 0) {
  1159.         if (gi < 6) {
  1160.             return 1
  1161.         } else if (gi < 12) {
  1162.             return 2
  1163.         } else {
  1164.             return 3
  1165.         }
  1166.     } else {
  1167.         return 0
  1168.     }
  1169. };
  1170. Mudim.SetPreference = function () {
  1171.     var d = new Date();
  1172.     d.setTime(d.getTime() + 604800000);
  1173.     var tail = ';expires=' + d.toGMTString() + ';path=/';
  1174.     var value = Mudim.method;
  1175.     var value = CHIM.Speller.enabled ? value + 8 : value;
  1176.     value = Mudim.newAccentRule ? value + 16 : value;
  1177.     value = Mudim.showPanel ? value + 32 : value;
  1178.     value += Mudim.displayMode * 64;
  1179.     document.cookie = '|mudim-settings=' + value + tail
  1180. };
  1181. Mudim.GetPreference = function () {
  1182.     var c = document.cookie.split(';');
  1183.     for (var i = 0; i < c.length && c[i].indexOf('|mudim-settings') < 0; i++);
  1184.     if (i == c.length) {
  1185.         CHIM.SetDisplay()
  1186.     } else {
  1187.         var value = parseInt(c[i].split('=')[1], 10);
  1188.         Mudim.method = value & 7;
  1189.         CHIM.Speller.enabled = (value & 8) ? true : false;
  1190.         CHIM.newAccentRule = (value & 16) ? true : false;
  1191.         Mudim.showPanel = (value & 32) ? true : false;
  1192.         Mudim.displayMode = (value & 64) >> 6
  1193.     }
  1194. };
  1195. Mudim.ToggleAccentRule = function () {
  1196.     Mudim.newAccentRule = !Mudim.newAccentRule
  1197. };
  1198. Mudim.TogglePanel = function () {
  1199.     Mudim.showPanel = !Mudim.showPanel;
  1200.     Mudim.Panel.style.display = Mudim.showPanel ? '' : 'None';
  1201.     Mudim.SetPreference()
  1202. };
  1203. Mudim.ShowPanel = function () {
  1204.     Mudim.showPanel = true;
  1205.     Mudim.Panel.style.display = ''
  1206. };
  1207. Mudim.HidePanel = function () {
  1208.     Mudim.showPanel = false;
  1209.     Mudim.Panel.style.display = 'None'
  1210. };
  1211. Mudim.InitPanel = function (id, insert) {
  1212.     if (!Mudim.Panel) {
  1213.         Mudim.GetPreference();
  1214.         Mudim.panels = ['<div id="mudimPanel" style="position: fixed; bottom: 0; right:0; left:0; width: 100%; border: 1px solid black; padding: 1px; background: ' + Mudim.PANEL_BACKGROUND + '; color:' + Mudim.COLOR + '; z-index:4000; text-align: center; font-size: 10pt;"><a href="http://mudim.googlecode.com" title="Mudzot\'s Input Method" onclick="Mudim.ToggleDisplayMode();return false;">Mudim</a> v0.8 <input name="mudim" id="mudim-off" onclick="Mudim.SetMethod(0);" type="radio">' + Mudim.LANG[0] + '<input name="mudim" id="mudim-vni" onclick="Mudim.SetMethod(1);" type="radio"> ' + Mudim.LANG[1] + ' <input name="mudim" id="mudim-telex" onclick="Mudim.SetMethod(2);" type="radio"> ' + Mudim.LANG[2] + ' <input name="mudim" id="mudim-viqr" onclick="Mudim.SetMethod(3);" type="radio"> ' + Mudim.LANG[3] + ' <input name="mudim" id="mudim-mix" onclick="Mudim.SetMethod(4);" type="radio"> ' + Mudim.LANG[4] + ' <input name="mudim" id="mudim-auto" onclick="Mudim.SetMethod(5);" type="radio"> ' + Mudim.LANG[5] + ' <input id="mudim-checkspell" onclick="javascript:Mudim.ToggleSpeller();" type="checkbox">' + Mudim.LANG[6] + '<input id="mudim-accentrule" onclick="javascript:Mudim.ToggleAccentRule();" type="checkbox">' + Mudim.LANG[7] + ' [&nbsp;<a href="#" onclick="Mudim.Toggle();return false;">' + Mudim.LANG[8] + '</a> (F9) <a href="#" onclick="Mudim.TogglePanel();return false;">' + Mudim.LANG[9] + '</a> (F8) ]</div>', '<div id="mudimPanel">Tiếng Việt:&nbsp;<a href="javascript:void(0)" onclick="Mudim.Toggle();typingtoggle(this);return false;">#TOGGLE#</a>&nbsp;-&nbsp;Kiểu gõ:&nbsp;<a href="javascript:void(0)" onclick="typingupdate(this);return false;">#METHOD#</a></div>'];
  1215.         var f = document.createElement('div');
  1216.         var tg = 'Bật';
  1217.         if (Mudim.method === 0) {
  1218.             tg = 'Tắt';
  1219.         }
  1220.         f.innerHTML = Mudim.panels[Mudim.displayMode].replace('#TOGGLE#', tg).replace('#METHOD#', Mudim.LANG[Mudim.method]);
  1221.         f.style.display = 'None';
  1222.         if (id) {
  1223.             var ele = document.getElementById(id);
  1224.             if (insert == 'after') {
  1225.                 ele.appendChild(f);
  1226.             } else if (insert == 'before') {
  1227.                 ele.insertBefore(f, ele.childNodes[0]);
  1228.             } else {
  1229.                 ele.innerHTML = f.innerHTML;
  1230.             }
  1231.         } else {
  1232.             document.body.appendChild(f);
  1233.         }
  1234.         Mudim.Panel = f;
  1235.         if (Mudim.showPanel) {
  1236.             Mudim.ShowPanel()
  1237.         } else {
  1238.             Mudim.HidePanel()
  1239.         }
  1240.     }
  1241. };
  1242. Mudim.ToggleSpeller = function () {
  1243.     CHIM.Speller.Toggle()
  1244. };
  1245. Mudim.Toggle = function () {
  1246.     CHIM.Toggle()
  1247. };
  1248. Mudim.ToggleDisplayMode = function () {
  1249.     if (Mudim.displayMode) {
  1250.         Mudim.displayMode = 0
  1251.     } else {
  1252.         Mudim.displayMode = 1
  1253.     }
  1254.     Mudim.BeforeInit();
  1255.     Mudim.Panel.innerHTML = Mudim.panels[Mudim.displayMode].replace('#METHOD#', Mudim.LANG[Mudim.method]);
  1256.     Mudim.AfterInit();
  1257.     Mudim.SetPreference()
  1258. };
  1259. Mudim.SetMethod = function (m) {
  1260.     CHIM.SetMethod(m)
  1261. };
  1262. Mudim.SwitchMethod = function () {
  1263.     CHIM.SwitchMethod()
  1264. };
  1265. Mudim.BeforeInit = function () {};
  1266. Mudim.AfterInit = function () {};
  1267. Mudim.Init = function () {
  1268.     Mudim.BeforeInit();
  1269.     Mudim.InitPanel('zzMudimFork');
  1270.     CHIM.Activate();
  1271.     Mudim.AfterInit()
  1272. };
  1273. Mudim.GetPanelStyle = function () {
  1274.     return document.getElementById('mudimPanel').style;
  1275. };
  1276. Mudim.newAccentRule = true;
  1277. Mudim.showPanel = true;
  1278. Mudim.accent = [-1, 0, null, -1];
  1279. Mudim.w = 0;
  1280. Mudim.tempOff = false;
  1281. Mudim.tempDisableSpellCheck = false;
  1282. Mudim.newTempDisableSpellCheckRequest = false;
  1283. Mudim.ctrlSerie = 0;
  1284. Mudim.shiftSerie = 0;
  1285. Mudim.headConsonants = '';
  1286. Mudim.tailConsonants = '';
  1287. Mudim.startWordOffset = 0;
  1288. Mudim.COLOR = 'Black';
  1289. Mudim.PANEL_BACKGROUND = 'lightYellow';
  1290. Mudim.IGNORE_ID = [];
  1291. Mudim.displayMode = 0;
  1292. Mudim.panels = ['', ''];
  1293. Mudim.REV = 153;
  1294. Mudim.LANG = ['Tắt', 'VNI', 'Telex', 'Viqr', 'Tổng hợp', 'Tự động', 'Chính tả', 'Bỏ dấu kiểu mới', 'Bật/Tắt', 'Ẩn/Hiện'];
  1295. Mudim.method = 4;
  1296. Mudim.oldMethod = 4;
  1297.  
  1298. function typingtoggle(x) {
  1299.     if (Mudim.method === 0) {
  1300.         x.innerHTML = 'Tắt';
  1301.     } else {
  1302.         x.innerHTML = 'Bật';
  1303.     }
  1304.     x.nextSibling.nextSibling.innerHTML = Mudim.LANG[Mudim.method];
  1305. }
  1306.  
  1307. function typingupdate(x) {
  1308.     Mudim.method++;
  1309.     if (Mudim.method >= 6) {
  1310.         Mudim.method = 1
  1311.     }
  1312.     var pre = x.previousSibling.previousSibling;
  1313.     if (Mudim.method === 0) {
  1314.         pre.innerHTML = 'Tắt';
  1315.     } else {
  1316.         pre.innerHTML = 'Bật';
  1317.     }
  1318.     x.innerHTML = Mudim.LANG[Mudim.method];
  1319.     Mudim.SetPreference();
  1320. }
  1321. Mudim.BeforeInit = function () {
  1322.     Mudim.displayMode = 1;
  1323.     Mudim.IGNORE_ID = ['email', 'url'];
  1324. };
  1325. Mudim.AfterInit = function () {
  1326.     // s = Mudim.GetPanelStyle();
  1327.     // s.fontSize = '11px';
  1328.     // s.cssFloat = 'right';
  1329. };
  1330. for (var i = 1; i < 100; i++) {
  1331.     setTimeout("Mudim.Init()", 2000 * i)
  1332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement