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