Newer
Older
ubFramework / Portal / docroot / assets / js / user / voip / seats.js
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 6 KB Cleaning Up Making It A Sub Module
/**
 * Copyright (c) 2015, 2016 Christopher W. Olsen <cwolsen@uBixTechnologies.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer. 2. Redistributions in
 * binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution. 3. All advertising materials
 * mentioning features or use of this software must display the following
 * acknowledgement: This product includes software developed by uBix
 * Technologies. 4. Neither the name of uBix Technologies nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY uBix Technologies ''AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL uBix Technologies BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * $Id: seats.js 334 2015-12-19 02:35:30Z reddawg $
 * 
 */

/* Datatables Variables */
var SDT;
var edit_seat;
var edit_seat_form;

var add_seat;
var add_seat_form;

var cur_seat = 0;

var _quickCreate;

function editSeat() {
  _AJAX(
    "/user/voip/seats_json.php",
    edit_seat_form.serialize(),
    function(data) {
      alert(data.msg);
    }
  );
}

function editCloseSeat() {
  _AJAX(
    "/user/voip/seats_json.php",
    edit_seat_form.serialize(),
    function(data) {
      if (data.code == 1) {
        edit_seat.dialog('close');
        SDT.fnClearTable();
        _start();
      }
      else {
        alert(data.msg);
      }
    }
  );
}

function addExtension() {

}

$(function() {
  // Initialize Account DataTable
  SDT = $('#seatTable').dataTable({
    "columns": [
      { "visible": false },
      { "width": "50" },
      null,
      null,
      null,
      { "width": "75" },
      { "width": "75" }
    ],
    "drawCallback": function(settings) {
/*
      if (cur_seat > 0) {
        alert('cur_seat: ' + cur_seat);
        alert("Data: " + this.api().rows( {page:'current'} ).data());
        alert('cur_seat: ' + this.api().column(0, {order:'current'}).data().length);
        this.api().page.jumpToData( 9, 0 );
        cur_seat = 0;
      }
*/
    }
  });

  // Initialize Quick Create
  _quickCreate = $("#quickCreate").dialog({
    autoOpen : false,
    height : 400,
    width: 750,
    modal : true,
    buttons : {
      close : function() {
      }
    }
  });

  // Initialize Edit Extension Modal
  edit_seat = $("#dialog-edit-seat").dialog({
    autoOpen : false,
    height : 400,
    width : 750,
    modal : true,
    buttons : {
      "Save" : editSeat,
      "Save & Close" : editCloseSeat,
      Cancel : function() {

        edit_seat.dialog("close");
      }
    },
    close : function() {

      edit_seat_form[0].reset();
    }
  });

  edit_seat_form = $("#edit-seat-form").on("submit", function(event) {

    event.preventDefault();
  });

  SDT.delegate(".edit_seat", "click", function() {
    cur_seat = this.id;

    edit_seat.dialog("open");

    _AJAX(
      "/user/voip/seats_json.php",
      "data[req]=seat_info&data[seat]=" + this.id,
      function(data) {
        $('#e_seat').val(data.seat_id);
        $('#e_seat_label').text(data.seat_id);
        $('#e_status').val(data.status);
        
        _AJAX(
          "/user/voip/seats_json.php",
          "data[req]=seat_users",
          function(uData) {
            $("#e_user option").remove();
            $.each(uData, function(index, item) {
              if (item.uid == data.uid)
                $("#e_user").append($("<option selected></option>").text(item.email).val(item.uid));
              else
                $("#e_user").append($("<option></option>").text(item.email).val(item.uid));
            });
          },
          function(data) {
          }
        );

        _AJAX(
          "/user/voip/seats_json.php",
          "data[req]=seat_devices&data[seat]=" + data.seat_id,
          function(dData) {
            $("#e_device option").remove();
            $.each(dData, function(index, item) {
              if (item.id == data.vdid)
                $("#e_device").append($("<option selected></option>").text(item.label).val(item.id));
              else
                $("#e_device").append($("<option></option>").text(item.label).val(item.id));
            });
          },
          function(data) {
          }
        );
        
    });
  });

  // Initialize Edit Extension Modal
  add_seat = $("#dialog-add-seat").dialog({
    autoOpen : false,
    height : 400,
    width : 900,
    modal : true,
    buttons : {
      "Save" : editSeat,
      Cancel : function() {

        add_seats.dialog("close");
      }
    },
    close : function() {

      add_seat_form[0].reset();
    }
  });

  add_seat_form = $("#add-seat-form").on("submit", function(event) {

    event.preventDefault();
  });

  $('#addExt').on(
      "click",
      function() {

        add_seats.dialog("open");

        _AJAX("/user/voip/seats_json.php", "data[req]=seats_existing",
            function(data) {

              $('#eExt tbody').append(data);
            }, function(data) {

              alert('There was a problem loading data');
            });
   });

  $('#addUser').on(
    "click",
    function() {
      //alert('test');
      _quickCreate.load("/user/account/qc.php?data[req]=user");
      _quickCreate.dialog("option", "title", "Add User");
      _quickCreate.dialog("open");
    }
 );

});

/* Entry Point */
function _start() {

  _AJAX("/user/voip/seats_json.php", "data[req]=seats_list", function(data) {
    SDT.fnAddData(data);
    if (cur_seat > 0) {
      //alert('cur_seat: ' + cur_seat);
      //alert("Data: " + JSON.stringify(SDT.api().column(0, {order:'current'} ).data()));
      //alert('cur_seat: ' + SDT.api().column(0, {order:'current'}).data().indexOf(cur_seat));
      SDT.api().page.jumpToData( cur_seat, 0 );
      cur_seat = 0;
      }

  }, function(data) {

    alert('There was a problem loading data');
  });
}