layer.js 3.21 KB
!function(e){"use strict";var t,n,i,s,a,o=document,d="getElementsByClassName",c=function(e){return o.querySelectorAll(e)},l={type:0,shade:!0,shadeClose:!0,fixed:!0,anim:"scale"},y={extend:function(e){var t=JSON.parse(JSON.stringify(l));for(var n in e)t[n]=e[n];return t},timer:{},end:{},touch:function(e,t){e.addEventListener("click",function(e){t.call(this,e)},!1)}},u=0,m=["layui-m-layer"],r=function(e){this.config=y.extend(e),this.view()};r.prototype.view=function(){var e=this,n=e.config,t=o.createElement("div");e.id=t.id=m[0]+u,t.setAttribute("class",m[0]+" "+m[0]+(n.type||0)),t.setAttribute("index",u);var i,s=(i="object"==typeof n.title,n.title?'<h3 style="'+(i?n.title[1]:"")+'">'+(i?n.title[0]:n.title)+"</h3>":""),a=function(){"string"==typeof n.btn&&(n.btn=[n.btn]);var e,t=(n.btn||[]).length;return 0!==t&&n.btn?(e='<span yes type="1">'+n.btn[0]+"</span>",2===t&&(e='<span no type="0">'+n.btn[1]+"</span>"+e),'<div class="layui-m-layerbtn">'+e+"</div>"):""}();if(n.fixed||(n.top=n.hasOwnProperty("top")?n.top:100,n.style=n.style||"",n.style+=" top:"+(o.body.scrollTop+n.top)+"px"),2===n.type&&(n.content='<i class="layui-m-layerload"></i><span>'+(n.content||"")+"</span>"),n.skin&&(n.anim="up"),"msg"===n.skin&&(n.shade=!1),t.innerHTML=(n.shade?"<div "+("string"==typeof n.shade?'style="'+n.shade+'"':"")+' class="layui-m-layershade"></div>':"")+'<div class="layui-m-layermain" '+(n.fixed?"":'style="position:static;"')+'><div class="layui-m-layersection layersection-'+(n.position||"center")+'"><div class="layui-m-layerchild '+(n.skin?"layui-m-layer-"+n.skin+" ":"")+(n.className?n.className:"")+" "+(n.anim?"layui-m-anim-"+n.anim:"")+'" '+(n.style?'style="'+n.style+'"':"")+">"+s+'<div class="layui-m-layercont">'+n.content+"</div>"+a+"</div></div></div>",!n.type||2===n.type){var l=o[d](m[0]+n.type);1<=l.length&&layer.close(l[0].getAttribute("index"))}document.body.appendChild(t);var r=e.elem=c("#"+e.id)[0];n.success&&n.success(r),e.index=u++,e.action(n,r)},r.prototype.action=function(e,t){var n=this;e.time&&(y.timer[n.index]=setTimeout(function(){if("msg"!=e.skin)return layer.close(n.index);t.classList.add("layer-fadeout"),setTimeout(function(){t.classList.remove("layer-fadeout"),layer.close(n.index)},300)},1e3*e.time));var i=function(){0==this.getAttribute("type")?(e.no&&e.no(),layer.close(n.index)):e.yes?e.yes(n.index):layer.close(n.index)};if(e.btn)for(var s=t[d]("layui-m-layerbtn")[0].children,a=s.length,l=0;l<a;l++)y.touch(s[l],i);if(e.shade&&e.shadeClose){var r=t[d]("layui-m-layershade")[0];y.touch(r,function(){layer.close(n.index,e.end)})}e.end&&(y.end[n.index]=e.end)},e.layer={v:"2.0",index:u,open:function(e){return new r(e||{}).index},close:function(e){var t=c("#"+m[0]+e)[0];t&&(t.innerHTML="",o.body.removeChild(t),clearTimeout(y.timer[e]),delete y.timer[e],"function"==typeof y.end[e]&&y.end[e](),delete y.end[e])},closeAll:function(){for(var e=o[d](m[0]),t=0,n=e.length;t<n;t++)layer.close(0|e[0].getAttribute("index"))}},"function"==typeof define?define(function(){return layer}):(n=document.scripts,i=n[n.length-1],s=i.src,a=s.substring(0,s.lastIndexOf("/")+1),i.getAttribute("merge")||document.head.appendChild(((t=o.createElement("link")).href=a+"need/layer.css?2.0",t.type="text/css",t.rel="styleSheet",t.id="layermcss",t)))}(window);