feat: 项目基础框架+配置+Kernel
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
/*!
|
||||
|
||||
@Name:layer mobile v2.0.0 弹层组件移动版
|
||||
@Author:贤心
|
||||
@Site:http://layer.layui.com/mobie/
|
||||
@License:MIT
|
||||
|
||||
*/
|
||||
|
||||
layui.define(function(exports){
|
||||
|
||||
"use strict";
|
||||
|
||||
var win = window, doc = document, query = 'querySelectorAll', claname = 'getElementsByClassName', S = function(s){
|
||||
return doc[query](s);
|
||||
};
|
||||
|
||||
//默认配置
|
||||
var config = {
|
||||
type: 0
|
||||
,shade: true
|
||||
,shadeClose: true
|
||||
,fixed: true
|
||||
,anim: 'scale' //默认动画类型
|
||||
};
|
||||
|
||||
var ready = {
|
||||
extend: function(obj){
|
||||
var newobj = JSON.parse(JSON.stringify(config));
|
||||
for(var i in obj){
|
||||
newobj[i] = obj[i];
|
||||
}
|
||||
return newobj;
|
||||
},
|
||||
timer: {}, end: {}
|
||||
};
|
||||
|
||||
//点触事件
|
||||
ready.touch = function(elem, fn){
|
||||
elem.addEventListener('click', function(e){
|
||||
fn.call(this, e);
|
||||
}, false);
|
||||
};
|
||||
|
||||
var index = 0, classs = ['layui-m-layer'], Layer = function(options){
|
||||
var that = this;
|
||||
that.config = ready.extend(options);
|
||||
that.view();
|
||||
};
|
||||
|
||||
Layer.prototype.view = function(){
|
||||
var that = this, config = that.config, layerbox = doc.createElement('div');
|
||||
|
||||
that.id = layerbox.id = classs[0] + index;
|
||||
layerbox.setAttribute('class', classs[0] + ' ' + classs[0]+(config.type || 0));
|
||||
layerbox.setAttribute('index', index);
|
||||
|
||||
//标题区域
|
||||
var title = (function(){
|
||||
var titype = typeof config.title === 'object';
|
||||
return config.title
|
||||
? '<h3 style="'+ (titype ? config.title[1] : '') +'">'+ (titype ? config.title[0] : config.title) +'</h3>'
|
||||
: '';
|
||||
}());
|
||||
|
||||
//按钮区域
|
||||
var button = (function(){
|
||||
typeof config.btn === 'string' && (config.btn = [config.btn]);
|
||||
var btns = (config.btn || []).length, btndom;
|
||||
if(btns === 0 || !config.btn){
|
||||
return '';
|
||||
}
|
||||
btndom = '<span yes type="1">'+ config.btn[0] +'</span>'
|
||||
if(btns === 2){
|
||||
btndom = '<span no type="0">'+ config.btn[1] +'</span>' + btndom;
|
||||
}
|
||||
return '<div class="layui-m-layerbtn">'+ btndom + '</div>';
|
||||
}());
|
||||
|
||||
if(!config.fixed){
|
||||
config.top = config.hasOwnProperty('top') ? config.top : 100;
|
||||
config.style = config.style || '';
|
||||
config.style += ' top:'+ ( doc.body.scrollTop + config.top) + 'px';
|
||||
}
|
||||
|
||||
if(config.type === 2){
|
||||
config.content = '<i></i><i class="layui-m-layerload"></i><i></i><p>'+ (config.content||'') +'</p>';
|
||||
}
|
||||
|
||||
if(config.skin) config.anim = 'up';
|
||||
if(config.skin === 'msg') config.shade = false;
|
||||
|
||||
layerbox.innerHTML = (config.shade ? '<div '+ (typeof config.shade === 'string' ? 'style="'+ config.shade +'"' : '') +' class="layui-m-layershade"></div>' : '')
|
||||
+'<div class="layui-m-layermain" '+ (!config.fixed ? 'style="position:static;"' : '') +'>'
|
||||
+'<div class="layui-m-layersection">'
|
||||
+'<div class="layui-m-layerchild '+ (config.skin ? 'layui-m-layer-' + config.skin + ' ' : '') + (config.className ? config.className : '') + ' ' + (config.anim ? 'layui-m-anim-' + config.anim : '') +'" ' + ( config.style ? 'style="'+config.style+'"' : '' ) +'>'
|
||||
+ title
|
||||
+'<div class="layui-m-layercont">'+ config.content +'</div>'
|
||||
+ button
|
||||
+'</div>'
|
||||
+'</div>'
|
||||
+'</div>';
|
||||
|
||||
if(!config.type || config.type === 2){
|
||||
var dialogs = doc[claname](classs[0] + config.type), dialen = dialogs.length;
|
||||
if(dialen >= 1){
|
||||
layer.close(dialogs[0].getAttribute('index'))
|
||||
}
|
||||
}
|
||||
|
||||
document.body.appendChild(layerbox);
|
||||
var elem = that.elem = S('#'+that.id)[0];
|
||||
config.success && config.success(elem);
|
||||
|
||||
that.index = index++;
|
||||
that.action(config, elem);
|
||||
};
|
||||
|
||||
Layer.prototype.action = function(config, elem){
|
||||
var that = this;
|
||||
|
||||
//自动关闭
|
||||
if(config.time){
|
||||
ready.timer[that.index] = setTimeout(function(){
|
||||
layer.close(that.index);
|
||||
}, config.time*1000);
|
||||
}
|
||||
|
||||
//确认取消
|
||||
var btn = function(){
|
||||
var type = this.getAttribute('type');
|
||||
if(type == 0){
|
||||
config.no && config.no();
|
||||
layer.close(that.index);
|
||||
} else {
|
||||
config.yes ? config.yes(that.index) : layer.close(that.index);
|
||||
}
|
||||
};
|
||||
if(config.btn){
|
||||
var btns = elem[claname]('layui-m-layerbtn')[0].children, btnlen = btns.length;
|
||||
for(var ii = 0; ii < btnlen; ii++){
|
||||
ready.touch(btns[ii], btn);
|
||||
}
|
||||
}
|
||||
|
||||
//点遮罩关闭
|
||||
if(config.shade && config.shadeClose){
|
||||
var shade = elem[claname]('layui-m-layershade')[0];
|
||||
ready.touch(shade, function(){
|
||||
layer.close(that.index, config.end);
|
||||
});
|
||||
}
|
||||
|
||||
config.end && (ready.end[that.index] = config.end);
|
||||
};
|
||||
|
||||
var layer = {
|
||||
v: '2.0 m',
|
||||
index: index,
|
||||
|
||||
//核心方法
|
||||
open: function(options){
|
||||
var o = new Layer(options || {});
|
||||
return o.index;
|
||||
},
|
||||
|
||||
close: function(index){
|
||||
var ibox = S('#'+classs[0]+index)[0];
|
||||
if(!ibox) return;
|
||||
ibox.innerHTML = '';
|
||||
doc.body.removeChild(ibox);
|
||||
clearTimeout(ready.timer[index]);
|
||||
delete ready.timer[index];
|
||||
typeof ready.end[index] === 'function' && ready.end[index]();
|
||||
delete ready.end[index];
|
||||
},
|
||||
|
||||
//关闭所有layer层
|
||||
closeAll: function(){
|
||||
var boxs = doc[claname](classs[0]);
|
||||
for(var i = 0, len = boxs.length; i < len; i++){
|
||||
layer.close((boxs[0].getAttribute('index')|0));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports('layer-mobile', layer);
|
||||
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
|
||||
@Name:layim mobile 开源包
|
||||
@Author:贤心
|
||||
@License:MIT
|
||||
|
||||
*/
|
||||
|
||||
layui.define(function(exports){
|
||||
exports('layim-mobile', layui.v);
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,166 @@
|
||||
/*!
|
||||
|
||||
@Title: layui.upload 单文件上传 - 全浏览器兼容版
|
||||
@Author: 贤心
|
||||
@License:MIT
|
||||
|
||||
*/
|
||||
|
||||
layui.define(['layer-mobile', 'zepto'] , function(exports){
|
||||
"use strict";
|
||||
|
||||
var $ = layui.zepto;
|
||||
var layer = layui['layer-mobile'];
|
||||
var device = layui.device();
|
||||
|
||||
var elemDragEnter = 'layui-upload-enter';
|
||||
var elemIframe = 'layui-upload-iframe';
|
||||
|
||||
var msgConf = {
|
||||
icon: 2
|
||||
,shift: 6
|
||||
}, fileType = {
|
||||
file: '文件'
|
||||
,video: '视频'
|
||||
,audio: '音频'
|
||||
};
|
||||
|
||||
layer.msg = function(content){
|
||||
return layer.open({
|
||||
content: content || ''
|
||||
,skin: 'msg'
|
||||
,time: 2 //2秒后自动关闭
|
||||
});
|
||||
};
|
||||
|
||||
var Upload = function(options){
|
||||
this.options = options;
|
||||
};
|
||||
|
||||
//初始化渲染
|
||||
Upload.prototype.init = function(){
|
||||
var that = this, options = that.options;
|
||||
var body = $('body'), elem = $(options.elem || '.layui-upload-file');
|
||||
var iframe = $('<iframe id="'+ elemIframe +'" class="'+ elemIframe +'" name="'+ elemIframe +'"></iframe>');
|
||||
|
||||
//插入iframe
|
||||
$('#'+elemIframe)[0] || body.append(iframe);
|
||||
|
||||
return elem.each(function(index, item){
|
||||
item = $(item);
|
||||
var form = '<form target="'+ elemIframe +'" method="'+ (options.method||'post') +'" key="set-mine" enctype="multipart/form-data" action="'+ (options.url||'') +'"></form>';
|
||||
|
||||
var type = item.attr('lay-type') || options.type; //获取文件类型
|
||||
|
||||
//包裹ui元素
|
||||
if(!options.unwrap){
|
||||
form = '<div class="layui-box layui-upload-button">' + form + '<span class="layui-upload-icon"><i class="layui-icon"></i>'+ (
|
||||
item.attr('lay-title') || options.title|| ('上传'+ (fileType[type]||'图片') )
|
||||
) +'</span></div>';
|
||||
}
|
||||
|
||||
form = $(form);
|
||||
|
||||
//拖拽支持
|
||||
if(!options.unwrap){
|
||||
form.on('dragover', function(e){
|
||||
e.preventDefault();
|
||||
$(this).addClass(elemDragEnter);
|
||||
}).on('dragleave', function(){
|
||||
$(this).removeClass(elemDragEnter);
|
||||
}).on('drop', function(){
|
||||
$(this).removeClass(elemDragEnter);
|
||||
});
|
||||
}
|
||||
|
||||
//如果已经实例化,则移除包裹元素
|
||||
if(item.parent('form').attr('target') === elemIframe){
|
||||
if(options.unwrap){
|
||||
item.unwrap();
|
||||
} else {
|
||||
item.parent().next().remove();
|
||||
item.unwrap().unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
//包裹元素
|
||||
item.wrap(form);
|
||||
|
||||
//触发上传
|
||||
item.off('change').on('change', function(){
|
||||
that.action(this, type);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
//提交上传
|
||||
Upload.prototype.action = function(input, type){
|
||||
var that = this, options = that.options, val = input.value;
|
||||
var item = $(input), ext = item.attr('lay-ext') || options.ext || ''; //获取支持上传的文件扩展名;
|
||||
|
||||
if(!val){
|
||||
return;
|
||||
};
|
||||
|
||||
//校验文件
|
||||
switch(type){
|
||||
case 'file': //一般文件
|
||||
if(ext && !RegExp('\\w\\.('+ ext +')$', 'i').test(escape(val))){
|
||||
layer.msg('不支持该文件格式', msgConf);
|
||||
return input.value = '';
|
||||
}
|
||||
break;
|
||||
case 'video': //视频文件
|
||||
if(!RegExp('\\w\\.('+ (ext||'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(val))){
|
||||
layer.msg('不支持该视频格式', msgConf);
|
||||
return input.value = '';
|
||||
}
|
||||
break;
|
||||
case 'audio': //音频文件
|
||||
if(!RegExp('\\w\\.('+ (ext||'mp3|wav|mid') +')$', 'i').test(escape(val))){
|
||||
layer.msg('不支持该音频格式', msgConf);
|
||||
return input.value = '';
|
||||
}
|
||||
break;
|
||||
default: //图片文件
|
||||
if(!RegExp('\\w\\.('+ (ext||'jpg|png|gif|bmp|jpeg') +')$', 'i').test(escape(val))){
|
||||
layer.msg('不支持该图片格式', msgConf);
|
||||
return input.value = '';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
options.before && options.before(input);
|
||||
item.parent().submit();
|
||||
|
||||
var iframe = $('#'+elemIframe), timer = setInterval(function() {
|
||||
var res;
|
||||
try {
|
||||
res = iframe.contents().find('body').text();
|
||||
} catch(e) {
|
||||
layer.msg('上传接口存在跨域', msgConf);
|
||||
clearInterval(timer);
|
||||
}
|
||||
if(res){
|
||||
clearInterval(timer);
|
||||
iframe.contents().find('body').html('');
|
||||
try {
|
||||
res = JSON.parse(res);
|
||||
} catch(e){
|
||||
res = {};
|
||||
return layer.msg('请对上传接口返回JSON字符', msgConf);
|
||||
}
|
||||
typeof options.success === 'function' && options.success(res, input);
|
||||
}
|
||||
}, 30);
|
||||
|
||||
input.value = '';
|
||||
};
|
||||
|
||||
//暴露接口
|
||||
exports('upload-mobile', function(options){
|
||||
var upload = new Upload(options = options || {});
|
||||
upload.init();
|
||||
});
|
||||
});
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user