From: handa Date: Sat, 20 Feb 2010 09:04:08 +0000 (+0000) Subject: *** empty log message *** X-Git-Tag: ALPHA~52 X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d611a01174765fa935d0f3bd944558a5a3e1d32e;p=m17n%2Fm17n-lib-js.git *** empty log message *** --- diff --git a/xex.js b/xex.js index 0024512..a726a77 100644 --- a/xex.js +++ b/xex.js @@ -1313,7 +1313,7 @@ MIM.Keymap = function () for (var i = 0; i < len; i++) { var key = keyseq.val[i]; - var sub; + var sub = false; if (! keymap.submaps) keymap.submaps = {}; @@ -1497,6 +1497,7 @@ MIM.im_domain.DefType (MIM.State.prototype); this.domain = new Xex.Domain ('context', im.domain, this); this.active = true; this.reset (); + this.spot = 0; } MIM.CandidateTable = function () @@ -1904,35 +1905,288 @@ MIM.im_domain.DefType (MIM.State.prototype); }) (); (function () { + var keys = new Array (); + keys[0x09] = 'tab'; + keys[0x08] = 'backspace'; + keys[0x0D] = 'return'; + keys[0x1B] = 'escape'; + keys[0x20] = 'space'; + keys[0x21] = 'pageup'; + keys[0x22] = 'pagedown'; + keys[0x23] = 'end'; + keys[0x24] = 'home'; + keys[0x25] = 'left'; + keys[0x26] = 'up'; + keys[0x27] = 'right'; + keys[0x28] = 'down'; + keys[0x2D] = 'insert'; + keys[0x2E] = 'delete'; + for (var i = 1; i <= 12; i++) + keys[111 + i] = "f" + i; + keys[0x90] = "numlock"; + keys[0xF0] = "capslock"; + + MIM.decode_key_event = function (event) + { + var key = ((event.type == 'keydown' || event.keyCode) ? event.keyCode + : event.charCode ? event.charCode + : false); + if (! key) + return false; + if (event.type == 'keydown') + { + key = keys[key]; + if (! key) + return false; + if (event.shiftKey) key = "S-" + key ; + } + else + key = String.fromCharCode (key); + if (event.altKey) key = "A-" + key ; + if (event.ctrlKey) key = "C-" + key ; + return new MIM.Key (key); + } +}) (); - MIMTEST = function (name) { this.nam = name; }; +MIM.add_event_listener + = (window.addEventListener + ? function (target, type, listener) { + target.addEventListener (type, listener, false); + } + : window.attachEvent + ? function (target, type, listener) { + target.attachEvent ('on' + type, + function() { + listener.call (target, window.event); + }); + } + : function (target, type, listener) { + target['on' + type] + = function (e) { listener.call (target, e || window.event); }; + }); + +MIM.debug_print = function (event, ic) +{ + if (! MIM.debug) + return; + if (! MIM.debug_nodes) + { + MIM.debug_nodes = new Array (); + MIM.debug_nodes['keydown'] = document.getElementById ('keydown'); + MIM.debug_nodes['keypress'] = document.getElementById ('keypress'); + MIM.debug_nodes['status0'] = document.getElementById ('status0'); + MIM.debug_nodes['status1'] = document.getElementById ('status1'); + MIM.debug_nodes['keyseq0'] = document.getElementById ('keyseq0'); + MIM.debug_nodes['keyseq1'] = document.getElementById ('keyseq1'); + MIM.debug_nodes['range0'] = document.getElementById ('range0'); + MIM.debug_nodes['range1'] = document.getElementById ('range1'); + } + var target = event.target; + var code = event.keyCode; + var ch = event.type == 'keydown' ? 0 : event.charCode; + var key = MIM.decode_key_event (event); + var index; + + MIM.debug_nodes[event.type].innerHTML = "" + code + "/" + ch + " : " + key; + index = (event.type == 'keydown' ? '0' : '1'); + MIM.debug_nodes['status' + index].innerHTML = ic.im.load_status; + MIM.debug_nodes['keyseq' + index].innerHTML = ic.keys; + MIM.debug_nodes['preedit' + index].innerHTML = ic.preedit; +}; - function testshow () { alert (this.nam); }; +MIM.get_range = function (target, range) +{ + if (target.selectionStart != null) // for Mozilla + { + range[0] = target.selectionStart; + range[1] = target.selectionEnd; + } + else // for IE + { + var r = document.selection.createRange (); + var rr = r.duplicate (); - MIMTEST.prototype.show = function () { testshow.call (this); } - MIMTEST.prototype.cut = function (from, to) { - this.val.splice (from, to -from); return this.val; } + rr.moveToElementText (target); + rr.setEndPoint ('EndToEnd', range); + range[0] = rr.text.length - r.text.length; + range[1] = rr.text.length; + } +} - MIMTEST2 = function (name) { this.nam = name; - this.val = new Array (1,2,3,4,5);} +MIM.set_caret = function (target, ic) +{ + if (target.selectionStart != null) // Mozilla + { + target.focus (); + target.setSelectionRange (ic.spot, ic.spot + ic.preedit.length); + } + else // IE + { + var range = target.createTextRange (); + range.move ('character', pos); + range.select (); + } +}; + +(function () { + var range = new Array (); + + MIM.check_range = function (target, ic) + { + MIM.get_range (target, range); + if (range[0] != ic.spot || range[1] - range[0] != ic.preedit.length) + { + ic.reset (); + ic.spot = range[0]; + } + } +}; + +MIM.produce = function (target, ic) +{ + target.value = (text.substring (0, ic.spot) + + ic.produced + + text.substring (ic.)); + ic.range[1] = ic.range[0] + insert.length; + MIM.set_caret (ic.target, ic.range[1]); +}; + +MIM.reset_ic = function (event) +{ + var ic = event.target.mim_ic; + if (ic) + ic.reset (); +}; + +MIM.keydown = function (event) +{ + var target = event.target; + if (! (target.type == "text" || target.type == "textarea")) + return; + + var ic = target.mim_ic; + if (! ic || ic.im != MIM.current) + { + ic = new MIM.IC (MIM.current_im); + target.mim_ic = ic; + MIM.add_event_listener (target, 'blur', MIM.reset_ic); + } + if (ic.im.load_status != MIM.LoadStatus.Loaded) + return; + MIM.check_range (target, ic); + MIM.debug_print (event, ic); + ic.key = MIM.decode_key_event (event); +}; + +MIM.keypress = function (event) +{ + if (! (event.target.type == "text" || event.target.type == "textarea")) + return; + + var ic = event.target.mim_ic; + var i; + + try { + if (ic.im.loaded != MIM.LoadStatus.Loaded) + return; + if (! ic.key) + ic.key = MIM.decode_key_event (event); + if (! ic.key) + { + ic.reset (); + return; + } + if (ic.Filter (ic.key)) + MIM.set_caret (target, ic); + else + { + if (ic.preedit.length > 0) + { + MIM.insert + + } + } + + + if (ic.im.status == 1) // Still loading. + return; + MIM.handle_keyseq (event, ic); + } finally { + MIM.debug_print (event, ic); + } + return; +}; + +MIM.select_im = function (event) +{ + var target = event.target.parentNode; + while (target.tagName != "SELECT") + target = target.parentNode; + var idx = 0; + var im = false; + for (var lang in MIM.list) + for (var name in MIM.list[lang]) + if (idx++ == target.selectedIndex) + { + im = MIM.list[lang][name]; + break; + } + document.getElementsByTagName ('body')[0].removeChild (target); + target.target.focus (); + if (im && im != MIM.current_im) + MIM.current_im = MIM.load_sync (im); +}; + +MIM.destroy_menu = function (event) +{ + if (event.target.tagName == "SELECT") + document.getElementsByTagName ('body')[0].removeChild (event.target); +}; + +MIM.select_menu = function (event) +{ + var target = event.target; + + if (! ((target.type == "text" || target.type == "textarea") + && event.which == 1 && event.ctrlKey)) + return; + + var sel = document.createElement ('select'); + sel.onclick = MIM.select_im; + sel.onmouseout = MIM.destroy_menu; + sel.style.position='absolute'; + sel.style.left = (event.clientX - 10) + "px"; + sel.style.top = (event.clientY - 10) + "px"; + sel.target = target; + var idx = 0; + for (var lang in MIM.list) + for (var name in MIM.list[lang]) + { + var option = document.createElement ('option'); + var imname = lang + "-" + name; + option.appendChild (document.createTextNode (imname)); + option.value = imname; + sel.appendChild (option); + if (MIM.list[lang][name] == MIM.current_im) + sel.selectedIndex = idx; + idx++; + } + sel.size = idx; + document.getElementsByTagName ('body')[0].appendChild (sel); +}; - MIMTEST2.prototype = MIMTEST.prototype; - var x = new MIMTEST2 ('test2'); -}) (); MIM.test = function () { var im = MIM.im_list['t']['latn-post']; var ic = new MIM.IC (im); - document.AIM = im; - ic.Filter (new MIM.Key ('a')); ic.Filter (new MIM.Key ("'")); if (true) - document.getElementById ('text').value = ic.preedit; + document.getElementById ('text').value = ic.produced + ic.preedit; else { try { document.getElementById ('text').value