Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- var callcenter = new CallCenter();
- callcenter.StartUp();
- });
- var CallCenter = function () {
- var self = this;
- var calldetails = null;
- var view = new View();
- var eventSourceLibrary = null;
- this.Guid = null;
- this.StartUp = function () {
- addSaveIpBtnClickEvent();
- addLoginBtnClickEvent();
- closeAppBtnClickEvent();
- logoutBtnClickEvent();
- setIpToInput();
- if (this.GetServerIp() != null && this.GetServerIp() != "") {
- this.RenderUserList();
- }
- };
- this.RenderUserList = function () {
- var users = comm(null, false, this.GetServerIp() + URLS.USERS + "/CALLCENTER", "html", null, null);
- var users = JSON.parse(users);
- var userListHTML = view.GetUserListHTML(users);
- $('#userList').html("");
- $('#userList').html(userListHTML);
- addPickUserClickEvent();
- };
- this.GetSelectedUserId = function () {
- var id_user = $('.selected').attr('id_user')
- id_user == undefined ? null : (id_user * 1)
- return id_user;
- };
- this.GetPassword = function () {
- var password = $('#txt-password').val();
- return password;
- };
- this.Login = function () {
- var message = {
- id: this.GetSelectedUserId(),
- password: this.GetPassword()
- }
- if (message.id == null) {
- alert('Proszę wybrać pracownika');
- return;
- }
- var res = comm(message, false, this.GetServerIp() + URLS.VERIFYLOGIN + "/CALLCENTER", "html", null);
- if (checkLogin(JSON.parse(res)) == true) {
- self.ChangePage("#menu");
- eventSourceLibrary = new EventSourceLibrary(self.Guid, self.GetServerIp());
- eventSourceLibrary.StartUp();
- onMakePhoneCall();
- var startCheckingCallLog = setInterval(function () {
- getCallLog()
- }, 1000);
- }
- };
- this.SetServerIp = function (ip) {
- window.localStorage.setItem("ServerIp", ip);
- };
- this.GetServerIp = function () {
- return window.localStorage.getItem("ServerIp");
- };
- this.ChangePage = function (id) {
- $(':mobile-pagecontainer').pagecontainer('change', id, {
- transition: 'flip',
- changeHash: false,
- reverse: true,
- showLoadMsg: false,
- });
- };
- this.SendResponse = function (phone,datefrom,dateto,callguid) {
- }
- this.StopOnMakeCallEventListener = function () {
- document.removeEventListener("OnMakePhoneCall", function (e) { });
- }
- //private-----------------------------------------------------------------------------------------------
- var checkLogin = function (res) {
- if (res.Content == 'loggedIn') {
- alert('Zalogowany');
- self.Guid = res.Guid;
- return true;
- //redirectPost(UrlConst.SERVICEBROKER, { guid: res.Guid });
- }
- else if (res.Content == 'badPassword') {
- alert('Złe hasło');
- return false;
- }
- };
- var setIpToInput = function () {
- $('#server-ip').val(self.GetServerIp());
- };
- var makeCall = function (phone) {
- phonedialer.dial(phone,
- function (err) {
- if (err == "empty") alert("Nieznany numer telefonu !");
- else alert("Błąd:" + err);
- },
- function (success) {
- }
- );
- }
- var getCallLog = function () {
- window.plugins.calllog.list(1, function (response) {
- if (calldetails != null && response.rows.length >0) {
- if (response.rows[0].number == calldetails.message.telefon_kom) {
- var datefrom = moment(response.rows[0].date).format("YYYY-MM-DD HH:mm:ss");
- var dateto = moment(response.rows[0].date + response.rows[0].duration).format("YYYY-MM-DD HH:mm:ss");
- self.SendResponse(calldetails.message.telefon_kom, datefrom, dateto, calldetails.message.callguid);
- $('#lastcalllist').append(view.GetCallLogRowHTML(calldetails.message.nazwa, datefrom, calldetails.message.telefon_kom, response.rows[0].duration));
- calldetails = null;
- //clearInterval(startCheckingCallLog);
- }
- }
- }, function (error) {
- alert(JSON.stringify(error));
- });
- };
- //events------------------------------------------------------------------------------------------------
- var addSaveIpBtnClickEvent = function () {
- $('#btn-save-ip').click(function () {
- self.SetServerIp($('#server-ip').val());
- self.RenderUserList();
- });
- };
- var addPickUserClickEvent = function () {
- $('.user-row').click(function () {
- $('.user-row').removeClass('selected');
- $(this).addClass('selected');
- });
- };
- var addLoginBtnClickEvent = function () {
- $('#btn-login').click(function () {
- self.Login();
- });
- };
- var onMakePhoneCall = function () {
- document.addEventListener("OnMakePhoneCall", function (e) {
- calldetails = e.detail();
- makeCall(calldetails.message.telefon_kom);
- });
- };
- var closeAppBtnClickEvent = function () {
- $('#closecallcenter').click(function () {
- navigator.app.exitApp();
- });
- };
- var logoutBtnClickEvent = function () {
- $('#logoutbtn').click(function () {
- self.StopOnMakeCallEventListener();
- eventSourceLibrary.Stop();
- });
- };
- };
- var View = function () {
- this.GetUserListHTML = function (users) {
- var html = "";
- if(users !=null){
- for (var i = 0; i < users.length; i++) {
- html+="<div class ='user-row' id_user ="+users[i].id_pracownika+"> <img src ='"+users[i].avatar+"'><span>"+users[i].username+"</span></div>";
- };
- }
- return html;
- }
- this.GetCallLogRowHTML = function (nazwa, datefrom, phone, duration) {
- var html = "";
- if (duration == 0) {
- html = '<div class="alert alert-danger"><strong>' + datefrom + ' ' + nazwa + ' </strong>tel: ' + phone + ' czas: ' + duration + '</div>';
- }
- else {
- html = '<div class="alert alert-success"><strong>' + datefrom + ' ' + nazwa + ' </strong>tel: ' + phone + ' czas: ' + duration + '</div>';
- }
- return html;
- }
- };
- var ErrorStatus = function (data) {
- switch (data.status) {
- case 404:
- return "Zły adres serwera, proszę wpisać poprawny numer ip";
- case 500:
- return "Błąd wewnetrzny serwera"
- }
- }
- function comm(message, modeasync, url, dataType, content, callback) {
- var toJsonString = JSON.stringify(message);
- var res = null;
- $.ajax
- ({
- url: url,
- type: 'post',
- dataType: dataType,
- data: { "json": toJsonString, "content": content },
- async: modeasync,
- success: function (data) {
- res = $.trim(data);
- if (callback != null && typeof (callback) === typeof (Function))
- callback(JSON.parse(res));
- },
- error: function (data) {
- alert(ErrorStatus(data));
- }
- });
- return res;
- }
- var URLS = {
- MOBILE: "/CALLCENTER/index.php/Mobile",
- LOGIN: "/CALLCENTER/index.php/Mobile/Login",
- VERIFYLOGIN: "/CALLCENTER/index.php/Login/VerifyLogin",
- USERS: "/CALLCENTER/index.php/Login/Getusers",
- EVENTSOURCE: "/CALLCENTER/index.php/EventSource/MobileService/"
- }
- var EventSourceLibrary = function (_guid, _ip) {
- var jsonObj = null;
- var ip = _ip;
- var guid = _guid;
- var eventSource = null;
- var getJsonObj = function () {
- return jsonObj;
- };
- var onMakePhoneCall = new CustomEvent("OnMakePhoneCall", { "detail": getJsonObj });
- this.StartUp = function () {
- eventSource = new EventSource(ip + URLS.EVENTSOURCE + guid);
- eventSource.onmessage = function (event) {
- jsonObj = JSON.parse(event.data);
- if (jsonObj.OnMakePhoneCall == true)
- document.dispatchEvent(onMakePhoneCall);
- };
- };
- this.Stop = function () {
- eventSource.close();
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement