From 08cfc99988548d72b7785dab5bb577bf9a2e39cc Mon Sep 17 00:00:00 2001 From: Ben Smithett Date: Wed, 6 Feb 2013 13:46:22 +1100 Subject: [PATCH 1/4] Namespace automatically attached click event listeners --- jquery.modal.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jquery.modal.js b/jquery.modal.js index 5ceffdc..21403fd 100644 --- a/jquery.modal.js +++ b/jquery.modal.js @@ -177,8 +177,8 @@ }; // Automatically bind links with rel="modal:close" to, well, close the modal. - $(document).on('click', 'a[rel="modal:close"]', $.modal.close); - $(document).on('click', 'a[rel="modal:open"]', function(event) { + $(document).on('click.modal', 'a[rel="modal:close"]', $.modal.close); + $(document).on('click.modal', 'a[rel="modal:open"]', function(event) { event.preventDefault(); $(this).modal(); }); From 04e8fad424bc9b30352e399bfa3363c57a4122c9 Mon Sep 17 00:00:00 2001 From: zjr Date: Sat, 6 Apr 2013 01:06:04 -0400 Subject: [PATCH 2/4] Would close #39 if necessary --- README.md | 3 +-- jquery.modal.js | 4 +++- jquery.modal.min.js | 5 ++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 72cca15..634e5a7 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,6 @@ Because there can be only one modal active at a single time, there's no need to $.modal.close(); -_TODO: this should be changed so that when called on a specific element, the element is returned (normal jQuery fashion)._ Similar to how links can be automatically bound to open modals, they can be bound to close modals using `rel="modal:close"`: @@ -215,4 +214,4 @@ jQuery Modal is distributed under the [MIT License](Learn more at http://opensou NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/jquery.modal.js b/jquery.modal.js index 21403fd..0506d02 100644 --- a/jquery.modal.js +++ b/jquery.modal.js @@ -1,6 +1,6 @@ /* A simple jQuery modal (http://github.com/kylefox/jquery-modal) - Version 0.5.2 + Version 0.5.3 */ (function($) { @@ -136,7 +136,9 @@ if (!current) return; if (event) event.preventDefault(); current.close(); + that = current.$elm; current = null; + return that; }; $.modal.resize = function() { diff --git a/jquery.modal.min.js b/jquery.modal.min.js index 19adf6d..69d978b 100644 --- a/jquery.modal.min.js +++ b/jquery.modal.min.js @@ -1,5 +1,4 @@ /* A simple jQuery modal (http://github.com/kylefox/jquery-modal) - Version 0.5.2 -*/ -(function($){var current=null;$.modal=function(el,options){$.modal.close();var remove,target;this.$body=$('body');this.options=$.extend({},$.modal.defaults,options);if(el.is('a')){target=el.attr('href');if(/^#/.test(target)){this.$elm=$(target);if(this.$elm.length!==1)return null;this.open()}else{this.$elm=$('
');this.$body.append(this.$elm);remove=function(event,modal){modal.elm.remove()};this.showSpinner();el.trigger($.modal.AJAX_SEND);$.get(target).done(function(html){if(!current)return;el.trigger($.modal.AJAX_SUCCESS);current.$elm.empty().append(html).on($.modal.CLOSE,remove);current.hideSpinner();current.open();el.trigger($.modal.AJAX_COMPLETE)}).fail(function(){el.trigger($.modal.AJAX_FAIL);current.hideSpinner();el.trigger($.modal.AJAX_COMPLETE)})}}else{this.$elm=el;this.open()}};$.modal.prototype={constructor:$.modal,open:function(){this.block();this.show();if(this.options.escapeClose){$(document).on('keydown.modal',function(event){if(event.which==27)$.modal.close()})}if(this.options.clickClose)this.blocker.click($.modal.close)},close:function(){this.unblock();this.hide();$(document).off('keydown.modal')},block:function(){this.$elm.trigger($.modal.BEFORE_BLOCK,[this._ctx()]);this.blocker=$('
').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($.modal.BLOCK,[this._ctx()])},unblock:function(){this.blocker.remove()},show:function(){this.$elm.trigger($.modal.BEFORE_OPEN,[this._ctx()]);if(this.options.showClose){this.closeButton=$(''+this.options.closeText+'');this.$elm.append(this.closeButton)}this.$elm.addClass(this.options.modalClass+' current');this.center();this.$elm.show().trigger($.modal.OPEN,[this._ctx()])},hide:function(){this.$elm.trigger($.modal.BEFORE_CLOSE,[this._ctx()]);if(this.closeButton)this.closeButton.remove();this.$elm.removeClass('current').hide();this.$elm.trigger($.modal.CLOSE,[this._ctx()])},showSpinner:function(){if(!this.options.showSpinner)return;this.spinner=this.spinner||$('
').append(this.options.spinnerHtml);this.$body.append(this.spinner);this.spinner.show()},hideSpinner:function(){if(this.spinner)this.spinner.remove()},center:function(){this.$elm.css({position:'fixed',top:"50%",left:"50%",marginTop:-(this.$elm.outerHeight()/2),marginLeft:-(this.$elm.outerWidth()/2),zIndex:this.options.zIndex+1})},_ctx:function(){return{elm:this.$elm,blocker:this.blocker,options:this.options}}};$.modal.prototype.resize=$.modal.prototype.center;$.modal.close=function(event){if(!current)return;if(event)event.preventDefault();current.close();current=null};$.modal.resize=function(){if(!current)return;current.resize()};$.modal.defaults={overlay:"#000",opacity:0.75,zIndex:1,escapeClose:true,clickClose:true,closeText:'Close',modalClass:"modal",spinnerHtml:null,showSpinner:true,showClose:true};$.modal.BEFORE_BLOCK='modal:before-block';$.modal.BLOCK='modal:block';$.modal.BEFORE_OPEN='modal:before-open';$.modal.OPEN='modal:open';$.modal.BEFORE_CLOSE='modal:before-close';$.modal.CLOSE='modal:close';$.modal.AJAX_SEND='modal:ajax:send';$.modal.AJAX_SUCCESS='modal:ajax:success';$.modal.AJAX_FAIL='modal:ajax:fail';$.modal.AJAX_COMPLETE='modal:ajax:complete';$.fn.modal=function(options){if(this.length===1){current=new $.modal(this,options)}return this};$(document).on('click','a[rel="modal:close"]',$.modal.close);$(document).on('click','a[rel="modal:open"]',function(event){event.preventDefault();$(this).modal()})})(jQuery); \ No newline at end of file + Version 0.5.3 +*/(function(e){var t=null;e.modal=function(n,r){e.modal.close();var i,s;this.$body=e("body"),this.options=e.extend({},e.modal.defaults,r);if(n.is("a")){s=n.attr("href");if(/^#/.test(s)){this.$elm=e(s);if(this.$elm.length!==1)return null;this.open()}else this.$elm=e("
"),this.$body.append(this.$elm),i=function(e,t){t.elm.remove()},this.showSpinner(),n.trigger(e.modal.AJAX_SEND),e.get(s).done(function(r){if(!t)return;n.trigger(e.modal.AJAX_SUCCESS),t.$elm.empty().append(r).on(e.modal.CLOSE,i),t.hideSpinner(),t.open(),n.trigger(e.modal.AJAX_COMPLETE)}).fail(function(){n.trigger(e.modal.AJAX_FAIL),t.hideSpinner(),n.trigger(e.modal.AJAX_COMPLETE)})}else this.$elm=n,this.open()},e.modal.prototype={constructor:e.modal,open:function(){this.block(),this.show(),this.options.escapeClose&&e(document).on("keydown.modal",function(t){t.which==27&&e.modal.close()}),this.options.clickClose&&this.blocker.click(e.modal.close)},close:function(){this.unblock(),this.hide(),e(document).off("keydown.modal")},block:function(){this.$elm.trigger(e.modal.BEFORE_BLOCK,[this._ctx()]),this.blocker=e('
').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(e.modal.BLOCK,[this._ctx()])},unblock:function(){this.blocker.remove()},show:function(){this.$elm.trigger(e.modal.BEFORE_OPEN,[this._ctx()]),this.options.showClose&&(this.closeButton=e(''+this.options.closeText+""),this.$elm.append(this.closeButton)),this.$elm.addClass(this.options.modalClass+" current"),this.center(),this.$elm.show().trigger(e.modal.OPEN,[this._ctx()])},hide:function(){this.$elm.trigger(e.modal.BEFORE_CLOSE,[this._ctx()]),this.closeButton&&this.closeButton.remove(),this.$elm.removeClass("current").hide(),this.$elm.trigger(e.modal.CLOSE,[this._ctx()])},showSpinner:function(){if(!this.options.showSpinner)return;this.spinner=this.spinner||e('
').append(this.options.spinnerHtml),this.$body.append(this.spinner),this.spinner.show()},hideSpinner:function(){this.spinner&&this.spinner.remove()},center:function(){this.$elm.css({position:"fixed",top:"50%",left:"50%",marginTop:-(this.$elm.outerHeight()/2),marginLeft:-(this.$elm.outerWidth()/2),zIndex:this.options.zIndex+1})},_ctx:function(){return{elm:this.$elm,blocker:this.blocker,options:this.options}}},e.modal.prototype.resize=e.modal.prototype.center,e.modal.close=function(e){if(!t)return;return e&&e.preventDefault(),t.close(),that=t.$elm,t=null,that},e.modal.resize=function(){if(!t)return;t.resize()},e.modal.defaults={overlay:"#000",opacity:.75,zIndex:1,escapeClose:!0,clickClose:!0,closeText:"Close",modalClass:"modal",spinnerHtml:null,showSpinner:!0,showClose:!0},e.modal.BEFORE_BLOCK="modal:before-block",e.modal.BLOCK="modal:block",e.modal.BEFORE_OPEN="modal:before-open",e.modal.OPEN="modal:open",e.modal.BEFORE_CLOSE="modal:before-close",e.modal.CLOSE="modal:close",e.modal.AJAX_SEND="modal:ajax:send",e.modal.AJAX_SUCCESS="modal:ajax:success",e.modal.AJAX_FAIL="modal:ajax:fail",e.modal.AJAX_COMPLETE="modal:ajax:complete",e.fn.modal=function(n){return this.length===1&&(t=new e.modal(this,n)),this},e(document).on("click.modal",'a[rel="modal:close"]',e.modal.close),e(document).on("click.modal",'a[rel="modal:open"]',function(t){t.preventDefault(),e(this).modal()})})(jQuery); \ No newline at end of file From b7865f5b2b09bb8efb20d5c8681cada6c7a8ce29 Mon Sep 17 00:00:00 2001 From: Kyle Fox Date: Sat, 6 Apr 2013 09:44:42 -0600 Subject: [PATCH 3/4] `var statement to prevent `that` from leaking into the global scope --- jquery.modal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.modal.js b/jquery.modal.js index 0506d02..938eeb7 100644 --- a/jquery.modal.js +++ b/jquery.modal.js @@ -136,7 +136,7 @@ if (!current) return; if (event) event.preventDefault(); current.close(); - that = current.$elm; + var that = current.$elm; current = null; return that; }; From b7c16e669a57bd070f6d01bbad128d9fa45d189d Mon Sep 17 00:00:00 2001 From: Kyle Fox Date: Sat, 6 Apr 2013 09:49:32 -0600 Subject: [PATCH 4/4] minify with closure compiler --- jquery.modal.min.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/jquery.modal.min.js b/jquery.modal.min.js index 69d978b..ee0e373 100644 --- a/jquery.modal.min.js +++ b/jquery.modal.min.js @@ -1,4 +1,11 @@ /* A simple jQuery modal (http://github.com/kylefox/jquery-modal) Version 0.5.3 -*/(function(e){var t=null;e.modal=function(n,r){e.modal.close();var i,s;this.$body=e("body"),this.options=e.extend({},e.modal.defaults,r);if(n.is("a")){s=n.attr("href");if(/^#/.test(s)){this.$elm=e(s);if(this.$elm.length!==1)return null;this.open()}else this.$elm=e("
"),this.$body.append(this.$elm),i=function(e,t){t.elm.remove()},this.showSpinner(),n.trigger(e.modal.AJAX_SEND),e.get(s).done(function(r){if(!t)return;n.trigger(e.modal.AJAX_SUCCESS),t.$elm.empty().append(r).on(e.modal.CLOSE,i),t.hideSpinner(),t.open(),n.trigger(e.modal.AJAX_COMPLETE)}).fail(function(){n.trigger(e.modal.AJAX_FAIL),t.hideSpinner(),n.trigger(e.modal.AJAX_COMPLETE)})}else this.$elm=n,this.open()},e.modal.prototype={constructor:e.modal,open:function(){this.block(),this.show(),this.options.escapeClose&&e(document).on("keydown.modal",function(t){t.which==27&&e.modal.close()}),this.options.clickClose&&this.blocker.click(e.modal.close)},close:function(){this.unblock(),this.hide(),e(document).off("keydown.modal")},block:function(){this.$elm.trigger(e.modal.BEFORE_BLOCK,[this._ctx()]),this.blocker=e('
').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(e.modal.BLOCK,[this._ctx()])},unblock:function(){this.blocker.remove()},show:function(){this.$elm.trigger(e.modal.BEFORE_OPEN,[this._ctx()]),this.options.showClose&&(this.closeButton=e(''+this.options.closeText+""),this.$elm.append(this.closeButton)),this.$elm.addClass(this.options.modalClass+" current"),this.center(),this.$elm.show().trigger(e.modal.OPEN,[this._ctx()])},hide:function(){this.$elm.trigger(e.modal.BEFORE_CLOSE,[this._ctx()]),this.closeButton&&this.closeButton.remove(),this.$elm.removeClass("current").hide(),this.$elm.trigger(e.modal.CLOSE,[this._ctx()])},showSpinner:function(){if(!this.options.showSpinner)return;this.spinner=this.spinner||e('
').append(this.options.spinnerHtml),this.$body.append(this.spinner),this.spinner.show()},hideSpinner:function(){this.spinner&&this.spinner.remove()},center:function(){this.$elm.css({position:"fixed",top:"50%",left:"50%",marginTop:-(this.$elm.outerHeight()/2),marginLeft:-(this.$elm.outerWidth()/2),zIndex:this.options.zIndex+1})},_ctx:function(){return{elm:this.$elm,blocker:this.blocker,options:this.options}}},e.modal.prototype.resize=e.modal.prototype.center,e.modal.close=function(e){if(!t)return;return e&&e.preventDefault(),t.close(),that=t.$elm,t=null,that},e.modal.resize=function(){if(!t)return;t.resize()},e.modal.defaults={overlay:"#000",opacity:.75,zIndex:1,escapeClose:!0,clickClose:!0,closeText:"Close",modalClass:"modal",spinnerHtml:null,showSpinner:!0,showClose:!0},e.modal.BEFORE_BLOCK="modal:before-block",e.modal.BLOCK="modal:block",e.modal.BEFORE_OPEN="modal:before-open",e.modal.OPEN="modal:open",e.modal.BEFORE_CLOSE="modal:before-close",e.modal.CLOSE="modal:close",e.modal.AJAX_SEND="modal:ajax:send",e.modal.AJAX_SUCCESS="modal:ajax:success",e.modal.AJAX_FAIL="modal:ajax:fail",e.modal.AJAX_COMPLETE="modal:ajax:complete",e.fn.modal=function(n){return this.length===1&&(t=new e.modal(this,n)),this},e(document).on("click.modal",'a[rel="modal:close"]',e.modal.close),e(document).on("click.modal",'a[rel="modal:open"]',function(t){t.preventDefault(),e(this).modal()})})(jQuery); \ No newline at end of file +*/ +(function(a){var c=null;a.modal=function(b,f){a.modal.close();var e,d;this.$body=a("body");this.options=a.extend({},a.modal.defaults,f);if(b.is("a"))if(d=b.attr("href"),/^#/.test(d)){this.$elm=a(d);if(1!==this.$elm.length)return null;this.open()}else this.$elm=a("
"),this.$body.append(this.$elm),e=function(a,b){b.elm.remove()},this.showSpinner(),b.trigger(a.modal.AJAX_SEND),a.get(d).done(function(d){c&&(b.trigger(a.modal.AJAX_SUCCESS),c.$elm.empty().append(d).on(a.modal.CLOSE,e),c.hideSpinner(), +c.open(),b.trigger(a.modal.AJAX_COMPLETE))}).fail(function(){b.trigger(a.modal.AJAX_FAIL);c.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();if(this.options.escapeClose)a(document).on("keydown.modal",function(b){27==b.which&&a.modal.close()});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('
').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(''+ +this.options.closeText+""),this.$elm.append(this.closeButton));this.$elm.addClass(this.options.modalClass+" current");this.center();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('
').append(this.options.spinnerHtml),this.$body.append(this.spinner),this.spinner.show())},hideSpinner:function(){this.spinner&&this.spinner.remove()},center:function(){this.$elm.css({position:"fixed",top:"50%",left:"50%",marginTop:-(this.$elm.outerHeight()/2),marginLeft:-(this.$elm.outerWidth()/2),zIndex:this.options.zIndex+1})},_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){if(c)return a&& +a.preventDefault(),c.close(),a=c.$elm,c=null,a};a.modal.resize=function(){c&&c.resize()};a.modal.defaults={overlay:"#000",opacity:0.75,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&&(c=new a.modal(this,b));return this};a(document).on("click.modal",'a[rel="modal:close"]',a.modal.close);a(document).on("click.modal",'a[rel="modal:open"]',function(b){b.preventDefault();a(this).modal()})})(jQuery); \ No newline at end of file