--- /dev/null
+'use strict';
+
+angular
+ .module('motion')
+ .controller('deskController', deskController);
+
+function deskController($scope, $window, $interval, $location, angularLoad, $http) {
+
+ var vm = this;
+
+ // Integration
+ var version = null; // global
+ var uri = null;
+
+ var cti = null;
+ var agentCallbacks = {};
+ var callsCallbacks = {};
+
+ // Socket object
+ var socket = null;
+
+ // Data
+ vm.timer = '';
+ vm.phone = ''; // used inside DOM
+ vm.inCall = false; // used inside DOM
+
+ // task
+ var _task = {}; // global
+ var _xml = {};
+
+ // Methods
+ vm.compose = compose;
+ vm.remove = remove;
+ vm.hangup = hangup;
+ vm.dial = dial;
+
+ var dialCount = 0;
+
+ //********************************************* */
+
+ var scriptTag = angular.element(document.createElement('base'));
+ scriptTag.href = window.location.pathname;
+
+ //*****************************************//
+ // LOADING
+ //********************************************* */
+
+ angularLoad
+ .loadScript(window.location.pathname.substring(0,window.location.pathname.lastIndexOf('/')) + 'js/salesforce/interaction-desk.js') //
+ .then(function () {
+ vm.host = $location.search().host;
+ desk.ready(function (data) {
+ desk.interaction.cti.enableClickToDial();
+ desk.interaction.cti.onClickToDial(onClickToDialListener);
+ });
+ }).catch(function (err) {
+ console.error(err);
+ });
+
+ function onClickToDialListener(data) {
+
+ if (dialCount < 1) {
+
+ if (vm.online && data.number) {
+ vm.phone = data.number;
+
+ vm.dial();
+ dialCount++;
+ }
+ }
+ }
+
+ //*****************************************//
+ // display
+ //*****************************************//
+
+ function display(task) {
+ console.log('Display', task);
+ desk.interaction.screenPop(task.recordId.toString(), 'object=case');
+ }
+
+ //****************************************************** */
+ // compose
+ //****************************************************** */
+ function compose(value) {
+ vm.phone = vm.phone.concat(value);
+ }
+
+ //****************************************************** */
+ // remove
+ //****************************************************** */
+ function remove() {
+ vm.phone = vm.phone.substring(0, vm.phone.length - 1);
+ }
+
+ //****************************************************** */
+ // postExtMessage
+ //****************************************************** */
+ function postExtMessage(url, uniqueid) {
+ $window.parent.postMessage({
+ id: null,
+ tag: 'iframe',
+ className: null,
+ uniqueid: uniqueid,
+ options: {
+ url: url
+ }
+ }, "*");
+ }
+
+ //****************************************************** */
+ // KEEPALIVE
+ //****************************************************** */
+ function keepalive() {
+
+ var uniqueid = Date.now();
+
+ agentCallbacks[uniqueid] = function (data) {
+ if (data.response && data.response.status >= 200 && data.response.status < 300) {
+ var message = JSON.parse(data.response.message);
+
+ vm.id = message.id;
+ vm.name = message.name;
+ vm.fullname = message.fullname;
+ vm.internal = String(message.internal); // message.accountcode;
+
+ vm.online = true;
+
+ //****************************************************** */
+ if (!socket) {
+ if (vm.host && vm.id) {
+ console.log('host', vm.host);
+ console.log('id', vm.id);
+
+ socket = io(vm.host, {
+ query: {
+ id: vm.id
+ },
+ autoConnect: false,
+ transports: ['websocket', 'polling']
+ });
+
+ // NOTE: RIVEDERE CON ANDREA
+ socket.on('trigger:desk:display', display);
+ }
+ }
+ //****************************************************** */
+
+ if (socket && socket.disconnected) {
+ socket.connect();
+ }
+ } else {
+ vm.online = false;
+ }
+ };
+
+ postExtMessage('http://localhost:9888/api/agent', uniqueid);
+
+ // NOTA: Perchè viene inviato un'iframe message e non viene fatta direttamente la HTTP Request?
+
+ // $http.get('http://localhost:9888/api/agent')
+ // .then(function (res) {
+ // console.log(res);
+ // }, function (err) {
+ // console.log(err);
+ // });
+ }
+
+ //****************************************************** */
+ // getCalls (checking status)
+ //****************************************************** */
+
+ function getCalls() {
+ var uniqueid = Date.now();
+
+ callsCallbacks[uniqueid] = function (data) {
+
+ if (data.response && data.response.status >= 200 && data.response.status < 300) {
+ var message = JSON.parse(data.response.message);
+
+ if (_.isArray(message) && _.find(message, {
+ stateId: 8
+ })) {
+ //******************************** */
+ // activeDuration exists
+
+ vm.timer = parseTime(message[0].callingNumber, message[0].activeDuration);
+
+ //******************************** */
+ vm.inCall = true;
+
+ } else {
+ // activeDuration undefined
+ vm.inCall = false;
+ vm.timer = '';
+ }
+ } else {
+ vm.inCall = false;
+ vm.timer = '';
+ }
+ };
+
+ postExtMessage('http://localhost:9888/api/calls', uniqueid);
+ }
+
+ function parseTime(cl_n, aD) {
+ var parsedTime = cl_n + ' ACTIVE (';
+ parsedTime += aD.hours < 10 ? '0' : '';
+ parsedTime += aD.hours;
+ parsedTime += ':';
+ parsedTime += aD.minutes < 10 ? '0' : '';
+ parsedTime += aD.minutes;
+ parsedTime += ':';
+ parsedTime += aD.seconds < 10 ? '0' : '';
+ parsedTime += aD.seconds;
+ parsedTime += ')';
+
+ return parsedTime;
+ }
+
+ //******************************** */
+ //
+
+ //******************************** */
+ function dial() {
+
+ var uniqueid = Date.now();
+
+ //postExtMessage('http://localhost:9888/api/originate/' + _task.endUserPhone, uniqueid);
+ postExtMessage('http://localhost:9888/api/originate/' + vm.phone, uniqueid);
+ postExtMessage('http://localhost:9888/api/answer', uniqueid);
+
+ }
+
+
+ //***************************************** */
+ // HANGUP
+ //***************************************** */
+
+ function hangup() {
+
+ vm.phone = '';
+ dialCount = 0;
+
+ var uniqueid = Date.now();
+ postExtMessage('http://localhost:9888/api/hangup', uniqueid);
+
+ // hangup comunicato alla phonebar
+ // phonebar comunica a asterisk* che invia evt relativo all'hangup
+ // intercettiamo hangup via socket - verifichiamo il numero della chiamata
+ }
+
+ //***************************************** */
+ //***************************************** */
+
+ /**
+ * receiveMessage
+ */
+ function receiveMessage(message) {
+ var data = message.data;
+
+ if (agentCallbacks[data.uniqueid]) {
+ agentCallbacks[data.uniqueid](data);
+ delete agentCallbacks[data.uniqueid];
+ } else if (callsCallbacks[data.uniqueid]) {
+ callsCallbacks[data.uniqueid](data);
+ delete callsCallbacks[data.uniqueid];
+ }
+
+ $scope.$apply();
+ }
+
+ $interval(keepalive, 2000);
+ $interval(getCalls, 2000);
+
+ $window.addEventListener('message', receiveMessage, false);
+
+}
\ No newline at end of file