✏️ 正在编辑: admin-booking.js
路径:
/home/h359620/public_html/wp-content/themes/datis/assets/js/admin-booking.js
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
jQuery(function ($) { const $schedule = $("#datis_cb_schedule_id"); const $date = $("#datis_cb_date"); // hidden stored value: Gregorian YYYY-MM-DD const $time = $("#datis_cb_time"); function getCalendarMode() { if (window.DatisAdminBooking && DatisAdminBooking.calendarMode) return DatisAdminBooking.calendarMode; return "gregorian"; } // ----------------------- // Date helpers // ----------------------- function pad2(n){ return String(n).padStart(2,'0'); } function formatGregorianYMD(dateObj) { const d = (dateObj instanceof Date) ? dateObj : new Date(dateObj); if (isNaN(d.getTime())) return ""; return `${d.getFullYear()}-${pad2(d.getMonth()+1)}-${pad2(d.getDate())}`; } function parseGregorianYMD(ymd) { ymd = (ymd || "").trim(); const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(ymd); if (!m) return null; const dt = new Date(parseInt(m[1],10), parseInt(m[2],10)-1, parseInt(m[3],10)); return isNaN(dt.getTime()) ? null : dt; } function ensureDisplayInputForDate($stored) { let $display = $stored.prev(".datis-cs-date-display"); if ($display.length) return $display; // Display input (clickable) - picker attaches here $display = $('<input type="text" class="datis-cs-date-display" readonly autocomplete="off" />'); $display.css({ width: "100%", maxWidth: "260px", cursor: "pointer" }); // Put display before hidden stored input $stored.before($display); // Hide stored input but keep it in form submit $stored.css({ position: "absolute", left: "-9999px", width: "1px", height: "1px" }) .prop("readonly", true) .attr("autocomplete", "off"); return $display; } function setDisplayValue($display, gYMD) { const mode = (getCalendarMode() === "jalali") ? "jalali" : "gregorian"; const dt = parseGregorianYMD(gYMD); if (!dt) { $display.val(""); return; } if (mode === "jalali" && window.moment && window.moment.fn && typeof window.moment.fn.jYear === "function") { try { if (typeof window.moment.loadPersian === "function") { window.moment.loadPersian({ usePersianDigits: false }); } $display.val(window.moment(dt).format("jYYYY/jMM/jDD")); return; } catch(e) {} } $display.val(gYMD); } // ----------------------- // DatePicker init // ----------------------- function initBookingDatePicker() { if (!$date.length) return; const mode = (getCalendarMode() === "jalali") ? "jalali" : "gregorian"; const $display = ensureDisplayInputForDate($date); // Initial render from stored hidden value setDisplayValue($display, ($date.val() || "").trim()); // If MDS picker exists, attach it to display input if (window.mds && typeof window.mds.MdsPersianDateTimePicker === "function") { if ($display.data("mdpInited")) return; $display.data("mdpInited", true); new window.mds.MdsPersianDateTimePicker($display[0], { placement: "bottom", modalMode: false, enableTimePicker: false, // UI follows calendar mode isGregorian: (mode === "gregorian"), textFormat: "YYYY/MM/DD", dateFormat: "YYYY/MM/DD", // IMPORTANT: use setting.selectedDate (stable) instead of reading input value onDayClick: function (setting) { try { if (setting && setting.selectedDate) { const g = formatGregorianYMD(setting.selectedDate); // Store Gregorian in hidden field $date.val(g).trigger("change"); // Show date in display field based on mode setDisplayValue($display, g); } } catch (e) { $date.val("").trigger("change"); $display.val(""); } } }); // Open picker on click $display.on("click", function () { $(this).trigger("focus"); }); return; } // Fallback (no picker): allow manual input on hidden field $display.prop("readonly", false); } // ----------------------- // Times fetching (keep selected) // ----------------------- function clearTimes(keepValue) { $time.empty().append($("<option>", { value: "", text: "— Select —" })); if (keepValue) { $time.append($("<option>", { value: keepValue, text: keepValue, selected: true })); } } function fetchTimes() { const scheduleId = parseInt($schedule.val(), 10) || 0; const dateVal = ($date.val() || "").trim(); const current = ($time.val() || "").trim(); // Keep current selection while loading clearTimes(current); if (!scheduleId || !dateVal) return; $.post(DatisAdminBooking.ajaxurl, { action: DatisAdminBooking.actions.getTimes, nonce: (DatisAdminBooking.ajaxNonce || DatisAdminBooking.nonce || ""), schedule_id: scheduleId, date: dateVal }) .done(function (res) { let times = []; if (res && res.success && res.data && Array.isArray(res.data.times)) times = res.data.times; $time.empty().append($("<option>", { value: "", text: "— Select —" })); if (!times.length) { if (current) { $time.append($("<option>", { value: current, text: current, selected: true })); $time.val(current); } else { $time.append($("<option>", { value: "", text: "No available times" })); } return; } let hasCurrent = false; times.forEach(function (t) { if (typeof t === "string") { if (t === current) hasCurrent = true; $time.append($("<option>", { value: t, text: t })); return; } if (t && t.time) { if (t.time === current) hasCurrent = true; const txt = t.time + (t.label ? " - " + t.label : "") + (t.left != null ? " (ظرفیت باقی مانده: " + t.left + ")" : ""); $time.append($("<option>", { value: t.time, text: txt })); } }); if (current && hasCurrent) { $time.val(current); } else if (current && !hasCurrent) { $time.append($("<option>", { value: current, text: current, selected: true })); $time.val(current); } }) .fail(function () { $time.empty().append($("<option>", { value: "", text: "— Select —" })); if (current) { $time.append($("<option>", { value: current, text: current, selected: true })); $time.val(current); } else { $time.append($("<option>", { value: "", text: "Error loading times" })); } }); } // Init initBookingDatePicker(); $schedule.on("change", fetchTimes); $date.on("change blur", fetchTimes); fetchTimes(); });
💾 保存文件
← 返回文件管理器