The spec for this has changed a lot and due to time constraints the code developed in a piece meal fashion. The problem that I am trying to solve is to develop a queue of chat requests with a Sharepoint list being used to store them. This is the jQuery part of a page that contains a table which reads from a Sharepoint list and updates the list when the chat has been responded to. It is working but looks a bit spaghetti and I would like to make it more robust and improve performance if possible.
/*! List of tickets
---------------------------------------------*/
(function($) {
$.fn.TicketList = function() {
var count = 0;
var pass = 0;
var responseCount = 0;
(function TicketQueue(event) {
var newCount = 0;
var newResponseCount = 0;
$().SPServices({
operation: "GetListItems",
async: true,
listName: "Queue",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Email' /><FieldRef Name='ID' /><FieldRef Name='RQName' /><FieldRef Name='RQNumber' /><FieldRef Name='RQSalaryNumber' /><FieldRef Name='TicketStatus' /><FieldRef Name='TicketTimeSubmitted' /></ViewFields>",
completefunc: function (xData, Status) {
var liHtml = "<tr><th>Name</th><th>Salary Number</th><th>Time of request</th><th>Answer request</th><th>Status</th><th>Close request</th></tr>";
$(xData.responseXML).SPFilterNode("z:row").each(function() {
if ($(this).attr("ows_TicketStatus") == "Created" || $(this).attr("ows_TicketStatus") == "Responded") {
var alertClass = "normal";
var takenClass = "notTaken";
var disabled = "";
var status = "Waiting"
var twoMinutesAgo = moment().subtract(2, 'minutes');
var tltest = $(this).attr("ows_TicketTimeSubmitted").replace(/ /g,'T') + ".000Z";
var timeLogged = moment($(this).attr("ows_TicketTimeSubmitted"));
if ( timeLogged.isBefore(twoMinutesAgo)) {
alertClass = "red" ;
}
if ($(this).attr("ows_TicketStatus") == "Responded")
{
takenClass = "taken";
status = "In Progress"
disabled = "disabled";
newResponseCount++;
}
newCount++;
liHtml += "<tr class='" + alertClass + " " + takenClass + "'><td>" + $(this).attr("ows_RQName") + "</td><td> " + $(this).attr("ows_RQSalaryNumber") + "</td><td> " + $(this).attr("ows_TicketTimeSubmitted")
+ "</td><td><a href='im:<sip:" + $(this).attr("ows_RQNumber") + ">' onclick='takeCall("+$ (this).attr("ows_ID") +");'" + disabled + ">Take call</a></td><td>" + status + "</td><td><button type='submit' class='answerChat btn btn-warning' onclick='closeCall(" + $(this).attr("ows_ID") +")'>Close chat request </button>" + "</td></tr>";
}
});
if (newCount != count) {
$("#tasks").html(liHtml);
$("a").removeAttr("shape");
if ( pass > 0 && newCount > count) {
newItemAlert();
}
count = newCount;
responseCount = 0;
}
else if (newResponseCount > responseCount) {
$("#tasks").html(liHtml);
$("a").removeAttr("shape");
responseCount = newResponseCount;
}
newCount = 0;
pass = 1;
}
});
setTimeout(arguments.callee, 1000);
})();
var newItemAlert = function (){
$.playSound('/glass_ping');
}
};
})(jQuery);
/*! OnLoad
---------------------------------------------*/
$(document).ready(function() {
$().TicketList();
});
/*! Utils
---------------------------------------------*/
var closeCall = function (ItemID){
var datetimeClosed = moment().format();
$().SPServices({
operation: "UpdateListItems",
async: true,
listName: "Queue",
batchCmd: "Update",
valuepairs: [["TicketStatus", "Answered"], ["TicketTimeClosed", datetimeClosed]],
ID: ItemID,
completefunc: function(xData, Status) {
}
});
}
var takeCall = function (ItemID){
var datetimeResponded = moment().format();
$().SPServices({
operation: "UpdateListItems",
async: true,
listName: "Queue",
batchCmd: "Update",
valuepairs: [["TicketStatus", "Responded"], ["TicketTimeResponded", datetimeResponded]],
ID: ItemID,
completefunc: function(xData, Status) {
}
});
return true;
}