User:Ryuch/monobook.js

/* AIM (A Input Method or ALee's Input Method) for Hangul

Author: A Lee  Date: 2007-02-05 Usage: See http://aim.anj.kr

Copyright 2006 (C) A Lee . All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of  the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

The license can be found at http://www.gnu.org/licenses/gpl.txt. */

var aimQ	= new Array(0,0,0,0,0,0); /* cho=aimQ[0]+aimQ[1], jung=aimQ[2]+aimQ[3], jong=aimQ[4]+aimQ[5] */ var aimCho	= new Array(0,1,3,6,7,8,16,17,18,20,21,22,23,24,25,26,27,28,29); var aimJong	= new Array(0,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,17,19,20,21,22,23,25,26,27,28,29); var aimDJung	= new Array(Array(9,14,19),Array(1,2,21),Array(5,6,21),Array(21,0)); var aimDJong	= new Array(Array(1,4,8,17,19),Array(1,19),Array(22,27),Array(1,16,17,19,25,26,27),Array(19,0),Array(19,0)); var aimStatus	= document.createElement('div'); aimStatus.innerHTML = 'En'; aimStatus.style.fontFamily = 'GulimChe,monospace'; aimStatus.style.fontSize = '10pt'; aimStatus.style.lineHeight = '10pt'; aimStatus.style.color = 'white'; aimStatus.style.backgroundColor = 'blue'; aimStatus.style.right = 0; aimStatus.style.bottom = 0; aimStatus.style.position = document.all ? 'absolute' : 'fixed';

function aimIndex(a, c) { for (var i=0, l=a.length; i<l; i++) if (c == a[i]) return i+1; return 0; }

