优化 layer 代码书写规范

pull/1307/head
贤心 2023-07-12 16:17:26 +08:00
parent 9359023af4
commit ed38da9773
1 changed files with 159 additions and 155 deletions

View File

@ -6,12 +6,15 @@
;!function(window, undefined){ ;!function(window, undefined){
"use strict"; "use strict";
var isLayui = window.layui && layui.define, $, win, ready = { var isLayui = window.layui && layui.define;
var $;
var win;
var ready = {
getPath: function(){ getPath: function(){
var jsPath = document.currentScript ? document.currentScript.src : function(){ var jsPath = document.currentScript ? document.currentScript.src : function(){
var js = document.scripts var js = document.scripts;
,last = js.length - 1 var last = js.length - 1;
,src; var src;
for(var i = last; i > 0; i--){ for(var i = last; i > 0; i--){
if(js[i].readyState === 'interactive'){ if(js[i].readyState === 'interactive'){
src = js[i].src; src = js[i].src;
@ -19,8 +22,8 @@ var isLayui = window.layui && layui.define, $, win, ready = {
} }
} }
return src || js[last].src; return src || js[last].src;
}() }();
,GLOBAL = window.LAYUI_GLOBAL || {}; var GLOBAL = window.LAYUI_GLOBAL || {};
return GLOBAL.layer_dir || jsPath.substring(0, jsPath.lastIndexOf('/') + 1); return GLOBAL.layer_dir || jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
}(), }(),
config: { config: {
@ -46,15 +49,15 @@ var isLayui = window.layui && layui.define, $, win, ready = {
// 未设置路径,则不主动加载 css // 未设置路径,则不主动加载 css
if(!layer.path) return; if(!layer.path) return;
var head = document.getElementsByTagName("head")[0] var head = document.getElementsByTagName("head")[0];
,link = document.createElement('link'); var link = document.createElement('link');
if(typeof fn === 'string') cssname = fn; if(typeof fn === 'string') cssname = fn;
var app = (cssname || href).replace(/\.|\//g, ''); var app = (cssname || href).replace(/\.|\//g, '');
var id = 'layuicss-'+ app var id = 'layuicss-'+ app;
,STATUS_NAME = 'creating' var STATUS_NAME = 'creating'
,timeout = 0; var timeout = 0;
link.rel = 'stylesheet'; link.rel = 'stylesheet';
link.href = layer.path + href; link.href = layer.path + href;
@ -68,8 +71,8 @@ var isLayui = window.layui && layui.define, $, win, ready = {
// 轮询 css 是否加载完毕 // 轮询 css 是否加载完毕
(function poll(status) { (function poll(status) {
var delay = 100 var delay = 100;
,getLinkElem = document.getElementById(id); //获取动态插入的 link 元素 var getLinkElem = document.getElementById(id); // 获取动态插入的 link 元素
// 如果轮询超过指定秒数,则视为请求文件失败或 css 文件不符合规范 // 如果轮询超过指定秒数,则视为请求文件失败或 css 文件不符合规范
if(++timeout > 10 * 1000 / delay){ if(++timeout > 10 * 1000 / delay){
@ -97,7 +100,7 @@ var isLayui = window.layui && layui.define, $, win, ready = {
// 默认内置方法。 // 默认内置方法。
var layer = { var layer = {
v: '3.6.0', v: '3.7.0',
ie: function(){ // ie 版本 ie: function(){ // ie 版本
var agent = navigator.userAgent.toLowerCase(); var agent = navigator.userAgent.toLowerCase();
return (!!window.ActiveXObject || "ActiveXObject" in window) ? ( return (!!window.ActiveXObject || "ActiveXObject" in window) ? (
@ -539,17 +542,17 @@ Class.pt.auto = function(index){
var that = this, config = that.config, layero = $('#'+ doms[0] + index); var that = this, config = that.config, layero = $('#'+ doms[0] + index);
if(config.area[0] === '' && config.maxWidth > 0){ if(config.area[0] === '' && config.maxWidth > 0){
//为了修复IE7下一个让人难以理解的bug // 适配 ie7
if(layer.ie && layer.ie < 8 && config.btn){ if(layer.ie && layer.ie < 8 && config.btn){
layero.width(layero.innerWidth()); layero.width(layero.innerWidth());
} }
layero.outerWidth() > config.maxWidth && layero.width(config.maxWidth); layero.outerWidth() > config.maxWidth && layero.width(config.maxWidth);
} }
var area = [layero.innerWidth(), layero.innerHeight()] var area = [layero.innerWidth(), layero.innerHeight()];
,titHeight = layero.find(doms[1]).outerHeight() || 0 var titHeight = layero.find(doms[1]).outerHeight() || 0;
,btnHeight = layero.find('.'+doms[6]).outerHeight() || 0 var btnHeight = layero.find('.'+doms[6]).outerHeight() || 0;
,setHeight = function(elem){ var setHeight = function(elem){
elem = layero.find(elem); elem = layero.find(elem);
elem.height(area[1] - titHeight - btnHeight - 2*(parseFloat(elem.css('padding-top'))|0)); elem.height(area[1] - titHeight - btnHeight - 2*(parseFloat(elem.css('padding-top'))|0));
}; };
@ -597,16 +600,16 @@ Class.pt.offset = function(){
that.offsetTop = win.height() - area[1]; that.offsetTop = win.height() - area[1];
} else if(config.offset === 'l'){ // 左 } else if(config.offset === 'l'){ // 左
that.offsetLeft = 0; that.offsetLeft = 0;
} else if(config.offset === 'lt'){ //左上 } else if(config.offset === 'lt'){ // 左上
that.offsetTop = 0; that.offsetTop = 0;
that.offsetLeft = 0; that.offsetLeft = 0;
} else if(config.offset === 'lb'){ //左下 } else if(config.offset === 'lb'){ // 左下
that.offsetTop = win.height() - area[1]; that.offsetTop = win.height() - area[1];
that.offsetLeft = 0; that.offsetLeft = 0;
} else if(config.offset === 'rt'){ //右上 } else if(config.offset === 'rt'){ // 右上
that.offsetTop = 0; that.offsetTop = 0;
that.offsetLeft = win.width() - area[0]; that.offsetLeft = win.width() - area[0];
} else if(config.offset === 'rb'){ //右下 } else if(config.offset === 'rb'){ // 右下
that.offsetTop = win.height() - area[1]; that.offsetTop = win.height() - area[1];
that.offsetLeft = win.width() - area[0]; that.offsetLeft = win.width() - area[0];
} else { } else {
@ -706,13 +709,13 @@ Class.pt.tips = function(){
// 拖拽层 // 拖拽层
Class.pt.move = function(){ Class.pt.move = function(){
var that = this var that = this;
,config = that.config var config = that.config;
,_DOC = $(document) var _DOC = $(document);
,layero = that.layero var layero = that.layero;
,DATA_NAME = ['LAY_MOVE_DICT', 'LAY_RESIZE_DICT'] var DATA_NAME = ['LAY_MOVE_DICT', 'LAY_RESIZE_DICT'];
,moveElem = layero.find(config.move) var moveElem = layero.find(config.move);
,resizeElem = layero.find('.layui-layer-resize'); var resizeElem = layero.find('.layui-layer-resize');
// 给指定元素添加拖动光标 // 给指定元素添加拖动光标
if(config.move) moveElem.css('cursor', 'move'); if(config.move) moveElem.css('cursor', 'move');
@ -720,15 +723,15 @@ Class.pt.move = function(){
// 按下拖动元素 // 按下拖动元素
moveElem.on('mousedown', function(e){ moveElem.on('mousedown', function(e){
if (e.button) {return;} // 不是左键不处理 if (e.button) {return;} // 不是左键不处理
var othis = $(this) var othis = $(this);
,dict = {}; var dict = {};
if(config.move){ if(config.move){
dict.layero = layero; dict.layero = layero;
dict.config = config; dict.config = config;
dict.offset = [ dict.offset = [
e.clientX - parseFloat(layero.css('left')) e.clientX - parseFloat(layero.css('left')),
,e.clientY - parseFloat(layero.css('top')) e.clientY - parseFloat(layero.css('top'))
]; ];
othis.data(DATA_NAME[0], dict); othis.data(DATA_NAME[0], dict);
@ -741,8 +744,8 @@ Class.pt.move = function(){
// 按下右下角拉伸 // 按下右下角拉伸
resizeElem.on('mousedown', function(e){ resizeElem.on('mousedown', function(e){
var othis = $(this) var othis = $(this);
,dict = {}; var dict = {};
if(config.resize){ if(config.resize){
dict.layero = layero; dict.layero = layero;
@ -771,9 +774,9 @@ Class.pt.move = function(){
,layero = dict.layero ,layero = dict.layero
,config = dict.config; ,config = dict.config;
var X = e.clientX - dict.offset[0] var X = e.clientX - dict.offset[0];
,Y = e.clientY - dict.offset[1] var Y = e.clientY - dict.offset[1];
,fixed = layero.css('position') === 'fixed'; var fixed = layero.css('position') === 'fixed';
e.preventDefault(); e.preventDefault();
@ -782,8 +785,8 @@ Class.pt.move = function(){
// 控制元素不被拖出窗口外 // 控制元素不被拖出窗口外
if(!config.moveOut){ if(!config.moveOut){
var setRig = win.width() - layero.outerWidth() + dict.stX var setRig = win.width() - layero.outerWidth() + dict.stX;
,setBot = win.height() - layero.outerHeight() + dict.stY; var setBot = win.height() - layero.outerHeight() + dict.stY;
X < dict.stX && (X = dict.stX); X < dict.stX && (X = dict.stX);
X > setRig && (X = setRig); X > setRig && (X = setRig);
Y < dict.stY && (Y = dict.stY); Y < dict.stY && (Y = dict.stY);
@ -792,18 +795,18 @@ Class.pt.move = function(){
// 拖动时跟随鼠标位置 // 拖动时跟随鼠标位置
layero.css({ layero.css({
left: X left: X,
,top: Y top: Y
}); });
} }
// Resize // Resize
if(ready.eventResizeElem){ if(ready.eventResizeElem){
var dict = ready.eventResizeElem.data(DATA_NAME[1]) || {} var dict = ready.eventResizeElem.data(DATA_NAME[1]) || {};
,config = dict.config; var config = dict.config;
var X = e.clientX - dict.offset[0] var X = e.clientX - dict.offset[0];
,Y = e.clientY - dict.offset[1]; var Y = e.clientY - dict.offset[1];
e.preventDefault(); e.preventDefault();
@ -817,8 +820,8 @@ Class.pt.move = function(){
} }
}).on('mouseup', function(e){ }).on('mouseup', function(e){
if(ready.eventMoveElem){ if(ready.eventMoveElem){
var dict = ready.eventMoveElem.data(DATA_NAME[0]) || {} var dict = ready.eventMoveElem.data(DATA_NAME[0]) || {};
,config = dict.config; var config = dict.config;
ready.eventMoveElem.removeData(DATA_NAME[0]); ready.eventMoveElem.removeData(DATA_NAME[0]);
delete ready.eventMoveElem; delete ready.eventMoveElem;
@ -1279,13 +1282,14 @@ layer.closeLast = function(type){
layer.close($('.layui-layer-'+ type +':last').attr("times")); layer.close($('.layui-layer-'+ type +':last').attr("times"));
}; };
/**
拓展模块layui 开始合并在一起
/*
* 拓展模块layui 开始合并在一起
*/ */
var cache = layer.cache||{}, skin = function(type){
var cache = layer.cache || {};
var skin = function(type){
return (cache.skin ? (' ' + cache.skin + ' ' + cache.skin + '-'+type) : ''); return (cache.skin ? (' ' + cache.skin + ' ' + cache.skin + '-'+type) : '');
}; };
@ -1312,18 +1316,18 @@ layer.prompt = function(options, yes){
delete options.success; delete options.success;
return layer.open($.extend({ return layer.open($.extend({
type: 1 type: 1,
,btn: ['&#x786E;&#x5B9A;','&#x53D6;&#x6D88;'] btn: ['&#x786E;&#x5B9A;','&#x53D6;&#x6D88;'],
,content: content content: content,
,skin: 'layui-layer-prompt' + skin('prompt') skin: 'layui-layer-prompt' + skin('prompt'),
,maxWidth: win.width() maxWidth: win.width(),
,success: function(layero){ success: function(layero){
prompt = layero.find('.layui-layer-input'); prompt = layero.find('.layui-layer-input');
prompt.val(options.value || '').focus(); prompt.val(options.value || '').focus();
typeof success === 'function' && success(layero); typeof success === 'function' && success(layero);
} },
,resize: false resize: false,
,yes: function(index){ yes: function(index){
var value = prompt.val(); var value = prompt.val();
if(value.length > (options.maxlength||500)) { if(value.length > (options.maxlength||500)) {
layer.tips('&#x6700;&#x591A;&#x8F93;&#x5165;'+ (options.maxlength || 500) +'&#x4E2A;&#x5B57;&#x6570;', prompt, {tips: 1}); layer.tips('&#x6700;&#x591A;&#x8F93;&#x5165;'+ (options.maxlength || 500) +'&#x4E2A;&#x5B57;&#x6570;', prompt, {tips: 1});
@ -1390,10 +1394,10 @@ layer.photos = function(options, loop, key){
if(!options.photos) return; if(!options.photos) return;
// 若 photos 并非选择器或 jQuery 对象,则为普通 object // 若 photos 并非选择器或 jQuery 对象,则为普通 object
var isObject = !(typeof options.photos === 'string' || options.photos instanceof $) var isObject = !(typeof options.photos === 'string' || options.photos instanceof $);
,photos = isObject ? options.photos : {} var photos = isObject ? options.photos : {};
,data = photos.data || [] var data = photos.data || [];
,start = photos.start || 0; var start = photos.start || 0;
dict.imgIndex = (start|0) + 1; dict.imgIndex = (start|0) + 1;
options.img = options.img || 'img'; options.img = options.img || 'img';
@ -1401,7 +1405,7 @@ layer.photos = function(options, loop, key){
var success = options.success; var success = options.success;
delete options.success; delete options.success;
//如果 options.photos 不是一个对象 // options.photos 不是一个对象
if(!isObject){ // 页面直接获取 if(!isObject){ // 页面直接获取
var parent = $(options.photos), pushData = function(){ var parent = $(options.photos), pushData = function(){
data = []; data = [];
@ -1544,7 +1548,7 @@ layer.photos = function(options, loop, key){
var imgarea = [img.width, img.height]; var imgarea = [img.width, img.height];
var winarea = [$(window).width() - 100, $(window).height() - 100]; var winarea = [$(window).width() - 100, $(window).height() - 100];
//如果 实际图片的宽或者高比 屏幕大(那么进行缩放) // 实际图片的宽或者高比 屏幕大(那么进行缩放)
if(!options.full && (imgarea[0]>winarea[0]||imgarea[1]>winarea[1])){ if(!options.full && (imgarea[0]>winarea[0]||imgarea[1]>winarea[1])){
var wh = [imgarea[0]/winarea[0],imgarea[1]/winarea[1]];// 取宽度缩放比例、高度缩放比例 var wh = [imgarea[0]/winarea[0],imgarea[1]/winarea[1]];// 取宽度缩放比例、高度缩放比例
if(wh[0] > wh[1]){// 取缩放比例最大的进行缩放 if(wh[0] > wh[1]){// 取缩放比例最大的进行缩放
@ -1633,20 +1637,20 @@ ready.run = function(_$){
// 加载方式 // 加载方式
window.layui && layui.define ? ( window.layui && layui.define ? (
layer.ready() layer.ready(),
,layui.define('jquery', function(exports){ //layui 加载 layui.define('jquery', function(exports){ // layui
layer.path = layui.cache.dir; layer.path = layui.cache.dir;
ready.run(layui.$); ready.run(layui.$);
//暴露模块 // export api
window.layer = layer; window.layer = layer;
exports('layer', layer); exports('layer', layer);
}) })
) : ( ) : (
(typeof define === 'function' && define.amd) ? define(['jquery'], function(){ //requirejs 加载 (typeof define === 'function' && define.amd) ? define(['jquery'], function(){ // requirejs
ready.run(window.jQuery); ready.run(window.jQuery);
return layer; return layer;
}) : function(){ //普通 script 标签加载 }) : function(){ // 普通 script 标签引入
layer.ready(); layer.ready();
ready.run(window.jQuery); ready.run(window.jQuery);
}() }()