function show_calendar(oInputName, dataHoje) { const jPrincipal = getPrincipal(); const bodyArea = jPrincipal.document.getElementById("_bodyArea"); var oInput = document.getElementById(oInputName); if (oInput && !oInput.readOnly && !oInput.getAttribute("disabled")) { if (oInput._flatpickr) { const inst = oInput._flatpickr; if (inst._customListeners?.timeout) { clearTimeout(inst._customListeners.timeout); inst._customListeners.timeout = null; } inst.open(); return; } const _flatpickr = window.flatpickr; if (!_flatpickr) return; function posicionarCalendario(instance) { const cal = instance.calendarContainer; const oIpt = document.getElementById(oInputName); const cIndex = getIndex(); cal.style.visibility = "hidden"; function aplicarPosicao() { const scrollTop = $("#_divScroll").scrollTop() || 0; const scrollLeft = $("#_divScroll").scrollLeft() || 0; const baseY = getTopVlr(oIpt) + oIpt.offsetHeight - scrollTop; const baseX = getLeftVlr(oIpt) - scrollLeft; const rect = cal.getBoundingClientRect(); const width = rect.width || cal.offsetWidth; const height = rect.height || cal.offsetHeight; if (width === 0 || height === 0) { setTimeout(aplicarPosicao, 30); return; } const maxX = jPrincipal.getInnerWidth() - width; const maxY = jPrincipal.getInnerHeight() - height; let x = Math.max(0, Math.min(baseX, maxX)); let y = baseY; if (y > maxY) y = baseY - oIpt.offsetHeight - height; y = Math.max(0, Math.min(y, maxY)); cal.style.position = "fixed"; cal.style.top = `${y}px`; cal.style.left = `${x}px`; cal.style.zIndex = String(1240 * cIndex + 2); cal.style.visibility = "visible"; } setTimeout(() => requestAnimationFrame(aplicarPosicao), 30); } const fp = _flatpickr(oInput, { allowInput: true, clickOpens: false, disableMobile: true, static: false, dateFormat: "d/m/Y", locale: "pt", appendTo: bodyArea, onReady: function(selectedDates, dateStr, instance) { const cal = instance.calendarContainer; let interagindoInterno = false; instance._customListeners = instance._customListeners || {}; instance._customListeners.timeout = null; function cancelarFechamento() { if (instance._customListeners?.timeout) { clearTimeout(instance._customListeners.timeout); instance._customListeners.timeout = null; } } function agendarFechamento(e) { const destino = e.relatedTarget; if (interagindoInterno) return; if (destino && ( cal.contains(destino) || destino.closest(".flatpickr-calendar") || destino.closest(".flatpickr-monthDropdown-months") || destino.closest(".numInputWrapper") )) return; cancelarFechamento(); instance._customListeners.timeout = setTimeout(() => { instance._customListeners.timeout = null; instance.close(); }, 300); } function onMouseDown() { interagindoInterno = true; cancelarFechamento(); } function onMouseUp() { setTimeout(() => { interagindoInterno = false; }, 300); } cal.addEventListener("mousedown", onMouseDown); cal.addEventListener("mouseup", onMouseUp); cal.addEventListener("mouseenter", cancelarFechamento); cal.addEventListener("mouseleave", agendarFechamento); oInput.addEventListener("mouseenter", cancelarFechamento); oInput.addEventListener("mouseleave", agendarFechamento); instance._customListeners.cal = cal; instance._customListeners.oInput = oInput; instance._customListeners.onMouseDown = onMouseDown; instance._customListeners.onMouseUp = onMouseUp; instance._customListeners.cancelarFechamento = cancelarFechamento; instance._customListeners.agendarFechamento = agendarFechamento; }, onOpen: function(selectedDates, dateStr, instance) { if (instance._customListeners?.timeout) { clearTimeout(instance._customListeners.timeout); instance._customListeners.timeout = null; } posicionarCalendario(instance); }, onDestroy: function(_, __, instance) { const l = instance._customListeners; if (!l) return; if (l.timeout) clearTimeout(l.timeout); l.cal.removeEventListener("mousedown", l.onMouseDown); l.cal.removeEventListener("mouseup", l.onMouseUp); l.cal.removeEventListener("mouseenter", l.cancelarFechamento); l.cal.removeEventListener("mouseleave", l.agendarFechamento); l.oInput.removeEventListener("mouseenter", l.cancelarFechamento); l.oInput.removeEventListener("mouseleave", l.agendarFechamento); delete instance._customListeners; } }); if (fp && typeof fp.open === "function") fp.open(); } }