forked from Aoiujz/thinkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.thinkbox.min.js
11 lines (11 loc) · 10.9 KB
/
jquery.thinkbox.min.js
1
2
3
4
5
6
7
8
9
10
11
/**
+-------------------------------------------------------------------
* jQuery thinkbox - 弹出层插件 - http://zjzit.cn/thinkbox
+-------------------------------------------------------------------
* @version 1.0.0 beta2
* @since 2013.05.10
* @author 麦当苗儿 <[email protected]> <http://www.zjzit.cn>
* @github https://github.com/Aoiujz/thinkbox.git
+-------------------------------------------------------------------
*/
(function(e){function c(){this.css({zIndex:s++})}function h(){return{width:f.width(),height:f.height(),left:f.scrollLeft(),top:f.scrollTop()}}function p(t,n){if(e.isFunction(t))return t.call(this,n)}function d(t,n){function r(e){if(e in n)delete n[e]}if(e.isArray(t)){for(i in t){r(t[i])}}else{r(t)}}function v(){var t=e("body")[0];t.onselectstart=function(){return false};t.unselectable="on";t.style.MozUserSelect="none";t.style.WebkitUserSelect="none"}function m(){var t=e("body")[0];t.onselectstart=function(){return true};t.unselectable="off";t.style.MozUserSelect="";t.style.WebkitUserSelect=""}function g(n,r){var i=t.slice(0,t.lastIndexOf("/"));if(e("link[href='"+i+n+"']").length){p(r);return}e("<link/>").load(function(){p(r)}).attr({href:i+n,type:"text/css",rel:"stylesheet"}).appendTo("head")}function y(){o.current=this.key;c.call(this.box)}function b(){var t=o[this.key][1];p.call(this,t.beforeUnload);this.box.remove();this.modal.remove();f.off(".thinkbox-"+this.key);o.del(this.key);t.dataEle&&e(t.dataEle).removeData("thinkbox")}function w(){var t=this,n=o[this.key][1];if(this.modal){this.modal.show();return}if(l[0]){a.width=function(){return document.documentElement.scrollWidth};a.height=function(){return document.documentElement.scrollHeight}}this.modal=e('<div class="thinkbox-modal-blackout-'+n.style+'"></div>').css({zIndex:s++,width:a.width(),height:a.height(),position:"absolute",left:0,top:0}).click(function(e){n.modalClose&&o.current&&o.get(0).hide();e.stopPropagation()}).mousedown(function(e){e.stopPropagation()}).appendTo(e("body"));f.on("resize.thinkbox-"+this.key,function(){t.modal.hide().css({width:a.width(),height:a.height()}).show()})}function E(){var t=o[this.key][1];title=e('<div class="thinkbox-title"></div>');if(t.drag){title.addClass("thinkbox-draging");S.call(this,title)}e(".thinkbox-inner",this.box).prepend(title);this.setTitle(t.title)}function S(e){var t=null,n=o[this.key][1],r=this.box,i=this;a.mousemove(function(e){t&&r.css({left:e.pageX-t[0],top:e.pageY-t[1]})});e.mousedown(function(e){var i=r.offset();if(n.fixed){i.left-=f.scrollLeft();i.top-=f.scrollTop()}v();t=[e.pageX-i.left,e.pageY-i.top]}).mouseup(function(){t=null;m();p.call(i,n.afterDrag)})}function x(){function i(t,n,i){var s=e('<button class="thinkbox-button-'+t+'"></button>').html(n).click(function(){i()}).appendTo(r)}var t=o[this.key][1],n=this,t=o[this.key][1],r=e('<div class="thinkbox-tools"><span class="thinkbox-status"></span></div>');for(key in t.button){switch(key){case"ok":i(key,t.button[key],function(){n.hide(true)});break;case"submit":i(key,t.button[key],function(){n.box.find("form").submit()});break;default:i("cancel",t.button[key],function(){n.hide(false)})}}this.box.find(".thinkbox-inner").append(r)}function T(){var t=o[this.key][1],n=this;e("<div/>").addClass("thinkbox-close").html(t.close).click(function(e){n.hide();e.stopPropagation()}).mousedown(function(e){e.stopPropagation()}).appendTo(e(".thinkbox-"+(t.title?"title":"inner"),this.box))}var t=e("script").last().attr("src"),n,r={title:null,fixed:true,center:true,clone:true,x:0,y:0,modal:true,modalClose:true,resize:true,unload:false,close:"×",escHide:true,delayClose:0,drag:false,display:true,width:"",height:"",dataEle:"",locate:["left","top"],show:["fadeIn","normal"],hide:["fadeOut","normal"],button:false,style:"default",beforeShow:undefined,afterShow:undefined,afterHide:undefined,beforeUnload:undefined,afterDrag:undefined},s=2013,o={length:0,current:null,del:function(e){delete this[e];this.length--},get:function(e){return e!=undefined?this[this.current][e]:this[this.current]},push:function(e,t){this[e.key]=[e,t];this.length++;return this}},u=['<div class="thinkbox" style="position:absolute;width: auto">','<table cellspacing="0" cellpadding="0" border="0" style="width: auto">',"<tr>",'<td class="thinkbox-top-left"></td>','<td class="thinkbox-top"></td>','<td class="thinkbox-top-right"></td>',"</tr>","<tr>",'<td class="thinkbox-left"></td>','<td class="thinkbox-inner">','<div class="thinkbox-body"></div>',"</td>",'<td class="thinkbox-right"></td>',"</tr>","<tr>",'<td class="thinkbox-bottom-left"></td>','<td class="thinkbox-bottom"></td>','<td class="thinkbox-bottom-right"></td>',"</tr>","</table>","</div>"].join(""),a=e(document),f=e(window),l=/(msie) ([\w.]+)/.exec(navigator.userAgent.toLowerCase())||[];n=function(t,n){var i=this,n,a,c;n=e.extend({},r,n||{});this.key=s;g("/skin/"+n.style+"/style.css",function(){i.box=a=e(u).addClass("thinkbox-"+n.style).data("thinkbox",i);n.dataEle&&e(n.dataEle).data("thinkbox",i);o.push(i,n);a.mousedown(function(e){y.call(i);e.stopPropagation()}).click(function(e){e.stopPropagation()});n.title!==null&&E.call(i);n.button&&x.call(i);n.close&&T.call(i);a.css("display","none").appendTo("body");c=e(".thinkbox-left",a).width();c&&e(".thinkbox-left",a).append(e("<div/>").css("width",c));n.fixed&&(l[0]&&l[1]<7?n.fixed=false:a.css("position","fixed"));i.setContent(t||"<div></div>");n.resize&&f.resize(function(){i.resetLocate()});n.display&&i.show()})};n.prototype={show:function(){function i(){n.delayClose&&e.isNumeric(n.delayClose)&&setTimeout(function(){t.hide()},n.delayClose);p.call(t,n.afterShow)}var t=this,n=o[this.key][1],r=this.box;if(r.is(":visible"))return this;n.modal&&w.call(this);p.call(this,n.beforeShow);switch(n.show[0]){case"slideDown":r.stop(true,true).slideDown(n.show[1],i);break;case"fadeIn":r.stop(true,true).fadeIn(n.show[1],i);break;default:r.show(n.show[1],i)}y.call(this);return this},hide:function(e){function i(){p.call(t,r.afterHide,e);r.unload&&b.call(t)}var t=this,n=this.box,r=o[this.key][1];if(!n.is(":visible"))return this;this.modal&&this.modal.fadeOut();switch(r.hide[0]){case"slideUp":n.stop(true,true).slideUp(r.hide[1],i);break;case"fadeOut":n.stop(true,true).fadeOut(r.hide[1],i);break;default:n.hide(r.hide[1],i)}return this},toggle:function(){return this.box.is(":visible")?self.hide():self.show()},find:function(t){var n=e(".thinkbox-body",this.box);return t?e(t,n):n.children()},getContent:function(){return e(".thinkbox-body",this.box).html()},setContent:function(t){var n=o[this.key][1];t=(n.clone?e(t).clone(true,true):e(t)).show();e(".thinkbox-body",this.box).empty().css({width:n.width,height:n.height}).append(t);this.resetLocate();return this},setSize:function(t,n){e(".thinkbox-body",this.box).css({width:t,height:n});return this},moveToCenter:function(){var e=this.getSize(),t=h(),n=this.box.css("position")=="fixed"?[0,0]:[t.left,t.top],r=n[0]+t.width/2,i=n[1]+t.height/2;this.moveTo(r-e[0]/2,i-e[1]/2)},moveTo:function(t,n){var r=this.box,i=o[this.key][1];e.isNumeric(t)&&(i.locate[0]=="left"?r.css({left:t}):r.css({right:t}));e.isNumeric(n)&&(i.locate[1]=="top"?r.css({top:n}):r.css({bottom:n}))},getSize:function(){var e=[0,0],t=this.box;if(t.is(":visible"))e=[t.width(),t.height()];else{t.css({visibility:"hidden",display:"block"});var e=[t.width(),t.height()];t.css("display","none").css("visibility","visible")}return e},setTitle:function(t){e(".thinkbox-title",this.box).empty().append("<span>"+t+"</span>");return this},resetLocate:function(){var t=o[this.key][1];t.center?this.moveToCenter():this.moveTo(e.isNumeric(t.x)?t.x:e.isFunction(t.x)?t.x.call(e(t.dataEle)):0,e.isNumeric(t.y)?t.y:e.isFunction(t.y)?t.y.call(e(t.dataEle)):0)},setStarts:function(t,n){var n=n?"thinkbox-status-"+n:"";e(".thinkbox-status",this.box).addClass(n).html(t)}};a.mousedown(function(){o.current=null}).keydown(function(e){o.current&&o.get(1).escHide&&e.keyCode==27&&o.get(0).hide()});e.thinkbox=function(t,r){if(e.isPlainObject(r)&&r.dataEle){var i=e(r.dataEle).data("thinkbox");if(i)return r.display===false?i:i.show()}return new n(t,r)};e.extend(e.thinkbox,{defaults:function(t,n){if(e.isPlainObject(t)){e.extend(r,t)}else{r[t]=n}},load:function(t,n){var r={clone:false,loading:"加载中...",type:"GET",dataType:"text",cache:false,onload:undefined},i,s,o,u,t=t.split(/\s+/);e.extend(r,n||{});o=r.onload;u=r.loading;s={data:r.data,type:r.type,dataType:r.dataType,cache:r.cache,success:function(n){t[1]&&(n=e(n).find(t[1]));if(e.isFunction(o))n=p.call(i,o,n);i.setContent(n);u||i.show()}};d(["data","type","cache","dataType","onload","loading"],r);i=u?e.thinkbox('<div class="thinkbox-load-loading">'+u+"</div>",r):e.thinkbox("<div/>",e.extend({},r,{display:false}));e.ajax(t[0],s);return i},iframe:function(t,n){var r={width:500,height:400,scrolling:"no",onload:undefined},i,s,o;e.extend(r,n||{});o=r.onload;s=e("<iframe/>").attr({width:r.width,height:r.height,frameborder:0,scrolling:r.scrolling,src:t}).load(function(){p.call(i,o)});d(["width","height","scrolling","onload"],r);i=e.thinkbox(s,r);return i},tips:function(t,n,r){var i={modalClose:false,escHide:false,unload:true,close:false,delayClose:1e3},s;switch(n){case 0:n="error";break;case 1:n="success";break}s='<div class="thinkbox-tips thinkbox-tips-'+n+'">'+t+"</div>";e.extend(i,r||{});return e.thinkbox(s,i)},success:function(e,t){return this.tips(e,"success",t)},error:function(e,t){return this.tips(e,"error",t)},loading:function(e,t){var n=t||{};n.delayClose=0;return this.tips(e,"loading",n)},msg:function(t,n){var r={drag:false,escHide:false,delayClose:0,center:false,title:"消息",x:0,y:0,locate:["right","bottom"],show:["slideDown","slow"],hide:["slideUp","slow"]},i;e.extend(r,n||{});i=e("<div/>").addClass("thinkbox-msg").html(t);return e.thinkbox(i,r)},alert:function(t,n){var r={title:"提示",modal:false,modalClose:false,unload:false},i={ok:"确定"};e.extend(r,n||{});r.ok&&(i.ok=r.ok);d("ok",r);r.button=i;var s=e("<div/>").addClass("thinkbox-alert").html(t);return e.thinkbox(s,r)},confirm:function(t,n){var r={title:"确认",modal:false,modalClose:false},i={ok:"确定",cancel:"取消"};e.extend(r,n||{});r.ok&&(i.ok=r.ok);r.cancel&&(i.cancel=r.cancel);d(["ok","cancel"],r);r.button=i;var s=e("<div/>").addClass("thinkbox-confirm").html(t);return e.thinkbox(s,r)},get:function(t){return e(t).closest(".thinkbox").data("thinkbox")}});e.fn.thinkbox=function(t){function n(t){var n=this.data("href")||this.attr("href");if(n.substr(0,1)=="#"){e.thinkbox(n,t)}else if(n.substr(0,7)=="http://"||n.substr(0,8)=="https://"){e.thinkbox.iframe(n,t)}else{e.thinkbox.load(n,t)}}if(t=="get")return e(this).data("thinkbox");return this.each(function(){var r=e(this),i=r.data("thinkbox"),s,o;switch(t){case"show":i&&i.show();break;case"hide":i&&i.hide();break;case"toggle":i&&i.toggle();break;default:s={title:r.attr("title"),dataEle:this,fixed:false,center:false,modal:false,drag:false};t=e.isPlainObject(t)?t:{};e.extend(s,{x:function(){return e(this).offset().left},y:function(){return e(this).offset().top+e(this).outerHeight()}},t);if(s.event){r.on(o,function(){n.call(r,s);return false})}else{n.call(r,s)}}})}})(jQuery)