优化 laydate 代码细节

This commit is contained in:
贤心 2022-09-04 22:04:53 +08:00
parent 77bb19352c
commit 239ac1ebc3

View File

@ -6,32 +6,32 @@
var isLayui = window.layui && layui.define, ready = { var isLayui = window.layui && layui.define, ready = {
getPath: (window.lay && lay.getPath) ? lay.getPath : '' getPath: (window.lay && lay.getPath) ? lay.getPath : ''
//载入 CSS 依赖 // 载入 CSS 依赖
,link: function(href, fn, cssname){ ,link: function(href, fn, cssname){
//未设置路径,则不主动加载 css // 未设置路径,则不主动加载 css
if(!laydate.path) return; if(!laydate.path) return;
//加载 css // 加载 css
if(window.lay && lay.layui){ if(window.lay && lay.layui){
lay.layui.link(laydate.path + href, fn, cssname); lay.layui.link(laydate.path + href, fn, cssname);
} }
} }
} };
//识别预先可能定义的指定全局对象 // 识别预先可能定义的指定全局对象
,GLOBAL = window.LAYUI_GLOBAL || {} var GLOBAL = window.LAYUI_GLOBAL || {};
//外部调用 // 外部调用
,laydate = { var laydate = {
v: '5.4.0' //layDate 版本号 v: '5.4.0' // layDate 版本号
,config: { ,config: {
weekStart: 0, // 默认周日一周的开始 weekStart: 0, // 默认周日一周的开始
} //全局配置项 } // 全局配置项
,index: (window.laydate && window.laydate.v) ? 100000 : 0 ,index: (window.laydate && window.laydate.v) ? 100000 : 0
,path: GLOBAL.laydate_dir || ready.getPath ,path: GLOBAL.laydate_dir || ready.getPath
//设置全局项 // 设置全局项
,set: function(options){ ,set: function(options){
var that = this; var that = this;
that.config = lay.extend({}, that.config, options); that.config = lay.extend({}, that.config, options);
@ -52,10 +52,10 @@
return this; return this;
} }
} };
//操作当前实例 // 操作当前实例
,thisModule = function(){ var thisModule = function(){
var that = this var that = this
,options = that.config ,options = that.config
,id = options.id; ,id = options.id;
@ -69,15 +69,31 @@
} }
,config: that.config ,config: that.config
}; };
} };
//字符常量 // 字符常量
,MOD_NAME = 'laydate', ELEM = '.layui-laydate', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'laydate-disabled', LIMIT_YEAR = [100, 200000] var MOD_NAME = 'laydate';
var ELEM = '.layui-laydate';
var THIS = 'layui-this';
var SHOW = 'layui-show';
var HIDE = 'layui-hide';
var DISABLED = 'laydate-disabled';
var LIMIT_YEAR = [100, 200000];
,ELEM_STATIC = 'layui-laydate-static', ELEM_LIST = 'layui-laydate-list', ELEM_SELECTED = 'laydate-selected', ELEM_HINT = 'layui-laydate-hint', ELEM_PREV = 'laydate-day-prev', ELEM_NEXT = 'laydate-day-next', ELEM_FOOTER = 'layui-laydate-footer', ELEM_CONFIRM = '.laydate-btns-confirm', ELEM_TIME_TEXT = 'laydate-time-text', ELEM_TIME_BTN = 'laydate-btns-time', ELEM_PREVIEW = 'layui-laydate-preview' var ELEM_STATIC = 'layui-laydate-static';
var ELEM_LIST = 'layui-laydate-list';
var ELEM_SELECTED = 'laydate-selected';
var ELEM_HINT = 'layui-laydate-hint';
var ELEM_PREV = 'laydate-day-prev';
var ELEM_NEXT = 'laydate-day-next';
var ELEM_FOOTER = 'layui-laydate-footer';
var ELEM_CONFIRM = '.laydate-btns-confirm';
var ELEM_TIME_TEXT = 'laydate-time-text';
var ELEM_TIME_BTN = 'laydate-btns-time';
var ELEM_PREVIEW = 'layui-laydate-preview';
//组件构造器 // 组件构造器
,Class = function(options){ var Class = function(options){
var that = this; var that = this;
that.index = ++laydate.index; that.index = ++laydate.index;
that.config = lay.extend({}, that.config, laydate.config, options); that.config = lay.extend({}, that.config, laydate.config, options);
@ -93,20 +109,20 @@
return that; return that;
} }
//初始化 id 参数 // 初始化 id 参数
options = that.config; options = that.config;
options.id = ('id' in options) ? options.id : that.index; options.id = ('id' in options) ? options.id : that.index;
//初始化 // 初始化
laydate.ready(function(){ laydate.ready(function(){
that.init(); that.init();
}); });
} };
//日期格式字符 // 日期格式字符
,dateType = 'yyyy|y|MM|M|dd|d|HH|H|mm|m|ss|s'; var dateType = 'yyyy|y|MM|M|dd|d|HH|H|mm|m|ss|s';
//将日期格式字符转换为数组 // 将日期格式字符转换为数组
thisModule.formatArr = function(format){ thisModule.formatArr = function(format){
return (format || '').match(new RegExp(dateType + '|.', 'g')) || [] return (format || '').match(new RegExp(dateType + '|.', 'g')) || []
}; };
@ -115,12 +131,12 @@
组件操作 组件操作
*/ */
//是否闰年 // 是否闰年
Class.isLeapYear = function(year){ Class.isLeapYear = function(year){
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}; };
//默认配置 // 默认配置
Class.prototype.config = { Class.prototype.config = {
type: 'date' //控件类型支持year/month/date/time/datetime type: 'date' //控件类型支持year/month/date/time/datetime
,range: false //是否开启范围选择,即双控件 ,range: false //是否开启范围选择,即双控件