(function ($) {
var originalModal = $.fn.modal;
function shouldIntercept(option) {
// No existe el modal en el DOM y el selector es un #id directo
if (this.length !== 0) return false;
var sel = this.selector;
if (typeof sel !== 'string' || sel.charAt(0) !== '#' || this.context !== document) return false;
// Casos que equivalen a "mostrar" en Bootstrap 3
if (typeof option === 'string') {
return (option === 'show' || option === 'toggle');
}
if (typeof option === 'object') {
// .modal(options) muestra si show !== false
return option.show !== false;
}
// .modal() sin argumentos -> muestra
return true;
}
function modalOverride(option, _relatedTarget) {
if (shouldIntercept.call(this, option)) {
var selector = this.selector;
var modalId = selector.slice(1);
var version = (typeof MODAL_VERSIONS !== "undefined" ? MODAL_VERSIONS[modalId] : '1') || '1';
var url = '/modal/' + modalId;
var cachedHtml = (typeof getModalFromCache === "function") ? getModalFromCache(modalId, version) : null;
if (cachedHtml) {
if (!document.getElementById(modalId)) $('body').append(cachedHtml);
// Si el pedido fue 'toggle' y no existe aún, al final es 'show'
return $('#' + modalId).modal((option === 'toggle') ? 'show' : option, _relatedTarget);
}
if (typeof showModalLoader === "function") showModalLoader();
$.get(url, function (html) {
if (typeof hideModalLoader === "function") hideModalLoader();
if (typeof saveModalToCache === "function") saveModalToCache(modalId, html, version);
if (!document.getElementById(modalId)) $('body').append(html);
if (typeof option === 'string' && option === 'toggle') {
$('#' + modalId).modal('show', _relatedTarget);
} else {
$('#' + modalId).modal(option, _relatedTarget);
}
}).fail(function () {
if (typeof hideModalLoader === "function") hideModalLoader();
alert('Error al cargar el modal. Intenta de nuevo.');
});
return this;
}
// Llama al método original en todos los demás casos
return originalModal.apply(this, arguments);
}
// Copia propiedades estáticas (Constructor, noConflict, etc.)
for (var key in originalModal) {
if (Object.prototype.hasOwnProperty.call(originalModal, key)) {
modalOverride[key] = originalModal[key];
}
}
// Asigna el override
$.fn.modal = modalOverride;
})(jQuery);
Rutas de Montaña, Cerros y Volcanes – Andeshandbook