/** * * @authors yutent (yutent@doui.cc) * @date 2016-09-21 01:36:29 * */ "use strict"; define(['yua', 'lib/drag', 'css!./skin/def'], function(yua){ if(window.layer){ return window.layer } yua.ui.layer = '0.0.4-base' var layerDom = {}, layerObj = {}, unique = null, //储存当前打开的1/2/3类型的弹窗 lid = 0, defconf = { type: 1, // 弹窗类型 skin: 'def', //默认主题 icon: 1, //图标类型 background: '#fff', shade: true, //遮罩 shadeClose: false, //遮罩点击关闭弹窗 radius: '0px', //弹窗圆角半径 area: ['auto', 'auto'], title: '', //弹窗主标题(在工具栏上的) menubar: true, //是否显示菜单栏 content: '', // 弹窗的内容 fixed: false, //是否固定不可拖拽 offset: null, //弹窗出来时的坐标, 为数组,可有4个值,依次是 上右下左 btns: ['确定', '取消'], //弹窗的2个按钮的文字 yes: close, //确定按钮对应的回调 no: close, //取消按钮对应的回调 success: null //弹窗初始化完成时的回调 }; function uuid(){ return 'layer-' + (++lid) } function close(id){ if(typeof id !== 'string' && typeof id !== 'number'){ return console.error(new Error('要关闭的layer实例不存在')) } if(/^\$wrap\-/.test(id) || layerObj['$wrap-' + id]){ try { id = (layerObj['$wrap-' + id] ? '$wrap-' : '') + id; //未显示过,忽略 if(!layerObj[id].show){ return } layerObj[id].parentElem.replaceChild(layerObj[id].wrap, layerDom[id][1]) layerObj[id].wrap.style.display = 'none' layerObj[id].show = false }catch(err){} }else{ try { document.body.removeChild(layerDom[id][1]) document.body.removeChild(layerDom[id][0]) unique = null }catch(err){} delete layerDom[id] delete yua.vmodels[id] } } function reapeat(str, num){ var idx = 0, result = '' while(idx < num){ result += str idx++ } return result } function fixOffset(val){ if(!val && val !== 0){ return 'auto' }else{ return val } } var __constructor = function(conf){ if(conf){ this.ready(conf).append().show() } }, __layer = { alert: function(msg, conf){ if(typeof conf === 'function'){ conf = {yes: conf, no: conf} }else if(typeof conf === 'object'){ conf = conf }else{ conf = {} } conf.icon = 6 conf.content = msg return __layer.open(conf) }, confirm: function(msg, conf){ if(typeof conf === 'function'){ conf = {yes: conf} }else if(typeof conf === 'object'){ conf = conf }else{ conf = {} } conf.type = 2 conf.icon = 8 conf.content = msg return __layer.open(conf) }, msg: function(msg, conf){ if(typeof conf !== 'object'){ var tmp = conf conf = {timeout: 2500} if(typeof tmp === 'number'){ conf.icon = tmp } } if(!conf.hasOwnProperty('timeout')){ conf.timeout = 2500 } conf.specialMode = true;//特殊模式 conf.content = '
' + msg + '
' conf.type = 7 conf.fixed = true conf.shade = false conf.menubar = false conf.radius = '5px' return __layer.open(conf) }, loading: function(style, time, cb){ style = style >>> 0 if(typeof time === 'function'){ cb = time; time = 0 } else{ time = time >>> 0 if(typeof cb !== 'function'){ cb = yua.noop } } return __layer.open({type: 6, load: style, yes: cb, timeout: time, menubar: false, background: 'none', fixed: true}) }, tips: function(msg, elem, conf){ if(!(elem instanceof HTMLElement)){ return console.error(new Error('tips类型必须指定一个目标容器')) } if(typeof conf !== 'object'){ var tmp = conf conf = {timeout: 2500} if(typeof tmp === 'number'){ conf.icon = tmp } } if(!conf.hasOwnProperty('timeout')){ conf.timeout = 2500 } if(!conf.background){ conf.background = 'rgba(0,0,0,.5)' } if(!conf.color){ conf.color = '#fff' } conf.$elem = elem conf.content = msg conf.type = 5; conf.icon = 0; conf.fixed = true; conf.shade = false; conf.menubar = false; return __layer.open(conf) }, prompt: function(msg, yescb, nocb){ if(typeof yescb !== 'function'){ return console.error('argument [callback] requires a function, but ' + (typeof yescb) + ' given') } var conf = { type: 3, icon: 7, prompt: '', title: msg, content: '', yes: function(id){ yescb(id, yua.vmodels[id].prompt) } } if(typeof nocb === 'function'){ conf.no = nocb } return __layer.open(conf) }, use: function(skin, callback){ require(['css!./skin/' + skin], callback) }, close: close, open: function(conf){ if(typeof conf === 'string'){ conf = '$wrap-' + conf if(!layerObj[conf]){ throw new Error('layer实例不存在') }else{ //只能显示一个实例 if(layerObj[conf].show){ return conf } layerObj[conf].show = true if(!yua.vmodels[conf]){ yua(layerObj[conf].obj.init) } layerObj[conf].parentElem.appendChild(layerDom[conf][1]) layerDom[conf][1].querySelector('.detail').appendChild(layerObj[conf].wrap) layerObj[conf].wrap.style.display = '' yua.scan(layerDom[conf][1]) layerObj[conf].obj.show() return conf } }else{ return new __constructor(conf).init.$id } }, version: yua.ui.layer }; /*type: { // 弹窗类型对应的id值 1: 'alert', 2: 'confirm', 3: 'prompt', 4: 'iframe', 5: 'tips', 6: 'loading', 7: 'msg', }*/ __constructor.prototype = { dot: { //loading的子元素数量 0: 4, 1: 9, 2: 2, 3: 3, 4: 2, 5: 5, 6: 5, 7: 5 }, timeout: null, create: function(){ var layBox = document.createElement('div'), coverBox = document.createElement('div'); coverBox.className = 'do-layer-cover type-' + this.init.type // 允许点击遮罩关闭弹层时, 添加控制器 if(this.init.shadeClose){ coverBox.setAttribute(':controller', this.cInit.$id) coverBox.setAttribute(':click', 'close(\'' + this.init.$id + '\')') } layBox.className = 'do-layer skin-' + this.init.skin + (this.init.type === 5 && ' active' || '') + ' type-' + ((!this.init.specialMode && this.init.type === 7) ? 'unspecial' : this.init.type); //暂时隐藏,避免修正定位时,能看到闪一下 layBox.style.visibility = 'hidden' layBox.style.borderRadius = this.init.radius layBox.setAttribute(':controller', this.init.$id) //没有菜单栏, 且未禁止拖拽,则加上可拖拽属性 if(!this.init.menubar && !this.init.fixed){ layBox.setAttribute(':drag', '') layBox.setAttribute('data-limit', 'window') } //弹窗的宽高 var boxcss = '' if(this.init.area[0] !== 'auto'){ boxcss += 'width: ' + this.init.area[0] + ';' } if(this.init.area[1] !== 'auto'){ boxcss += 'height: ' + this.init.area[1] + ';' } layBox.innerHTML = this.getMenubar() + ' ' + this.getBtns() + (this.init.type === 5 && '' || '') delete this.init.wrap return [this.init.shade ? coverBox : null, layBox] }, getCont: function(){ if(this.init.type === 6){ return this.getLoading(this.init.load) }else{ return this.getIcon() + '' } }, getLoading: function(style){ return '