From 1acb4dc2a2e7b1dce6ae48a81bfa806359147222 Mon Sep 17 00:00:00 2001 From: sight <26325820+Sight-wcg@users.noreply.github.com> Date: Wed, 17 Jan 2024 08:51:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(laydate):=20=E4=BC=98=E5=8C=96=E5=B9=B4?= =?UTF-8?q?=E3=80=81=E6=9C=88=E9=9D=A2=E6=9D=BF=E7=9A=84=E7=A6=81=E7=94=A8?= =?UTF-8?q?=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/laydate.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/modules/laydate.js b/src/modules/laydate.js index dc69c6e3..2976832e 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -1108,17 +1108,31 @@ var that = this; var options = that.config; - var position = options.range ? (opts.rangeType === 0 ? 'start' : 'end') : 'start' + var position = options.range ? (opts.rangeType === 0 ? 'start' : 'end') : 'start'; + var ONE_DAY = 24 * 60 * 60 * 1000; + + var isDisabledYearOrMonth = function(date, type){ + var startDay = type === 'year' ? new Date(date.year, 0, 1) : new Date(date.year, date.month, 1); // startOfYear/startOfMonth + var endDay = type === 'year' ? new Date(date.year, 11, 31) : new Date(date.year, date.month + 1, 0); // endOfYear/endOfMonth + var numOfDays = Math.floor((endDay.getTime() - startDay.getTime()) / ONE_DAY) + 1; + var disabledDays = 0; + for(var i = 0; i < numOfDays; i++){ + if(options.disabledDate.call(options, getFutureDate(startDay, i), position)){ + disabledDays++; + } + } + + return disabledDays === numOfDays; + } var isDisabledDate = function(date){ if(!options.disabledDate) return; if(options.type === 'time')return; if(!(opts.disabledType === 'date' || opts.disabledType === 'datetime'))return; - // 切换到年月面板时不检测(更好的方案是检测面板年/月中所有的日期,以判断是否禁用当前的年/月) - if(!(options.type === 'year' || options.type === 'month') && (opts.type === 'year' || opts.type === 'month'))return; - if(options.type === 'month' && opts.type === 'year')return; - return options.disabledDate.call(options, that.newDate({year: date.year, month: date.month, date: date.date}), position); + return opts.type === 'year' || opts.type === 'month' + ? isDisabledYearOrMonth(date, opts.type) + : options.disabledDate.call(options, that.newDate({year: date.year, month: date.month, date: date.date}), position); } var isDisabledItem = function(val, rangeFn){ @@ -1129,9 +1143,7 @@ if(!options.disabledTime) return; if(!(options.type === "time" || options.type === "datetime")) return; if(!(opts.disabledType === 'time' || opts.disabledType === 'datetime'))return; - var disabledTime = options.disabledTime.length <= 2 - ? options.disabledTime.call(options, that.newDate(date), position) - : options.disabledTime.call(options, date.hours, date.minutes, date.seconds, position); + var disabledTime = options.disabledTime.call(options, that.newDate(date), position); return opts.disabledType === 'datetime' ? isDisabledItem(date.hours, disabledTime.disabledHours) @@ -1142,6 +1154,12 @@ isDisabledItem(date.seconds, disabledTime.disabledSeconds)][opts.time.length - 1]; } + function getFutureDate(date, days){ + var futureDate = new Date(date); + futureDate.setDate(futureDate.getDate() + days); + return futureDate; + } + var datetimeObj = that.systemDate(new Date(currentDataTime)) return isDisabledDate(datetimeObj) || isDisabledTime(datetimeObj); }