326 lines
13 KiB
JavaScript
326 lines
13 KiB
JavaScript
![]() |
"use strict";
|
|||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|||
|
exports.ECalendarSelectMode = void 0;
|
|||
|
var tslib_1 = require("tslib");
|
|||
|
var dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
|||
|
var calendar_1 = tslib_1.__importDefault(require("./calendar"));
|
|||
|
var ECalendarSelectMode;
|
|||
|
(function (ECalendarSelectMode) {
|
|||
|
ECalendarSelectMode["single"] = "single";
|
|||
|
ECalendarSelectMode["range"] = "range";
|
|||
|
})(ECalendarSelectMode = exports.ECalendarSelectMode || (exports.ECalendarSelectMode = {}));
|
|||
|
var Calendar = /** @class */ (function () {
|
|||
|
function Calendar(params) {
|
|||
|
var min = params.min, max = params.max, _a = params.disableDates, disableDates = _a === void 0 ? [] : _a, selectionMode = params.selectionMode, customDateList = params.customDateList, defaultValue = params.defaultValue, defaultRange = params.defaultRange, showlunar = params.showlunar;
|
|||
|
if (!min || !max) {
|
|||
|
min = (0, dayjs_1.default)().date(1).format("YYYY-MM-DD");
|
|||
|
max = (0, dayjs_1.default)().add(1, 'M').date(1).subtract(1, 'd').format("YYYY-MM-DD");
|
|||
|
}
|
|||
|
this.showlunar = showlunar;
|
|||
|
this.calendarList = [];
|
|||
|
this.customDateList = customDateList;
|
|||
|
this.showToday = true;
|
|||
|
this.selectionMode = selectionMode;
|
|||
|
// 范围开始
|
|||
|
this.min = min;
|
|||
|
// 范围结束
|
|||
|
this.max = max;
|
|||
|
// 禁止日期列表
|
|||
|
this.disableDates = disableDates;
|
|||
|
this.selectEndDate = null;
|
|||
|
this.selectStartDate = null;
|
|||
|
this.selectDateList = [];
|
|||
|
if (selectionMode === ECalendarSelectMode.range && (defaultRange === null || defaultRange === void 0 ? void 0 : defaultRange.length) === 2) {
|
|||
|
this.selectStartDate = (0, dayjs_1.default)(defaultRange[0]);
|
|||
|
this.selectEndDate = (0, dayjs_1.default)(defaultRange[1]);
|
|||
|
this.selectDateList = this.getSelectDateList(defaultRange[0], defaultRange[1]);
|
|||
|
}
|
|||
|
else if (selectionMode === ECalendarSelectMode.single && defaultValue) {
|
|||
|
this.selectStartDate = (0, dayjs_1.default)(defaultValue);
|
|||
|
}
|
|||
|
}
|
|||
|
Calendar.prototype.updateStartEndDate = function (start, end) {
|
|||
|
if (!start || !end)
|
|||
|
return;
|
|||
|
this.min = start;
|
|||
|
this.max = end;
|
|||
|
};
|
|||
|
Calendar.prototype.updateSelectDate = function (start, end) {
|
|||
|
this.selectStartDate = start;
|
|||
|
this.selectEndDate = end;
|
|||
|
this.selectDateList = this.getSelectDateList(start, end);
|
|||
|
};
|
|||
|
Calendar.prototype.updateDisableDates = function (dates) {
|
|||
|
this.disableDates = dates;
|
|||
|
};
|
|||
|
Calendar.prototype.updateCustomDateList = function (dates) {
|
|||
|
this.customDateList = dates;
|
|||
|
};
|
|||
|
Calendar.prototype.getSelectDateList = function (start, end) {
|
|||
|
if (!start || !end) {
|
|||
|
return [];
|
|||
|
}
|
|||
|
var list = this.getRangeAllDates(start, end);
|
|||
|
return list;
|
|||
|
};
|
|||
|
/**
|
|||
|
* 获取日期范围内所有日期
|
|||
|
* @param {Object} begin
|
|||
|
* @param {Object} end
|
|||
|
*/
|
|||
|
Calendar.prototype.getRangeAllDates = function (begin, end) {
|
|||
|
begin = (0, dayjs_1.default)(begin, "YYYY-MM-DD");
|
|||
|
end = (0, dayjs_1.default)(end, "YYYY-MM-DD");
|
|||
|
var dates = [];
|
|||
|
while (!(0, dayjs_1.default)(begin).isAfter(end, 'dates')) {
|
|||
|
dates.push(begin.format("YYYY-MM-DD"));
|
|||
|
begin = begin.add(1, 'day');
|
|||
|
}
|
|||
|
return dates;
|
|||
|
};
|
|||
|
Calendar.prototype.calculateCalendarList = function () {
|
|||
|
var _a = this, min = _a.min, max = _a.max;
|
|||
|
var calendarList = [];
|
|||
|
var posDateStr = (0, dayjs_1.default)(min).format('YYYY-MM-01');
|
|||
|
while (!(0, dayjs_1.default)(posDateStr).isAfter((0, dayjs_1.default)(max), 'dates')) {
|
|||
|
this.setDate(posDateStr);
|
|||
|
var entry = {
|
|||
|
monthStartDate: (0, dayjs_1.default)(posDateStr).format('YYYY-MM-01'),
|
|||
|
monthDisplay: (0, dayjs_1.default)(posDateStr).format('YYYY年MM月'),
|
|||
|
weeks: this._getWeekList(posDateStr).weeks,
|
|||
|
};
|
|||
|
calendarList.push(entry);
|
|||
|
posDateStr = (0, dayjs_1.default)(posDateStr)
|
|||
|
.add(1, 'month')
|
|||
|
.format('YYYY-MM-01');
|
|||
|
}
|
|||
|
this.calendarList = calendarList;
|
|||
|
};
|
|||
|
/**
|
|||
|
* 设置日期
|
|||
|
* @param {Object} date
|
|||
|
*/
|
|||
|
Calendar.prototype.setDate = function (date) {
|
|||
|
this._getWeekList(date);
|
|||
|
};
|
|||
|
Calendar.prototype.getDateInfo = function (date, days, str) {
|
|||
|
if (days === void 0) { days = 0; }
|
|||
|
if (str === void 0) { str = 'day'; }
|
|||
|
if (!date) {
|
|||
|
date = new Date();
|
|||
|
}
|
|||
|
if (typeof date !== 'object') {
|
|||
|
date = date.replace(/-/g, '/');
|
|||
|
}
|
|||
|
var dd = new Date(date);
|
|||
|
switch (str) {
|
|||
|
case 'day':
|
|||
|
dd.setDate(dd.getDate() + days); // 获取days天后的日期
|
|||
|
break;
|
|||
|
case 'month':
|
|||
|
if (dd.getDate() === 31) {
|
|||
|
dd.setDate(dd.getDate() + days);
|
|||
|
}
|
|||
|
else {
|
|||
|
dd.setMonth(dd.getMonth() + days); // 获取days天后的日期
|
|||
|
}
|
|||
|
break;
|
|||
|
case 'year':
|
|||
|
dd.setFullYear(dd.getFullYear() + days); // 获取days天后的日期
|
|||
|
break;
|
|||
|
}
|
|||
|
var y = dd.getFullYear();
|
|||
|
var m = dd.getMonth() + 1 < 10 ? "0".concat(dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期,不足10补0
|
|||
|
var d = dd.getDate() < 10 ? "0".concat(dd.getDate()) : dd.getDate(); // 获取当前几号,不足10补0
|
|||
|
return {
|
|||
|
fullDate: "".concat(y, "-").concat(m, "-").concat(d),
|
|||
|
year: y,
|
|||
|
month: m,
|
|||
|
date: d,
|
|||
|
day: dd.getDay()
|
|||
|
};
|
|||
|
};
|
|||
|
Calendar.prototype._getWeekList = function (date) {
|
|||
|
var displayInfo = this.getDateInfo(date);
|
|||
|
var year = displayInfo.year, month = displayInfo.month;
|
|||
|
var firstDay = new Date(+year, +month - 1, 0).getDay();
|
|||
|
var curMonthDays = new Date(+year, +month, 0).getDate();
|
|||
|
var dates = {
|
|||
|
lastMonthDays: this._getLastMonthDays(firstDay, displayInfo),
|
|||
|
currentMonthDays: this._currentMonthDays(curMonthDays, displayInfo),
|
|||
|
nextMonthDays: [],
|
|||
|
weeks: []
|
|||
|
};
|
|||
|
var calender = [];
|
|||
|
var surplus = (dates.lastMonthDays.length + dates.currentMonthDays.length) % 7;
|
|||
|
if (surplus) {
|
|||
|
surplus = 7 - surplus;
|
|||
|
}
|
|||
|
dates.nextMonthDays = this._getNextMonthDays(surplus, displayInfo);
|
|||
|
calender = calender.concat(dates.lastMonthDays, dates.currentMonthDays, dates.nextMonthDays);
|
|||
|
var weeks = [];
|
|||
|
// 拼接数组 上个月开始几天 + 本月天数+ 下个月开始几天
|
|||
|
for (var i = 0; i < calender.length; i++) {
|
|||
|
if (i % 7 === 0) {
|
|||
|
weeks[parseInt("".concat(i / 7))] = new Array(7);
|
|||
|
}
|
|||
|
weeks[parseInt("".concat(i / 7))][i % 7] = calender[i];
|
|||
|
}
|
|||
|
weeks.forEach(function (week) {
|
|||
|
week.forEach(function (item, i) {
|
|||
|
if (item.isRangeStart || item.isRangeEnd) {
|
|||
|
item.showBorderRadiusLeft = true;
|
|||
|
item.showBorderRadiusRight = true;
|
|||
|
return;
|
|||
|
}
|
|||
|
if (i == 0 && item.isRangeArea) {
|
|||
|
item.showBorderRadiusLeft = true;
|
|||
|
}
|
|||
|
else if (i == week.length - 1) {
|
|||
|
item.showBorderRadiusRight = true;
|
|||
|
}
|
|||
|
});
|
|||
|
});
|
|||
|
return {
|
|||
|
calender: calender,
|
|||
|
weeks: weeks,
|
|||
|
displayMonth: date
|
|||
|
};
|
|||
|
};
|
|||
|
/**
|
|||
|
* 获取上月剩余天数
|
|||
|
*/
|
|||
|
Calendar.prototype._getLastMonthDays = function (firstDay, full) {
|
|||
|
var dateArr = [];
|
|||
|
for (var i = firstDay; i > 0; i--) {
|
|||
|
var posDate = new Date(full.year, full.month - 1, -i + 1);
|
|||
|
var dateValue = posDate.getDate();
|
|||
|
var dayValue = posDate.getDay();
|
|||
|
dateArr.push({
|
|||
|
year: full.year,
|
|||
|
date: dateValue,
|
|||
|
month: full.month - 1,
|
|||
|
day: dayValue,
|
|||
|
lunar: this.getlunar(full.year, full.month - 1, dateValue),
|
|||
|
disable: true,
|
|||
|
hide: true
|
|||
|
});
|
|||
|
}
|
|||
|
return dateArr;
|
|||
|
};
|
|||
|
/**
|
|||
|
* 计算阴历日期显示
|
|||
|
*/
|
|||
|
Calendar.prototype.getlunar = function (year, month, date) {
|
|||
|
return calendar_1.default.solar2lunar(year, month, date);
|
|||
|
};
|
|||
|
/**
|
|||
|
* 获取本月天数
|
|||
|
*/
|
|||
|
Calendar.prototype._currentMonthDays = function (daycount, full) {
|
|||
|
var _this = this;
|
|||
|
var _a;
|
|||
|
var dateArr = [];
|
|||
|
var nowFullDate = this.getDateInfo(undefined).fullDate;
|
|||
|
var _loop_1 = function (i) {
|
|||
|
var posDate = "".concat(full.year, "-").concat(full.month < 10 ? full.month : full.month, "-").concat(i < 10 ? '0' + i : i);
|
|||
|
// 是否今天
|
|||
|
var isToday = nowFullDate === posDate;
|
|||
|
// 日期禁用
|
|||
|
var beforeStartDate = this_1.min ? (0, dayjs_1.default)(posDate).isBefore(this_1.min, 'dates') : false;
|
|||
|
var afterEndDate = this_1.max ? (0, dayjs_1.default)(posDate).isAfter(this_1.max, 'dates') : false;
|
|||
|
var selectDateList = this_1.selectDateList;
|
|||
|
var selectIndex = -1;
|
|||
|
var checked = false;
|
|||
|
var isRangeStart = false;
|
|||
|
var isRangeEnd = false;
|
|||
|
if (selectDateList && selectDateList.length > 0) {
|
|||
|
selectIndex = selectDateList.findIndex(function (item) {
|
|||
|
return _this.dateEqual(item, posDate);
|
|||
|
});
|
|||
|
}
|
|||
|
if (selectIndex !== -1) {
|
|||
|
checked = true;
|
|||
|
}
|
|||
|
if (selectDateList.length > 0) {
|
|||
|
if (selectIndex === 0) {
|
|||
|
isRangeStart = true;
|
|||
|
}
|
|||
|
if (selectIndex === selectDateList.length - 1) {
|
|||
|
isRangeEnd = true;
|
|||
|
}
|
|||
|
}
|
|||
|
else if (this_1.dateEqual(this_1.selectStartDate, posDate)) {
|
|||
|
isRangeStart = true;
|
|||
|
}
|
|||
|
// 判断日期是否在禁用日期列表中
|
|||
|
var disabled = this_1.disableDates.find(function (item) {
|
|||
|
return _this.dateEqual(item, posDate);
|
|||
|
});
|
|||
|
var data = {
|
|||
|
fullDate: posDate,
|
|||
|
year: full.year,
|
|||
|
day: (0, dayjs_1.default)(posDate).day(),
|
|||
|
date: i,
|
|||
|
month: full.month,
|
|||
|
lunar: this_1.showlunar && this_1.getlunar(full.year, full.month, i),
|
|||
|
disable: beforeStartDate || afterEndDate || !!disabled,
|
|||
|
hide: false,
|
|||
|
isToday: isToday,
|
|||
|
isRangeArea: this_1.selectionMode === ECalendarSelectMode.range ? checked : false,
|
|||
|
isRangeStart: isRangeStart,
|
|||
|
isRangeEnd: isRangeEnd,
|
|||
|
isSingleSelect: this_1.selectionMode === ECalendarSelectMode.single && isRangeStart,
|
|||
|
showToday: false,
|
|||
|
tag: null
|
|||
|
};
|
|||
|
// if (dayjs(posDate).isBefore(nowFullDate)) {
|
|||
|
// data.disable = true
|
|||
|
// }
|
|||
|
if (isToday) {
|
|||
|
data.showToday = this_1.showToday;
|
|||
|
}
|
|||
|
var tagItem = (_a = this_1.customDateList) === null || _a === void 0 ? void 0 : _a.find(function (item) {
|
|||
|
return (0, dayjs_1.default)(item.date).format('YYYY-MM-DD') === posDate;
|
|||
|
});
|
|||
|
if (tagItem) {
|
|||
|
data.tag = tagItem;
|
|||
|
}
|
|||
|
dateArr.push(data);
|
|||
|
};
|
|||
|
var this_1 = this;
|
|||
|
for (var i = 1; i <= daycount; i++) {
|
|||
|
_loop_1(i);
|
|||
|
}
|
|||
|
return dateArr;
|
|||
|
};
|
|||
|
/**
|
|||
|
* 比较时间是否相等
|
|||
|
*/
|
|||
|
Calendar.prototype.dateEqual = function (before, after) {
|
|||
|
before = (0, dayjs_1.default)(before, "YYYY-MM-DD");
|
|||
|
after = (0, dayjs_1.default)(after, "YYYY-MM-DD");
|
|||
|
return before.isSame(after, 'dates');
|
|||
|
};
|
|||
|
/**
|
|||
|
* 获取下月天数
|
|||
|
*/
|
|||
|
Calendar.prototype._getNextMonthDays = function (surplus, full) {
|
|||
|
var dateArr = [];
|
|||
|
for (var i = 1; i < surplus + 1; i++) {
|
|||
|
var month = Number(full.month) + 1;
|
|||
|
var posDate = (0, dayjs_1.default)("".concat(full.year, "-").concat(month, "-").concat(i));
|
|||
|
dateArr.push({
|
|||
|
date: i,
|
|||
|
day: posDate.day(),
|
|||
|
month: month,
|
|||
|
lunar: this.getlunar(full.year, Number(full.month) + 1, i),
|
|||
|
disable: true,
|
|||
|
hide: true
|
|||
|
});
|
|||
|
}
|
|||
|
return dateArr;
|
|||
|
};
|
|||
|
return Calendar;
|
|||
|
}());
|
|||
|
exports.default = Calendar;
|