forked from kylefox/jquery-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.modal.min.js
12 lines (12 loc) · 3.86 KB
/
jquery.modal.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*
A simple jQuery modal (http://github.com/kylefox/jquery-modal)
Davis Tan's Fork version 0.5.2
*/
(function(a){var d=null;a.modal=function(b,c){var f,e;this.$body=a("body");this.options=a.extend({},a.modal.defaults,c);if(b.is("a"))if(e=b.attr("href"),/^#/.test(e)){this.$elm=a(e);if(1!==this.$elm.length)return null;this.open()}else this.$elm=a("<div>"),this.$body.append(this.$elm),f=function(a,b){b.elm.remove()},this.showSpinner(),b.trigger(a.modal.AJAX_SEND),a.get(e).done(function(c){d&&(b.trigger(a.modal.AJAX_SUCCESS),d.$elm.empty().append(c).on(a.modal.CLOSE,f),d.hideSpinner(),d.open(),b.trigger(a.modal.AJAX_COMPLETE))}).fail(function(){b.trigger(a.modal.AJAX_FAIL);
d.hideSpinner();b.trigger(a.modal.AJAX_COMPLETE)});else this.$elm=b,this.open()};a.modal.prototype={constructor:a.modal,open:function(){this.block();this.show();var b=a(":input:enabled:visible",this.$elm);0<b.length&&b[0].focus();a(document).on("keydown.modal",this.options,function(c){27==c.which&&c.data.escapeClose&&a.modal.close();if(9==c.which){var d=c.shiftKey&&c.target===b[0];return!c.shiftKey&&c.target===b[b.length-1]||d?(setTimeout(function(){if(b){var a=b[!0===d?b.length-1:0];a&&a.focus()}},
10),!1):0<a(c.target).parents().filter(".modal, .current").length}});this.options.clickClose&&this.blocker.click(a.modal.close)},close:function(){this.unblock();this.hide();a(document).off("keydown.modal")},block:function(){this.$elm.trigger(a.modal.BEFORE_BLOCK,[this._ctx()]);this.blocker=a('<div class="jquery-modal blocker"></div>').css({top:0,right:0,bottom:0,left:0,width:"100%",height:"100%",position:"fixed",zIndex:this.options.zIndex,background:this.options.overlay,opacity:this.options.opacity});
this.$body.append(this.blocker);this.$elm.trigger(a.modal.BLOCK,[this._ctx()])},unblock:function(){this.blocker.remove()},show:function(){this.$elm.trigger(a.modal.BEFORE_OPEN,[this._ctx()]);this.options.showClose&&(this.closeButton=a('<a href="#close-modal" rel="modal:close" class="close-modal">'+this.options.closeText+"</a>"),this.$elm.append(this.closeButton));this.$elm.addClass(this.options.modalClass+" current");this.center(!0);this.$elm.show().trigger(a.modal.OPEN,[this._ctx()])},hide:function(){this.$elm.trigger(a.modal.BEFORE_CLOSE,
[this._ctx()]);this.closeButton&&this.closeButton.remove();this.$elm.removeClass("current").hide();this.$elm.trigger(a.modal.CLOSE,[this._ctx()])},showSpinner:function(){this.options.showSpinner&&(this.spinner=this.spinner||a('<div class="'+this.options.modalClass+'-spinner"></div>').append(this.options.spinnerHtml),this.$body.append(this.spinner),this.spinner.show())},hideSpinner:function(){this.spinner&&this.spinner.remove()},center:function(b){var b=b||!1,c={position:"fixed",top:"50%",left:"50%",
marginTop:-(this.$elm.outerHeight()/2),marginLeft:-(this.$elm.outerWidth()/2),zIndex:this.options.zIndex+1};a.support.transition&&!b?this.$elm.transition(c,300):this.$elm.css(c)},_ctx:function(){return{elm:this.$elm,blocker:this.blocker,options:this.options}}};a.modal.prototype.resize=a.modal.prototype.center;a.modal.close=function(a){d&&(a&&a.preventDefault(),d.close(),d=null)};a.modal.resize=function(){d&&d.resize()};a.modal.defaults={overlay:"#000",opacity:0.6,zIndex:1,escapeClose:!0,clickClose:!0,
closeText:"Close",modalClass:"modal",spinnerHtml:null,showSpinner:!0,showClose:!0};a.modal.BEFORE_BLOCK="modal:before-block";a.modal.BLOCK="modal:block";a.modal.BEFORE_OPEN="modal:before-open";a.modal.OPEN="modal:open";a.modal.BEFORE_CLOSE="modal:before-close";a.modal.CLOSE="modal:close";a.modal.AJAX_SEND="modal:ajax:send";a.modal.AJAX_SUCCESS="modal:ajax:success";a.modal.AJAX_FAIL="modal:ajax:fail";a.modal.AJAX_COMPLETE="modal:ajax:complete";a.fn.modal=function(b){1===this.length&&(d=new a.modal(this,
b));return this};a(document).on("click",'a[rel="modal:close"]',a.modal.close);a(document).on("click",'a[rel="modal:open"]',function(b){b.preventDefault();a(this).modal()})})(jQuery);