233 lines
8.5 KiB
JavaScript
233 lines
8.5 KiB
JavaScript
|
|
var NotificacionesADS = function () {
|
|
var VentanaConConexion = false;
|
|
|
|
let nointentos = 0;
|
|
const maxintentos = 4;
|
|
var wNum;
|
|
const valueok = "1";
|
|
const AdsNotificacionesReconected = "AdsNotificacionesReconected";
|
|
const Windownumber = "ADSWindownumber";
|
|
const AdsNotificacionesIsConnected = "AdsNotificacionesIsConnected";
|
|
function initherramientas() {
|
|
if (("Notification" in window)) {
|
|
if (Notification.permission !== 'denied') {
|
|
Notification.requestPermission(function (permission) {
|
|
// If the user accepts, let's create a notification
|
|
if (permission === "granted") {
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
//-------------------------------------------------------------------- Eventos ----------------------------------------------------------------------
|
|
var AlertaVentana = function (Titulo, Mensaje, Tipo, timeoutseconds) {
|
|
console.log(Mensaje);
|
|
let obj = { metodo: "AlertaVentana", titulo: Titulo, mensaje: Mensaje, timeout: timeoutseconds, tipo: Tipo, tick: (new Date()).getTime() };
|
|
let myjson = JSON.stringify(obj);
|
|
localStorage.setItem("ADSNotificaciones", myjson);
|
|
execAlertaVentana(obj);
|
|
};
|
|
function Execstring(script) {
|
|
try {
|
|
eval(script);
|
|
} catch (e) {
|
|
console.error('(SignalR) ErrorScript \'' + script + '\'');
|
|
}
|
|
}
|
|
var execAlertaVentana = function (obj) {
|
|
console.log(obj);
|
|
if (obj) {
|
|
|
|
//toastr.warning(obj.mensaje, obj.titulo, {
|
|
// "closeButton": true,
|
|
// "debug": false,
|
|
// "positionClass": "toast-top-right",
|
|
// "onclick": (function () { }),
|
|
// "showDuration": 0,
|
|
// "hideDuration": 0,
|
|
// "timeOut": 0,
|
|
// "extendedTimeOut": 0,
|
|
// "showEasing": "swing",
|
|
// "hideEasing": "linear",
|
|
// "showMethod": "fadeIn",
|
|
// "hideMethod": "fadeOut"
|
|
|
|
//});
|
|
let Mensaje = obj.mensaje;
|
|
let titulo = obj.titulo;
|
|
let timeout = obj.timeout;
|
|
if (timeout) {
|
|
timeout = timeout * 1000;//convierto a milisegundos
|
|
} else { timeout = 0; }
|
|
let opciones = {
|
|
"closeButton": true,
|
|
"debug": false,
|
|
"positionClass": "toast-top-right",
|
|
"onclick": function () { },
|
|
"showDuration": 0,
|
|
"hideDuration": 0,
|
|
"timeOut": timeout,
|
|
"extendedTimeOut": 0,
|
|
"showEasing": "swing",
|
|
"hideEasing": "linear",
|
|
"showMethod": "fadeIn",
|
|
"hideMethod": "fadeOut"
|
|
|
|
};
|
|
|
|
switch (obj.tipo) {
|
|
case 5:
|
|
toastr.success(Mensaje, titulo, opciones);
|
|
break;
|
|
case 6:
|
|
toastr.info(Mensaje, titulo, opciones);
|
|
break;
|
|
case 7:
|
|
toastr.error(Mensaje, titulo, opciones);
|
|
break;
|
|
case 8:
|
|
toastr.warning(Mensaje, titulo, opciones);
|
|
break;
|
|
default:
|
|
toastr.warning(Mensaje, titulo, opciones);
|
|
}
|
|
}
|
|
};
|
|
var execContadorCampana = function (contadormessage) {
|
|
$(".SigCampananoti").text(contadormessage);
|
|
$(".SigCampananoti").addClass('badge-danger');
|
|
$(".SigCampananoti").removeClass('badge-default');
|
|
//console.log('(SignalR) contadormessage \'' + contadormessage);
|
|
}
|
|
var ContadorCampana = function (contadormessage) {
|
|
//console.log('(SignalR) ContadorCampana \'' + contadormessage);
|
|
localStorage.setItem("ContadorCampana", '');
|
|
localStorage.setItem("ContadorCampana", contadormessage);
|
|
execContadorCampana(contadormessage);
|
|
}
|
|
//-------------------------------------------------------------------- EndEventos ----------------------------------------------------------------------
|
|
function closeopenConexion() {
|
|
if (VentanaConConexion) {
|
|
localStorage.removeItem(AdsNotificacionesIsConnected);
|
|
|
|
let varreconected = localStorage.getItem(AdsNotificacionesReconected);//obtengo la variable para cambiarla y dispara el evento local change
|
|
if (varreconected === "1") {
|
|
localStorage.removeItem(AdsNotificacionesReconected);
|
|
} else { localStorage.setItem(AdsNotificacionesReconected, "1"); }
|
|
}
|
|
nointentos = maxintentos;
|
|
}
|
|
function beforeunloadcloseopenConexion(e) {
|
|
closeopenConexion();
|
|
}
|
|
function loadConection() {
|
|
VentanaConConexion = true;
|
|
localStorage.setItem(AdsNotificacionesIsConnected, valueok);
|
|
if ($.connection) {
|
|
var proxy = $.connection.notificaciones;
|
|
/*$.connection.hub.url = '/signalr';*/
|
|
$.connection.hub.disconnected(function () {
|
|
console.log(' (SignalR) disconnected');
|
|
signalrconectionStar(1200);
|
|
});
|
|
|
|
proxy.client.alertaVentana = AlertaVentana;
|
|
proxy.client.ContadorCampana = ContadorCampana;
|
|
|
|
proxy.client.Execstring = Execstring;
|
|
|
|
signalrconectionStar(1200);
|
|
|
|
} else {
|
|
console.error('(SignalR) Error :\'no se encontro $.connection\'');
|
|
|
|
}
|
|
window.addEventListener("beforeunload", beforeunloadcloseopenConexion);
|
|
}
|
|
function signalrconectionStar(TimeOut) {
|
|
|
|
setTimeout(function () {
|
|
nointentos = nointentos + 1;
|
|
if (nointentos < maxintentos) {
|
|
$.connection.hub.start().done(function () { console.log('(SignalR) Bienvenido'); nointentos = 0; })
|
|
.fail(function () { console.log(' (SignalR) no conectado (Intento:' + nointentos + ')'); signalrconectionStar(1200); });
|
|
}
|
|
}, TimeOut);
|
|
}
|
|
function initConnection() {
|
|
let isconnected = localStorage.getItem(AdsNotificacionesIsConnected);
|
|
|
|
|
|
if (isconnected !== valueok) {
|
|
loadConection();
|
|
} else {
|
|
console.log('is some there conected (' + wNum+')');
|
|
}
|
|
}
|
|
function handlelocalstorage() {
|
|
window.addEventListener('storage', function (evt) {
|
|
if (evt.key === "ADSNotificaciones") {
|
|
///
|
|
x = 1;
|
|
}
|
|
console.log({ key: evt.key });
|
|
console.log({newValue: evt.newValue});
|
|
switch (evt.key + "") {
|
|
case 'ADSCloseConection':
|
|
closeopenConexion();
|
|
$.connection.hub.stop().done(function () {
|
|
console.log('stopped');
|
|
});
|
|
break;
|
|
case 'AdsNotificacionesReconected':
|
|
if (!VentanaConConexion) {
|
|
setTimeout(function () { initConnection(); }, (wNum * 200));
|
|
} else {
|
|
window.removeEventListener("beforeunload", beforeunloadcloseopenConexion);
|
|
}
|
|
break;
|
|
|
|
case 'ContadorCampana':
|
|
try {
|
|
execContadorCampana(evt.newValue);
|
|
|
|
} catch (e) {
|
|
console.error('(SignalR) contadormessage \''+e);
|
|
}
|
|
break;
|
|
case 'ADSNotificaciones':
|
|
try {
|
|
let obj = JSON.parse(evt.newValue);
|
|
if (obj && obj.metodo === "AlertaVentana") {
|
|
execAlertaVentana(obj);
|
|
}
|
|
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
break;
|
|
}
|
|
|
|
}, false);
|
|
}
|
|
|
|
return {
|
|
init: function () {
|
|
|
|
wNum = parseInt(localStorage.getItem(Windownumber));
|
|
if (isNaN(wNum) || (wNum > 50)) { wNum = 12; }
|
|
wNum = wNum + 1;
|
|
localStorage.setItem(Windownumber, wNum);
|
|
initherramientas();
|
|
handlelocalstorage();
|
|
initConnection();
|
|
},
|
|
initAjax: function () {
|
|
console.log(VentanaConConexion);
|
|
},
|
|
GetTieneConexion: function () { return VentanaConConexion; },
|
|
closeopenConexion
|
|
};
|
|
|
|
}(); |