var popup = (function (window, undefined) { "use strict"; var urlPopup, popupElem, popupCloseBtn, popupSubmitBtn, popupSubmitCloseBtn, popupCloseBtnLength, newEventPopupLoading, newEventPopupLoaded, popupSubmitAction, popupActionArgs, topScroll, popupBg, popupBgPattern = '.popup-container', popupBgCursorClassname = 'popup-container--cursor', pattern = 'data-js-popup', popupMatch = '[' + pattern + '="popup"]', btnMatch = '[' + pattern + '="close"]', submitMatch = '[' + pattern + '="submitAction"]', submitCloseMatch = '[' + pattern + '="submitActionClose"]', actionPopup = function(e) { e.preventDefault(); e.stopPropagation(); if (popupSubmitAction) { //popupSubmitAction(popupActionArgs);Al poner esta línea se llama a la función y se envía el array como un único argumento popupSubmitAction.apply(null,popupActionArgs); //De este modo enviamos cada elemento del array como argumento de la función } }, removePopup = function() { lockBody(false); document.body.removeEventListener('click', closePopup); /** * JM 2018-06-05 * Comentado porque impedía mostrar un segundo popup if(popupBg) { popupBg.parentNode.removeChild(popupBg); return false; }**/ newEventPopupLoaded = loading.setLoadedEvent(newEventPopupLoading); document.documentElement.dispatchEvent(newEventPopupLoaded); //document.documentElement.dispatchEvent(newEventPopupLoaded); */ return true; }, closePopup = function(evt) { var target = evt.target || evt.srcElement, isBtnClose = target.getAttribute('data-js-popup') === "close" ? removePopup() : !getParents(target, '[data-js-popup="popup"]') ? removePopup() : false; /* var e = evt || document.createEvent('Event'), body = document.body, target = e.target || e.srcElement, popupGrid = document.querySelector('[data-js-popupGrid]') || popupBg.children[0], btnClose = target.getAttribute('data-js-popup') === "close", parentNode = target.parentNode; if (parentNode !== popupGrid && parentNode !== body && !btnClose) { return false; } lockBody(false); removeEventHandler(document.body, 'mousedown', closePopup); removeClass(popupBg, popupBgCursorClassname); document.documentElement.dispatchEvent(newEventPopupLoaded); // para cerrar el loading if(document.body.classList.contains('ioslocked')) { document.body.classList.remove('ioslocked'); }*/ }, relocateBody = function(e) { e.preventDefault(); window.removeEventListener('scroll', relocateBody); }, lockBody = function(activate) { var act = activate || false, isMobile = document.querySelector('.mobileHeader')|| false, bodyTop = "-"+topScroll+"px", body = document.body; if (act) { popupBg.style.overflowY = "scroll"; addClass(body, 'body--locked'); body.style.top = bodyTop; return false; } removeClass(body, 'body--locked'); body.removeAttribute("style"); popupBg.removeAttribute("style"); }, setListeners = function() { popupCloseBtn = popupElem.querySelectorAll(btnMatch); popupSubmitBtn = popupElem.querySelector(submitMatch); popupCloseBtnLength = popupCloseBtn.length; if (popupCloseBtn) { for (var i = 0; i < popupCloseBtnLength; i++) { addEventHandler(popupCloseBtn[i], 'mousedown', closePopup); } } if (popupSubmitBtn) { addEventHandler(popupSubmitBtn, 'mousedown', actionPopup); } popupBg = document.querySelector(popupBgPattern); addClass(popupBg, popupBgCursorClassname); lockBody(true); if(popupElem.getAttribute('data-locked')){ popupElem.querySelector('[type="submit"]').addEventListener('click', actionPopup, false); return false; } addEventHandler(document.body, 'click', closePopup); }, createDOMObject = function(stringElem) { var DOMObj, div = document.createElement('div'); div.innerHTML = stringElem; DOMObj = div.firstChild; return DOMObj; }, getPopupInfo = function(msg, icn) { var message = msg || null, icon = icn || 'draw_error'; $.post(baseUri + 'errors/getPopupInfo', { msg: message, icon: icon }, function(result) { setPopup(result); }); }, getPopupElem = function() { return popupElem; }, setPopup = function(popupElm) { var replaceEvent; popupElem = popupElm || null; topScroll = document.documentElement.scrollTop || document.body.scrollTop; if (!popupElem) { return false; } if ((typeof popupElem) === 'string') { popupElem = createDOMObject(popupElm); } replaceEvent = new CustomEvent('replace'); newEventPopupLoading = loading.setLoadingEvent(popupElem); newEventPopupLoaded = loading.setLoadedEvent(newEventPopupLoading); document.documentElement.dispatchEvent(newEventPopupLoading); document.documentElement.dispatchEvent(replaceEvent); setListeners(); }, getPopupPost = function (url, postArgs, popSbmtAction,listeners) { var eventPopupLoading = loading.setLoadingEvent(); popupSubmitAction = popSbmtAction || null; urlPopup = url; document.documentElement.dispatchEvent(eventPopupLoading); // para iniciar el loading $.post(urlPopup, postArgs,function (result) { setPopup(result); if(typeof listeners !== 'undefined'){ listeners.apply(null); } }); }, getPopup = function (url, popSbmtAction, pActionArgs) { var eventPopupLoading = loading.setLoadingEvent(); popupSubmitAction = popSbmtAction || null; popupActionArgs = pActionArgs || []; urlPopup = url; document.documentElement.dispatchEvent(eventPopupLoading); // para iniciar el loading $('
').load(urlPopup, function() { popupElem = $(this).children()[0]; setPopup(popupElem); }); }; return { getPopup: getPopup, setPopup: setPopup, getPopupInfo: getPopupInfo, getPopupElem: getPopupElem, getPopupPost: getPopupPost, removePopup: removePopup }; })(window);