-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMDialog.min.js
10 lines (10 loc) · 13.1 KB
/
MDialog.min.js
1
2
3
4
5
6
7
8
9
10
/**
* M.Dialog 2.2.0
* Date: 2014-07-10
* Update: 2014-11-30
* (c) 2014-2014 M.J, http://webjyh.com
*
* This is licensed under the GNU LGPL, version 2.1 or later.
* For details, see: http://creativecommons.org/licenses/LGPL/2.1/
*/
(function(b){var c=b;var a=c.document;var d=function(e){return new d.fn.init(e)};d.version="2.2.0";d.fn=d.prototype={init:function(e){var g,f;this.IE6=!-[1]&&!c.XMLHttpRequest;this.closeBoolean=false;this.iframeId=null;this.top=null;this.config=this._cover(e,d.defaults);(c.MDialog.zIndex>=this.config.zIndex)?c.MDialog.zIndex=c.MDialog.zIndex+2:c.MDialog.zIndex=this.config.zIndex;if(this.config.id){f=a.getElementById(this.config.id);if(f){return false}}this.DOM=g=this._createDOM();g._body()._css({width:this.config.width,height:this.config.height});g._content()._padding(this.config.padding);g._footer()._hide();g._title()._css("cursor",this.config.drag?"move":"auto");if(this.config.lock){this._createLock()}g._wrap()._show();this._title(this.config.title)._content(this.config.content)._statusbar(this.config.statusbar)._button(this.config.button)._position(this.config.top,this.config.left)._addEvent();if(typeof this.config.init=="function"){this.config.init.call(this)}},title:function(e){this._title(e);this._position(this.config.top,this.config.left,true);return this},untitle:function(f){var e=this.DOM;this.config.untitle=true;f&&(this.config.unclose=true);this._title(this.config.title);this._position(this.config.top,this.config.left,true);return this},content:function(e){this._content(e);this._position(this.config.top,this.config.left,true);return this},statusbar:function(e){this._statusbar(e);this._position(this.config.top,this.config.left,true);return this},time:function(e){var f=this;setTimeout(function(){f._closeEvent(f.config.close)},e*1000);return this},close:function(e){if(typeof e=="function"){this.config.close=e}this._closeEvent(this.config.close);return this},lock:function(){var e=this.DOM,f=this;if(!this.config.lock){this.config.lock=true;this._createLock();e._lock()._bind("dblclick",function(){f._closeEvent(f.config.close)})}return this},unlock:function(){var e=this.DOM;if(this.config.lock){if(this.config.lock){a.body.removeChild(e._lock()[0])}this.config.lock=false}return this},width:function(e){this.config.width=e;this._size("width",e);return this},height:function(e){this.config.height=e;this._size("height",e);return this},padding:function(f){var e=this.DOM;this.config.padding=f;e._content()._padding(f);this._position(this.config.top,this.config.left,true);return this},position:function(g,f){var h,e;h=(typeof g=="number")?g+"px":g;e=(typeof f=="number")?f+"px":f;this.config.top=h;this.config.left=e;this._position(this.config.top,this.config.left,true);return this},button:function(e){if(this._isArray(e)){this._button(e);this._position(this.config.top,this.config.left,true)}return this},msg:function(f){var e=this.DOM;this.config.untitle=true;this.config.unclose=true;this.config.fixed=true;this.config.padding="10px 15px";this.config.top="50%";e._content()._addClass("ui-MDialog-msg");e._content()._padding(this.config.padding);e._footer()._hide();this._title(this.config.title);this._content(f);this._position(this.config.top,this.config.left,true);return this},_cover:function(e,g){var f,e=e||{};for(f in g){if(e[f]===undefined){e[f]=g[f]}}return e},_title:function(f){var e=this.DOM;e._title()._text(f);if(this.config.untitle){e._title()._hide();e._close()._addClass("untitle");this.config.drag=false}this.config.unclose&&e._close()._hide();return this},_content:function(f){var e=this.DOM;if(this.config.iframe){this._createIframe(f)}else{e._content()._html(f)}return this},_statusbar:function(f){var e=this.DOM;if(f!=null){e._footer()._show();e._statusbar()._html(f)}return this},_button:function(e){var f=e||[],h=["name","callback","focus","disabled"];var g=function(j){var l={};for(var k=0;k<h.length;k++){l[h[k]]=j[k]}return l};if(this.config.cancel!=null){f.unshift(g([this.config.cancelVal,this.config.cancel,false,false]))}if(this.config.ok!=null){f.unshift(g([this.config.okVal,this.config.ok,true,false]))}this._createButton(f);return this},_addEvent:function(){var g=this.DOM,h=this,f=this._sizzle(a),e=this._sizzle(c);g._close()._bind("click",function(){h._closeEvent(h.config.close)});if(this.config.lock){g._lock()._bind("dblclick",function(){h._closeEvent(h.config.close)})}if(this.config.fixed&&this.IE6){e()._bind("scroll",function(){h._fixedEvent()})}g._wrap()._bind("mousedown",function(){h._focusEvent()});g._header()._bind("mousedown",function(){g._wrap()._addClass("ui-MDialog-focus")});g._wrap()._bind("mouseup",function(){g._wrap()._removeClass("ui-MDialog-focus")});if(this.config.time){this.time(this.config.time)}if(this.config.esc){f()._bind("keydown",function(j){var j=j||b.event,k=j.which||j.keyCode;if(k==27){h._closeEvent(h.config.close)}})}if(this.config.resize){e()._bind("resize",function(){h._resize()})}if(this.config.drag){this._drag()}return this},_createDOM:function(){var f=a.createElement("div");f.innerHTML=d.templates;f.className="ui-MDialog-wrap";if(this.config.id){f.setAttribute("id",this.config.id)}a.body.appendChild(f);var j={_wrap:this._sizzle(f)},g=0,h=f.getElementsByTagName("*");elemLen=h.length;for(;g<elemLen;g++){var e=h[g].className.replace("ui-MDialog-","");if(e){j["_"+e]=this._sizzle(h[g])}}return j},_createLock:function(){var j=this.DOM,h=a.createElement("div"),f=a.documentElement.clientHeight,k=a.documentElement.scrollHeight,e=(f>k)?f+"px":k+"px",l='<iframe style="position:absolute;width:100%;height:100%;_filter:alpha(opacity=0);opacity=0;"></iframe>',g=c.MDialog.zIndex-1;h.className="ui-MDialog-lock";if(this.IE6){h.innerHTML=l}a.body.appendChild(h);j._lock=this._sizzle(h);j._lock()._css({position:"absolute",top:"0px",left:"0px",width:"100%",height:e,backgroundColor:this.config.background,opacity:this.config.opacity,zIndex:g});return this},_createButton:function(e){var j=this.DOM,l=this,h=0,f=e.length;for(;h<f;h++){var g=this._sizzle(a.createElement("button"));g()._html(e[h].name);if(e[h].focus){g()._addClass("ui-MDialog-autofocus")}if(e[h].disabled){g()._addClass("ui-MDialog-disabled")}if(typeof e[h].callback=="function"){var k=function(m){return function(){m.call(l)}}(e[h].callback);g()._bind("click",k)}else{if(!e[h].disabled){g()._bind("click",function(){l._closeEvent(l.config.close)})}}j._bottom()[0].appendChild(g()[0]);j._footer()._show()}},_createIframe:function(h){var e,g,j=this,f=this.DOM;this.iframeId="MDialog_IFRAME_"+c.MDialog.zIndex;c.MDialog.iframeData[this.iframeId]=this;f._title()._text("Loading...");f._body()._addClass("ui-MDialog-loading");e='<p class="loading-text">Loading...</p><iframe src="'+h+'" name="'+this.iframeId+'" id="'+this.iframeId+'" allowtransparency="true" scrolling="auto" frameborder="0" width="100%" height="100%" style="display: none;"></iframe>';f._content()._html(e);e=f._content()[0].getElementsByTagName("iframe")[0];g=f._content()[0].getElementsByTagName("p")[0];g.style.display="block";this._sizzle(e)()._bind("load",function(){e.style.display="block";g.style.display="none";f._title()._text(j.config.title);f._body()._removeClass("ui-MDialog-loading");var l;try{l=a.getElementById(j.iframeId).contentWindow.document}catch(k){}if(l){outWidth=(j.config.width!="auto")?j.config.width:l.documentElement.scrollWidth+"px",outHeight=(j.config.height!="auto")?j.config.height:l.documentElement.scrollHeight+"px";f._body()._css({width:"auto",height:"auto"});f._content()._css({width:outWidth,height:outHeight})}else{f._body()._css({width:"auto",height:"auto"});f._content()._css({width:j.config.width,height:j.config.height})}j._position(j.config.top,j.config.left,true);if(typeof j.config.oniframeload=="function"){j.config.oniframeload.call(j,l)}})},_position:function(n,h,g){var s=this.DOM,j=a.documentElement.clientWidth,l=a.documentElement.clientHeight,r=a.documentElement.scrollHeight,f=c.pageYOffset||a.documentElement.scrollTop||a.body.scrollTop,q=n.toString().indexOf("%"),m=h.toString().indexOf("%"),t,p,k,e;(this.IE6)?s._wrap()._css("position","absolute"):s._wrap()._css("position",(this.config.fixed)?"fixed":"absolute");k=parseInt(s._wrap()._getCurrentStyle("width"));e=parseInt(s._wrap()._getCurrentStyle("height"));var o=function(u,x,w){var v=(x*(u/100))-(w/2);if(v>(x-w)){return x-w}return v};t=(m>-1)?o(parseInt(h),j,k)+"px":h;p=(q>-1)?o(parseInt(n),l,e)+"px":n;if(!this.config.fixed&&r>l){p=parseInt(p)+f+"px"}this.top=p;if(this.IE6&&this.config.fixed){p=parseInt(p)+f+"px"}s._wrap()._css({left:t,top:p});if(!g){s._wrap()._css("zIndex",c.MDialog.zIndex)}return this},_sizzle:function(f){var e=this;return function(){e[0]=f;return e}},_closeEvent:function(g){var f=this,e=this.DOM;if(!this.closeBoolean){a.body.removeChild(e._wrap()[0]);if(this.config.lock){a.body.removeChild(e._lock()[0])}if(typeof g==="function"){g.call(this)}}if(this.config.iframe){delete d.iframeData[this.iframeId]}this.closeBoolean=true;delete d;return this},_fixedEvent:function(){var e=this.DOM,g=b.pageYoffset||a.documentElement.scrollTop||a.body.scrollTop,f=parseFloat(this.top);e._wrap()._css("top",(g+f)+"px");return this},_focusEvent:function(){var e=this.DOM;c.MDialog.zIndex=c.MDialog.zIndex+2;e._wrap()._css("zIndex",c.MDialog.zIndex);return this},_resize:function(){if(this.config.lock){var f=a.documentElement.clientHeight,h=a.documentElement.scrollHeight,e=(f>h)?f+"px":h+"px",g=this.DOM;if(g._lock()){g._lock()._css("height",e)}}this._position(this.config.top,this.config.left,true);return this},_drag:function(){var p=this.DOM,j=this.config.fixed,l=this.IE6,g=this._sizzle(a),k=0,e=0,o=0,n=0;var f=function(q){var q=q||b.event,z=q.clientX-o,w=q.clientY-n,s=a.documentElement.scrollHeight,A=a.documentElement.clientHeight,u=a.documentElement.clientWidth-k,t=(j&&!l)?A:((A>s)?A:s),t=t-e,r,v;r=(z<0)?0:(z>=0&&z<=u)?z:u;v=(w<0)?0:(w>=0&&w<=t)?w:t;p._wrap()._css({left:r+"px",top:v+"px"})};var m=function(){g()._unbind("mousemove",f)};var h=function(r){var r=r||b.event,s=parseFloat(p._wrap()._getCurrentStyle("top")),q=parseFloat(p._wrap()._getCurrentStyle("left"));o=r.clientX-q;n=r.clientY-s;k=parseInt(p._wrap()._getCurrentStyle("width"));e=parseInt(p._wrap()._getCurrentStyle("height"));g()._bind("mousemove",f);g()._bind("mouseup",m)};p._header()._bind("mousedown",h);return this},_getCurrentStyle:function(f){var g,h,e,j=this[0];if("defaultView" in a&&"getComputedStyle" in a.defaultView){g=a.defaultView.getComputedStyle(j,false)[f]}else{if(f=="width"||f=="height"){var k=j.getBoundingClientRect(),h=k.right-k.left,e=k.bottom-k.top;(f=="width")?g=h:g=e}else{g=j.currentStyle[f]}}return g},_css:function(e,h){var f="",g=this[0],j=arguments[0];if(typeof e==="string"){if(typeof h!==undefined){(e=="opacity")?this._opacity(h):g.style[e]=h}}else{for(i in j){(i=="opacity")?this._opacity(j[i]):g.style[i]=j[i]}}return this},_padding:function(e){if(typeof e==="string"){this._css("padding",e)}else{this._css("padding",e+"px")}return this},_size:function(e,h){var g,f=this.DOM;(typeof h=="string")?g=h:g=h+"px";f._body()._css(e,g);this._position(this.config.top,this.config.left,true);return this},_opacity:function(g){var e=this[0],f="opacity" in a.documentElement.style;if(typeof g!==undefined){f?e.style.opacity=g:e.style.filter="Alpha(opacity="+g*100+")"}return this},_addClass:function(e){var g=this[0],f=g.className.replace(/^\s+|\s+$/g,"");g.className=f+" "+e;return this},_removeClass:function(e){var g=this[0],f=g.className.replace(e,"").replace(/^\s+|\s+$/g,"");g.className=f;return this},_show:function(){this._css("display","block");return this},_hide:function(){this._css("display","none");return this},_text:function(f){var e=this[0];if(a.all){e.innerText=f}else{e.textContent=f}return this},_html:function(e){var f=this[0];f.innerHTML="";if(e.nodeType&&e.nodeType==1){f.appendChild(e)}if(e.nodeType&&e.nodeType==3||typeof e=="string"){f.innerHTML=e}return this},_isArray:function(e){return Object.prototype.toString.call(e)==="[object Array]"},_bind:function(e,f){var g=this[0];if(g.addEventListener){g.addEventListener(e,f,false)}else{g.attachEvent("on"+e,f)}return this},_unbind:function(e,f){var g=this[0];if(g.removeEventListener){g.removeEventListener(e,f,false)}else{g.detachEvent("on"+e,f)}return this}};d.fn.init.prototype=d.fn;d.iframeData={};d.getIframe=function(e){if(!e){return false}return d.iframeData[e]};d.templates='<div class="MDialog-wrapper"><table class="ui-MDialog-table"><tbody><tr><td><div class="ui-MDialog-header"><a class="ui-MDialog-close" href="javascript:void(0);">x</a><div class="ui-MDialog-title"></div></div></td></tr><tr><td><div class="ui-MDialog-body"><div class="ui-MDialog-content"></div></div></td></tr><tr><td><div class="ui-MDialog-footer"><div class="ui-MDialog-statusbar"></div><div class="ui-MDialog-bottom"></div><div style="clear:both;height:0;overflow:hidden;"></div></div></td></tr></tbody></table></div>';d.defaults={title:"\u6d88\u606f",untitle:false,content:"Loading....",statusbar:null,init:null,close:null,unclose:false,width:"auto",height:"auto",padding:"20px 15px",lock:false,background:"#000",opacity:0.3,fixed:false,esc:true,time:null,left:"50%",top:"38.2%",zIndex:1992,id:null,ok:null,cancel:null,okVal:"\u786e\u5b9a",cancelVal:"\u53d6\u6d88",button:null,resize:true,data:null,iframe:false,oniframeload:null,drag:true};if(typeof define==="function"&&define.amd){define("MDialog",[],function(){return d})}c.MDialog=$M=d}(window));