" +
"" :
"";
html +=
'
";
}
$("#riliData").html(html);
// 销毁之前的 Swiper
if (window.mySwiperxsdata) {
window.mySwiperxsdata.destroy(true, true);
window.mySwiperxsdata = null;
}
// 有数据时重新初始化 Swiper
if (dayData.length > 0) {
window.mySwiperxsdata = new Swiper(
".calendar-article-left .swiper-container",
{
speed: 1200,
preventClicksPropagation: false,
touchMoveStopPropagation: true,
paginationClickable: true,
autoplayDisableOnInteraction: false,
nextButton:
".calendar-article-left .swiper-button-next",
prevButton:
".calendar-article-left .swiper-button-prev"
}
);
refreshAcademicSwiper();
}
}
function refreshAcademicSwiper() {
if (
window.mySwiperxsdata &&
typeof window.mySwiperxsdata.update === "function"
) {
window.mySwiperxsdata.update(true);
}
}
// 初始化时加载当天,并把 cur 放到当天上
function getInitialDate(calendar) {
if (calendarInitialized) {
return;
}
if (typeof calUtil === "undefined") {
return;
}
var targetTd = getDefaultCalendarTd();
if (!targetTd) {
setCalendarCur(null);
initData([]);
return;
}
var date = getTdDate(targetTd);
if (date) {
calendarInitialized = true;
setCalendarCur(targetTd);
handleCalendarDate(date, "initToday");
}
}
// 初始化日历监听
function initCalendarListener() {
if (calendarListenerInited) {
return true;
}
var calendar = document.getElementById("calendar");
if (!calendar) {
return false;
}
calendarListenerInited = true;
// 监听日历 AJAX 加载后的重新渲染
calendarObserver = new MutationObserver(function () {
setTimeout(function () {
if (currentSelectedDate) {
setCalendarCurByDate(currentSelectedDate);
}
getInitialDate(calendar);
}, 0);
});
calendarObserver.observe(calendar, {
childList: true,
subtree: true
});
// 日历已经渲染时直接尝试初始化
getInitialDate(calendar);
/*
* 捕获阶段处理日期点击。
* 没有 on 类的日期会在组件原点击事件执行前被拦截。
*/
calendar.addEventListener("click", function (event) {
var target = event.target;
var td = null;
// 向上查找被点击的 td
while (target && target !== calendar) {
if (
target.tagName &&
target.tagName.toLowerCase() === "td"
) {
td = target;
break;
}
target = target.parentNode;
}
// 点击的不是日期,例如月份切换按钮
if (!td) {
return;
}
// 没有on类,不允许点击
if (!$(td).hasClass("on")) {
event.preventDefault();
event.stopPropagation();
if (event.stopImmediatePropagation) {
event.stopImmediatePropagation();
}
return false;
}
// 点击td.on,获取完整日期
var selectedDate = getTdDate(td);
if (selectedDate) {
currentSelectedDate = selectedDate;
setCalendarCur(td);
handleCalendarDate(
selectedDate,
"selectOnDay"
);
setTimeout(function () {
setCalendarCurByDate(selectedDate);
}, 0);
}
}, true);
return true;
}
function startCalendarListener() {
if (initCalendarListener()) {
return;
}
var tryCount = 0;
var timer = setInterval(function () {
tryCount++;
if (initCalendarListener() || tryCount > 100) {
clearInterval(timer);
}
}, 100);
}
$(function () {
startCalendarListener();
});
// 切换到上一个月
$(document)
.off("click.externalCalendar", ".calendar_month_prev")
.on(
"click.externalCalendar",
".calendar_month_prev",
function () {
/*
* 等待文章日历组件完成月份切换和重新渲染,
* 再读取calUtil中的新年月。
*/
setTimeout(function () {
var targetTd = getDefaultCalendarTd();
var selectedDate = targetTd ?
getTdDate(targetTd) :
null;
currentSelectedDate = selectedDate;
if (targetTd) {
setCalendarCur(targetTd);
}
if (selectedDate) {
handleCalendarDate(selectedDate, "prevMonth");
} else {
selectedDate = getCurrentCalendarDate();
currentSelectedDate = selectedDate;
setCalendarCur(null);
if (selectedDate) {
handleCalendarDate(selectedDate, "prevMonth");
}
}
}, 0);
}
);
// 切换到下一个月
$(document)
.off("click.externalCalendar", ".calendar_month_next")
.on(
"click.externalCalendar",
".calendar_month_next",
function () {
setTimeout(function () {
var targetTd = getDefaultCalendarTd();
var selectedDate = targetTd ?
getTdDate(targetTd) :
null;
currentSelectedDate = selectedDate;
if (targetTd) {
setCalendarCur(targetTd);
}
if (selectedDate) {
handleCalendarDate(selectedDate, "nextMonth");
} else {
selectedDate = getCurrentCalendarDate();
currentSelectedDate = selectedDate;
setCalendarCur(null);
if (selectedDate) {
handleCalendarDate(selectedDate, "nextMonth");
}
}
}, 0);
}
);
})(jQuery);