function aimInsert(f, m, c) { // Insert to caret if (c.length == 6) { if (!m) m = '0,0,0,0,0,0'; if (c[0] && c[2]) c = 0xac00+(c[0]+c[1]-1)*588+(c[2]+c[3]-30)*28+c[4]+c[5]; else c = 0x3131+(c[0]?aimCho[c[0]+c[1]-1]:c[2]?c[2]+c[3]:aimJong[c[4]-1]); }	else if (c) aimQ = Array(0,0,0,0,0,0); if (!c) { if (aimQ == '0,0,0,0,0,0') return true; else aimQ = Array(0,0,0,0,0,0); }	if (document.selection) { var s = document.selection.createRange, t = s.text; if (t && document.selection.clear) document.selection.clear; s.text = (m == '0,0,0,0,0,0' || c && t.length > 1 ? '' : t) + String.fromCharCode(c); if (c && m) s.moveStart('character', -1); s.select; }	else if (f.selectionStart+1) { if (m != '0,0,0,0,0,0' && f.selectionEnd-f.selectionStart == 1) f.selectionStart = f.selectionEnd; var e = document.createEvent('KeyEvents'); e.initKeyEvent('keypress',0,0,null,0,0,0,0,0,c); if (c) f.dispatchEvent(e); if (c && m) f.selectionStart--; } }

function aimHangul2(f, c) { // for 2-Bulsik if (c < 65 || (c-1)%32 > 25) return aimInsert(f, '', c); c = (c==79||c==80?2:c==69||c==81||c==82||c==84||c==87?1:0) + Array(16,47,25,22,6,8,29,38,32,34,30,50,48,43,31,35,17,0,3,20,36,28,23,27,42,26)[c%32-1];

if (c < 30) { // Jaum if (aimQ[5]) aimQ[0] = -1; else if (aimQ[2] && (aimQ[5]=aimIndex(aimDJong[0], aimQ[4]))) aimQ[5] = aimIndex(aimDJong[aimQ[5]], aimIndex(aimJong, c)); if (aimQ[2] && (!aimQ[0] || aimQ[0] > 0 && (aimQ[4] && aimQ[5] || !aimQ[4] && aimIndex(aimJong, c)))) { if (!aimQ[0]) aimQ[0] = aimIndex(aimCho, c); else if (!aimQ[4]) aimQ[4] = aimIndex(aimJong, c); aimInsert(f, '', aimQ); if (aimQ[5]) aimQ[5] = aimIndex(aimCho, c); }		else if (!aimQ[1] && !aimQ[2] && aimQ[0]==aimIndex(aimCho, c) && !(697015%(aimQ[0]*2+3))) aimInsert(f, '', aimQ=Array(aimIndex(aimCho,c),1,0,0,0,0)); else aimInsert(f, aimQ, aimQ=Array(aimIndex(aimCho,c),0,0,0,0,0)); }	else { // Moum if (aimQ[3] && !aimQ[4]) aimQ[2] = -1; else if (!aimQ[4] && (aimQ[3]=aimIndex(aimDJung[0], aimQ[2]-29))) aimQ[3] = aimIndex(aimDJung[aimQ[3]], c-29); if (aimQ[0] && !aimQ[2] || aimQ[2] > 0 && aimQ[3] && !aimQ[4]) { if (!aimQ[2]) aimQ[2] = c;			aimInsert(f, '', aimQ); }		else if (aimQ[0] && aimQ[2] > 0 && aimQ[4]) { if (!aimQ[5]) { aimQ[5] = aimIndex(aimCho, aimJong[aimQ[4]-1]); aimQ[4] = 0; } aimInsert(f, '', Array(aimQ[0],aimQ[1],aimQ[2],aimQ[3],aimQ[4],0)); aimInsert(f, aimQ, aimQ=Array(aimQ[5],0,c,0,0,0)); }		else aimInsert(f, aimQ, aimQ=Array(0,0,c,0,0,0)); } }

function aimHangul3(f, c) { // for 3-Bulsik c = Array(402,183,422,414,413,8220,217,39,126,8221,43,44,41,46,309,216,427,420,417,313,318,303,308,320,314,52,208,44,62,46,33,409,		/*A~*/407,63,424,411,405,410,304,48,55,49,50,51,34,45,56,57,426,415,406,412,54,403,425,418,53,423,40,58,60,61,59,42,421,314,		/*a~*/306,321,307,301,319,203,207,212,201,213,219,210,215,218,419,302,404,305,204,309,408,401,206,416,37,92,47,8251)[c-33]; if (c%512 < 200) return aimInsert(f, '', c);

if (c < 220) { // Cho if (!aimQ[1] && !aimQ[2] && aimQ[0]==c-200 && !(697015%(aimQ[0]*2+3))) aimInsert(f, '', aimQ=Array(c-200,1,0,0,0,0)); else aimInsert(f, aimQ, aimQ=Array(c-200,0,0,0,0,0)); }	else if (c < 322) { // Jung if (aimQ[3]) aimQ[2] = -1; else if (aimQ[3]=aimIndex(aimDJung[0], aimQ[2]-29)) aimQ[3] = aimIndex(aimDJung[aimQ[3]], c-300); if (aimQ[0] && !aimQ[2] || aimQ[2] > 0 && aimQ[3] && !aimQ[4]) { if (!aimQ[2]) aimQ[2] = c-300+29; aimInsert(f, '', aimQ); }		else aimInsert(f, aimQ, aimQ=Array(0,0,c-300+29,0,0,0)); }	else { // Jong if (aimQ[5]) aimQ[4] = -1; else if (aimQ[5]=aimIndex(aimDJong[0], aimQ[4])) aimQ[5] = aimIndex(aimDJong[aimQ[5]], c-400); if (aimQ[0] && aimQ[2] && (!aimQ[4] || aimQ[4] > 0 && aimQ[5])) { if (!aimQ[4]) aimQ[4] = c-400; aimInsert(f, '', aimQ); }		else aimInsert(f, aimQ, aimQ=Array(0,0,0,0,c-400,0)); } }

function aimKeypress(e) { if (e) var f = e.target, n = f.nodeName; else var e = window.event, f = e.srcElement, n = f.tagName; var c = (e.which || e.which == 0) ? e.which : e.keyCode; if (f.type == 'text' && n == 'INPUT' || n == 'TEXTAREA') { if (f.selectionEnd) if (f.selectionEnd-f.selectionStart != 1) aimQ = Array(0,0,0,0,0,0); if (c == 13 || c == 32) aimInsert(f, , ); if (c == 32 && (e.altKey || e.ctrlKey || e.shiftKey && aimStatus.innerHTML != 'H3')) { // Toggle aimStatus.innerHTML = (!e.ctrlKey&&aimStatus.innerHTML!='H2')?'H2':(e.ctrlKey&&aimStatus.innerHTML!='H3')?'H3':'En'; document.body.appendChild(aimStatus); return false; }		else if (document.body == aimStatus.parentNode) aimStatus.parentNode.removeChild(aimStatus); if (aimStatus.innerHTML != 'En' && c > 32 && c < 127 && !e.altKey && !e.ctrlKey) { if (c > 64 && c < 91 && !e.shiftKey) c += 32; if (c > 96 && c < 123 && e.shiftKey) c -= 32; if (aimStatus.innerHTML == 'H2') aimHangul2(f, c); if (aimStatus.innerHTML == 'H3') aimHangul3(f, c); return false; }	} }

function aimKeydown(e) { if (e) var f = e.target, n = f.nodeName; else var e = window.event, f = e.srcElement, n = f.tagName; if (f.type == 'text' && n == 'INPUT' || n == 'TEXTAREA') { if (e.keyCode == 8 && (aimQ[1] || aimQ[3] || aimQ[0] && aimQ[2])) { // Backspace for (var i=5; !aimQ[i];) i--; aimQ[i] = 0; aimInsert(f, '', aimQ); return false; }		if (e.keyCode != 16 && e.keyCode < 47) aimInsert(f, , ); if (e.keyCode == 27) f.blur; // Esc } }

document.onkeypress = aimKeypress; document.onkeydown = aimKeydown;