$(document).ready(function () { window.is_show_notification_free_trial7days = { date_start: "2026-10-01T08:30:00+07:00", // ISO-8601 date_end: "2026-12-31T23:59:59+07:00", cookie_name: "is_show_notification_sticky", domain: "bangkokpost.com", age: 60 * 60 * 24 * 1, // อายุคุกกี้วินาที (1 วัน) get_cookie_notification: function () { var v = document.cookie.match( "(^|;) ?" + this.cookie_name + "=([^;]*)(;|$)", ); return v ? v[2] : null; }, close_notification: function () { var d = new Date(); d.setTime(d.getTime() + this.age * 1000); document.cookie = this.cookie_name + "=1;domain=" + this.domain + ";path=/;expires=" + d.toUTCString() + ";SameSite=Lax"; $(".update-news--topic.free_trial7days").hide(); // <-- ซ่อนเฉพาะของ forum }, show_notification: function () { // กันซ้ำ if ( document.getElementById("draggable-pc") || document.getElementById("draggable-mobile") ) return; var tpl = '\
\ '; $("body").append(tpl); // ทำให้กล่องเดสก์ท็อปลากได้ทันทีหลัง append this.init_desktop_drag(); // ทำให้กล่องมือถือรองรับลากแบบ custom this.init_mobile_drag(); }, check_show_notification: function () { // ตรวจสอบประเทศ ถ้ามี USER_COUNTRY และไม่ใช่ TH จะไม่แสดง sticky if (typeof window.USER_COUNTRY !== "undefined" && window.USER_COUNTRY !== "TH") { return false; } // [แก้ไขแล้ว] ใช้ getTime() ตรงๆ เพื่อนำ Timestamp แบบ UTC มาเทียบกันได้เลย var now = new Date().getTime(); var begin = new Date(this.date_start).getTime(); var end = new Date(this.date_end).getTime(); if (now >= begin && now <= end) { return !this.get_cookie_notification(); } return false; }, bind_events: function () { // ปุ่มปิด $(document).on("click", ".update--btnclose", (e) => { e.preventDefault(); this.close_notification(); $(".update-news--topic").fadeOut(150); }); // คลิกลิงก์ใน sticky -> ตั้งคุกกี้ + ซ่อน + ปล่อยเบราเซอร์ทำงานต่อ $(document).on("click", ".update--imgnoti", (e) => { const a = e.currentTarget; const href = a.getAttribute("href"); const target = (a.getAttribute("target") || "").toLowerCase(); this.close_notification(); $(".update-news--topic").fadeOut(150); if (target === "_blank") { // ปล่อยเปิดแท็บใหม่ตามปกติ return; } e.preventDefault(); setTimeout(() => { window.location.href = href; }, 0); }); // คลิกพื้นที่กล่อง (ยกเว้นปุ่มปิด) -> ทำเหมือนคลิกลิงก์ $(document).on("click", "#draggable-pc, #draggable-mobile", (e) => { if ($(e.target).closest(".update--btnclose").length) return; const $a = $(e.currentTarget).find(".update--imgnoti").first(); if (!$a.length) return; const href = $a.attr("href"); const target = ($a.attr("target") || "").toLowerCase(); this.close_notification(); $(".update-news--topic").fadeOut(150); if (target === "_blank") { window.open(href, "_blank"); } else { window.location.href = href; } }); }, init_desktop_drag: function () { const $el = $("#draggable-pc"); if (!$el.length) return; // ถ้ามี jQuery UI -> ใช้ draggable if ($.fn && typeof $.fn.draggable === "function") { $el.draggable({ containment: "window", cancel: ".update--btnclose", // กดปิด = ไม่ลาก distance: 3, }); return; } // Fallback: ลากด้วยเมาส์ (เดสก์ท็อป) หากไม่มี jQuery UI let isDown = false, offX = 0, offY = 0; const start = (x, y, target) => { if ($(target).closest(".update--btnclose").length) return; // ไม่เริ่มลากถ้ากดปุ่มปิด const rect = $el[0].getBoundingClientRect(); offX = x - rect.left; offY = y - rect.top; isDown = true; // ตั้งค่าให้วางด้วย left/top $el.css({ left: rect.left + "px", top: rect.top + "px", right: "auto", bottom: "auto", }); }; const move = (x, y) => { if (!isDown) return; const maxL = window.innerWidth - $el.outerWidth(); const maxT = window.innerHeight - $el.outerHeight(); const left = Math.max(0, Math.min(x - offX, maxL)); const top = Math.max(0, Math.min(y - offY, maxT)); $el.css({ left: left + "px", top: top + "px" }); }; const end = () => { isDown = false; }; $el.on("mousedown", function (e) { start(e.clientX, e.clientY, e.target); }); $(document).on("mousemove", function (e) { move(e.clientX, e.clientY); }); $(document).on("mouseup", end); }, init_mobile_drag: function () { const el = document.getElementById("draggable-mobile"); if (!el) return; let offsetX = 0, offsetY = 0, isDragging = false; const startDrag = (x, y, target) => { // ไม่เริ่มลากถ้ากดปุ่มปิด if (target.closest && target.closest(".update--btnclose")) return; const rect = el.getBoundingClientRect(); offsetX = x - rect.left; offsetY = y - rect.top; isDragging = true; // เปลี่ยนเป็นใช้ left/top el.style.left = rect.left + "px"; el.style.top = rect.top + "px"; el.style.right = "auto"; el.style.bottom = "auto"; }; const onMove = (x, y) => { if (!isDragging) return; const maxL = window.innerWidth - el.offsetWidth; const maxT = window.innerHeight - el.offsetHeight; const newLeft = Math.max(0, Math.min(x - offsetX, maxL)); const newTop = Math.max(0, Math.min(y - offsetY, maxT)); el.style.left = newLeft + "px"; el.style.top = newTop + "px"; }; const endDrag = () => { isDragging = false; }; // mouse (รองรับ tablet ที่ใช้ pointer แบบ mouse) el.addEventListener("mousedown", (e) => startDrag(e.clientX, e.clientY, e.target), ); document.addEventListener("mousemove", (e) => onMove(e.clientX, e.clientY), ); document.addEventListener("mouseup", endDrag); // touch el.addEventListener( "touchstart", (e) => { const t = e.touches[0]; startDrag(t.clientX, t.clientY, e.target); }, { passive: false }, ); document.addEventListener( "touchmove", (e) => { if (!isDragging) return; const t = e.touches[0]; onMove(t.clientX, t.clientY); e.preventDefault(); }, { passive: false }, ); document.addEventListener("touchend", endDrag); }, set_notification: function () { if (this.check_show_notification()) { this.show_notification(); this.bind_events(); } }, }; var path = (window.location.pathname || "/").replace(/\/+$/, "") || "/"; // แสดงเฉพาะหน้า Home, /subscriptions และ /digital-subscription if ( path === "/" || path === "/subscriptions" || path === "/digital-subscription" ) { is_show_notification_free_trial7days.set_notification(); } });