forked from aiman1717a/nova-menu-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjtk.js
211 lines (176 loc) · 6.89 KB
/
jtk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Javascript Thaana Keyboard 4.2.2
*
* Copyright (c) 2014 Jawish Hameed (jawish.org)
* Licensed under the MIT license.
*/
var thaanaKeyboard = {
// Default keyboard (used only when not explicitly declared using state)
defaultKeyboard: '',
// Default class name used for hooking at initialization
className: 'thaanaKeyboardInput',
// Setup Ascii Thaana -> Unicode translation matrix
_transFrom: 'qwertyuiop[]\\asdfghjkl;\'zxcvbnm,./QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?()',
_transToKbd: { 'phonetic': 'ްއެރތޔުިޮޕ][\\ަސދފގހޖކލ؛\'ޒ×ޗވބނމ،./ޤޢޭޜޓޠޫީޯ÷}{|ާށޑﷲޣޙޛޚޅ:\"ޡޘޝޥޞޏޟ><؟)(',
'phonetic-hh': 'ޤަެރތޔުިޮޕ][\\އސދފގހޖކލ؛\'ޒޝްވބނމ،./ﷲާޭޜޓޠޫީޯޕ}{|ޢށޑޟޣޙޛޚޅ:\"ޡޘޗޥޞޏމ><؟)(',
'typewriter': 'ޫޮާީޭގރމތހލ[]ިުްަެވއނކފﷲޒޑސޔޅދބށޓޯ×’“/:ޤޜޣޠޙ÷{}<>.،\"ޥޢޘޚޡ؛ޖޕޏޗޟޛޝ\\ޞ؟)('
},
/**
* Apply input handling to elements with the special JTK class name
*/
init: function () {
this.setHandlerByClass(this.className, 'enable');
},
/**
* Set key event handlers for a given element
*
* @param element DOM Element to affect
* @param action String specifying action to take. Should be 'enable' or 'disable'.
*/
setHandlerByElement: function (elem, action) {
// Respond to action specified
switch (action) {
case 'enable':
// Enable input handling
elem.onkeypress = this.handleKey;
break;
case 'disable':
// Disable input handling
elem.onkeypress = null;
break;
}
},
/**
* Set key event handlers for elements with the specified id
*
* @param id String id of the element to affect
* @param action String specifying action to take. Should be 'enable' or 'disable'.
*/
setHandlerById: function (id, action) {
// Get the element object
var elem = document.getElementById(id);
this.setHandlerByElement(elem, action);
},
/**
* Set key event handlers for elements of the specified class
*
* @param clsname String class name of the elements to affect
* @param action String specifying action to take. Should be 'enable' or 'disable'.
*/
setHandlerByClass: function (clsname, action) {
// Setup regular expression for finding the target class
var rePat = new RegExp('\\b' + clsname + '\\b');
// Get all the elements on page
var elemList = document.all || document.getElementsByTagName('*');
// Loop through the elements
for (n = 0; n < elemList.length; n++) {
// Ensure the current element is of the target class
if (rePat.test(elemList[n].className)) {
// Set handler
this.setHandlerByElement(elemList[n], action);
}
}
},
/**
* Do character translation on key activity
*/
handleKey: function (e) {
// Get the event data
if (!e) var e = window.event;
// Get the pressed key
if (e.which == null) {
// IE:
keycode = e.keyCode;
}
else if (e.which > 0) {
// Non-IE printable chars:
keycode = e.which;
// Handle special keys misfire in Opera
if (window.opera && [45, 46, 35, 36].indexOf(keycode) == 1) return true;
}
else {
// Ignore special keys
return true;
}
// Check for CTRL modifier key
if (e.modifier) {
var ctrl = e.modifiers & Event.CONTROL_MASK;
}
else if (typeof(e.ctrlKey) != 'undefined') {
var ctrl = e.ctrlKey;
}
// Get translation details for the entered char
try {
var transIndex = thaanaKeyboard._transFrom.indexOf(String.fromCharCode(keycode));
// If pressed key does not require translation, let default action proceed
if (transIndex == -1 || ctrl) return true;
// Set default state
var state = thaanaKeyboard.defaultKeyboard;
// Get state
var elemState = document.getElementsByName(this.id + '_thaanaKeyboardState');
if (elemState) {
// State element has been defined:
// Get the state specified by the element
for (i = 0; i < elemState.length; i++) {
if (elemState[i].type == 'radio' || elemState[i].type == 'checkbox') {
// Handle radio/checkbox fields
if (elemState[i].checked == true) {
state = elemState[i].value;
break;
}
}
else if (elemState[i].type == 'select-one') {
// Handle select/combo list fields
state = elemState[i].options[elemState[i].selectedIndex].value;
break;
}
else if (elemState[i].type == 'hidden' || elemState[i].type == 'text') {
// Handle hidden and text fields
state = elemState[i].value;
break;
}
}
}
// Look up the translated char
var transChar = thaanaKeyboard._transToKbd[state].substr(transIndex, 1);
}
catch (err) {
// Incorrect type or type not defined, return immediately
return;
}
// Cancel default action for the key
if (typeof e.preventDefault == 'function') {
e.preventDefault();
}
else {
e.returnValue = false;
}
// Store scroll position for textarea
var sPos = this.scrollTop;
// Insert text at cursor position
if (this.selectionStart) {
// For Firefox/Safari:
var selOld = this.selectionStart + 1;
this.value = this.value.substring(0, this.selectionStart)
+ transChar
+ this.value.substring(this.selectionEnd, this.value.length);
this.setSelectionRange(selOld, selOld);
this.focus();
}
else if (document.selection) {
// For IE/Opera:
sel = document.selection.createRange();
sel.text = transChar;
this.focus();
}
else {
// If all fails, just append
this.value += transChar;
}
// Restore scroll position for textarea
this.scrollTop = sPos;
}
};
// Start the Thaana Keyboard soon as the page is loaded
var old = (window.onload) ? window.onload : function () {};
window.onload = function(){old(); thaanaKeyboard.init();};