5 .controller('deskController', deskController);
7 function deskController($scope, $window, $interval, $location, angularLoad, $http) {
12 var version = null; // global
16 var agentCallbacks = {};
17 var callsCallbacks = {};
24 vm.phone = ''; // used inside DOM
25 vm.inCall = false; // used inside DOM
28 var _task = {}; // global
39 //********************************************* */
41 var scriptTag = angular.element(document.createElement('base'));
42 scriptTag.href = window.location.pathname;
44 //*****************************************//
46 //********************************************* */
49 .loadScript(window.location.pathname.substring(0,window.location.pathname.lastIndexOf('/')) + 'js/salesforce/interaction-desk.js') //
51 vm.host = $location.search().host;
52 desk.ready(function (data) {
53 desk.interaction.cti.enableClickToDial();
54 desk.interaction.cti.onClickToDial(onClickToDialListener);
56 }).catch(function (err) {
60 function onClickToDialListener(data) {
64 if (vm.online && data.number) {
65 vm.phone = data.number;
73 //*****************************************//
75 //*****************************************//
77 function display(task) {
78 console.log('Display', task);
79 desk.interaction.screenPop(task.recordId.toString(), 'object=case');
82 //****************************************************** */
84 //****************************************************** */
85 function compose(value) {
86 vm.phone = vm.phone.concat(value);
89 //****************************************************** */
91 //****************************************************** */
93 vm.phone = vm.phone.substring(0, vm.phone.length - 1);
96 //****************************************************** */
98 //****************************************************** */
99 function postExtMessage(url, uniqueid) {
100 $window.parent.postMessage({
111 //****************************************************** */
113 //****************************************************** */
114 function keepalive() {
116 var uniqueid = Date.now();
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);
123 vm.name = message.name;
124 vm.fullname = message.fullname;
125 vm.internal = String(message.internal); // message.accountcode;
129 //****************************************************** */
131 if (vm.host && vm.id) {
132 console.log('host', vm.host);
133 console.log('id', vm.id);
135 socket = io(vm.host, {
140 transports: ['websocket', 'polling']
143 // NOTE: RIVEDERE CON ANDREA
144 socket.on('trigger:desk:display', display);
147 //****************************************************** */
149 if (socket && socket.disconnected) {
157 postExtMessage('http://localhost:9888/api/agent', uniqueid);
159 // NOTA: Perchè viene inviato un'iframe message e non viene fatta direttamente la HTTP Request?
161 // $http.get('http://localhost:9888/api/agent')
162 // .then(function (res) {
164 // }, function (err) {
169 //****************************************************** */
170 // getCalls (checking status)
171 //****************************************************** */
173 function getCalls() {
174 var uniqueid = Date.now();
176 callsCallbacks[uniqueid] = function (data) {
178 if (data.response && data.response.status >= 200 && data.response.status < 300) {
179 var message = JSON.parse(data.response.message);
181 if (_.isArray(message) && _.find(message, {
184 //******************************** */
185 // activeDuration exists
187 vm.timer = parseTime(message[0].callingNumber, message[0].activeDuration);
189 //******************************** */
193 // activeDuration undefined
203 postExtMessage('http://localhost:9888/api/calls', uniqueid);
206 function parseTime(cl_n, aD) {
207 var parsedTime = cl_n + ' ACTIVE (';
208 parsedTime += aD.hours < 10 ? '0' : '';
209 parsedTime += aD.hours;
211 parsedTime += aD.minutes < 10 ? '0' : '';
212 parsedTime += aD.minutes;
214 parsedTime += aD.seconds < 10 ? '0' : '';
215 parsedTime += aD.seconds;
221 //******************************** */
224 //******************************** */
227 var uniqueid = Date.now();
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);
236 //***************************************** */
238 //***************************************** */
245 var uniqueid = Date.now();
246 postExtMessage('http://localhost:9888/api/hangup', uniqueid);
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
253 //***************************************** */
254 //***************************************** */
259 function receiveMessage(message) {
260 var data = message.data;
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];
273 $interval(keepalive, 2000);
274 $interval(getCalls, 2000);
276 $window.addEventListener('message', receiveMessage, false);