Files
Maphre-PLD-2026/js/site.js
T
2026-06-04 12:20:40 -06:00

800 lines
22 KiB
JavaScript

var __jsonObject, __spresponse;
var __temas = new Array();
var __rootxml = 'xml/main.xml';
var __curso = new Object();
__curso.databasename = "Mapfre_PLD.V10";
__curso.modoDesarrollo = false;
if(__curso.modoDesarrollo ) { __curso.databasename = __curso.databasename +"."+Math.random(); }
__curso.maxAvance = 0;
__curso.libre = false;
__curso.actual = 0;
var __htmlmenu = "";
var __auxi = 0;
var __currentSound = null;
var circleProgressBar;
var ___scala = 1;
var __click = newSound("audio/click.mp3");
jQuery(document).ready(function($) {
$(".wloader").show();
var success = pipwerks.SCORM.init();
var status = "";
startTimer();
if (success) {
status = pipwerks.SCORM.get("cmi.core.lesson_status");
if (!(status.localeCompare("completed") == 0 || status.localeCompare("passed") == 0)) {
success = pipwerks.SCORM.set("cmi.core.lesson_status", "incomplete");
}
}
ready();
});
function ready() {
resizeRutine();
window.addEventListener("resize", resizeRutine);
$.ajax({
url: __rootxml,
data: {
nochache: 'cache-' + new Date().getTime()
},
dataType: "xml"
})
.done(function(response) {
__spresponse = response;
indexes();
__jsonObject = $.xml2json(__spresponse);
crearMenu();
recuperarSesion();
eliminarDuplicadosDelMenu();
})
.fail(function() {
alert("error al leer configuración.");
});
// eventos de botones de menu
$(".btn-close-menu").click(__closemenu);
$(".btn__menu").click(__openmenu);
$(".btn__siguiente").click(__siguiente);
$(".btn__atras").click(__atras);
$(".btn__home").click(__home);
$(".btn__reload").click(__reload);
$(".btn-show-subs").click(__togglesubs);
$(".wcontainer").on('click', '.btn_menu_padre', function(event) {
$(this).toggleClass("close_menu_items");
$(this).parent().find(".btn_menu_tema").toggle("fast");
});
$(".wcontainer").on('click', '[data-indexmenubtn]', function(event) {
if (!$(this).hasClass('disabled')) {
__goto(Number($(this).data('indexmenubtn')));
__closemenu();
} else {
Swal.fire({
title: '<b>Tema bloqueado</b>',
icon: 'info',
showCloseButton: false,
showCancelButton: false,
cancelButtonText: 'No',
confirmButtonText: 'Aceptar',
height: '100%',
showConfirmButton: true,
allowOutsideClick: false,
allowEscapeKey: false,
allowEnterKey: false,
target: '.wcontainer',
customClass: 'popscoinit',
onBeforeOpen: function() {},
onOpen: function() {},
onRender: function() {},
onClose: function() {},
onAfterClose: function() {}
});
}
});
$(".btn-sound-control-sco").click(function(event) {
if ($(this).hasClass('mute')) {
Howler.volume(1);
if ($(".sco").find('video').length > 0) {
$(".sco").find('video').each(function(index, el) {
el.volume = 1;
});
}
$(this).removeClass('mute');
} else {
$(this).addClass('mute');
Howler.volume(0);
if ($(".sco").find('video').length > 0) {
$(".sco").find('video').each(function(index, el) {
el.volume = 0;
});
}
}
});
}
function eliminarDuplicadosDelMenu() {
var cmp = "---";
$(".btn_menu_tema").each(function(index, el) {
var sco = __curso.temas[index];
if (!sco.visitado && !__curso.modoDesarrollo && !__curso.libre) {
$(el).addClass('disabled');
}
if ($(el).html() != cmp) {
cmp = $(el).html();
} else {
$(el).remove();
}
});
}
function __togglesubs() {
$(".subtenma").toggleClass("abierto");
}
function indexes() {
var _i = 0;
$.each($(__spresponse).find('modulo'), function(index, modulo) {
var moduloname = $(modulo).find('nombre').text();
$.each($(modulo).find('tema'), function(index, val) {
if (val.childElementCount == 0) {
var tema = new Object();
tema.index = _i;
tema.visitado = false;
tema.modulo = moduloname;
var element = $(val);
$.each(element.get(0).attributes, function(i, attrib) {
tema[attrib.name] = attrib.value;
});
tema.text = $(val).text();
__temas.push(tema);
$(val).attr("index", _i);
$(val).attr("visitado", "false");
_i++;
}
});
});
}
function crearMenu() {
var nombredelcurso = __jsonObject["#document"].curso.nombre;
$(".nombredelcurso").html(nombredelcurso);
var menudiv = ".itemsmenu";
$(menudiv).parent().prepend("<div class='menu_nombrecurso w-100'>" + nombredelcurso + "</div>");
if ($.isArray(__jsonObject["#document"].curso.modulo)) {
$.each(__jsonObject["#document"].curso.modulo, function(index, modulo) {
$(menudiv).append("<div class='menu_modulo" + index + "'></div>");
var modulodiv = '.menu_modulo' + index;
$(modulodiv).append("<div class='menu_nombremodulo'>" + modulo.nombre + "</div>");
agregaModuloAlMenu(index, modulo);
});
} else {
$(menudiv).append("<div class='menu_modulo" + 0 + "'></div>");
var modulodiv = '.menu_modulo' + 0;
$(modulodiv).append("<div class='menu_nombremodulo'>" + __jsonObject["#document"].curso.modulo.nombre + "</div>");
agregaModuloAlMenu(0, __jsonObject["#document"].curso.modulo);
}
}
function agregaModuloAlMenu(index, modulo) {
var modulodiv = '.menu_modulo' + index;
if ($.isArray(modulo.tema)) {
$.each(modulo.tema, function(indextema, tema) {
agregaTemaAlModulo(indextema, tema, modulodiv, index);
});
} else {
agregaTemaAlModulo(0, tema, modulodiv);
}
}
function agregaTemaAlModulo(indextema, tema, modulodiv, indexmodulo) {
if (tema.hasOwnProperty("tema")) {
if (!tema.$.nombre) {
alert("curso mal configurado");
}
var temadiv = '.menu_wtema' + indextema + indexmodulo;
$(modulodiv).append('<div class="menu_wtema' + indextema + indexmodulo + '"><div class="btn_menu_padre">' + tema.$.nombre + '</div></div>');
agregaTemaAlModulo(indextema, tema.tema, temadiv, indexmodulo);
} else if ($.isArray(tema)) {
$.each(tema, function(index, subs) {
var temadiv = '.menu_wtema' + indextema + indexmodulo;
agregaTemaAlModulo(index, subs, temadiv, indexmodulo);
});
} else {
$(modulodiv).append('<div data-indexmenubtn="' + tema.$.index + '" class="btn_menu_tema">' + tema.txt + '</div>');
}
}
function cp() {
circleProgressBar = new ProgressBar.Circle('.curso-progress', {
strokeWidth: 8,
color: '#FFF',
trailWidth: 8,
trailColor: 'rgba(0,0,0,0.125)',
text: {
autoStyleContainer: false
},
from: {
color: '#DC4D54'
},
to: {
color: '#C3360E'
},
step: function(state, circle) {
circle.path.setAttribute('stroke', state.color);
var value = Math.round(circle.value() * 100);
if (value === 0) {
circle.setText('0%');
} else {
circle.setText(value + "%");
}
},
fill: 'rgba(0,0,0,0.0)'
});
circleProgressBar.text.style.fontSize = '1rem';
}
function recuperarSesion() {
var tmpcur = recuperarAvance();
cp();
$(".wloader").hide();
if (tmpcur == null) {
__curso.temas = __temas;
__curso.actual = 0;
__curso.maxAvance = 0;
__loadsco();
} else {
__curso = tmpcur;
$.each(__curso.temas, function(index, val) {
if (val.visitado) {
$("[data-indexmenubtn='" + val.index + "']").removeClass("noclick");
}
});
//preguntar al usuario si desea recuperar el avance
Swal.fire({
title: '<b>¿Recuperar el máximo avance en el curso?</b>',
icon: 'info',
showCloseButton: false,
showCancelButton: true,
cancelButtonText: 'No',
confirmButtonText: 'Sí',
showConfirmButton: true,
allowOutsideClick: false,
allowEscapeKey: false,
allowEnterKey: false,
target: '.wcontainer',
customClass: 'popscoinit',
onBeforeOpen: function() {},
onOpen: function() {},
onRender: function() {},
onClose: function() {},
onAfterClose: function() {}
}).then(function(result) {
if (result.value === true) {
__curso.actual = __curso.maxAvance;
} else {
__curso.actual = 0;
}
__loadsco();
});
}
}
function __reload() {
__loadsco();
}
/**
* Description
* @method __closemenu
* @return
*/
function __closemenu() {
$(".wmenu").removeClass('open');
}
/**
* Description
* @method __openmenu
* @return
*/
function __openmenu() {
updateProgess();
$(".wmenu").addClass('open');
}
/**
* Description
* @method __siguiente
* @return
*/
function __siguiente() {
if (!$(".btn__siguiente").hasClass('blocknav')) {
__curso.actual++;
__loadsco();
}
}
function __nextslide() {
__curso.actual++;
__loadsco();
}
/**
* Description
* @method __atras
* @return
*/
function __atras() {
if (!$(".btn__atras").hasClass('blocknav')) {
__curso.actual--;
__loadsco();
}
}
/**
* Description
* @method __home
* @return
*/
function __home() {
__curso.actual = 1;
__loadsco();
}
/**
* Description
* @method __goto
* @param {} index
* @return
*/
function __goto(index) {
__curso.actual = index;
__loadsco();
}
/**
* Description
* @method resizeRutine
* @return
*/
function resizeRutine() {
var selector = ".wcontainer";
var width = $(selector).outerWidth();
var height = $(selector).outerHeight();
___scala = Math.min(((window.innerWidth * 100) / width), ((window.innerHeight * 100) / height)) / 100;
var left = window.innerWidth / 2 - ($(selector).outerWidth() * ___scala) / 2;
console.log(left);
TweenMax.set(selector, {
scale: ___scala,
transformOrigin: "0% 0%"
});
$(selector).css('left', left + 'px');
}
function vhToPixels(vh) {
return Math.round(window.innerHeight / (100 / vh)) + 'px';
}
function vwToPixels(vw) {
return Math.round(window.innerWidth / (100 / vw)) + 'px';
}
/**
* Description
* @method __loadsco
* @return
*/
function __loadsco() {
var sco = __curso.temas[__curso.actual];
//ajuste de subtema solo aplica para mapfre responsivo
$(".subtematxt").html(sco.text);
var toleft = ($(".subtenma").outerWidth() - $(".btn-show-subs").outerWidth()) * -1;
$(".subtenma").css("left", toleft);
$(".subtenma").removeClass("abierto");
$(".sco").removeClass('hasvideo');
if (swal.isVisible()) {
swal.close();
}
//
if (__curso.actual > __curso.maxAvance) {
__curso.maxAvance = __curso.actual;
}
$(".btn__atras, .btn__home, .btn__siguiente").addClass('noclick');
$(".wloader").show();
stopAllSounds();
instruccion(0);
if (sco.fullscreen == "1") {
$(".subtenma").removeClass('d-flex');
$(".subtenma").hide();
$(".subtematxt").hide();
$(".sco").addClass('scofullscreen');
} else {
$(".subtematxt").show();
$(".subtenma").addClass('d-flex');
$(".subtenma").show();
$(".sco").removeClass('scofullscreen');
}
__currentSound = null;
preload();
}
function preload() {
var sco = __curso.temas[__curso.actual];
updateNav();
instruccion(0);
$.ajax({
url: sco.src,
data: {
nochache: 'cache-' + new Date().getTime()
}
}).done(function(response) {
$(".sco").hide();
$(".sco").html(response).show();
$(".sco").find(".animated").on($(".sco").whichAnimationEvent(), function() {
if ((!$(this).hasClass("pulse") || $(this).hasClass("flash"))) {
$(this).removeClass("animated");
}
});
$(".wloader").hide();
console.info('file: ' + sco.src);
$('.__tema').html(sco.txt);
resizeRutine();
}).fail(function() {
alert("Error al leer: " + sco.src);
});
}
/**
* Description
* @method updateNav
* @return
*/
function updateNav() {
if (__curso.temas.length == 1) {
$(".btn__atras").hide();
$(".btn__siguiente").hide();
} else if (__curso.actual == 0) {
$(".btn__atras").hide();
$(".btn__siguiente").show();
} else if (__curso.actual > 0 && __curso.actual < __curso.temas.length - 1) {
$(".btn__atras").show();
$(".btn__siguiente").show();
} else {
$(".btn__atras").show();
$(".btn__siguiente").hide();
}
/*if (__curso.temas[__curso.actual].fullscreen) {
$(".btn__siguiente").hide();
}*/
var slideactual = "";
if (Number(__curso.actual + 1) < 10) {
slideactual = "0" + Number(__curso.actual + 1);
} else {
slideactual = Number(__curso.actual + 1);
}
var totalslides = "";
if (__curso.temas.length < 10) {
totalslides = "0" + Number(__curso.temas.length);
} else {
totalslides = __curso.temas.length;
}
$(".__paginacion").html(slideactual + "/" + totalslides);
$("[data-indexmenubtn='" + __curso.actual + "']").removeClass("disabled");
if ($("[data-indexmenubtn='" + __curso.actual + "']").length > 0) {
$(".btn_menu_tema").removeClass("actual");
$("[data-indexmenubtn='" + __curso.actual + "']").addClass('actual');
}
/* Calculo de progreso*/
updateProgess();
guardarAvance();
}
/**
* Description
* @method updateProgess
* @return
*/
function updateProgess() {
var totales = __curso.temas.length;
var visitados = $.grep(__curso.temas, function(element, index) {
return element.visitado == true;
});
var porcentajeDeAvance = round((visitados.length) / totales);
circleProgressBar.set(porcentajeDeAvance);
if (porcentajeDeAvance == 1) {
setLessonStatus("completed");
}
}
/**
* Description
* @method whichTransitionEvent
* @return
*/
$.fn.whichTransitionEvent = function() {
var t,
el = document.createElement("fakeelement");
var transitions = {
"transition": "transitionend",
"OTransition": "oTransitionEnd",
"MozTransition": "transitionend",
"WebkitTransition": "webkitTransitionEnd"
}
for (t in transitions) {
if (el.style[t] !== undefined) {
return transitions[t];
}
}
}
/**
* Description
* @method whichAnimationEvent
* @return
*/
$.fn.whichAnimationEvent = function() {
var t,
el = document.createElement("fakeelement");
var animations = {
"animation": "animationend",
"OAnimation": "oAnimationEnd",
"MozAnimation": "animationend",
"WebkitAnimation": "webkitAnimationEnd"
}
for (t in animations) {
if (el.style[t] !== undefined) {
return animations[t];
}
}
}
Howler.mobileAutoEnable = true;
Howler.autoSuspend = false;
/**
* Description
* @method newSoundAuto
* @param {} src
* @return snd
*/
function newSoundAuto(src) {
var snd = new Howl({
src: [src + "?nochache=" + new Date().getTime()],
autoplay: true
});
return snd;
}
/**
* Description
* @method newSound
* @param {} src
* @return snd
*/
function newSound(src) {
var snd = new Howl({
src: [src],
autoplay: false
});
return snd;
}
/**
* Description
* @method stopAllSounds
* @return
*/
function stopAllSounds() {
if (Array.isArray(Howler._howls)) {
$.each(Howler._howls, function(index, sound) {
sound.stop();
sound.off("end");
sound.off("load");
sound.off();
});
}
if (__currentSound != null) {
__currentSound.stop();
__currentSound.off("load");
__currentSound.off("end");
__currentSound = null;
}
}
/**
* Description
* @method stopAllSoundsAndPlay
* @param {} audio
* @return
*/
function stopAllSoundsAndPlay(audio) {
if (Array.isArray(Howler._howls)) {
$.each(Howler._howls, function(index, sound) {
sound.stop();
});
}
audio.play();
console.info('audio src: ' + audio._src);
if (__currentSound != null) {
__currentSound.off("end");
}
__currentSound = audio;
}
/**
* Description
* @method getCurrentSound
* @return __currentSound
*/
function getCurrentSound() {
return __currentSound;
}
/**
* Description
* @method recuperarAvance
* @return
*/
function recuperarAvance() {
if (__curso.modoDesarrollo && !pipwerks.SCORM.connection.isActive) {
sessionStorage.removeItem(__curso.databasename);
//return JSON.parse(sessionStorage.getItem(__curso.databasename));
return null;
} else if (pipwerks.SCORM.connection.isActive) {
var _recuperado = JSON.parse(getSuspendData());
if (_recuperado != null) {
if (_recuperado.temas.length == __temas.length) {
__curso.actual = _recuperado.actual;
__curso.maxAvance = _recuperado.maxAvance;
__curso.modoDesarrollo = _recuperado.modoDesarrollo;
$.each(_recuperado.temas, function(index, val) {
if (index < __temas.length) {
__temas[index].visitado = val.visitado;
}
});
__curso.temas = __temas;
return __curso;
} else {
return null;
}
} else {
return null;
}
} else if (sessionStorage.getItem(__curso.databasename)) {
return JSON.parse(sessionStorage.getItem(__curso.databasename));
} else {
return null;
}
}
/**
* Description
* @method guardarAvance
* @return
*/
function guardarAvance() {
if (pipwerks.SCORM.connection.isActive) {
var _tosave = new Object();
_tosave.actual = __curso.actual;
_tosave.maxAvance = __curso.maxAvance;
_tosave.modoDesarrollo = __curso.modoDesarrollo;
_tosave.temas = new Array();
$.each(__curso.temas, function(index, val) {
var _tema = new Object();
_tema.visitado = val.visitado;
_tosave.temas.push(_tema);
});
setSuspendData(JSON.stringify(_tosave));
} else {
sessionStorage.setItem(__curso.databasename, JSON.stringify(__curso));
}
}
function round(num) {
var decimales = 2;
var signo = (num >= 0 ? 1 : -1);
num = num * signo;
if (decimales === 0) //con 0 decimales
return signo * Math.round(num);
// round(x * 10 ^ decimales)
num = num.toString().split('e');
num = Math.round(+(num[0] + 'e' + (num[1] ? (+num[1] + decimales) : decimales)));
// x * 10 ^ (-decimales)
num = num.toString().split('e');
return signo * (num[0] + 'e' + (num[1] ? (+num[1] - decimales) : -decimales));
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
$.fn.loadsvg = function(svgUrl) {
var div = $(this);
$.get(svgUrl, function(data) {
var data = new XMLSerializer().serializeToString(data.documentElement)
div.html(data);
div.trigger("complete", [data]);
});
}
function rotateCard(btn) {
var $card = $(btn).closest('.card-container');
console.log($card);
if ($card.hasClass('hover')) {
$card.removeClass('hover');
} else {
$card.addClass('hover');
}
}
$.fn.addRandomClassAnimation = function() {
var animaciones = ['bounce', 'shake', 'wobble', 'bounceInLeft', 'fadeIn', 'fadeInLeftBig', 'fadeInUpBig', 'rotateIn', 'rotateInUpRight', 'rollIn', 'zoomInLeft', 'flash', 'headShake', 'jello', 'bounceInRight', 'fadeInDown', 'fadeInRight', 'rotateInDownLeft', 'zoomInRight', 'pulse', 'swing', 'bounceIn', 'bounceInUp', 'fadeInDownBig', 'fadeInRightBig', 'flipInX', 'lightSpeedIn', 'rotateInDownRight', 'zoomIn', 'zoomInUp', 'slideInRight', 'rubberBand', 'tada', 'bounceInDown', 'fadeInLeft', 'fadeInUp', 'flipInY', 'rotateInUpLeft', 'jackInTheBox', 'zoomInDown', 'slideInUp'];
$(this).addClass('animated');
animaciones = animaciones.sort(function() {
return 0.5 - Math.random()
});
var animation = animaciones[0];
$(this).addClass(animation);
var animationEnd = (function(el) {
var animations = {
animation: 'animationend',
OAnimation: 'oAnimationEnd',
MozAnimation: 'mozAnimationEnd',
WebkitAnimation: 'webkitAnimationEnd',
};
for (var t in animations) {
if (el.style[t] !== undefined) {
return animations[t];
}
}
})(document.createElement('div'));
$(this).one(animationEnd, resizeRutine);
}
$.fn.changeOrderOfElements = function() {
var parent = $(this);
var divs = parent.children();
parent.html("");
while (divs.length) {
parent.append(divs.splice(Math.floor(Math.random() * divs.length), 1)[0]);
}
}