-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdotabs.js
More file actions
640 lines (594 loc) · 17.5 KB
/
dotabs.js
File metadata and controls
640 lines (594 loc) · 17.5 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
Copyright 2013, BroadSoft, Inc.
Licensed under the Apache License,Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "ASIS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var MODULE = "dotabs.js";
// retrieve stored name
$("#name").text(localStorage["name"]);
function formatTimestamp(timestamp) {
var today = new Date();
var dt = new Date(timestamp);
if (today.getDay() == dt.getDay() && today.getMonth() == dt.getMonth()
&& today.getFullYear() == dt.getFullYear()) {
var hours = dt.getHours();
var minutes = dt.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
return hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + " "
+ ampm;
}
return dt.getMonth() + "/" + dt.getDay() + "/" + dt.getFullYear();
}
// Event Listeners
// call history
$("#calllogentries").disableSelection();
$("#calllogentries").on("dblclick", "tr", function(e) {
e.preventDefault();
var number = $(this).attr("id").replace(/calllogentry.*_/g, "");
XSIACTIONS.API.call(number);
});
// dialer
$("#destination")
.autocomplete(
{
minLength : 2,
source : function(request, responseCallback) {
var suggestions = [];
var contacts = XSIACTIONS.API
.searchEnterpriseDirectory(request.term);
$(contacts).find("directoryDetails").each(
function() {
var name = $(this).find("firstName").text()
+ " "
+ $(this).find("lastName").text();
var number = $(this).find("number").text();
var mobile = $(this).find("mobile").text();
if (number != "") {
suggestions.push({
value : number,
label : name + " (work: " + number
+ ")"
});
}
if (mobile != "") {
suggestions.push({
value : mobile,
label : name + " (mobile: "
+ mobile + ")"
});
}
});
var url = "https://www.google.com/m8/feeds/contacts/default/full?v=3.0&max-results=50&q="
+ request.term;
authenticatedXhr(
'GET',
url,
function(error, status, response) {
$(response)
.find("entry")
.each(
function() {
var title = $(this)
.find("title")
.text();
if (title == "") {
title = "Unknown";
}
$(this)
.find(
"gd\\:phoneNumber")
.each(
function() {
var number = $(
this)
.text();
var type = $(
this)
.attr(
"rel")
.replace(
"http://schemas.google.com/g/2005#",
"");
suggestions
.push({
value : number,
label : title
+ " ("
+ type
+ ": "
+ number
+ ")"
});
});
});
console.log(suggestions);
responseCallback(suggestions);
});
},
autoFocus : true
});
$("#destination")
.keyup(
function(e) {
if (e.keyCode == 13) {
try {
var number = $("#destination").val();
var re = new RegExp();
re
.compile("((([2-9][0-8][0-9])|([\(][2-9][0-8][0-9][\)]))(.|-| )?([2-9][0-9]{2})(.|-| )?([0-9]{4}))");
var tokens = number.match(re);
LOGGER.API.log(MODULE, "number:" + number
+ " tokens:" + tokens);
if (tokens) {
XSIACTIONS.API.call(tokens[0]);
}
} catch (error) {
LOGGER.API.error(MODULE, error.message);
}
$("#destination").val("");
}
});
// signout
document.querySelector('#signout').addEventListener('click', function() {
signout(true);
});
// about
document.querySelector('#about_link_tabs').addEventListener('click',
showAboutBox);
// Click to dial checkbox
document.querySelector('#clicktodialbox').addEventListener('click', function() {
var clicktodial = $("#clicktodialbox").prop("checked");
localStorage["clicktodial"] = clicktodial;
});
// Notifications checkbox
document.querySelector('#notificationsbox').addEventListener('click',
function() {
var notifications = $("#notificationsbox").prop("checked");
localStorage["notifications"] = notifications;
});
// Text to speech checkbox
document.querySelector('#texttospeechbox').addEventListener('click',
function() {
var texttospeech = $("#texttospeechbox").prop("checked");
localStorage["texttospeech"] = texttospeech;
});
// Do Not disturb button
document.querySelector('#dndbutton').addEventListener('click', function() {
var dnd = localStorage["dnd"];
if (dnd != "unassigned") {
if (dnd == "true") {
dnd = "false";
} else {
dnd = "true";
}
try {
XSIACTIONS.API.setDoNotDisturb(dnd);
} catch (error) {
if (textToSpeechEnabled() == "true") {
chrome.tts.speak('An error occured saving your DND settings.');
LOGGER.API.error(MODULE, error.message);
}
}
}
});
// CFA button
document
.querySelector('#cfabutton')
.addEventListener(
'click',
function() {
var cfa = localStorage["cfa"];
if (cfa != "unassigned") {
if (cfa == "true") {
cfa = "false";
} else {
cfa = "true";
}
try {
XSIACTIONS.API.setCallForwardAlways(cfa);
} catch (error) {
if (textToSpeechEnabled() == "true") {
chrome.tts
.speak('An error occured saving your Call Forwarding Always settings.');
LOGGER.API.error(MODULE, error.message);
}
}
}
});
// Remote Office button
document
.querySelector('#robutton')
.addEventListener(
'click',
function() {
var ro = localStorage["ro"];
if (ro != "unassigned") {
if (ro == "true") {
ro = "false";
} else {
ro = "true";
}
try {
XSIACTIONS.API.setRemoteOffice(ro);
} catch (error) {
if (textToSpeechEnabled() == "true") {
chrome.tts
.speak('An error occured saving your Remote Office settings.');
LOGGER.API.error(MODULE, error.message);
}
}
}
});
// Signout. A manual signout means the user signed out themselves. In this case,
// clear out all info. If a force logout
// due to an authentication error or some other error, then retain some
// information.
function signout(manual) {
localStorage["password"] = "";
localStorage["name"] = "";
localStorage["cfa"] = "";
localStorage["ro"] = "";
localStorage["dnd"] = "";
localStorage["currentTab"] = "";
localStorage["connectionStatus"] = "signedOut";
if (manual) {
localStorage["url"] = "";
localStorage["username"] = "";
localStorage["clicktodial"] = "";
localStorage["notifications"] = "";
localStorage["texttospeech"] = "";
localStorage["errorMessage"] = "";
}
top.location.assign("options.html");
}
var services = {
dnd : {
buttonId : "#dndbutton",
activeImage : "images/dnd_active.png",
normalImage : "images/dnd_normal.png",
disabledImage : "images/dnd_disabled.png",
textToSpeech : "Do not disturb is "
},
ro : {
buttonId : "#robutton",
activeImage : "images/remoteoffice_active.png",
normalImage : "images/remoteoffice_normal.png",
disabledImage : "images/remoteoffice_disabled.png",
textToSpeech : "Remote office is "
},
cfa : {
buttonId : "#cfabutton",
activeImage : "images/callforward_active.png",
normalImage : "images/callforward_normal.png",
disabledImage : "images/callforward_disabled.png",
textToSpeech : "Call forward always is "
}
};
function setButtonState(service, value) {
if (value == "true") {
$(services[service].buttonId).removeAttr("disabled");
$(services[service].buttonId)
.attr("src", services[service].activeImage);
$(services[service].buttonId).css("background-color", "#F7A300");
} else if (value == "false") {
$(services[service].buttonId).removeAttr("disabled");
$(services[service].buttonId)
.attr("src", services[service].normalImage);
$(services[service].buttonId).css("background-color", "transparent");
} else {
$(services[service].buttonId).attr("disabled", "disabled");
$(services[service].buttonId).attr("src",
services[service].disabledImage);
$(services[service].buttonId).css("background-color", "transparent");
}
}
function announceServiceState(service, value) {
if (textToSpeechEnabled() == "true") {
if (value == "true") {
chrome.tts.speak(services[service].textToSpeech + "on");
} else if (value == "false") {
chrome.tts.speak(services[service].textToSpeech + "off");
} else {
chrome.tts.speak(services[service].textToSpeech + "unknown");
}
}
}
function restoreTabs() {
// restore Xsi-Actions
var xsiactions_options = {
host : localStorage["url"],
username : localStorage["username"],
password : localStorage["password"],
};
XSIACTIONS.API.init(xsiactions_options);
try {
var name = XSIACTIONS.API.getName();
localStorage["name"] = name;
} catch (error) {
LOGGER.API.error(MODULE, error.message);
if (error.message.indexOf("401") != -1
|| error.message.indexOf("403") != -1) {
localStorage["errorMessage"] = "An authentication error occurred. Please login again.";
} else {
localStorage["errorMessage"] = "An error occurred. Please login again.";
}
signout(false);
return;
}
// create tabs
$("#tabs").tabs();
// add activate(select) event handler for tabs
$("#tabs")
.tabs(
{
activate : function(event, ui) {
if (ui.newPanel.attr("id") == "history") {
localStorage["currentTab"] = "history";
var callLogs = XSIACTIONS.API.getCallLogs();
var list = [];
$(callLogs)
.find("callLogsEntry")
.each(
function() {
var name = $(this).find(
"name").text();
var number = $(this).find(
"phoneNumber")
.text();
var time = $(this).find(
"time").text();
var type = $(this).parent()[0].nodeName;
list.push({
name : name,
number : number,
time : time,
type : type
});
});
list.sort(function(a, b) {
a = new Date(a.time);
b = new Date(b.time);
return b.getTime() - a.getTime();
});
$("#calllogentries").empty();
for ( var i = 0; i < list.length; i++) {
var row = "<tr id='calllogentry" + i + "_"
+ list[i].number + "'>";
row = row + "<td><p>" + list[i].name
+ "</p>" + list[i].number + "</td>";
row = row + "<td>"
+ formatTimestamp(list[i].time)
+ "</td>";
if (list[i].type == "placed") {
row = row
+ "<td><img src='images/history_outgoing_normal.png'/></td>";
} else if (list[i].type == "received") {
row = row
+ "<td><img src='images/history_incoming_normal.png'/></td>";
} else {
row = row
+ "<td><img src='images/history_missed_normal.png'/></td>";
}
$("#calllogentries").append(row);
}
} else if (ui.newPanel.attr("id") == "dialer") {
localStorage["currentTab"] = "dialer";
$('#destination').focus();
} else if (ui.newPanel.attr("id") == "preferences") {
localStorage["currentTab"] = "preferences";
}
}
});
// restore current tab
var currentTab = localStorage["currentTab"];
if (currentTab == "dialer") {
$("#tabs").tabs("option", "active", 0);
$('#destination').focus();
} else if (currentTab == "history") {
$("#tabs").tabs("option", "active", 1);
} else if (currentTab == "preferences") {
$("#tabs").tabs("option", "active", 2);
} else {
$("#tabs").tabs("option", "active", 0);
$('#destination').focus();
}
// restore service button states
setButtonState("dnd", localStorage["dnd"]);
setButtonState("ro", localStorage["ro"]);
setButtonState("cfa", localStorage["cfa"]);
var clicktodial = localStorage["clicktodial"];
if (clicktodial == "true") {
$("#clicktodialbox").prop('checked', true);
} else {
$("#clicktodialbox").prop('checked', false);
}
var notifications = localStorage["notifications"];
if (notifications == "true") {
$("#notificationsbox").prop('checked', true);
} else {
$("#notificationsbox").prop('checked', false);
}
var texttospeech = localStorage["texttospeech"];
if (texttospeech == "true") {
$("#texttospeechbox").prop('checked', true);
} else {
$("#texttospeechbox").prop('checked', false);
}
updateCalls();
$(window).bind(
"storage",
function(e) {
LOGGER.API.log(MODULE, "Event received with key: "
+ e.originalEvent.key);
if (e.originalEvent.key == "dnd" || e.originalEvent.key == "ro"
|| e.originalEvent.key == "cfa") {
setButtonState(e.originalEvent.key,
localStorage[e.originalEvent.key]);
announceServiceState(e.originalEvent.key,
e.originalEvent.newValue);
} else if (e.originalEvent.key == "calls") {
updateCalls();
} else if (e.originalEvent.key == "errorMessage") {
signout(false);
}
});
}
var callTimerId;
function updateCalls() {
$("#calls").empty();
window.clearInterval(callTimerId);
var calls = JSON.parse(localStorage["calls"]);
LOGGER.API.log(MODULE, calls);
for ( var callId in calls) {
var call = calls[callId];
var nameAndNumber = "<div class='nameAndNumber'>";
if (call.name == "") {
nameAndNumber += call.number;
} else {
nameAndNumber += call.name + " (" + call.number + ")";
}
nameAndNumber += "</div>";
var html = "<tr>";
switch (call.state) {
case "Alerting":
var callType = "Incoming Call";
if (call.personality != "Terminator") {
callType = "Outgoing Call";
}
html = html + "<td>" + nameAndNumber + "<div class='callDuration'>"
+ callType + "</div></td>";
html = html + "<td>";
if (call.personality == "Terminator") {
html = html + "<img id='answer_" + callId
+ "' src='images/answer.png' />";
}
html = html + "</td>";
html = html + "<td><img id='end_" + callId
+ "' src='images/decline.png' /></td>";
break;
case "Active":
html = html + "<td>" + nameAndNumber + "<div class='callDuration'>"
+ getCallDuration(call.answerTime) + "</div></td>";
html = html + "<td><img id='end_" + callId
+ "' src='images/hangup.png' /></td>";
html = html + "<td><img id='hold_" + callId
+ "' src='images/hold.png' /></td>";
break;
case "Held":
html = html + "<td>" + nameAndNumber + "<div class='callDuration'>"
+ getCallDuration(call.answerTime) + "</div></td>";
html = html + "<td><img id='end_" + callId
+ "' src='images/hangup.png' /></td>";
html = html + "<td><img id='answer_" + callId
+ "' src='images/hold_active.png' /></td>";
break;
}
html = html + "</tr>";
$("#calls").append(html);
}
$("#calls tbody tr td img").on("click", function(event) {
var tokens = $(this).attr("id").split("_");
callControl(tokens[0], tokens[1]);
});
callTimerId = window.setInterval(updateCallDurations, 1000);
}
function getCallDuration(answerTime) {
var delta = parseInt((new Date().getTime() - answerTime) / 1000);
var hh = parseInt(delta / 3600);
var mm = parseInt((delta - (hh * 3600)) / 60);
var ss = parseInt(delta - ((hh * 3600) + (mm * 60)));
var ret = hhmmssToString(hh, mm, ss);
return ret;
}
function hhmmssToString(hh, mm, ss) {
var ret = ss;
if (ss < 10) {
ret = "0" + ss;
}
if (mm < 10) {
ret = "0" + mm + ":" + ret;
} else {
ret = mm + ":" + ret;
}
if (hh > 0) {
ret = hh + ":" + ret;
}
return ret;
}
function updateCallDurations() {
$(".callDuration").each(function() {
var duration = $(this).text();
if (duration != "Incoming Call" && duration != "Outgoing Call") {
duration = incrementCallDuration(duration);
$(this).text(duration);
}
});
}
function textToSpeechEnabled() {
var texttospeech = localStorage["texttospeech"];
return texttospeech;
}
function showAboutBox() {
top.location.assign("about.html");
}
function callControl(action, callId) {
LOGGER.API.log(MODULE, action + " " + callId);
try {
switch (action) {
case "answer":
XSIACTIONS.API.talk(callId);
break;
case "hold":
XSIACTIONS.API.hold(callId);
break;
case "end":
XSIACTIONS.API.hangup(callId);
break;
default:
action = "unknown";
}
} catch (error) {
var calls = JSON.parse(localStorage["calls"]);
delete calls[callId];
localStorage["calls"] = JSON.stringify(calls);
if (textToSpeechEnabled() == "true") {
chrome.tts
.speak('Failed to perform ' + action + ' action on call.');
LOGGER.API.error(MODULE, error.message);
}
}
}
function incrementCallDuration(duration) {
var tokens = duration.split(":");
var hh = 0;
var mm = 0;
var ss = 0;
if (tokens.length == 3) {
hh = parseInt(tokens[0]);
mm = parseInt(tokens[1]);
ss = parseInt(tokens[2]);
} else {
mm = parseInt(tokens[0]);
ss = parseInt(tokens[1]);
}
ss++;
if (ss > 59) {
ss = 0;
mm++;
}
if (mm > 59) {
mm = 0;
hh++;
}
return hhmmssToString(hh, mm, ss);
}
document.addEventListener('DOMContentLoaded', restoreTabs);