refactor: 剔除 laydate 单独版的判断逻辑

为接下来全面支持国际化做铺垫
pull/2742/head
贤心 2025-05-28 11:01:00 +08:00
parent c3e88dc9aa
commit 4f5019704b
1 changed files with 74 additions and 110 deletions

View File

@ -1,26 +1,13 @@
/** laydate 日期与时间控件 | MIT Licensed */ /**
// @ts-expect-error * laydate
(function(window, document) { * 日期与时间组件
*/
layui.define(['lay', 'i18n'], function(exports) {
"use strict"; "use strict";
var isLayui = window.layui && layui.define; var lay = layui.lay;
var ready = { var i18n = layui.i18n;
getPath: window.lay && lay.getPath ? lay.getPath : '',
// 载入 CSS 依赖
link: function (href, fn, cssname) {
// 未设置路径,则不主动加载 css
if (!laydate.path) return;
// 加载 css
if (window.lay && lay.layui) {
lay.layui.link(laydate.path + href, fn, cssname);
}
}
};
// 识别预先可能定义的指定全局对象
var GLOBAL = window.LAYUI_GLOBAL || {};
// 模块名 // 模块名
var MOD_NAME = 'laydate'; var MOD_NAME = 'laydate';
@ -28,12 +15,11 @@
// 外部调用 // 外部调用
var laydate = { var laydate = {
v: '5.6.0', // layDate 版本号 v: '5.7.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,
// 设置全局项 // 设置全局项
set: function (options) { set: function (options) {
@ -45,14 +31,14 @@
// 主体 CSS 等待事件 // 主体 CSS 等待事件
ready: function (callback) { ready: function (callback) {
var cssname = 'laydate'; var cssname = 'laydate';
var ver = ''; var path = 'modules/laydate.css?v=' + laydate.v;
var path = (isLayui ? 'modules/' : '') + 'laydate.css?v=' + laydate.v + ver;
isLayui ? ( // 打包版直接执行回调函数
layui['layui.all'] ? if (layui['layui.all']) {
(typeof callback === 'function' && callback()) : typeof callback === 'function' && callback();
layui.addcss(path, callback, cssname) } else {
) : ready.link(path, callback, cssname); layui.addcss(path, callback, cssname);
}
return this; return this;
} }
@ -234,12 +220,10 @@
} }
}; };
// 为了避免边缘情况laydate 只提供「简体中文」和「英文」切换 // 临时代码
if (isLayui) { var locale = i18n.config.locale;
var locale = layui.i18n.config.locale; if (options.lang === 'cn' && locale !== 'zh-CN') {
if (options.lang === 'cn' && locale !== 'zh-CN') { options.lang = 'en';
options.lang = 'en';
}
} }
return text[options.lang] || text['cn']; return text[options.lang] || text['cn'];
@ -1117,8 +1101,7 @@
// chineseFestivals 仅简体中文生效 // chineseFestivals 仅简体中文生效
if (options.calendar) { if (options.calendar) {
var isZhCN = isLayui ? layui.i18n.config.locale === 'zh-CN' : options.lang === 'cn'; if (options.lang === 'cn') {
if (isZhCN) {
render(that.markerOfChineseFestivals); render(that.markerOfChineseFestivals);
} }
} }
@ -2611,64 +2594,63 @@
}; };
}; };
//记录所有实例 // 绑定关闭控件事件
lay(document).on('mousedown', function(e){
if(!laydate.thisId) return;
var that = thisModule.getThis(laydate.thisId);
if(!that) return;
var options = that.config;
if(
e.target === options.elem[0] ||
e.target === options.eventElem[0] ||
e.target === lay(options.closeStop)[0] ||
(options.elem[0] && options.elem[0].contains(e.target))
) return;
that.remove();
}).on('keydown', function(e){
if(!laydate.thisId) return;
var that = thisModule.getThis(laydate.thisId);
if(!that) return;
// 回车触发确认
if(that.config.position === 'static') return;
if(e.keyCode === 13){
if(lay('#'+ that.elemID)[0] && that.elemID === Class.thisElemDate){
e.preventDefault();
lay(that.footer).find(ELEM_CONFIRM)[0].click();
}
}
});
//自适应定位
lay(window).on('resize', function(){
if(!laydate.thisId) return;
var that = thisModule.getThis(laydate.thisId);
if(!that) return;
if(!that.elem || !lay(ELEM)[0]){
return false;
}
that.position();
});
// 记录所有实例
thisModule.that = {}; //记录所有实例对象 thisModule.that = {}; //记录所有实例对象
//获取当前实例对象 // 获取当前实例对象
thisModule.getThis = function(id){ thisModule.getThis = function(id) {
var that = thisModule.that[id]; var that = thisModule.that[id];
if(!that && isLayui) layui.hint().error(id ? (MOD_NAME +' instance with ID \''+ id +'\' not found') : 'ID argument required'); if (!that) {
layui.hint().error(id ? (MOD_NAME +' instance with ID \''+ id +'\' not found') : 'ID argument required');
}
return that; return that;
}; };
// 初始执行
ready.run = function(lay){
// 绑定关闭控件事件
lay(document).on('mousedown', function(e){
if(!laydate.thisId) return;
var that = thisModule.getThis(laydate.thisId);
if(!that) return;
var options = that.config;
if(
e.target === options.elem[0] ||
e.target === options.eventElem[0] ||
e.target === lay(options.closeStop)[0] ||
(options.elem[0] && options.elem[0].contains(e.target))
) return;
that.remove();
}).on('keydown', function(e){
if(!laydate.thisId) return;
var that = thisModule.getThis(laydate.thisId);
if(!that) return;
// 回车触发确认
if(that.config.position === 'static') return;
if(e.keyCode === 13){
if(lay('#'+ that.elemID)[0] && that.elemID === Class.thisElemDate){
e.preventDefault();
lay(that.footer).find(ELEM_CONFIRM)[0].click();
}
}
});
//自适应定位
lay(window).on('resize', function(){
if(!laydate.thisId) return;
var that = thisModule.getThis(laydate.thisId);
if(!that) return;
if(!that.elem || !lay(ELEM)[0]){
return false;
}
that.position();
});
};
// 渲染 - 核心接口 // 渲染 - 核心接口
laydate.render = function(options){ laydate.render = function(options){
var inst = new Class(options); var inst = new Class(options);
@ -2754,23 +2736,5 @@
return new Date(thisDate.getTime() - 1000*60*60*24).getDate(); return new Date(thisDate.getTime() - 1000*60*60*24).getDate();
}; };
//加载方式 exports(MOD_NAME, laydate);
isLayui ? ( });
laydate.ready(),
layui.define(['lay', 'i18n'], function(exports){ // layui 加载
laydate.path = layui.cache.dir;
ready.run(lay);
exports(MOD_NAME, laydate);
})
) : (
(typeof define === 'function' && define.amd) ? define(function(){ //requirejs 加载
ready.run(lay);
return laydate;
}) : function(){ //普通 script 标签加载
laydate.ready();
ready.run(window.lay);
window.laydate = laydate;
}()
);
})(window, window.document);