Files
crm.twinpol.com/include/ECM/EcmNotifications/EcmNotifications.js
2025-05-12 15:45:17 +00:00

295 lines
7.5 KiB
JavaScript
Executable File

// where our WebSockets logic will go later
var socket, host;
var notification_count = 0;
var notification_array=new Array();
var ajaxnotifications_url = "index.php?module=Schedulers&action=EcmNotificationAjax&to_pdf=1";
var audiotypes = {
"mp3" : "audio/mpeg",
"mp4" : "audio/mp4",
"ogg" : "audio/ogg",
"wav" : "audio/wav"
}
function ss_soundbits(sound) {
var audio_element = document.createElement('audio')
if (audio_element.canPlayType) {
for (var i = 0; i < arguments.length; i++) {
var source_element = document.createElement('source')
source_element.setAttribute('src', arguments[i])
if (arguments[i].match(/\.(\w+)$/i))
source_element.setAttribute('type', audiotypes[RegExp.$1])
audio_element.appendChild(source_element)
}
audio_element.load()
audio_element.playclip = function() {
audio_element.pause()
audio_element.currentTime = 0
audio_element.play()
}
return audio_element
}
}
var clicksound = ss_soundbits(
'include/ECM/EcmNotifications/sounds/bell_ring.ogg',
"include/ECM/EcmNotifications/sounds/bell_ring.mp3")
host = "wss://system.saas-systems.pl:444";
function connect() {
try {
socket = new WebSocket(host);
addMessage("Socket State: " + socket.readyState);
socket.onopen = function() {
addMessage("Socket Status: " + socket.readyState + " (open)");
}
socket.onclose = function() {
socket.close();
}
socket.onmessage = function(msg) {
DrawMessage(msg.data);
}
} catch (exception) {
addMessage("Error: " + exception);
}
}
function addMessage(msg) {
$("#chat-log").append("<p>" + msg + "</p>");
}
function DrawNotificationDiv(){
var html='';
var toggle="if($('#notification_list').css('display')=='none'){$('#notification_list').show();}else{$('#notification_list').hide();}";
if(notification_array.length>1){
html='<a href="#" onclick="'+toggle+'">Masz <span style="color:red;">'+notification_array.length+'</span> nowe przypomnienia!</a>';
}
if(notification_array.length==0){
html='<p>Brak nowych przypomnień.</p>';
}
if(notification_array.length==1) {
html='<a href="#" onclick="'+toggle+'">Masz <span style="color:red;">1</span> nowe przypomnienie!</a>';
}
var div_header='<div id="notification_list" style="border: 1px solid #cccccc;background:#e6e6e6;padding:5px;position:absolute;display:none;">';
for(var i=0;i<notification_array.length;i++){
div_header+='<p><a href="#" onclick="createDialog(\''+i+'\');$(\'#notification_list\').hide();">Temat: '+notification_array[i].name+'</a></p><br>';
}
div_header+='</div>';
$("#notification-Div").html(html+div_header);
}
function makeid()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function leaveSelect(id){
var html='<select id="leaveSel_'+id+'">';
html+='<option value="1">5 minut</option>';
html+='<option value="2">10 minut</option>';
html+='<option value="3">15 minut</option>';
html+='<option value="4">1 godzinię</option>';
html+='<option value="5">2 godziny</option>';
html+='<option value="6">3 godziny</option>';
html+='<option value="7">1 dzien</option>';
html+='<option value="8">2 dni</option>';
html+='<option value="9">3 dni</option>';
html+='</select>';
return html;
}
function dialogContent(obj,id){
if(obj.date_start === null){
obj.date_start='brak';
}
var toggle="if($('#leave_"+obj.id+"').css('display')=='none'){$('#leave_"+obj.id+"').show();}else{$('#leave_"+obj.id+"').hide();}";
var html='<table><tr><td>Temat:</td><td><b>'+obj.name+'</b></td></tr>';
html+='<tr><td>Data rozpoczęcia:</td><td><b>'+obj.date_start.substr(0,16)+'</b></td></tr>';
html+='<tr><td>Opis:</td><td>'+obj.description+'</td></tr></table>';
html+='<input type="button" value="Odłóż" name="button1" onclick="'+toggle+'" class="button" title="Odłóż"><br>';
html+='<div id="leave_'+obj.id+'" style="display:none;"><table><tr><td>Na:</td><td>'+leaveSelect(obj.id)+'</td></tr></table>';
html+='<br><input type="button" value="Zapisz" name="button1" onclick="saveNotification(\''+obj.id+'\',\''+id+'\');" class="button" title="Zapisz"></div>';
return html;
}
function saveNotification(id,index){
var params = {
job : 'changeNotificationDate',
id : id,
flag : $('#leaveSel_'+id+' :selected').val(),
};
$.ajax({
type : "POST",
url : ajaxnotifications_url,
dataType : "json",
success : function(data) {
if (data != '-1')
$( "#dialog_"+id ).dialog( "close" );
notification_array.splice(index, 1);
send(id);
DrawNotificationDiv();
},
data : params
});
}
function showLeavedNotifications(data){
for( var i=0; i < data.length; i++ ){
createDialog(data[i]);
}
}
function getLeaveadNotifications(){
var params = {
job : 'getLeaveadNotifications',
user_id : $('#current_user_id').val(),
};
$.ajax({
type : "POST",
url : ajaxnotifications_url,
dataType : "json",
success : function(data) {
if (data != '-1'){
notification_array=data;
DrawNotificationDiv();
}
},
data : params
});
}
function createDialog(obj){
var id=obj;
var obj=notification_array[obj];
var div_id=makeid();
$( "#notification-Dialog" ).html('<div id="dialog_'+obj.id+'"></div>');
$( "#dialog_"+obj.id).html(dialogContent(obj,id));
$( "#dialog_"+obj.id).dialog({
title: "Nowe przypomnienie o: "+obj.name.substr(0,15)+'...',
open: function() { // open event handler
$(this) // the element being dialogged
.parent() // get the dialog widget element
.find(".ui-dialog-titlebar-close") // find the close button for this dialog
.hide(); // hide it
},
modal: true,
buttons: {
'Przyjąłem': function() {
$( this ).dialog( "close" );
},
},
beforeClose: function( event, ui ) {
var params = {
job : 'updateNotificationStatus',
id : obj.id,
flag : '2',
};
$.ajax({
type : "POST",
url : ajaxnotifications_url,
dataType : "json",
success : function(data) {
if (data != '-1')
console.log(data);
notification_array.splice(id, 1);
send(obj.id);
DrawNotificationDiv();
},
data : params
});
}
});
}
function srt(desc) {
return function(a,b){
return desc ? ~~(a < b) : ~~(a > b);
};
}
function checkExist(id){
for(i=0;i<notification_array.length;i++){
if(notification_array[i].id==id){
notification_array.splice(i, 1);
$( "#dialog_"+id ).dialog( "close" );
}
}
}
function DrawMessage(msg) {
if (msg != '') {
if(msg.length==36){
// usuwanie nieaktualnych
checkExist(msg);
DrawNotificationDiv();
}
var obj = JSON.parse(msg);
if (obj.assigned_user_id == $('#current_user_id').val()) {
// dodawanie nowych
clicksound.playclip();
checkExist(obj.id);
notification_array.unshift(obj);
// notification_array.push(obj);
DrawNotificationDiv();
//createDialog(obj);
}
}
// if(obj.)
}
function send(id) {
var text = $("#message").val();
if (text == '') {
addMessage("Please Enter a Message");
return;
}
try {
socket.send(id);
addMessage("Sent: " + text)
} catch (exception) {
addMessage("Failed To Send")
}
}
$('#message').keypress(function(event) {
if (event.keyCode == '13') {
send();
}
});
$("#disconnect").click(function() {
socket.close()
});
$( document ).ready(function() {
connect();
getLeaveadNotifications();
});