优化 laydate 内部 limit 方法

pull/1134/head
贤心 2022-09-05 00:01:26 +08:00
parent 239ac1ebc3
commit 074ba3125a
1 changed files with 80 additions and 33 deletions

View File

@ -901,11 +901,15 @@
}; };
// 无效日期范围的标记 // 无效日期范围的标记
Class.prototype.limit = function(elem, date, index, time, type){ Class.prototype.limit = function(opts){
var that = this opts = opts || {};
,options = that.config, timestrap = {}
,dateTime = index > (time ? 0 : 41) ? that.endDate : options.dateTime var that = this;
,isOut, thisDateTime = lay.extend({}, dateTime, date || {}); var options = that.config;
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({ lay.each({
now: thisDateTime now: thisDateTime
@ -914,11 +918,11 @@
}, function(key, item){ }, function(key, item){
timestrap[key] = that.newDate(lay.extend({ timestrap[key] = that.newDate(lay.extend({
year: item.year year: item.year
,month: type === 'year' ? 0 : item.month // 年份的时候只比较年 ,month: opts.type === 'year' ? 0 : item.month // 年份的时候只比较年
,date: (type === 'year' || type === 'month') ? 1 : item.date // 年月只比较年月不与最大最小比日期 ,date: (opts.type === 'year' || opts.type === 'month') ? 1 : item.date // 年月只比较年月不与最大最小比日期
}, function(){ }, function(){
var hms = {}; var hms = {};
lay.each(time, function(i, keys){ lay.each(opts.time, function(i, keys){
hms[keys] = item[keys]; hms[keys] = item[keys];
}); });
return hms; return hms;
@ -926,7 +930,8 @@
}); });
isOut = timestrap.now < timestrap.min || timestrap.now > timestrap.max; isOut = timestrap.now < timestrap.min || timestrap.now > timestrap.max;
elem && elem[isOut ? 'addClass' : 'removeClass'](DISABLED); opts.elem && opts.elem[isOut ? 'addClass' : 'removeClass'](DISABLED);
return isOut; return isOut;
}; };
@ -985,11 +990,15 @@
YMD[1]++; YMD[1]++;
YMD[2] = st + 1; YMD[2] = st + 1;
item.attr('lay-ymd', YMD.join('-')).html('<div>' + YMD[2] + '</div>'); item.attr('lay-ymd', YMD.join('-')).html('<div>' + YMD[2] + '</div>');
that.mark(item, YMD).holidays(item, YMD).limit(item, { that.mark(item, YMD).holidays(item, YMD).limit({
year: YMD[0] elem: item,
,month: YMD[1] - 1 date: {
,date: YMD[2] year: YMD[0],
}, index_); month: YMD[1] - 1,
date: YMD[2]
},
index: index_
});
}); });
//同步头部年月 //同步头部年月
@ -1034,7 +1043,11 @@
//通过检测当前有效日期,来设定确定按钮是否可点 //通过检测当前有效日期,来设定确定按钮是否可点
if(!options.range){ if(!options.range){
that.limit(lay(that.footer).find(ELEM_CONFIRM), null, 0, ['hours', 'minutes', 'seconds']); that.limit({
elem: lay(that.footer).find(ELEM_CONFIRM),
index: 0,
time: ['hours', 'minutes', 'seconds']
});
} }
//同步按钮可点状态 //同步按钮可点状态
@ -1099,7 +1112,12 @@
} }
*/ */
that.limit(lay(li), ymd, index, null, type); that.limit({
elem: lay(li),
date: ymd,
index: index,
type: type
});
yearNum++; yearNum++;
}); });
@ -1131,7 +1149,12 @@
} }
*/ */
that.limit(lay(li), ymd, index, null, type); that.limit({
elem: lay(li),
date: ymd,
index: index,
type: type
});
}); });
lay(elemYM[isCN ? 0 : 1]).attr('lay-ym', listYM[0] + '-' + listYM[1]) lay(elemYM[isCN ? 0 : 1]).attr('lay-ym', listYM[0] + '-' + listYM[1])
@ -1144,7 +1167,9 @@
var setTimeStatus = function(){ var setTimeStatus = function(){
lay(ul).find('ol').each(function(i, ol){ lay(ul).find('ol').each(function(i, ol){
lay(ol).find('li').each(function(ii, li){ lay(ol).find('li').each(function(ii, li){
that.limit(lay(li), [{ that.limit({
elem: lay(li),
date: [{
hours: ii hours: ii
}, { }, {
hours: that[startEnd].hours hours: that[startEnd].hours
@ -1153,11 +1178,23 @@
hours: that[startEnd].hours hours: that[startEnd].hours
,minutes: that[startEnd].minutes ,minutes: that[startEnd].minutes
,seconds: ii ,seconds: ii
}][i], index, [['hours'], ['hours', 'minutes'], ['hours', 'minutes', 'seconds']][i]); }][i],
index: index,
time: [
['hours'],
['hours', 'minutes'],
['hours', 'minutes', 'seconds']
][i]
});
}); });
}); });
if(!options.range){ if(!options.range){
that.limit(lay(that.footer).find(ELEM_CONFIRM), that[startEnd], 0, ['hours', 'minutes', 'seconds']); that.limit({
elem: lay(that.footer).find(ELEM_CONFIRM),
date: that[startEnd],
inedx: 0,
time: ['hours', 'minutes', 'seconds']
});
} }
}; };
@ -1197,7 +1234,10 @@
if(lay(this).hasClass(DISABLED)) return; if(lay(this).hasClass(DISABLED)) return;
if(index === 0){ if(index === 0){
dateTime[type] = ym; dateTime[type] = ym;
that.limit(lay(that.footer).find(ELEM_CONFIRM), null, 0); that.limit({
elem: lay(that.footer).find(ELEM_CONFIRM),
index: 0
});
} else { //范围选择 } else { //范围选择
that.endDate[type] = ym; that.endDate[type] = ym;
} }
@ -1318,7 +1358,11 @@
isOut = that.newDate(start).getTime() > that.newDate(end).getTime(); isOut = that.newDate(start).getTime() > that.newDate(end).getTime();
//如果不在有效日期内,直接禁用按钮,否则比较开始和结束日期 //如果不在有效日期内,直接禁用按钮,否则比较开始和结束日期
(that.limit(null, start) || that.limit(null, end)) (that.limit({
date: start
}) || that.limit({
date: end
}))
? elemBtn.addClass(DISABLED) ? elemBtn.addClass(DISABLED)
: elemBtn[isOut ? 'addClass' : 'removeClass'](DISABLED); : elemBtn[isOut ? 'addClass' : 'removeClass'](DISABLED);
@ -1612,8 +1656,11 @@
}); });
if(isAlone) dateTime.year = listYM[0]; if(isAlone) dateTime.year = listYM[0];
options.range || that.done(null, 'change'); options.range || that.done(null, 'change');
options.range || that.limit(lay(that.footer).find(ELEM_CONFIRM), { options.range || that.limit({
elem: lay(that.footer).find(ELEM_CONFIRM),
date: {
year: listYM[0] year: listYM[0]
}
}); });
} }