Built xcally-motion-dialpad from commit 141221b.|1.0.96
[dialpad.git] / js / controllers / desk.controller.js
1 'use strict';
2
3 angular
4     .module('motion')
5     .controller('deskController', deskController);
6
7 function deskController($scope, $window, $interval, $location, angularLoad, $http) {
8
9     var vm = this;
10
11     // Integration
12     var version = null; // global
13     var uri = null;
14
15     var cti = null;
16     var agentCallbacks = {};
17     var callsCallbacks = {};
18
19     // Socket object
20     var socket = null;
21
22     // Data
23     vm.timer = '';
24     vm.phone = ''; // used inside DOM
25     vm.inCall = false; // used inside DOM
26
27     // task
28     var _task = {}; // global 
29     var _xml = {};
30
31     // Methods
32     vm.compose = compose;
33     vm.remove = remove;
34     vm.hangup = hangup;
35     vm.dial = dial;
36
37     var dialCount = 0;
38
39     //********************************************* */
40
41     var scriptTag = angular.element(document.createElement('base'));
42     scriptTag.href = window.location.pathname;
43
44     //*****************************************//
45     // LOADING
46     //********************************************* */
47
48     angularLoad
49     .loadScript(window.location.pathname.substring(0,window.location.pathname.lastIndexOf('/'))  +  'js/salesforce/interaction-desk.js') //
50         .then(function () {
51             vm.host = $location.search().host;
52             desk.ready(function (data) {
53                 desk.interaction.cti.enableClickToDial();
54                 desk.interaction.cti.onClickToDial(onClickToDialListener);
55             });
56         }).catch(function (err) {
57             console.error(err);
58         });
59
60     function onClickToDialListener(data) {
61
62         if (dialCount < 1) {
63
64             if (vm.online && data.number) {
65                 vm.phone = data.number;
66
67                 vm.dial();
68                 dialCount++;
69             }
70         }
71     }
72
73     //*****************************************//
74     // display
75     //*****************************************//
76
77     function display(task) {
78         console.log('Display', task);
79         desk.interaction.screenPop(task.recordId.toString(), 'object=case');
80     }
81
82     //****************************************************** */
83     // compose
84     //****************************************************** */
85     function compose(value) {
86         vm.phone = vm.phone.concat(value);
87     }
88
89     //****************************************************** */
90     // remove
91     //****************************************************** */
92     function remove() {
93         vm.phone = vm.phone.substring(0, vm.phone.length - 1);
94     }
95
96     //****************************************************** */
97     // postExtMessage
98     //****************************************************** */
99     function postExtMessage(url, uniqueid) {
100         $window.parent.postMessage({
101             id: null,
102             tag: 'iframe',
103             className: null,
104             uniqueid: uniqueid,
105             options: {
106                 url: url
107             }
108         }, "*");
109     }
110
111     //****************************************************** */
112     //  KEEPALIVE
113     //****************************************************** */
114     function keepalive() {
115
116         var uniqueid = Date.now();
117
118         agentCallbacks[uniqueid] = function (data) {
119             if (data.response && data.response.status >= 200 && data.response.status < 300) {
120                 var message = JSON.parse(data.response.message);
121
122                 vm.id = message.id;
123                 vm.name = message.name;
124                 vm.fullname = message.fullname;
125                 vm.internal = String(message.internal); //  message.accountcode; 
126
127                 vm.online = true;
128
129                 //****************************************************** */
130                 if (!socket) {
131                     if (vm.host && vm.id) {
132                         console.log('host', vm.host);
133                         console.log('id', vm.id);
134
135                         socket = io(vm.host, {
136                             query: {
137                                 id: vm.id
138                             },
139                             autoConnect: false,
140                             transports: ['websocket', 'polling']
141                         });
142
143                         // NOTE: RIVEDERE CON ANDREA
144                         socket.on('trigger:desk:display', display);
145                     }
146                 }
147                 //****************************************************** */
148
149                 if (socket && socket.disconnected) {
150                     socket.connect();
151                 }
152             } else {
153                 vm.online = false;
154             }
155         };
156
157         postExtMessage('http://localhost:9888/api/agent', uniqueid);
158
159         // NOTA: Perchè viene inviato un'iframe message e non viene fatta direttamente la HTTP Request?
160
161         // $http.get('http://localhost:9888/api/agent')
162         //   .then(function (res) {
163         //     console.log(res);
164         //   }, function (err) {
165         //     console.log(err);
166         //   });
167     }
168
169     //****************************************************** */
170     //  getCalls (checking status)
171     //****************************************************** */
172
173     function getCalls() {
174         var uniqueid = Date.now();
175
176         callsCallbacks[uniqueid] = function (data) {
177
178             if (data.response && data.response.status >= 200 && data.response.status < 300) {
179                 var message = JSON.parse(data.response.message);
180
181                 if (_.isArray(message) && _.find(message, {
182                         stateId: 8
183                     })) {
184                     //******************************** */
185                     // activeDuration exists
186
187                     vm.timer = parseTime(message[0].callingNumber, message[0].activeDuration);
188
189                     //******************************** */
190                     vm.inCall = true;
191
192                 } else {
193                     // activeDuration undefined
194                     vm.inCall = false;
195                     vm.timer = '';
196                 }
197             } else {
198                 vm.inCall = false;
199                 vm.timer = '';
200             }
201         };
202
203         postExtMessage('http://localhost:9888/api/calls', uniqueid);
204     }
205
206     function parseTime(cl_n, aD) {
207         var parsedTime = cl_n + ' ACTIVE  (';
208         parsedTime += aD.hours < 10 ? '0' : '';
209         parsedTime += aD.hours;
210         parsedTime += ':';
211         parsedTime += aD.minutes < 10 ? '0' : '';
212         parsedTime += aD.minutes;
213         parsedTime += ':';
214         parsedTime += aD.seconds < 10 ? '0' : '';
215         parsedTime += aD.seconds;
216         parsedTime += ')';
217
218         return parsedTime;
219     }
220
221     //******************************** */
222     //  
223
224     //******************************** */
225     function dial() {
226
227         var uniqueid = Date.now();
228
229         //postExtMessage('http://localhost:9888/api/originate/' + _task.endUserPhone, uniqueid);
230         postExtMessage('http://localhost:9888/api/originate/' + vm.phone, uniqueid);
231         postExtMessage('http://localhost:9888/api/answer', uniqueid);
232
233     }
234
235
236     //***************************************** */
237     // HANGUP
238     //***************************************** */
239
240     function hangup() {
241
242         vm.phone = '';
243         dialCount = 0;
244         
245         var uniqueid = Date.now();
246         postExtMessage('http://localhost:9888/api/hangup', uniqueid);
247
248         // hangup comunicato alla phonebar
249         // phonebar comunica a asterisk* che invia evt relativo all'hangup
250         // intercettiamo hangup via socket - verifichiamo il numero della chiamata
251     }
252
253     //***************************************** */
254     //***************************************** */
255
256     /**
257      * receiveMessage
258      */
259     function receiveMessage(message) {
260         var data = message.data;
261
262         if (agentCallbacks[data.uniqueid]) {
263             agentCallbacks[data.uniqueid](data);
264             delete agentCallbacks[data.uniqueid];
265         } else if (callsCallbacks[data.uniqueid]) {
266             callsCallbacks[data.uniqueid](data);
267             delete callsCallbacks[data.uniqueid];
268         }
269
270         $scope.$apply();
271     }
272
273     $interval(keepalive, 2000);
274     $interval(getCalls, 2000);
275
276     $window.addEventListener('message', receiveMessage, false);
277
278 }