优化 注释字符间隔

pull/1249/head
贤心 2023-04-30 01:10:55 +08:00
parent 602bddd07e
commit e3cd9c9020
1 changed files with 118 additions and 116 deletions

View File

@ -18,10 +18,10 @@
this.v = '2.8.1'; // Layui 版本号 this.v = '2.8.1'; // Layui 版本号
} }
//识别预先可能定义的指定全局对象 // 识别预先可能定义的指定全局对象
,GLOBAL = win.LAYUI_GLOBAL || {} ,GLOBAL = win.LAYUI_GLOBAL || {}
//获取 layui 所在目录 // 获取 layui 所在目录
,getPath = function(){ ,getPath = function(){
var jsPath = doc.currentScript ? doc.currentScript.src : function(){ var jsPath = doc.currentScript ? doc.currentScript.src : function(){
var js = doc.scripts var js = doc.scripts
@ -39,7 +39,7 @@
return config.dir = GLOBAL.dir || jsPath.substring(0, jsPath.lastIndexOf('/') + 1); return config.dir = GLOBAL.dir || jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
}() }()
//异常提示 // 异常提示
,error = function(msg, type){ ,error = function(msg, type){
type = type || 'log'; type = type || 'log';
win.console && console[type] && console[type]('layui error hint: ' + msg); win.console && console[type] && console[type]('layui error hint: ' + msg);
@ -47,38 +47,38 @@
,isOpera = typeof opera !== 'undefined' && opera.toString() === '[object Opera]' ,isOpera = typeof opera !== 'undefined' && opera.toString() === '[object Opera]'
//内置模块 // 内置模块
,modules = config.builtin = { ,modules = config.builtin = {
lay: 'lay' //基础 DOM 操作 lay: 'lay' // 基础 DOM 操作
,layer: 'layer' //弹层 ,layer: 'layer' // 弹层
,laydate: 'laydate' //日期 ,laydate: 'laydate' // 日期
,laypage: 'laypage' //分页 ,laypage: 'laypage' // 分页
,laytpl: 'laytpl' //模板引擎 ,laytpl: 'laytpl' // 模板引擎
,form: 'form' //表单集 ,form: 'form' // 表单集
,upload: 'upload' //上传 ,upload: 'upload' // 上传
,dropdown: 'dropdown' //下拉菜单 ,dropdown: 'dropdown' // 下拉菜单
,transfer: 'transfer' //穿梭框 ,transfer: 'transfer' // 穿梭框
,tree: 'tree' //树结构 ,tree: 'tree' // 树结构
,table: 'table' //表格 ,table: 'table' // 表格
,treeTable: 'treeTable' //树表 ,treeTable: 'treeTable' // 树表
,element: 'element' //常用元素操作 ,element: 'element' // 常用元素操作
,rate: 'rate' //评分组件 ,rate: 'rate' // 评分组件
,colorpicker: 'colorpicker' //颜色选择器 ,colorpicker: 'colorpicker' // 颜色选择器
,slider: 'slider' //滑块 ,slider: 'slider' // 滑块
,carousel: 'carousel' //轮播 ,carousel: 'carousel' // 轮播
,flow: 'flow' //流加载 ,flow: 'flow' // 流加载
,util: 'util' //工具块 ,util: 'util' // 工具块
,code: 'code' //代码修饰器 ,code: 'code' // 代码修饰器
,jquery: 'jquery' //DOM 库(第三方) ,jquery: 'jquery' // DOM 库(第三方)
,all: 'all' ,all: 'all'
,'layui.all': 'layui.all' //聚合标识(功能性的,非真实模块) ,'layui.all': 'layui.all' // 聚合标识(功能性的,非真实模块)
}; };
//记录基础数据 // 记录基础数据
Layui.prototype.cache = config; Layui.prototype.cache = config;
//定义模块 // 定义模块
Layui.prototype.define = function(deps, factory){ Layui.prototype.define = function(deps, factory){
var that = this var that = this
,type = typeof deps === 'function' ,type = typeof deps === 'function'
@ -105,7 +105,7 @@
return that; return that;
}; };
//使用特定模块 // 使用特定模块
Layui.prototype.use = function(apps, callback, exports, from){ Layui.prototype.use = function(apps, callback, exports, from){
var that = this var that = this
,dir = config.dir = config.dir ? config.dir : getPath ,dir = config.dir = config.dir ? config.dir : getPath
@ -115,7 +115,7 @@
if(typeof apps === 'string'){ if(typeof apps === 'string'){
return [apps]; return [apps];
} }
//当第一个参数为 function 时,则自动加载所有内置模块,且执行的回调即为该 function 参数; // 当第一个参数为 function 时,则自动加载所有内置模块,且执行的回调即为该 function 参数;
else if(typeof apps === 'function'){ else if(typeof apps === 'function'){
callback = apps; callback = apps;
return ['all']; return ['all'];
@ -123,7 +123,7 @@
return apps; return apps;
}(); }();
//如果页面已经存在 jQuery 1.7+ 库且所定义的模块依赖 jQuery则不加载内部 jquery 模块 // 如果页面已经存在 jQuery 1.7+ 库且所定义的模块依赖 jQuery则不加载内部 jquery 模块
if(win.jQuery && jQuery.fn.on){ if(win.jQuery && jQuery.fn.on){
that.each(apps, function(index, item){ that.each(apps, function(index, item){
if(item === 'jquery'){ if(item === 'jquery'){
@ -137,10 +137,10 @@
,timeout = 0; ,timeout = 0;
exports = exports || []; exports = exports || [];
//静态资源host // 静态资源host
config.host = config.host || (dir.match(/\/\/([\s\S]+?)\//)||['//'+ location.host +'/'])[0]; config.host = config.host || (dir.match(/\/\/([\s\S]+?)\//)||['//'+ location.host +'/'])[0];
//加载完毕 // 加载完毕
function onScriptLoad(e, url){ function onScriptLoad(e, url){
var readyRegExp = navigator.platform === 'PLaySTATION 3' ? /^complete$/ : /^(complete|loaded)$/ var readyRegExp = navigator.platform === 'PLaySTATION 3' ? /^complete$/ : /^(complete|loaded)$/
if (e.type === 'load' || (readyRegExp.test((e.currentTarget || e.srcElement).readyState))) { if (e.type === 'load' || (readyRegExp.test((e.currentTarget || e.srcElement).readyState))) {
@ -155,13 +155,13 @@
} }
} }
//回调 // 回调
function onCallback(){ function onCallback(){
exports.push(layui[item]); exports.push(layui[item]);
apps.length > 1 ? apps.length > 1 ?
that.use(apps.slice(1), callback, exports, from) that.use(apps.slice(1), callback, exports, from)
: ( typeof callback === 'function' && function(){ : ( typeof callback === 'function' && function(){
//保证文档加载完毕再执行回调 // 保证文档加载完毕再执行回调
if(layui.jquery && typeof layui.jquery === 'function' && from !== 'define'){ if(layui.jquery && typeof layui.jquery === 'function' && from !== 'define'){
return layui.jquery(function(){ return layui.jquery(function(){
callback.apply(layui, exports); callback.apply(layui, exports);
@ -171,28 +171,30 @@
}() ); }() );
} }
//如果引入了聚合板,内置的模块则不必重复加载 // 如果引入了聚合板,内置的模块则不必重复加载
if( apps.length === 0 || (layui['layui.all'] && modules[item]) ){ if( apps.length === 0 || (layui['layui.all'] && modules[item]) ){
return onCallback(), that; return onCallback(), that;
} }
//获取加载的模块 URL /*
//如果是内置模块,则按照 dir 参数拼接模块路径 * 获取加载的模块 URL
//如果是扩展模块,则判断模块路径值是否为 {/} 开头, * 如果是内置模块则按照 dir 参数拼接模块路径
//如果路径值是 {/} 开头,则模块路径即为后面紧跟的字符。 * 如果是扩展模块则判断模块路径值是否为 {/}
//否则,则按照 base 参数拼接模块路径 * 如果路径值是 {/}
* 否则则按照 base 参数拼接模块路径
*/
var url = ( modules[item] ? (dir + 'modules/') var url = ( modules[item] ? (dir + 'modules/')
: (/^\{\/\}/.test(that.modules[item]) ? '' : (config.base || '')) : (/^\{\/\}/.test(that.modules[item]) ? '' : (config.base || ''))
) + (that.modules[item] || item) + '.js'; ) + (that.modules[item] || item) + '.js';
url = url.replace(/^\{\/\}/, ''); url = url.replace(/^\{\/\}/, '');
//如果扩展模块(即:非内置模块)对象已经存在,则不必再加载 // 如果扩展模块(即:非内置模块)对象已经存在,则不必再加载
if(!config.modules[item] && layui[item]){ if(!config.modules[item] && layui[item]){
config.modules[item] = url; //并记录起该扩展模块的 url config.modules[item] = url; // 并记录起该扩展模块的 url
} }
//首次加载模块 // 首次加载模块
if(!config.modules[item]){ if(!config.modules[item]){
var node = doc.createElement('script'); var node = doc.createElement('script');
@ -218,7 +220,7 @@
} }
config.modules[item] = url; config.modules[item] = url;
} else { //缓存 } else { // 缓存
(function poll() { (function poll() {
if(++timeout > config.timeout * 1000 / 4){ if(++timeout > config.timeout * 1000 / 4){
return error(item + ' is not a valid module', 'error'); return error(item + ' is not a valid module', 'error');
@ -238,7 +240,7 @@
apps = that.isArray(apps) ? apps : [apps]; apps = that.isArray(apps) ? apps : [apps];
that.each(apps, function (index, item) { that.each(apps, function (index, item) {
if (!config.status[item]) { if (!config.status[item]) {
//return error('module ' + item + ' is not exist'); // return error('module ' + item + ' is not exist');
} }
delete that[item]; delete that[item];
delete modules[item]; delete modules[item];
@ -249,13 +251,13 @@
return that; return that;
}; };
//获取节点的 style 属性值 // 获取节点的 style 属性值
Layui.prototype.getStyle = function(node, name){ Layui.prototype.getStyle = function(node, name){
var style = node.currentStyle ? node.currentStyle : win.getComputedStyle(node, null); var style = node.currentStyle ? node.currentStyle : win.getComputedStyle(node, null);
return style[style.getPropertyValue ? 'getPropertyValue' : 'getAttribute'](name); return style[style.getPropertyValue ? 'getPropertyValue' : 'getAttribute'](name);
}; };
//css外部加载器 // css 外部加载器
Layui.prototype.link = function(href, fn, cssname){ Layui.prototype.link = function(href, fn, cssname){
var that = this var that = this
,head = doc.getElementsByTagName('head')[0] ,head = doc.getElementsByTagName('head')[0]
@ -278,21 +280,21 @@
if(typeof fn !== 'function') return that; if(typeof fn !== 'function') return that;
//轮询 css 是否加载完毕 // 轮询 css 是否加载完毕
(function poll(status) { (function poll(status) {
var delay = 100 var delay = 100
,getLinkElem = doc.getElementById(id); //获取动态插入的 link 元素 ,getLinkElem = doc.getElementById(id); // 获取动态插入的 link 元素
//如果轮询超过指定秒数,则视为请求文件失败或 css 文件不符合规范 // 如果轮询超过指定秒数,则视为请求文件失败或 css 文件不符合规范
if(++timeout > config.timeout * 1000 / delay){ if(++timeout > config.timeout * 1000 / delay){
return error(href + ' timeout'); return error(href + ' timeout');
} }
//css 加载就绪 // css 加载就绪
if(parseInt(that.getStyle(getLinkElem, 'width')) === 1989){ if(parseInt(that.getStyle(getLinkElem, 'width')) === 1989){
//如果参数来自于初始轮询(即未加载就绪时的),则移除 link 标签状态 // 如果参数来自于初始轮询(即未加载就绪时的),则移除 link 标签状态
if(status === STAUTS_NAME) getLinkElem.removeAttribute('lay-status'); if(status === STAUTS_NAME) getLinkElem.removeAttribute('lay-status');
//如果 link 标签的状态仍为「创建中」,则继续进入轮询,直到状态改变,则执行回调 // 如果 link 标签的状态仍为「创建中」,则继续进入轮询,直到状态改变,则执行回调
getLinkElem.getAttribute('lay-status') === STAUTS_NAME ? setTimeout(poll, delay) : fn(); getLinkElem.getAttribute('lay-status') === STAUTS_NAME ? setTimeout(poll, delay) : fn();
} else { } else {
getLinkElem.setAttribute('lay-status', STAUTS_NAME); getLinkElem.setAttribute('lay-status', STAUTS_NAME);
@ -302,7 +304,7 @@
} }
}()); }());
//轮询css是否加载完毕 // 轮询css是否加载完毕
/* /*
(function poll() { (function poll() {
if(++timeout > config.timeout * 1000 / 100){ if(++timeout > config.timeout * 1000 / 100){
@ -317,15 +319,15 @@
return that; return that;
}; };
//css 内部加载器 // css 内部加载器
Layui.prototype.addcss = function(firename, fn, cssname){ Layui.prototype.addcss = function(firename, fn, cssname){
return layui.link(config.dir + 'css/' + firename, fn, cssname); return layui.link(config.dir + 'css/' + firename, fn, cssname);
}; };
//存储模块的回调 // 存储模块的回调
config.callback = {}; config.callback = {};
//重新执行模块的工厂函数 // 重新执行模块的工厂函数
Layui.prototype.factory = function(modName){ Layui.prototype.factory = function(modName){
if(layui[modName]){ if(layui[modName]){
return typeof config.callback[modName] === 'function' return typeof config.callback[modName] === 'function'
@ -334,7 +336,7 @@
} }
}; };
//图片预加载 // 图片预加载
Layui.prototype.img = function(url, callback, error) { Layui.prototype.img = function(url, callback, error) {
var img = new Image(); var img = new Image();
img.src = url; img.src = url;
@ -351,7 +353,7 @@
}; };
}; };
//全局配置 // 全局配置
Layui.prototype.config = function(options){ Layui.prototype.config = function(options){
options = options || {}; options = options || {};
for(var key in options){ for(var key in options){
@ -360,7 +362,7 @@
return this; return this;
}; };
//记录全部模块 // 记录全部模块
Layui.prototype.modules = function(){ Layui.prototype.modules = function(){
var clone = {}; var clone = {};
for(var o in modules){ for(var o in modules){
@ -369,11 +371,11 @@
return clone; return clone;
}(); }();
//拓展模块 // 拓展模块
Layui.prototype.extend = function(options){ Layui.prototype.extend = function(options){
var that = this; var that = this;
//验证模块是否被占用 // 验证模块是否被占用
options = options || {}; options = options || {};
for(var o in options){ for(var o in options){
if(that[o] || that.modules[o]){ if(that[o] || that.modules[o]){
@ -396,12 +398,12 @@
,hash: (hash.match(/[^#](#.*$)/) || [])[1] || '' ,hash: (hash.match(/[^#](#.*$)/) || [])[1] || ''
}; };
if(!/^#\//.test(hash)) return data; //禁止非路由规范 if(!/^#\//.test(hash)) return data; // 禁止非路由规范
hash = hash.replace(/^#\//, ''); hash = hash.replace(/^#\//, '');
data.href = '/' + hash; data.href = '/' + hash;
hash = hash.replace(/([^#])(#.*$)/, '$1').split('/') || []; hash = hash.replace(/([^#])(#.*$)/, '$1').split('/') || [];
//提取 Hash 结构 // 提取 Hash 结构
that.each(hash, function(index, item){ that.each(hash, function(index, item){
/^\w+=/.test(item) ? function(){ /^\w+=/.test(item) ? function(){
item = item.split('='); item = item.split('=');
@ -412,11 +414,11 @@
return data; return data;
}; };
//URL 解析 // URL 解析
Layui.prototype.url = function(href){ Layui.prototype.url = function(href){
var that = this var that = this
,data = { ,data = {
//提取 url 路径 // 提取 url 路径
pathname: function(){ pathname: function(){
var pathname = href var pathname = href
? function(){ ? function(){
@ -427,7 +429,7 @@
return pathname.replace(/^\//, '').split('/'); return pathname.replace(/^\//, '').split('/');
}() }()
//提取 url 参数 // 提取 url 参数
,search: function(){ ,search: function(){
var obj = {} var obj = {}
,search = (href ,search = (href
@ -436,12 +438,12 @@
return str.replace(/\#.+/, ''); return str.replace(/\#.+/, '');
}() }()
: location.search : location.search
).replace(/^\?+/, '').split('&'); //去除 ?,按 & 分割参数 ).replace(/^\?+/, '').split('&'); // 去除 ?,按 & 分割参数
//遍历分割后的参数 // 遍历分割后的参数
that.each(search, function(index, item){ that.each(search, function(index, item){
var _index = item.indexOf('=') var _index = item.indexOf('=')
,key = function(){ //提取 key ,key = function(){ // 提取 key
if(_index < 0){ if(_index < 0){
return item.substr(0, item.length); return item.substr(0, item.length);
} else if(_index === 0){ } else if(_index === 0){
@ -450,7 +452,7 @@
return item.substr(0, _index); return item.substr(0, _index);
} }
}(); }();
//提取 value // 提取 value
if(key){ if(key){
obj[key] = _index > 0 ? item.substr(_index + 1) : null; obj[key] = _index > 0 ? item.substr(_index + 1) : null;
} }
@ -459,7 +461,7 @@
return obj; return obj;
}() }()
//提取 Hash // 提取 Hash
,hash: that.router(function(){ ,hash: that.router(function(){
return href return href
? ((href.match(/#.+/) || [])[0] || '/') ? ((href.match(/#.+/) || [])[0] || '/')
@ -504,20 +506,20 @@
return this.data(table, settings, sessionStorage); return this.data(table, settings, sessionStorage);
} }
//设备信息 // 设备信息
Layui.prototype.device = function(key){ Layui.prototype.device = function(key){
var agent = navigator.userAgent.toLowerCase() var agent = navigator.userAgent.toLowerCase()
//获取版本号 // 获取版本号
,getVersion = function(label){ ,getVersion = function(label){
var exp = new RegExp(label + '/([^\\s\\_\\-]+)'); var exp = new RegExp(label + '/([^\\s\\_\\-]+)');
label = (agent.match(exp)||[])[1]; label = (agent.match(exp)||[])[1];
return label || false; return label || false;
} }
//返回结果集 // 返回结果集
,result = { ,result = {
os: function(){ //底层操作系统 os: function(){ // 底层操作系统
if(/windows/.test(agent)){ if(/windows/.test(agent)){
return 'windows'; return 'windows';
} else if(/linux/.test(agent)){ } else if(/linux/.test(agent)){
@ -528,20 +530,20 @@
return 'mac'; return 'mac';
} }
}() }()
,ie: function(){ //ie版本 ,ie: function(){ // ie 版本
return (!!win.ActiveXObject || "ActiveXObject" in win) ? ( return (!!win.ActiveXObject || "ActiveXObject" in win) ? (
(agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于ie11并没有msie的标识 (agent.match(/msie\s(\d+)/) || [])[1] || '11' // 由于 ie11 并没有 msie 的标识
) : false; ) : false;
}() }()
,weixin: getVersion('micromessenger') //是否微信 ,weixin: getVersion('micromessenger') // 是否微信
}; };
//任意的key // 任意的 key
if(key && !result[key]){ if(key && !result[key]){
result[key] = getVersion(key); result[key] = getVersion(key);
} }
//移动设备 // 移动设备
result.android = /android/.test(agent); result.android = /android/.test(agent);
result.ios = result.os === 'ios'; result.ios = result.os === 'ios';
result.mobile = (result.android || result.ios) ? true : false; result.mobile = (result.android || result.ios) ? true : false;
@ -549,32 +551,32 @@
return result; return result;
}; };
//提示 // 提示
Layui.prototype.hint = function(){ Layui.prototype.hint = function(){
return { return {
error: error error: error
}; };
}; };
//typeof 类型细分 -> string/number/boolean/undefined/null、object/array/function/… // typeof 类型细分 -> string/number/boolean/undefined/null、object/array/function/…
Layui.prototype._typeof = Layui.prototype.type = function(operand){ Layui.prototype._typeof = Layui.prototype.type = function(operand){
if(operand === null) return String(operand); if(operand === null) return String(operand);
//细分引用类型 // 细分引用类型
return (typeof operand === 'object' || typeof operand === 'function') ? function(){ return (typeof operand === 'object' || typeof operand === 'function') ? function(){
var type = Object.prototype.toString.call(operand).match(/\s(.+)\]$/) || [] //匹配类型字符 var type = Object.prototype.toString.call(operand).match(/\s(.+)\]$/) || [] // 匹配类型字符
,classType = 'Function|Array|Date|RegExp|Object|Error|Symbol'; //常见类型字符 ,classType = 'Function|Array|Date|RegExp|Object|Error|Symbol'; // 常见类型字符
type = type[1] || 'Object'; type = type[1] || 'Object';
//除匹配到的类型外,其他对象均返回 object // 除匹配到的类型外,其他对象均返回 object
return new RegExp('\\b('+ classType + ')\\b').test(type) return new RegExp('\\b('+ classType + ')\\b').test(type)
? type.toLowerCase() ? type.toLowerCase()
: 'object'; : 'object';
}() : typeof operand; }() : typeof operand;
}; };
//对象是否具备数组结构(此处为兼容 jQuery 对象) // 对象是否具备数组结构(此处为兼容 jQuery 对象)
Layui.prototype._isArray = Layui.prototype.isArray = function(obj){ Layui.prototype._isArray = Layui.prototype.isArray = function(obj){
var that = this var that = this
,len ,len
@ -582,24 +584,24 @@
if(!obj || (typeof obj !== 'object') || obj === win) return false; if(!obj || (typeof obj !== 'object') || obj === win) return false;
len = 'length' in obj && obj.length; //兼容 ie len = 'length' in obj && obj.length; // 兼容 ie
return type === 'array' || len === 0 || ( return type === 'array' || len === 0 || (
typeof len === 'number' && len > 0 && (len - 1) in obj //兼容 jQuery 对象 typeof len === 'number' && len > 0 && (len - 1) in obj // 兼容 jQuery 对象
); );
}; };
//遍历 // 遍历
Layui.prototype.each = function(obj, fn){ Layui.prototype.each = function(obj, fn){
var key var key
,that = this ,that = this
,callFn = function(key, obj){ //回调 ,callFn = function(key, obj){ // 回调
return fn.call(obj[key], key, obj[key]) return fn.call(obj[key], key, obj[key])
}; };
if(typeof fn !== 'function') return that; if(typeof fn !== 'function') return that;
obj = obj || []; obj = obj || [];
//优先处理数组结构 // 优先处理数组结构
if(that.isArray(obj)){ if(that.isArray(obj)){
for(key = 0; key < obj.length; key++){ for(key = 0; key < obj.length; key++){
if(callFn(key, obj)) break; if(callFn(key, obj)) break;
@ -623,7 +625,7 @@
// 若未传入 key则直接返回原对象 // 若未传入 key则直接返回原对象
if(that.type(arr) === 'object' && !key){ if(that.type(arr) === 'object' && !key){
return clone; return clone;
} else if(typeof arr !== 'object'){ //若 arr 非对象 } else if(typeof arr !== 'object'){ // 若 arr 非对象
return [clone]; return [clone];
} }
@ -665,11 +667,11 @@
// 若为数字比较 // 若为数字比较
if(isNum[0] && isNum[1]){ if(isNum[0] && isNum[1]){
if(v1 && (!v2 && v2 !== 0)){ //数字 vs 空 if(v1 && (!v2 && v2 !== 0)){ // 数字 vs 空
return 1; return 1;
} else if((!v1 && v1 !== 0) && v2){ //空 vs 数字 } else if((!v1 && v1 !== 0) && v2){ // 空 vs 数字
return -1; return -1;
} else { //数字 vs 数字 } else { // 数字 vs 数字
return v1 - v2; return v1 - v2;
} }
} }
@ -691,9 +693,9 @@
} }
// 若为混合比较 // 若为混合比较
if(isNum[0] || !isNum[1]){ //数字 vs 非数字 if(isNum[0] || !isNum[1]){ // 数字 vs 非数字
return -1; return -1;
} else if(!isNum[0] || isNum[1]) { //非数字 vs 数字 } else if(!isNum[0] || isNum[1]) { // 非数字 vs 数字
return 1; return 1;
} }
@ -703,7 +705,7 @@
return clone; return clone;
}; };
//阻止事件冒泡 // 阻止事件冒泡
Layui.prototype.stope = function(thisEvent){ Layui.prototype.stope = function(thisEvent){
thisEvent = thisEvent || win.event; thisEvent = thisEvent || win.event;
try { thisEvent.stopPropagation() } catch(e){ try { thisEvent.stopPropagation() } catch(e){
@ -711,10 +713,10 @@
} }
}; };
//字符常理 // 字符常理
var EV_REMOVE = 'LAYUI-EVENT-REMOVE'; var EV_REMOVE = 'LAYUI-EVENT-REMOVE';
//自定义模块事件 // 自定义模块事件
Layui.prototype.onevent = function(modName, events, callback){ Layui.prototype.onevent = function(modName, events, callback){
if(typeof modName !== 'string' if(typeof modName !== 'string'
|| typeof callback !== 'function') return this; || typeof callback !== 'function') return this;
@ -722,25 +724,25 @@
return Layui.event(modName, events, null, callback); return Layui.event(modName, events, null, callback);
}; };
//执行自定义模块事件 // 执行自定义模块事件
Layui.prototype.event = Layui.event = function(modName, events, params, fn){ Layui.prototype.event = Layui.event = function(modName, events, params, fn){
var that = this var that = this
,result = null ,result = null
,filter = (events || '').match(/\((.*)\)$/)||[] //提取事件过滤器字符结构select(xxx) ,filter = (events || '').match(/\((.*)\)$/)||[] // 提取事件过滤器字符结构select(xxx)
,eventName = (modName + '.'+ events).replace(filter[0], '') //获取事件名称form.select ,eventName = (modName + '.'+ events).replace(filter[0], '') // 获取事件名称form.select
,filterName = filter[1] || '' //获取过滤器名称,xxx ,filterName = filter[1] || '' // 获取过滤器名称,xxx
,callback = function(_, item){ ,callback = function(_, item){
var res = item && item.call(that, params); var res = item && item.call(that, params);
res === false && result === null && (result = false); res === false && result === null && (result = false);
}; };
//如果参数传入特定字符,则执行移除事件 // 如果参数传入特定字符,则执行移除事件
if(params === EV_REMOVE){ if(params === EV_REMOVE){
delete (that.cache.event[eventName] || {})[filterName]; delete (that.cache.event[eventName] || {})[filterName];
return that; return that;
} }
//添加事件 // 添加事件
if(fn){ if(fn){
config.event[eventName] = config.event[eventName] || {}; config.event[eventName] = config.event[eventName] || {};
@ -755,15 +757,15 @@
return this; return this;
} }
//执行事件回调 // 执行事件回调
layui.each(config.event[eventName], function(key, item){ layui.each(config.event[eventName], function(key, item){
//执行当前模块的全部事件 // 执行当前模块的全部事件
if(filterName === '{*}'){ if(filterName === '{*}'){
layui.each(item, callback); layui.each(item, callback);
return; return;
} }
//执行指定事件 // 执行指定事件
key === '' && layui.each(item, callback); key === '' && layui.each(item, callback);
(filterName && key === filterName) && layui.each(item, callback); (filterName && key === filterName) && layui.each(item, callback);
}); });
@ -771,20 +773,20 @@
return result; return result;
}; };
//新增模块事件 // 新增模块事件
Layui.prototype.on = function(events, modName, callback){ Layui.prototype.on = function(events, modName, callback){
var that = this; var that = this;
return that.onevent.call(that, modName, events, callback); return that.onevent.call(that, modName, events, callback);
} }
//移除模块事件 // 移除模块事件
Layui.prototype.off = function(events, modName){ Layui.prototype.off = function(events, modName){
var that = this; var that = this;
return that.event.call(that, modName, events, EV_REMOVE); return that.event.call(that, modName, events, EV_REMOVE);
}; };
//exports layui // exports layui
win.layui = new Layui(); win.layui = new Layui();
}(window); //gulp build: layui-footer }(window); // gulp build: layui-footer