Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
279 lines (250 loc) · 7.85 KB
/
Copy pathapp.js
File metadata and controls
279 lines (250 loc) · 7.85 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
define(function (require) {
var $ = require("jquery"),
_ = require("lodash"),
monster = require("monster");
var app = {
name: "parkinglot",
css: ["app"],
i18n: {
"en-US": { customCss: false },
},
// Defines API requests not included in the SDK
requests: {},
// Define the events available for other apps
subscribe: {},
// Method used by the Monster-UI Framework, shouldn't be touched unless you're doing some advanced kind of stuff!
load: function (callback) {
var self = this;
self.initApp(function () {
callback && callback(self);
});
},
// Method used by the Monster-UI Framework, shouldn't be touched unless you're doing some advanced kind of stuff!
initApp: function (callback) {
var self = this;
// Used to init the auth token and account id of this app
monster.pub("auth.initApp", {
app: self,
callback: callback,
});
},
//////////////////////////////////////////////////////////
// Entry Point of the app
//////////////////////////////////////////////////////////
render: function (container) {
var self = this,
$container = _.isEmpty(container)
? $("#monster_content")
: container,
$layout = $(
self.getTemplate({
name: "layout",
})
);
self.bindEvents({
template: $layout,
});
$container.empty().append($layout); //draw the main part of the page in views/layout.html
},
bindEvents: function (args) {
var self = this,
$template = args.template;
//load the parked calls on document ready, and repeat every 30 seconds
$(document).ready(function (e) {
loadParkingLot();
setInterval(loadParkingLot, 30000);
});
//Refresh parked calls button binding event:
$template.find("#refresh").on("click", function (e) {
loadParkingLot();
});
//Help Button/Dialog
$template.find("#help-button").on("click", function (e) {
var helptemplate = $(app.getTemplate({
name: 'dialog-help'
}));
monster.ui.dialog(helptemplate, {
title: app.i18n.active().parkinglot.help.title,
width: '600px',
onClose: function() {
//doStuff();
}
});
});
//function to load the parked calls onto the page and set dependent bindings
function loadParkingLot() {
self.getParkedCalls(function (listOfParkedCalls) {
var $results = $(
self.getTemplate({
name: "results",
data: {
parkedCalls: listOfParkedCalls.slots,
},
})
);
$template.find(".results").empty().append($results);
///Pickup a parked call binding
$template.find(".pickup").on("click", function (e) {
//var parkNum = parkedUri.substr(0, parkedUri.indexOf('@')); //strip off the @domain.tld suffix
var parkedUri = $(this)
.closest(".parked-call")
.attr("id"); //get the id, which is the parked call URI
var parkNum = parkedUri.split("@")[0]; //strip off the @domain.tld if it exists (but doesn't fail if not)
var parkSlot = parkNum.substring(2); //strip off the first 2 chars in case it's *4 instead of *3.
self.pickupCall("*3" + parkSlot);
//console.log(parkNum);
});
///////////////////////////////
///Call the parker (the device who parked the call):
$template.find(".call-parker").on("click", function (e) {
var ringbackId = $(this).attr("id"); //get the id, which is the parker device ID
self.callTheParker(ringbackId);
});
///////////////////////////////
$template.find(".parked-call").on("mouseover", function (e) { //load more details about parker when hover
var clickedItem = $(this);
if ($(clickedItem).find('.call-parker').html() == '') { //check if the parker info is empty
self.callApi({
// Get info on the parker device:
resource: "device.get",
data: {
accountId: self.accountId,
deviceId: $(this).find('.call-parker').attr('id')
},
success: function (deviceData) {
self.callApi({
// Get info on the user the parker device belongs to:
resource: "user.get",
data: {
accountId: self.accountId,
userId: deviceData.data.owner_id
},
success: function (userData) {
var parker_name = userData.data.caller_id.internal.name;
var parker_number = userData.data.caller_id.internal.number;
$(clickedItem).find('.call-parker').html(parker_name+' '+parker_number);
$(clickedItem).find('.call-parker_hidden').css('display', 'inline');
},
error: function (parsedError) {
monster.ui.alert("FAILED to get user info for parking slot #" + slot.parking_slot + ": " + parsedError);
},
}); //end get info on user
},
error: function (parsedError) {
monster.ui.alert("FAILED to get device info: " + slot.ringback_id + ": " + parsedError);
},
}); //end get info on parker device
} //if clickedItem parker info is empty
}); //end parked-call mouseover binding
}); //end self.getParkedCalls
} //end function loadParkingLot();
}, //bindEvents
getParkedCalls: function (callback) {
var self = this;
self.callApi({
resource: "parkedCalls.list",
data: {
accountId: self.accountId,
},
success: function (data) {
$.each(data.data.slots, function (index, slot) {
slot.parking_slot = index;
}); //end $.each
//console.log(JSON.stringify(data.data));
callback(data.data);
},
error: function (parsedError) {
//console.log(parsedError);
if (data.data.error == "401") {
//if we get a 401 when refreshing parked calls, log out so user can re-auth.
monster.util.logoutAndReload();
}
monster.ui.alert("ERROR: Failed to get parked calls list: " + parsedError);
callback([]); //Populate results with nothing
},
}); //end get parked calls list
}, //end getParkedCalls
pickupCall: function (dialNumber) {
var self = this;
self.callApi({
resource: "user.quickcall",
data: {
accountId: self.accountId,
userId: monster.apps.auth.currentUser.id,
//userId: 'd89c34618dc8fa28fc5deead6cc64a4d',
number: dialNumber,
},
success: function (data) {
console.log(
"Success creating quickcall! : " + JSON.stringify(data)
);
//monster.ui.alert("Success creating quickcall! : "+data);
//callback(data.data);
},
error: function (parsedError) {
monster.ui.alert(
"FAILED to create call:" +
number +
" for " +
userID +
": " +
parsedError
);
//callback([]);
},
});
},
callTheParker: function (parkerDeviceId) {
var self = this;
self.callApi({
resource: "user.get", //get my extension to use for quickcall
data: {
accountId: self.accountId,
userId: monster.apps.auth.currentUser.id,
},
success: function (data) {
console.log(
"Success getting user: " +
data.data.caller_id.internal.number
);
self.callApi({
resource: "device.quickcall",
data: {
accountId: self.accountId,
deviceId: parkerDeviceId,
number: data.data.caller_id.internal.number,
},
success: function (data) {
console.log(
"Called: " +
parkerDeviceId +
" from " +
data.data.caller_id.internal.number
);
},
error: function (parsedError) {
monster.ui.alert(
"FAILED to create call to " +
data.data.caller_id.internal.number +
" for original parker device: " +
parkerDeviceId +
": " +
parsedError
);
},
});
},
error: function (parsedError) {
monster.ui.alert(
"FAILED to get your extension - userId: " +
userID +
": " +
parsedError
);
},
});
}, //callTheParker
////////////////////////////////////////////////////////
};
return app;
});