From c9d3e6e00eebc4509f235a4f22e07b6e968760f5 Mon Sep 17 00:00:00 2001 From: sunxb <470459819@qq.com> Date: Thu, 10 Nov 2022 15:54:22 +0800 Subject: [PATCH 01/13] =?UTF-8?q?laydate=E4=BF=AE=E5=A4=8D=E5=87=A0?= =?UTF-8?q?=E4=B8=AA=E5=B0=8F=E9=97=AE=E9=A2=98=E4=BB=A5=E5=8F=8A=E6=96=B0?= =?UTF-8?q?=E5=A2=9EcalendarLinkage=E9=85=8D=E7=BD=AE=E8=AE=A9=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E9=80=89=E6=8B=A9=E6=97=A5=E5=8E=86=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E8=81=94=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/css/modules/laydate.css | 4 +- src/modules/laydate.js | 223 +++++++++++++++++++++++++++++++----- 2 files changed, 196 insertions(+), 31 deletions(-) diff --git a/src/css/modules/laydate.css b/src/css/modules/laydate.css index caa7840e..aa5624ca 100644 --- a/src/css/modules/laydate.css +++ b/src/css/modules/laydate.css @@ -98,9 +98,11 @@ html #layuicss-laydate{display: none; position: absolute; width: 1989px;} /* 双日历 */ .layui-laydate-range{width: 546px;} -.layui-laydate-range .layui-laydate-main{display: inline-block; vertical-align: middle;width:50%;} +.layui-laydate-range .layui-laydate-main{display: inline-block; vertical-align: middle;max-width: 50%;} .layui-laydate-range .laydate-main-list-1 .layui-laydate-header, .layui-laydate-range .laydate-main-list-1 .layui-laydate-content{border-left: 1px solid #e2e2e2;} +.layui-laydate-range.layui-laydate-linkage .laydate-main-list-0 .laydate-next-m, .layui-laydate-range.layui-laydate-linkage .laydate-main-list-0 .laydate-next-y, +.layui-laydate-range.layui-laydate-linkage .laydate-main-list-1 .laydate-prev-m, .layui-laydate-range.layui-laydate-linkage .laydate-main-list-1 .laydate-prev-y{display: none;} /* 默认简约主题 */ diff --git a/src/modules/laydate.js b/src/modules/laydate.js index 3cac3a65..074cd7e2 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -245,6 +245,9 @@ that.rangeStr = options.range ? ( typeof options.range === 'string' ? options.range : '-' ) : ''; + + //日期范围的日历面板是否联动 + that.calendarLinkage = !!(options.range && options.calendarLinkage && (options.type === 'date' || options.type === 'datetime')) //若 range 参数为数组,则表示为开始日期和结束日期的 input 对象 if(layui.type(options.range) === 'array'){ @@ -394,6 +397,7 @@ ,"class": [ 'layui-laydate' ,options.range ? ' layui-laydate-range' : '' + ,that.calendarLinkage ? ' layui-laydate-linkage' : '' ,isStatic ? (' '+ ELEM_STATIC) : '' ,options.fullPanel ? ' laydate-theme-fullpanel' : '' // 全面版 // ,options.theme && options.theme !== 'default' && !/^#/.test(options.theme) ? (' laydate-theme-' + options.theme) : '' @@ -559,7 +563,7 @@ lay.extend(dateTime, that.systemDate(layui.type(item) === 'date' ? item : new Date(item))) } - if (type === 'time') { + if (type === 'time' || type === 'datetime') { that[['startTime', 'endTime'][i]] = { hours: dateTime.hours, minutes: dateTime.minutes, @@ -568,10 +572,19 @@ } if (type === 'year' || type === 'month' || type === 'time') { that.listYM[i] = [dateTime.year, dateTime.month + 1]; - that.checkDate('limit').calendar(null,i); + that.checkDate('limit').calendar(null, i); that.list(type, i); } else { - that.checkDate('limit').calendar(null,i); + if (that.calendarLinkage) { + if (i === 0) { // 第一个值作为startDate + that.startDate = lay.extend({}, dateTime); + } else { + that.endState = !!(that.startDate && that.endDate); + that.checkDate('limit').calendar(null, null, 'init'); + } + } else { + that.checkDate('limit').calendar(null, i); + } that.closeList(); } }); @@ -670,7 +683,11 @@ that.checkDate(function(){ elem.remove(); //delete options.dateTime; - //delete that.endDate; + delete that.startDate; + delete that.endDate; + delete that.endState; + delete that.startTime; + delete that.endTime; delete laydate.thisId; typeof options.close === 'function' && options.close(that); }); @@ -815,7 +832,15 @@ checkValid(dateTime); }; - if(fn === 'limit') return checkValid(dateTime), that; + if(fn === 'limit') { + if (options.range) { + checkValid(that.calendarLinkage ? that.startDate : dateTime); // 校验开始时间 + checkValid(that.endDate); // 校验结束时间 + } else { + checkValid(dateTime); + } + return that; + } value = value || options.value; if(typeof value === 'string'){ @@ -857,6 +882,7 @@ lay.each([options.dateTime, that.endDate], function(i, item){ initDate(item, value[i], i); }); + that.startDate = lay.extend({}, options.dateTime); } else { initDate(dateTime, value); } @@ -896,6 +922,9 @@ //校验日期有效数字 checkValid(dateTime); if(options.range) checkValid(that.endDate); + if(that.calendarLinkage) { + that.endState = !!(that.startDate && that.endDate); // 初始化选中范围状态 + } //如果初始值格式错误,则纠正初始值 if(error && value){ @@ -991,10 +1020,9 @@ var timestrap = {} var dateTime = opts.index > (opts.time ? 0 : 41) ? that.endDate : options.dateTime; var isOut; - var thisDateTime = lay.extend({}, dateTime, opts.date || {}); - + lay.each({ - now: thisDateTime + now: lay.extend({}, dateTime, opts.date || {}) ,min: options.min ,max: options.max }, function(key, item){ @@ -1063,7 +1091,9 @@ YMD = that.getAsYM(dateTime.year, dateTime.month, 'sub'); } else if(index_ >= startWeek && index_ < thisMaxDate + startWeek){ st = index_ - startWeek; - st + 1 === dateTime.date && item.addClass(THIS); + if (!that.calendarLinkage) { + st + 1 === dateTime.date && item.addClass(THIS); + } } else { st = index_ - thisMaxDate - startWeek; item.addClass('laydate-day-next'); @@ -1098,9 +1128,18 @@ //初始默认选择器 if(isAlone){ //年、月等独立选择器 if(options.range){ + if (that.calendarLinkage) { + value ? that.endDate = (that.endDate || { + year: dateTime.year + (options.type === 'year' ? 1 : 0) + ,month: dateTime.month + (options.type === 'month' ? 0 : -1) + }) : (that.startDate = that.startDate || { + year: dateTime.year + ,month: dateTime.month + }); + } if(value){ that.listYM = [ - [options.dateTime.year, options.dateTime.month + 1] + [(that.startDate || options.dateTime).year, (that.startDate || options.dateTime).month + 1] ,[that.endDate.year, that.endDate.month + 1] ]; that.list(options.type, 0).list(options.type, 1); @@ -1118,9 +1157,17 @@ } //初始赋值双日历 - if(options.range && type === 'init' && !value){ + if(options.range && type === 'init'){ //执行渲染第二个日历 - that.calendar(that.endDate, 1); + if (that.calendarLinkage) { + var EYM = that.getAsYM(dateTime.year, dateTime.month, index ? 'sub' : null) + that.calendar(lay.extend({}, dateTime, { + year: EYM[0] + ,month: EYM[1] + }), 1 - index); // 渲染另外一个 + } else { + that.calendar(that.endDate, 1); + } } // 通过检测当前有效日期,来设定底部按钮状态 @@ -1147,7 +1194,10 @@ // 重置快捷栏选中状态 lay(that.shortcut).find('li.' + THIS).removeClass(THIS); - + + //标记选择范围 + if(that.calendarLinkage && type !== 'init') that.stampRange(); + return that; }; @@ -1334,7 +1384,17 @@ index: 0 }); } else { //范围选择 - that.endDate[type] = ym; + if(isAlone){ //非date/datetime类型 + that.endDate[type] = ym; + } else { + var YM = type === 'year' + ? that.getAsYM(ym, listYM[1] - 1, 'sub') + : that.getAsYM(listYM[0], ym, 'sub'); + lay.extend(dateTime, { + year: YM[0] + ,month: YM[1] + }); + } } //当为年选择器或者年月选择器 @@ -1350,7 +1410,11 @@ that.list('month', index); } } else { - that.checkDate('limit').calendar(null, index); + if (that.calendarLinkage) { + that.checkDate('limit').calendar(null, 0, 'init'); + } else { + that.checkDate('limit').calendar(null, index); + } that.closeList(); } @@ -1451,6 +1515,9 @@ start = start || options.dateTime; end = end || that.endDate; isOut = that.newDate(start).getTime() > that.newDate(end).getTime(); + if(that.calendarLinkage) { + isOut = !that.endState; + } //如果不在有效日期内,直接禁用按钮,否则比较开始和结束日期 (that.limit({ @@ -1476,7 +1543,7 @@ ? lay.extend({}, that.endDate, that.endTime) : ( options.range - ? lay.extend({}, options.dateTime, that.startTime) + ? lay.extend({}, that.calendarLinkage ? that.startDate : options.dateTime, that.startTime) : options.dateTime) ) ,format = laydate.parse(dateTime, that.format, 1); @@ -1549,7 +1616,7 @@ if(!options.isPreview) return; var elemPreview = lay(that.elem).find('.'+ ELEM_PREVIEW) - ,value = options.range ? (that.endDate ? that.parse() : '') : that.parse(); + ,value = options.range ? ((that.calendarLinkage ? that.endState : that.endDate) ? that.parse() : '') : that.parse(); //显示预览 elemPreview.html(value).css({ @@ -1573,6 +1640,51 @@ } }; + //标记范围内的日期 + Class.prototype.stampRange = function(){ + var that = this + ,options = that.config + ,startTime, endTime + ,tds = lay(that.elem).find('td'); + + if(options.range && !that.endState) lay(that.footer).find(ELEM_CONFIRM).addClass(DISABLED); + // if(!that.endState) return; + + startTime = that.startDate && that.newDate({ + year: that.startDate.year + ,month: that.startDate.month + ,date: that.startDate.date + }).getTime(); + + endTime = that.endState && that.endDate && that.newDate({ + year: that.endDate.year + ,month: that.endDate.month + ,date: that.endDate.date + }).getTime(); + + // if(startTime > endTime) return that.hint(TIPS_OUT); + + lay.each(tds, function(i, item){ + var ymd = lay(item).attr('lay-ymd').split('-') + ,thisTime = that.newDate({ + year: ymd[0] + ,month: ymd[1] - 1 + ,date: ymd[2] + }).getTime(); + lay(item).removeClass(ELEM_SELECTED + ' ' + THIS); + if(thisTime === startTime || thisTime === endTime){ + lay(item).addClass( + lay(item).hasClass(ELEM_PREV) || lay(item).hasClass(ELEM_NEXT) + ? ELEM_SELECTED + : THIS + ); + } + if(thisTime > startTime && thisTime < endTime){ + lay(item).addClass(ELEM_SELECTED); + } + }); + }; + //执行 done/change 回调 Class.prototype.done = function(param, type){ var that = this @@ -1599,8 +1711,20 @@ //选择日期 Class.prototype.choose = function(td, index){ var that = this - ,options = that.config - ,dateTime = that.thisDateTime(index) + ,options = that.config; + + if (that.calendarLinkage) { + if (that.endState || !that.startDate) { + // 重新选择或者第一次选择 + index = 0; + that.endState = false; + } else { + index = 1; + that.endState = true; + } + } + + var dateTime = that.thisDateTime(index) ,tds = lay(that.elem).find('td') ,YMD = td.attr('lay-ymd').split('-'); @@ -1643,7 +1767,22 @@ } } }); - that.calendar(null, index).done(null, 'change'); + if (that.calendarLinkage) { + if (!index) { + that.startDate = lay.extend({}, dateTime); + } + if (that.endState && that.newDate(that.startDate) > that.newDate(that.endDate)) { + // 判断是否反选 + var startDate = that.startDate; + that.startDate = that.endDate; + options.dateTime = lay.extend({}, that.startDate); + that.endDate = startDate; + startDate = that.startTime; + that.startTime = that.endTime; + that.endTime = startDate; + } + } + that.calendar(null, that.calendarLinkage ? null : index, that.calendarLinkage ? 'init' : null).done(null, 'change'); } else if(options.position === 'static'){ //直接嵌套的选中 that.calendar().done().done(null, 'change'); //同时执行 done 和 change 回调 } else if(options.type === 'date'){ @@ -1773,33 +1912,57 @@ return { prevYear: function(){ if(addSubYeay('sub')) return; - dateTime.year--; - that.checkDate('limit').calendar(null, index); - that.done(null, 'change'); + if (that.calendarLinkage) { + options.dateTime.year--; + that.checkDate('limit').calendar(null, null, 'init'); + } else { + dateTime.year--; + that.checkDate('limit').calendar(null, index); + that.done(null, 'change'); + } } ,prevMonth: function(){ + if (that.calendarLinkage) { + dateTime = options.dateTime; + } var YM = that.getAsYM(dateTime.year, dateTime.month, 'sub'); lay.extend(dateTime, { year: YM[0] ,month: YM[1] }); - that.checkDate('limit').calendar(null, index); - that.done(null, 'change'); + if (that.calendarLinkage) { + that.checkDate('limit').calendar(null, null, 'init'); + } else { + that.checkDate('limit').calendar(null, index); + that.done(null, 'change'); + } } ,nextMonth: function(){ + if (that.calendarLinkage) { + dateTime = options.dateTime; + } var YM = that.getAsYM(dateTime.year, dateTime.month); lay.extend(dateTime, { year: YM[0] ,month: YM[1] }); - that.checkDate('limit').calendar(null, index); - that.done(null, 'change'); + if (that.calendarLinkage) { + that.checkDate('limit').calendar(null, null, 'init'); + } else { + that.checkDate('limit').calendar(null, index); + that.done(null, 'change'); + } } ,nextYear: function(){ if(addSubYeay()) return; - dateTime.year++ - that.checkDate('limit').calendar(null, index); - that.done(null, 'change'); + if (that.calendarLinkage) { + options.dateTime.year++; + that.checkDate('limit').calendar(null, 0, 'init'); + } else { + dateTime.year++; + that.checkDate('limit').calendar(null, index); + that.done(null, 'change'); + } } }; }; From 2aaea403fa0c568c48b3418ed52afb8abceeffa4 Mon Sep 17 00:00:00 2001 From: sunxb <470459819@qq.com> Date: Fri, 11 Nov 2022 17:38:09 +0800 Subject: [PATCH 02/13] =?UTF-8?q?laydate=E6=8B=93=E5=B1=95calendarLinkage?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E7=9A=84=E9=85=8D=E7=BD=AE=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/laydate.js | 79 +++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 28 deletions(-) diff --git a/src/modules/laydate.js b/src/modules/laydate.js index 074cd7e2..d1750399 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -248,7 +248,18 @@ //日期范围的日历面板是否联动 that.calendarLinkage = !!(options.range && options.calendarLinkage && (options.type === 'date' || options.type === 'datetime')) - + + //切换日历联动方式 + that.autoCalendarModel = function () { + var state = that.calendarLinkage; + that.calendarLinkage = that.startDate && that.endDate && that.startDate.year === that.endDate.year && that.startDate.month === that.endDate.month; + lay(that.elem)[that.calendarLinkage ? 'addClass' : 'removeClass']('layui-laydate-linkage'); + return that.calendarLinkage !== state; // 返回发生了变化 + }; + + //是否自动切换 + that.autoCalendarModel.auto = that.calendarLinkage && options.calendarLinkage === 'auto'; + //若 range 参数为数组,则表示为开始日期和结束日期的 input 对象 if(layui.type(options.range) === 'array'){ that.rangeElem = [ @@ -883,6 +894,10 @@ initDate(item, value[i], i); }); that.startDate = lay.extend({}, options.dateTime); + if (that.autoCalendarModel.auto) { // 自动日历模式 + // 判断联动与否 + that.autoCalendarModel() + } } else { initDate(dateTime, value); } @@ -922,10 +937,8 @@ //校验日期有效数字 checkValid(dateTime); if(options.range) checkValid(that.endDate); - if(that.calendarLinkage) { - that.endState = !!(that.startDate && that.endDate); // 初始化选中范围状态 - } - + that.endState = !that.calendarLinkage || !!(that.startDate && that.endDate); // 初始化选中范围状态 + //如果初始值格式错误,则纠正初始值 if(error && value){ that.setValue( @@ -1166,7 +1179,7 @@ ,month: EYM[1] }), 1 - index); // 渲染另外一个 } else { - that.calendar(that.endDate, 1); + that.calendar(null, 1 - index); } } @@ -1196,7 +1209,7 @@ lay(that.shortcut).find('li.' + THIS).removeClass(THIS); //标记选择范围 - if(that.calendarLinkage && type !== 'init') that.stampRange(); + if(options.range && !isAlone && type !== 'init') that.stampRange(); return that; }; @@ -1644,16 +1657,16 @@ Class.prototype.stampRange = function(){ var that = this ,options = that.config - ,startTime, endTime + ,startTime = that.calendarLinkage ? that.startDate : options.dateTime, endTime ,tds = lay(that.elem).find('td'); if(options.range && !that.endState) lay(that.footer).find(ELEM_CONFIRM).addClass(DISABLED); // if(!that.endState) return; - startTime = that.startDate && that.newDate({ - year: that.startDate.year - ,month: that.startDate.month - ,date: that.startDate.date + startTime = startTime && that.newDate({ + year: startTime.year + ,month: startTime.month + ,date: startTime.date }).getTime(); endTime = that.endState && that.endDate && that.newDate({ @@ -1671,8 +1684,9 @@ ,month: ymd[1] - 1 ,date: ymd[2] }).getTime(); - lay(item).removeClass(ELEM_SELECTED + ' ' + THIS); + that.calendarLinkage && lay(item).removeClass(ELEM_SELECTED + ' ' + THIS); if(thisTime === startTime || thisTime === endTime){ + (that.calendarLinkage || (!that.calendarLinkage && (i < 42 ? thisTime === startTime : thisTime === endTime))) && lay(item).addClass( lay(item).hasClass(ELEM_PREV) || lay(item).hasClass(ELEM_NEXT) ? ELEM_SELECTED @@ -1767,22 +1781,27 @@ } } }); - if (that.calendarLinkage) { - if (!index) { - that.startDate = lay.extend({}, dateTime); - } - if (that.endState && that.newDate(that.startDate) > that.newDate(that.endDate)) { - // 判断是否反选 - var startDate = that.startDate; - that.startDate = that.endDate; - options.dateTime = lay.extend({}, that.startDate); - that.endDate = startDate; - startDate = that.startTime; - that.startTime = that.endTime; - that.endTime = startDate; - } + if (!index) { + that.startDate = lay.extend({}, dateTime); // 同步startDate } - that.calendar(null, that.calendarLinkage ? null : index, that.calendarLinkage ? 'init' : null).done(null, 'change'); + // 根据选择之后判断是否需要切换模式 + var isChange; + if (that.endState && that.autoCalendarModel.auto) { + isChange = that.autoCalendarModel(); + } + if (that.endState && that.newDate(that.startDate) > that.newDate(that.endDate)) { + // 判断是否反选 + var startDate = that.startDate; + that.startDate = lay.extend({}, that.endDate, that.startTime); + options.dateTime = lay.extend({}, that.startDate); + that.endDate = lay.extend({}, startDate, that.endTime); + // startDate = that.startTime; + // that.startTime = that.endTime; + // that.endTime = startDate; + } + isChange && (options.dateTime = lay.extend({}, that.startDate)); + that.calendar(null, that.calendarLinkage ? null : index, isChange || that.calendarLinkage ? 'init' : null).done(null, 'change'); + // that.calendar(null, null, 'init').done(null, 'change'); } else if(options.position === 'static'){ //直接嵌套的选中 that.calendar().done().done(null, 'change'); //同时执行 done 和 change 回调 } else if(options.type === 'date'){ @@ -1918,6 +1937,7 @@ } else { dateTime.year--; that.checkDate('limit').calendar(null, index); + that.choose(lay(elemCont).find('td.layui-this'), index); that.done(null, 'change'); } } @@ -1934,6 +1954,7 @@ that.checkDate('limit').calendar(null, null, 'init'); } else { that.checkDate('limit').calendar(null, index); + that.choose(lay(elemCont).find('td.layui-this'), index); that.done(null, 'change'); } } @@ -1950,6 +1971,7 @@ that.checkDate('limit').calendar(null, null, 'init'); } else { that.checkDate('limit').calendar(null, index); + that.choose(lay(elemCont).find('td.layui-this'), index); that.done(null, 'change'); } } @@ -1961,6 +1983,7 @@ } else { dateTime.year++; that.checkDate('limit').calendar(null, index); + that.choose(lay(elemCont).find('td.layui-this'), index); that.done(null, 'change'); } } From c96b6fb86f033ab0733759ee5eee048dc4cd2989 Mon Sep 17 00:00:00 2001 From: sunxb <470459819@qq.com> Date: Fri, 11 Nov 2022 19:30:00 +0800 Subject: [PATCH 03/13] =?UTF-8?q?laydate=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=B0=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/laydate.js | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/modules/laydate.js b/src/modules/laydate.js index d1750399..aeeea6b7 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -252,7 +252,7 @@ //切换日历联动方式 that.autoCalendarModel = function () { var state = that.calendarLinkage; - that.calendarLinkage = that.startDate && that.endDate && that.startDate.year === that.endDate.year && that.startDate.month === that.endDate.month; + that.calendarLinkage = (options.range && (options.type === 'date' || options.type === 'datetime')) && that.startDate && that.endDate && that.startDate.year === that.endDate.year && that.startDate.month === that.endDate.month; lay(that.elem)[that.calendarLinkage ? 'addClass' : 'removeClass']('layui-laydate-linkage'); return that.calendarLinkage !== state; // 返回发生了变化 }; @@ -583,22 +583,15 @@ } if (type === 'year' || type === 'month' || type === 'time') { that.listYM[i] = [dateTime.year, dateTime.month + 1]; - that.checkDate('limit').calendar(null, i); - that.list(type, i); } else { - if (that.calendarLinkage) { - if (i === 0) { // 第一个值作为startDate - that.startDate = lay.extend({}, dateTime); - } else { - that.endState = !!(that.startDate && that.endDate); - that.checkDate('limit').calendar(null, null, 'init'); - } + if (i === 0) { // 第一个值作为startDate + that.startDate = lay.extend({}, dateTime); } else { - that.checkDate('limit').calendar(null, i); + that.autoCalendarModel.auto && that.autoCalendarModel(); } - that.closeList(); } }); + that.checkDate('limit').calendar(null, null, 'init'); var timeBtn = lay(that.footer).find('.'+ ELEM_TIME_BTN).removeClass(DISABLED); timeBtn && timeBtn.attr('lay-type') === 'date' && timeBtn[0].click(); @@ -847,6 +840,7 @@ if (options.range) { checkValid(that.calendarLinkage ? that.startDate : dateTime); // 校验开始时间 checkValid(that.endDate); // 校验结束时间 + that.endState = !that.calendarLinkage || !!(that.startDate && that.endDate); } else { checkValid(dateTime); } @@ -1141,16 +1135,16 @@ //初始默认选择器 if(isAlone){ //年、月等独立选择器 if(options.range){ - if (that.calendarLinkage) { - value ? that.endDate = (that.endDate || { - year: dateTime.year + (options.type === 'year' ? 1 : 0) - ,month: dateTime.month + (options.type === 'month' ? 0 : -1) - }) : (that.startDate = that.startDate || { - year: dateTime.year - ,month: dateTime.month - }); - } - if(value){ + // if (that.calendarLinkage) { + // value ? that.endDate = (that.endDate || { + // year: dateTime.year + (options.type === 'year' ? 1 : 0) + // ,month: dateTime.month + (options.type === 'month' ? 0 : -1) + // }) : (that.startDate = that.startDate || { + // year: dateTime.year + // ,month: dateTime.month + // }); + // } + if(value || type !== 'init'){ // 判断是否需要显示年月时间列表 that.listYM = [ [(that.startDate || options.dateTime).year, (that.startDate || options.dateTime).month + 1] ,[that.endDate.year, that.endDate.month + 1] From 84f92c0a7e92327d5b05974e4f29d5333e72930d Mon Sep 17 00:00:00 2001 From: sunxb <470459819@qq.com> Date: Mon, 14 Nov 2022 17:09:48 +0800 Subject: [PATCH 04/13] =?UTF-8?q?laydate=E4=BF=AE=E5=A4=8D=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/laydate.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/modules/laydate.js b/src/modules/laydate.js index aeeea6b7..a848ff65 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -254,7 +254,7 @@ var state = that.calendarLinkage; that.calendarLinkage = (options.range && (options.type === 'date' || options.type === 'datetime')) && that.startDate && that.endDate && that.startDate.year === that.endDate.year && that.startDate.month === that.endDate.month; lay(that.elem)[that.calendarLinkage ? 'addClass' : 'removeClass']('layui-laydate-linkage'); - return that.calendarLinkage !== state; // 返回发生了变化 + return that.calendarLinkage != state; // 返回发生了变化 }; //是否自动切换 @@ -887,11 +887,6 @@ lay.each([options.dateTime, that.endDate], function(i, item){ initDate(item, value[i], i); }); - that.startDate = lay.extend({}, options.dateTime); - if (that.autoCalendarModel.auto) { // 自动日历模式 - // 判断联动与否 - that.autoCalendarModel() - } } else { initDate(dateTime, value); } @@ -931,7 +926,6 @@ //校验日期有效数字 checkValid(dateTime); if(options.range) checkValid(that.endDate); - that.endState = !that.calendarLinkage || !!(that.startDate && that.endDate); // 初始化选中范围状态 //如果初始值格式错误,则纠正初始值 if(error && value){ @@ -976,6 +970,10 @@ that.hint('value ' + lang.invalidDate + lang.formatError[1]); } + that.startDate = that.startDate || lay.extend({}, options.dateTime); + that.endState = !that.calendarLinkage || !!(that.startDate && that.endDate); // 初始化选中范围状态 + that.autoCalendarModel.auto && that.autoCalendarModel(); + fn && fn(); return that; }; @@ -1522,10 +1520,7 @@ start = start || options.dateTime; end = end || that.endDate; isOut = that.newDate(start).getTime() > that.newDate(end).getTime(); - if(that.calendarLinkage) { - isOut = !that.endState; - } - + //如果不在有效日期内,直接禁用按钮,否则比较开始和结束日期 (that.limit({ date: start @@ -1783,15 +1778,18 @@ if (that.endState && that.autoCalendarModel.auto) { isChange = that.autoCalendarModel(); } - if (that.endState && that.newDate(that.startDate) > that.newDate(that.endDate)) { + var isSameDate = that.startDate.year === that.endDate.year && that.startDate.month === that.endDate.month && that.startDate.date === that.endDate.date; + if (that.calendarLinkage && that.endState && that.newDate(that.startDate) > that.newDate(that.endDate)) { // 判断是否反选 var startDate = that.startDate; - that.startDate = lay.extend({}, that.endDate, that.startTime); + that.startDate = lay.extend({}, that.endDate, isSameDate ? {} : that.startTime); options.dateTime = lay.extend({}, that.startDate); - that.endDate = lay.extend({}, startDate, that.endTime); - // startDate = that.startTime; - // that.startTime = that.endTime; - // that.endTime = startDate; + that.endDate = lay.extend({}, startDate, isSameDate ? {} : that.endTime); + isSameDate && ( // 如果是同一天并且出现了反选证明是时分秒出现开始时间大于结束时间的现象 + startDate = that.startTime, + that.startTime = that.endTime, + that.endTime = startDate + ) } isChange && (options.dateTime = lay.extend({}, that.startDate)); that.calendar(null, that.calendarLinkage ? null : index, isChange || that.calendarLinkage ? 'init' : null).done(null, 'change'); From 37eb2c6a4f7adfaca9a04dc878eb2fef25736cd8 Mon Sep 17 00:00:00 2001 From: sunxb <470459819@qq.com> Date: Tue, 15 Nov 2022 17:40:41 +0800 Subject: [PATCH 05/13] =?UTF-8?q?laydate=E4=BF=AE=E5=A4=8D=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/laydate.js | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/modules/laydate.js b/src/modules/laydate.js index a848ff65..3393e972 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -1210,7 +1210,7 @@ Class.prototype.list = function(type, index){ var that = this ,options = that.config - ,dateTime = options.dateTime + ,dateTime = that.calendarLinkage ? options.dateTime : [options.dateTime, that.endDate][index] ,lang = that.lang() ,isAlone = options.range && options.type !== 'date' && options.type !== 'datetime' //独立范围选择器 @@ -1382,24 +1382,13 @@ lay(ul).find('li').on('click', function(){ var ym = lay(this).attr('lay-ym') | 0; if(lay(this).hasClass(DISABLED)) return; - if(index === 0){ - dateTime[type] = ym; - that.limit({ - elem: lay(that.footer).find(ELEM_CONFIRM), - index: 0 + if (that.calendarLinkage) { + lay.extend(dateTime, { + year: type === 'year' ? ym : listYM[0] + ,month: type === 'year' ? listYM[1] - 1 : ym }); - } else { //范围选择 - if(isAlone){ //非date/datetime类型 - that.endDate[type] = ym; - } else { - var YM = type === 'year' - ? that.getAsYM(ym, listYM[1] - 1, 'sub') - : that.getAsYM(listYM[0], ym, 'sub'); - lay.extend(dateTime, { - year: YM[0] - ,month: YM[1] - }); - } + } else { + dateTime[type] = ym; } //当为年选择器或者年月选择器 @@ -1415,11 +1404,8 @@ that.list('month', index); } } else { - if (that.calendarLinkage) { - that.checkDate('limit').calendar(null, 0, 'init'); - } else { - that.checkDate('limit').calendar(null, index); - } + that.checkDate('limit').calendar(dateTime, index, 'init'); // 重新渲染一下两个面板 + that.calendarLinkage || that.choose(lay(elemCont).find('td.layui-this'), index); that.closeList(); } @@ -1517,7 +1503,7 @@ ,lang = that.lang() ,isOut, elemBtn = lay(that.footer).find(ELEM_CONFIRM); if(options.range && options.type !== 'time'){ - start = start || options.dateTime; + start = start || that.startDate || options.dateTime; end = end || that.endDate; isOut = that.newDate(start).getTime() > that.newDate(end).getTime(); @@ -1673,7 +1659,7 @@ ,month: ymd[1] - 1 ,date: ymd[2] }).getTime(); - that.calendarLinkage && lay(item).removeClass(ELEM_SELECTED + ' ' + THIS); + lay(item).removeClass(ELEM_SELECTED + ' ' + THIS); if(thisTime === startTime || thisTime === endTime){ (that.calendarLinkage || (!that.calendarLinkage && (i < 42 ? thisTime === startTime : thisTime === endTime))) && lay(item).addClass( From 1d9056bd0ca59f20a15c6f0792f43fbd32cb62c6 Mon Sep 17 00:00:00 2001 From: sunxb <470459819@qq.com> Date: Wed, 16 Nov 2022 11:04:06 +0800 Subject: [PATCH 06/13] =?UTF-8?q?laydate=E4=BF=AE=E5=A4=8D=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E9=97=AE=E9=A2=98=EF=BC=9B=E4=BF=AE=E5=A4=8Dgulp?= =?UTF-8?q?=E6=89=93=E5=8C=85=E7=8B=AC=E7=AB=8B=E7=BB=84=E4=BB=B6laydate?= =?UTF-8?q?=E7=9A=84=E5=91=BD=E4=BB=A4=E8=AF=AD=E5=8F=A5=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=88=B0=E6=9C=80=E6=96=B0=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=E5=AF=BC=E8=87=B4=E6=89=93=E5=8C=85=E5=87=BA=E6=9D=A5?= =?UTF-8?q?=E7=9A=84laydate=E7=8B=AC=E7=AB=8B=E7=BB=84=E4=BB=B6=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gulpfile.js | 6 ++++-- src/modules/lay.js | 2 +- src/modules/laydate.js | 12 ++++++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 77f64418..7e33e880 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -136,9 +136,11 @@ exports.laydate = () => { // gulp laydate // js return gulp.src(['./src/layui.js', './src/modules/{lay,laydate}.js']) .pipe(replace('win.layui =', 'var layui =')) // 将 layui 替换为局部变量 - .pipe(replace('})(window); //gulp build: layui-footer', '')) // 替换 layui.js 的落脚 - .pipe(replace('(function(window){ //gulp build: lay-header', '')) // 替换 lay.js 的头部 + .pipe(replace('}(window); //gulp build: layui-footer', '')) // 替换 layui.js 的落脚 + .pipe(replace(';!function(window){ //gulp build: lay-header', '')) // 替换 lay.js 的头部 + .pipe(replace('}(window, window.document); //gulp build: lay-footer', '')) // 替换 lay.js 的落脚 .pipe(concat('laydate.js', {newLine: ''})) + .pipe(replace(';!function(window, document){ //gulp build: laydate-header', '')) // 替换 laydate.js 的头部 .pipe(header.apply(null, comment)) //追加头部 .pipe(gulp.dest(dest + 'src')); }; diff --git a/src/modules/lay.js b/src/modules/lay.js index 427c9359..fceff512 100644 --- a/src/modules/lay.js +++ b/src/modules/lay.js @@ -428,4 +428,4 @@ }); } -}(window, window.document); \ No newline at end of file +}(window, window.document); //gulp build: lay-footer \ No newline at end of file diff --git a/src/modules/laydate.js b/src/modules/laydate.js index 3393e972..102339a8 100644 --- a/src/modules/laydate.js +++ b/src/modules/laydate.js @@ -1,6 +1,6 @@ /** layDate 日期与时间控件 | MIT Licensed */ -;!function(window, document){ +;!function(window, document){ //gulp build: laydate-header "use strict"; var isLayui = window.layui && layui.define, ready = { @@ -42,7 +42,7 @@ ,ready: function(callback){ var cssname = 'laydate'; var ver = '' - var path = (isLayui ? 'modules/' : 'css/') + 'laydate.css?v='+ laydate.v + ver; + var path = (isLayui ? 'modules/' : '') + 'laydate.css?v='+ laydate.v + ver; isLayui ? ( layui['layui.all'] @@ -1503,7 +1503,7 @@ ,lang = that.lang() ,isOut, elemBtn = lay(that.footer).find(ELEM_CONFIRM); if(options.range && options.type !== 'time'){ - start = start || that.startDate || options.dateTime; + start = start || (that.calendarLinkage ? that.startDate : options.dateTime); end = end || that.endDate; isOut = that.newDate(start).getTime() > that.newDate(end).getTime(); @@ -1699,6 +1699,8 @@ //选择日期 Class.prototype.choose = function(td, index){ + if(td.hasClass(DISABLED)) return; + var that = this ,options = that.config; @@ -1723,9 +1725,7 @@ ,month: (YMD[1] | 0) - 1 ,date: YMD[2] | 0 }; - - if(td.hasClass(DISABLED)) return; - + lay.extend(dateTime, YMD); //同步 dateTime //范围选择 From d1b8932ca64353d91c0589906cf9340b395a9b4e Mon Sep 17 00:00:00 2001 From: mek <573883887@qq.com> Date: Thu, 17 Nov 2022 16:18:24 +0800 Subject: [PATCH 07/13] =?UTF-8?q?input=E7=9A=84type=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/transfer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/transfer.js b/src/modules/transfer.js index b1aecb9e..8c85dce4 100644 --- a/src/modules/transfer.js +++ b/src/modules/transfer.js @@ -73,7 +73,7 @@ layui.define(['laytpl', 'form'], function(exports){ ,'{{# if(d.data.showSearch){ }}' ,'