-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQWERTY_Dialing.js
173 lines (156 loc) · 5.59 KB
/
QWERTY_Dialing.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
import xapi from 'xapi';
const KEYBOARDLAYOUT = 'English'; // English || Norwegian
const KEYBORDTYPE = 'Keypad'; // Keyboard || Keypad
const defaultDomain = 'mydomain.com'
var currentNumber = '';
var dialHistory = [];
var dialHistoryPointer = 0;
var KEY_SHIFT_IS_DEPRESSED = 0;
var KEYMAP_NORWEGIAN_SHIFT = {
'KEY_2':'KEY_AT'
, 'KEY_SLASH':'KEY_UNDERSCORE'
};
var KEYMAP_NORWEGIAN_NORMAL = {
'KEY_SLASH': 'KEY_DASH'
, 'KEY_BACKSPACE': 'KEY_DELETE'
, 'KEY_MINUS' : 'KEY_PLUS'
};
var KEYMAP_ENGLISH_SHIFT = {
'KEY_2':'KEY_AT'
};
function getKeymappedCharacter(character){
if(KEYBOARDLAYOUT === 'Norwegian'){
if(KEY_SHIFT_IS_DEPRESSED){
return KEYMAP_NORWEGIAN_SHIFT[character] || character;
}
else{
return KEYMAP_NORWEGIAN_NORMAL[character] || character;
}
}
else{
if(KEY_SHIFT_IS_DEPRESSED){
return KEYMAP_ENGLISH_SHIFT[character] || character;
}
}
return character;
}
function clearCurrentNumber(){
currentNumber = '';
}
function removeLastCharacter(){
currentNumber = currentNumber.slice(0, -1);
}
function resetHistoryPointer(){
dialHistoryPointer = 0;
}
function showCurrentNumber(){
xapi.command('UserInterface Message Alert Display', {'Title': 'Place call', 'Text': currentNumber, 'Duration': 10});
}
function addCharacterToCurrentNumber(character){
currentNumber = currentNumber + character;
}
function dialCurrentNumber(){
if(currentNumber.length>0){
if(!currentNumber.includes('@')){
currentNumber = currentNumber + '@' + defaultDomain;
}
xapi.command('Dial', {'Number': currentNumber});
dialHistory.push(currentNumber);
clearCurrentNumber();
resetHistoryPointer();
}
}
function showAlertORErrorMessage(message){
xapi.command('UserInterface Message Alert Display', {'Title': message, 'Text': message, 'Duration': 2});
}
function disconnectCall(){
xapi.command('Call Disconnect');
showAlertORErrorMessage('Call Disconnected');
}
function copyHistoryToCurrentNumber(pointer){
console.log('Pointer: ' + pointer + ' history length: ' + dialHistory.length)
if(pointer > 0 && pointer <= dialHistory.length){
currentNumber = dialHistory[pointer-1];
showCurrentNumber();
}
else if(dialHistory.length === 0){
showAlertORErrorMessage('You have no numbers in call history');
}
else{
currentNumber = '';
showCurrentNumber();
}
}
xapi.event.on('UserInterface InputDevice Key Action', (event) => {
if(event.Type == 'Pressed'){
if(event.Key == 'KEY_RIGHTSHIFT' || event.Key == 'KEY_LEFTSHIFT'){
KEY_SHIFT_IS_DEPRESSED = 1;
}
else{
var key = getKeymappedCharacter(event.Key); //This is a bit of a hack as codec currently does not support meta keys. Thus, we need to keep state of metakeys in memory
// console.log('Translated key: ' + key);
if(key == 'KEY_BACK' || key == 'KEY_BACKSPACE' || key == 'KEY_DELETE' || key == 'KEY_KPBACK' || key == 'KEY_KPBACKSPACE' || key == 'KEY_KPDELETE'){
removeLastCharacter();
showCurrentNumber();
}
else if(key == 'KEY_UP'){
dialHistoryPointer++;
if(dialHistoryPointer >dialHistory.length) dialHistoryPointer = dialHistory.length;
copyHistoryToCurrentNumber(dialHistoryPointer);
}
else if(key == 'KEY_DOWN'){
dialHistoryPointer--;
if(dialHistoryPointer < 0) dialHistoryPointer = 0;
copyHistoryToCurrentNumber(dialHistoryPointer);
}
else if(key == 'KEY_ENTER' || key == 'KEY_KPENTER'){
dialCurrentNumber();
}
else if(key == 'KEY_ESC'){
disconnectCall();
}
else if(key == 'KEY_DOT' || key == 'KEY_KPDOT'){
addCharacterToCurrentNumber('.');
showCurrentNumber();
}
else if(key == 'KEY_DASH' || key == 'KEY_MINUS' || key == 'KEY_KPDASH' || key == 'KEY_KPMINUS'){
addCharacterToCurrentNumber('-');
showCurrentNumber();
}
else if(key == 'KEY_UNDERSCORE'){
addCharacterToCurrentNumber('_');
showCurrentNumber();
}
else if(key == 'KEY_PLUS' || key == 'KEY_KPPLUS'){
addCharacterToCurrentNumber('+');
showCurrentNumber();
}
else if(key == 'KEY_AT'){ //This is a bit of a hack as codec currently does not support variations of keyboard layouts
addCharacterToCurrentNumber('@');
showCurrentNumber();
}
else{
if (KEYBORDTYPE == 'Keypad') {
let match = /^KEY_KP(\S){1}$/.exec(event.Key);
if(match){
addCharacterToCurrentNumber(match[1]);
showCurrentNumber();
}else{//xapi.command('UserInterface Message Alert Display', {'Title': 'Remote Control Warning', 'Text':'This button is not in use yet. To program it use the "Key: ' + event.Key + ' (or Code: ' + event.Code + ')', 'Duration': 2});
}
}else{
let match = /^KEY_(\S){1}$/.exec(event.Key);
if(match){
addCharacterToCurrentNumber(match[1]);
showCurrentNumber();
}else{//xapi.command('UserInterface Message Alert Display', {'Title': 'Remote Control Warning', 'Text':'This button is not in use yet. To program it use the "Key: ' + event.Key + ' (or Code: ' + event.Code + ')', 'Duration': 2});
}
}
}
}
}
else{
if(event.Key == 'KEY_RIGHTSHIFT' || event.Key == 'KEY_LEFTSHIFT'){
KEY_SHIFT_IS_DEPRESSED = 0; //This is a bit of a hack as codec currently does not support meta keys. Thus, we need to keep state of metakeys in memory
}
}
});