-
Notifications
You must be signed in to change notification settings - Fork 5
/
jkeymagic.keyboard.js
executable file
·141 lines (122 loc) · 4.23 KB
/
jkeymagic.keyboard.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
if (!JKME.keyboard) {
JKME.keyboard = {
lastkb: window['KMKL'],
events: {
'keyboardchanged': []
},
keyboards: {
//default keyboard which is empty
'Default': {
path: '',
loaded: true,
keyboard: {
'eat' : false
}
}
},
addEventListener : function (event, listener) {
this.events[event].push(listener);
},
callEventListeners: function (event, arg) {
for (var i in this.events[event]) {
this.events[event][i](arg);
}
},
addKeyboard: function (path, name, keyboard) {
newKeyboard = {
path: path,
loaded: false
}
this.keyboards[name] = newKeyboard;
if (keyboard !== undefined) {
this.keyboards[name].loaded = true;
this.keyboards[name].keyboard = keyboard;
}
this.callEventListeners('keyboardchanged');
},
load: function (file, callback) {
var head = document.getElementsByTagName('body')[0];
newjs = document.createElement('script');
// IE
newjs.onreadystatechange = function () {
if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
newjs.onreadystatechange = null;
callback('success');
}
};
newjs.onerror = function () {
if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
newjs.onreadystatechange = null;
callback('fail');
head.removeChild(newjs);
}
};
// others
newjs.onload = function () {
callback('success');
};
newjs.onerror = function () {
callback('fail');
head.removeChild(newjs);
};
newjs.src = file;
newjs.type = 'text/javascript';
head.appendChild(newjs);
},
loadKeyboard: function (name, callback) {
var kbName = name;
var keyboard = this.keyboards[name];
loading = true;
if (keyboard.loaded == false) {
this.load(keyboard['path'] ,function (status) {
if (status === 'fail') {
//delete JKME.keyboard.keyboards[kbName];
keyboard.error = true;
error('Failed to load keyboard. Invalid keyboard path?');
callback('fail');
JKME.keyboard.callEventListeners('keyboardchanged');
return;
}
if (this.lastkb == window['KMKL']) {
//delete JKME.keyboard.keyboards[kbName];
keyboard.error = true;
error('Failed to load keyboard. Invalid keyboard file?');
callback('fail');
JKME.keyboard.callEventListeners('keyboardchanged');
return;
}
this.lastkb = window['KMKL'];
keyboard.keyboard = window['KMKL'];
keyboard.loaded = true;
callback('success');
JKME.keyboard.callEventListeners('keyboardchanged');
})
} else {
callback('success');
}
},
createOption: function (text, selected) {
if (selected === undefined) {
selected = true;
}
var option = document.createElement('option');
option.selected = selected;
option.defaultSelected = selected;
option.innerHTML = text;
option.value = text;
return option;
},
getKeyboardsAsOptions: function (selectedKeyboardName) {
var options = document.createElement('select');
var alt = false;
for (var kb in this.keyboards) {
var option = this.createOption(kb, kb == selectedKeyboardName);
if (this.keyboards[kb].error) option.className += 'error ';
if (alt) option.className += 'alt ';
options.appendChild(option);
alt = !alt;
}
return options;
}
}
}