Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.     var callcenter = new CallCenter();
  3.     callcenter.StartUp();
  4.  
  5. });
  6.  
  7. var CallCenter = function () {
  8.     var self = this;
  9.     var calldetails = null;
  10.     var view = new View();
  11.     var eventSourceLibrary = null;
  12.     this.Guid = null;
  13.     this.StartUp = function () {
  14.         addSaveIpBtnClickEvent();
  15.         addLoginBtnClickEvent();
  16.         closeAppBtnClickEvent();
  17.         logoutBtnClickEvent();
  18.         setIpToInput();
  19.         if (this.GetServerIp() != null && this.GetServerIp() != "") {
  20.             this.RenderUserList();
  21.         }
  22.     };
  23.     this.RenderUserList = function () {
  24.         var users = comm(null, false, this.GetServerIp() + URLS.USERS + "/CALLCENTER", "html", null, null);
  25.         var users = JSON.parse(users);
  26.         var userListHTML = view.GetUserListHTML(users);
  27.         $('#userList').html("");
  28.         $('#userList').html(userListHTML);
  29.         addPickUserClickEvent();
  30.     };
  31.     this.GetSelectedUserId = function () {
  32.         var id_user = $('.selected').attr('id_user')
  33.         id_user == undefined ? null : (id_user * 1)
  34.         return id_user;
  35.     };
  36.     this.GetPassword = function () {
  37.         var password = $('#txt-password').val();
  38.         return password;
  39.     };
  40.     this.Login = function () {
  41.         var message = {
  42.             id: this.GetSelectedUserId(),
  43.             password: this.GetPassword()
  44.         }
  45.         if (message.id == null) {
  46.             alert('Proszę wybrać pracownika');
  47.             return;
  48.         }
  49.         var res = comm(message, false, this.GetServerIp() + URLS.VERIFYLOGIN + "/CALLCENTER", "html", null);
  50.         if (checkLogin(JSON.parse(res)) == true) {
  51.             self.ChangePage("#menu");
  52.             eventSourceLibrary = new EventSourceLibrary(self.Guid, self.GetServerIp());
  53.             eventSourceLibrary.StartUp();
  54.             onMakePhoneCall();
  55.             var startCheckingCallLog = setInterval(function () {
  56.                 getCallLog()
  57.             }, 1000);
  58.         }
  59.     };
  60.     this.SetServerIp = function (ip) {
  61.         window.localStorage.setItem("ServerIp", ip);
  62.     };
  63.     this.GetServerIp = function () {
  64.         return window.localStorage.getItem("ServerIp");
  65.     };
  66.     this.ChangePage = function (id) {
  67.         $(':mobile-pagecontainer').pagecontainer('change', id, {
  68.             transition: 'flip',
  69.             changeHash: false,
  70.             reverse: true,
  71.             showLoadMsg: false,
  72.         });
  73.     };
  74.     this.SendResponse = function (phone,datefrom,dateto,callguid) {
  75.  
  76.     }
  77.     this.StopOnMakeCallEventListener = function () {
  78.         document.removeEventListener("OnMakePhoneCall", function (e) { });
  79.     }
  80.     //private-----------------------------------------------------------------------------------------------
  81.     var checkLogin = function (res) {
  82.         if (res.Content == 'loggedIn') {
  83.             alert('Zalogowany');
  84.             self.Guid = res.Guid;
  85.             return true;
  86.             //redirectPost(UrlConst.SERVICEBROKER, { guid: res.Guid });
  87.         }
  88.         else if (res.Content == 'badPassword') {
  89.             alert('Złe hasło');
  90.             return false;
  91.         }
  92.     };
  93.     var setIpToInput = function () {
  94.         $('#server-ip').val(self.GetServerIp());
  95.     };
  96.     var makeCall = function (phone) {
  97.         phonedialer.dial(phone,
  98.             function (err) {
  99.                 if (err == "empty") alert("Nieznany numer telefonu !");
  100.                 else alert("Błąd:" + err);
  101.             },
  102.             function (success) {
  103.                
  104.             }
  105.         );
  106.     }
  107.  
  108.     var getCallLog = function () {
  109.         window.plugins.calllog.list(1, function (response) {
  110.             if (calldetails != null && response.rows.length >0) {
  111.                 if (response.rows[0].number == calldetails.message.telefon_kom) {
  112.                     var datefrom = moment(response.rows[0].date).format("YYYY-MM-DD HH:mm:ss");
  113.                     var dateto = moment(response.rows[0].date + response.rows[0].duration).format("YYYY-MM-DD HH:mm:ss");
  114.                     self.SendResponse(calldetails.message.telefon_kom, datefrom, dateto, calldetails.message.callguid);
  115.                     $('#lastcalllist').append(view.GetCallLogRowHTML(calldetails.message.nazwa, datefrom, calldetails.message.telefon_kom, response.rows[0].duration));
  116.                     calldetails = null;
  117.                     //clearInterval(startCheckingCallLog);
  118.                 }
  119.  
  120.  
  121.             }
  122.         }, function (error) {
  123.             alert(JSON.stringify(error));
  124.         });
  125.     };
  126.         //events------------------------------------------------------------------------------------------------
  127.     var addSaveIpBtnClickEvent = function () {
  128.             $('#btn-save-ip').click(function () {
  129.                 self.SetServerIp($('#server-ip').val());
  130.                 self.RenderUserList();
  131.  
  132.             });
  133.         };
  134.     var addPickUserClickEvent = function () {
  135.             $('.user-row').click(function () {
  136.                 $('.user-row').removeClass('selected');
  137.                 $(this).addClass('selected');
  138.             });
  139.         };
  140.     var addLoginBtnClickEvent = function () {
  141.             $('#btn-login').click(function () {
  142.                 self.Login();
  143.             });
  144.         };
  145.     var onMakePhoneCall = function () {
  146.             document.addEventListener("OnMakePhoneCall", function (e) {
  147.                 calldetails = e.detail();
  148.                 makeCall(calldetails.message.telefon_kom);
  149.              
  150.                 });
  151.         };
  152.     var closeAppBtnClickEvent = function () {
  153.         $('#closecallcenter').click(function () {
  154.                 navigator.app.exitApp();
  155.         });
  156.     };
  157.     var logoutBtnClickEvent = function () {
  158.         $('#logoutbtn').click(function () {
  159.             self.StopOnMakeCallEventListener();
  160.             eventSourceLibrary.Stop();
  161.         });
  162.         };
  163.  
  164.  
  165.    
  166. };
  167. var View = function () {
  168.     this.GetUserListHTML = function (users) {
  169.         var html = "";
  170.         if(users !=null){
  171.             for (var i = 0; i < users.length; i++) {
  172.                
  173.                 html+="<div class ='user-row' id_user ="+users[i].id_pracownika+"> <img src ='"+users[i].avatar+"'><span>"+users[i].username+"</span></div>";
  174.             };
  175.         }
  176.         return html;
  177.     }
  178.     this.GetCallLogRowHTML = function (nazwa, datefrom, phone, duration) {
  179.         var html = "";
  180.         if (duration == 0) {
  181.             html = '<div class="alert alert-danger"><strong>' + datefrom + '&nbsp; ' + nazwa + '&nbsp;</strong>tel: ' + phone + '&nbsp; czas:&nbsp;' + duration + '</div>';
  182.         }
  183.         else {
  184.             html = '<div class="alert alert-success"><strong>' + datefrom + '&nbsp; ' + nazwa + '&nbsp;</strong>tel: ' + phone + '&nbsp; czas:&nbsp;' + duration + '</div>';
  185.         }
  186.         return html;
  187.     }
  188.    
  189. };
  190. var ErrorStatus = function (data) {
  191.     switch (data.status) {
  192.         case 404:
  193.             return "Zły adres serwera, proszę wpisać poprawny numer ip";
  194.         case 500:
  195.             return "Błąd wewnetrzny serwera"
  196.     }
  197. }
  198. function comm(message, modeasync, url, dataType, content, callback) {
  199.     var toJsonString = JSON.stringify(message);
  200.     var res = null;
  201.     $.ajax
  202.     ({
  203.         url: url,
  204.         type: 'post',
  205.         dataType: dataType,
  206.         data: { "json": toJsonString, "content": content },
  207.         async: modeasync,
  208.         success: function (data) {
  209.             res = $.trim(data);
  210.             if (callback != null && typeof (callback) === typeof (Function))
  211.                 callback(JSON.parse(res));
  212.         },
  213.         error: function (data) {
  214.             alert(ErrorStatus(data));
  215.         }
  216.     });
  217.     return res;
  218. }
  219. var URLS = {
  220.     MOBILE: "/CALLCENTER/index.php/Mobile",
  221.     LOGIN: "/CALLCENTER/index.php/Mobile/Login",
  222.     VERIFYLOGIN: "/CALLCENTER/index.php/Login/VerifyLogin",
  223.     USERS: "/CALLCENTER/index.php/Login/Getusers",
  224.     EVENTSOURCE: "/CALLCENTER/index.php/EventSource/MobileService/"
  225. }
  226. var EventSourceLibrary = function (_guid, _ip) {
  227.     var jsonObj = null;
  228.     var ip = _ip;
  229.     var guid = _guid;
  230.     var eventSource = null;
  231.     var getJsonObj = function () {
  232.         return jsonObj;
  233.     };
  234.  
  235.     var onMakePhoneCall = new CustomEvent("OnMakePhoneCall", { "detail": getJsonObj });
  236.     this.StartUp = function () {
  237.         eventSource = new EventSource(ip + URLS.EVENTSOURCE + guid);
  238.         eventSource.onmessage = function (event) {
  239.  
  240.             jsonObj = JSON.parse(event.data);
  241.             if (jsonObj.OnMakePhoneCall == true)
  242.                 document.dispatchEvent(onMakePhoneCall);
  243.         };
  244.  
  245.     };
  246.     this.Stop = function () {
  247.         eventSource.close();
  248.     };
  249.  
  250.  
  251. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement