Files
SIG/Scripts/Notificaciones.js
JORGE LUIS CAUDILLO ESPARZA c35cf91883 Commit inicial Ventas Productivo
2019-08-14 16:40:54 -07:00

166 lines
6.1 KiB
JavaScript

var NotificacionesADS = function () {
var VentanaConConexion = false;
const valueok = "1";
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,Opciones) {
console.log(Mensaje);
let obj = { metodo: "AlertaVentana", titulo: Titulo, mensaje: Mensaje, timeout: timeoutseconds, tipo: Tipo, opciones: Opciones, tick: (new Date()).getTime() }
let myjson = JSON.stringify(obj);
localStorage.setItem("ADSNotificaciones", myjson);
execAlertaVentana(obj);
}
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);
}
}
}
//-------------------------------------------------------------------- EndEventos ----------------------------------------------------------------------
function closeopenConexion() {
let varreconected = localStorage.getItem("AdsNotificacionesReconected");
localStorage.removeItem("AdsNotificacionesIsConnected");
if (varreconected == "1") {
localStorage.removeItem("AdsNotificacionesReconected");
} else { localStorage.setItem("AdsNotificacionesReconected", "1"); }
}
function beforeunloadcloseopenConexion(e) {
closeopenConexion()
}
function loadConection() {
VentanaConConexion = true;
localStorage.setItem("AdsNotificacionesIsConnected", valueok);
if ($.connection) {
let nointentos =0
var proxy = $.connection.notificaciones;
/*$.connection.hub.url = '/signalr';*/
$.connection.hub.disconnected(function () {
setTimeout(function () { nointentos = nointentos + 1; if (nointentos < 4) { $.connection.hub.start().done(function () { nointentos = 0 }); console.log('Reconected'); } }, 120000);
});
proxy.client.AlertaVentana = AlertaVentana;
$.connection.hub.start().done(function () {
console.log('Bienvenido');
});
} else {
console.error('Signal Error (no se encontro $.connection)');
}
window.addEventListener("beforeunload", beforeunloadcloseopenConexion);
}
function initConnection() {
let isconnected = localStorage.getItem("AdsNotificacionesIsConnected");
if (isconnected == valueok) {
} else {
loadConection()
}
}
function handlelocalstorage() {
window.addEventListener('storage', function (evt) {
if (evt.key == "ADSNotificaciones") {
}
switch (evt.key + "") {
case 'AdsNotificacionesReconected':
if (!VentanaConConexion) {
setTimeout(function () { initConnection() }, 5000)
} else {
window.removeEventListener("beforeunload", beforeunloadcloseopenConexion)
}
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 () {
initherramientas();
handlelocalstorage();
initConnection();
},
initAjax: function () {
console.log(VentanaConConexion);
},
GetTieneConexion: function () { return VentanaConConexion; },
closeopenConexion
};
}();