From ed0d53461df141fbc6e4385e314c24b92f381cbf Mon Sep 17 00:00:00 2001 From: David van Leeuwen Date: Wed, 5 Nov 2014 14:43:42 +0100 Subject: [PATCH] Bugfixes and added showViewOnRender --- README.md | 10 +-- backbone.marionette.modals-min.js | 2 +- backbone.marionette.modals.js | 27 +++--- backbone.modal-bundled-min.js | 2 +- backbone.modal-bundled.js | 86 +++++++++++-------- backbone.modal-min.js | 2 +- backbone.modal.css | 5 +- backbone.modal.js | 59 +++++++------ bower.json | 2 +- examples/2_tab_based.html | 4 +- examples/3_stacked_modal_with_marionette.html | 3 +- package.json | 2 +- src/backbone.marionette.modals.coffee | 7 +- src/backbone.modal.coffee | 44 +++++----- src/backbone.modal.sass | 5 +- 15 files changed, 143 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index 4497978..b1cdfd4 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,17 @@ For a complete overview of the documentation visit: [http://awkward.github.io/ba #### Bundled version If you're using Backbone and Marionette. -* [backbone.modal-bundled-min.js](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.1/backbone.modal-bundled-min.js) +* [backbone.modal-bundled-min.js](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.2/backbone.modal-bundled-min.js) #### Backbone.Modals.js and Backbone.Marionette.Modals.js separate If you just want to use the Backbone version without Marionette. -* [backbone.modal-min.js](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.1/backbone.modal-min.js) -* [backbone.marionette.modals-min.js](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.1/backbone.marionette.modals-min.js) +* [backbone.modal-min.js](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.2/backbone.modal-min.js) +* [backbone.marionette.modals-min.js](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.2/backbone.marionette.modals-min.js) #### CSS files There's default style, and you can use our theme to make things look pretty. -* [backbone.modal.css](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.1/backbone.modal.css) -* [backbone.modal.theme.css](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.1/backbone.modal.theme.css) +* [backbone.modal.css](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.2/backbone.modal.css) +* [backbone.modal.theme.css](https://raw.githubusercontent.com/awkward/backbone.modal/v1.1.2/backbone.modal.theme.css) ### How to contribute diff --git a/backbone.marionette.modals-min.js b/backbone.marionette.modals-min.js index bca7278..f028fee 100644 --- a/backbone.marionette.modals-min.js +++ b/backbone.marionette.modals-min.js @@ -1 +1 @@ -(function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b={}.hasOwnProperty,c=function(a,c){function d(){this.constructor=a}for(var e in c)b.call(c,e)&&(a[e]=c[e]);return d.prototype=c.prototype,a.prototype=new d,a.__super__=c.prototype,a};!function(a){return"function"==typeof define&&define.amd?define(["underscore","backbone","backbone.marionette","exports"],a):"object"==typeof exports?a(require("underscore"),require("backbone"),require("backbone.marionette"),exports):a(_,Backbone,Backbone.Marionette,{})}(function(b,d,e,f){return f=function(d){function f(){return this.destroy=a(this.destroy,this),f.__super__.constructor.apply(this,arguments)}return c(f,d),f.prototype.modals=[],f.prototype.zIndex=0,f.prototype.show=function(a,c){var d,f,g,h,i,j,k,l,m;if(null==c&&(c={}),this._ensureElement(),this.modals.length>0&&(d=b.last(this.modals),d.modalEl.addClass(""+d.prefix+"-view--stacked"),g=this.modals[this.modals.length-1],null!=g&&g.modalEl.removeClass(""+g.prefix+"-modal--stacked-reverse")),a.render(c),a.regionEnabled=!0,this.triggerMethod("before:swap",a),this.triggerMethod("before:show",a),e.triggerMethodOn(a,"before:show"),this.triggerMethod("swapOut",this.currentView),this.$el.append(a.el),this.currentView=a,this.triggerMethod("swap",a),this.triggerMethod("show",a),e.triggerMethodOn(a,"show"),this.modals.length>0)for(l=this.modals,h=0,j=l.length;j>h;h++)f=l[h],f.$el.css({background:"none"});for(m=this.modals,i=0,k=m.length;k>i;i++)f=m[i],f.undelegateModalEvents();return a.on("modal:destroy",this.destroy),this.modals.push(a),this.zIndex++},f.prototype.destroy=function(){var a,c;return(c=this.currentView)?(c.destroy&&!c.isDestroyed?c.destroy():c.remove&&c.remove(),c.off("modal:destroy",this.destroy),this.modals.splice(b.indexOf(this.modals,c),1),this.zIndex--,this.currentView=this.modals[this.zIndex-1],a=b.last(this.modals),a&&(a.$el.removeAttr("style"),a.modalEl.addClass(""+a.prefix+"-modal--stacked-reverse"),b.delay(function(){return function(){return a.modalEl.removeClass(""+a.prefix+"-modal--stacked")}}(this),300),0!==this.zIndex)?a.delegateModalEvents():void 0):void 0},f.prototype.destroyAll=function(){var a,b,c,d,e;for(d=this.modals,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(this.destroy());return e},f}(e.Region),e.Modals=f,e.Modals})}).call(this); \ No newline at end of file +(function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b={}.hasOwnProperty,c=function(a,c){function d(){this.constructor=a}for(var e in c)b.call(c,e)&&(a[e]=c[e]);return d.prototype=c.prototype,a.prototype=new d,a.__super__=c.prototype,a};!function(a){return"function"==typeof define&&define.amd?define(["underscore","backbone","backbone.marionette","exports"],a):"object"==typeof exports?a(require("underscore"),require("backbone"),require("backbone.marionette"),exports):a(_,Backbone,Backbone.Marionette,{})}(function(b,d,e,f){return f=function(f){function g(){return this.destroy=a(this.destroy,this),g.__super__.constructor.apply(this,arguments)}return c(g,f),g.prototype.modals=[],g.prototype.zIndex=0,g.prototype.show=function(a,c){var f,g,h,i,j,k,l,m,n;for(null==c&&(c={}),this._ensureElement(),d.$("body").css({overflow:"hidden"}),this.modals.length>0&&(f=b.last(this.modals),f.modalEl.addClass(""+f.prefix+"-view--stacked"),h=this.modals[this.modals.length-1],null!=h&&h.modalEl.removeClass(""+h.prefix+"-modal--stacked-reverse")),a.render(c),a.regionEnabled=!0,this.triggerMethod("before:swap",a),this.triggerMethod("before:show",a),e.triggerMethodOn(a,"before:show"),this.triggerMethod("swapOut",this.currentView),this.$el.append(a.el),this.currentView=a,this.triggerMethod("swap",a),this.triggerMethod("show",a),e.triggerMethodOn(a,"show"),m=this.modals,i=0,k=m.length;k>i;i++)g=m[i],g.undelegateModalEvents();for(n=this.modals,j=0,l=n.length;l>j;j++)g=n[j],g.$el.css({background:"none"});return a.on("modal:destroy",this.destroy),this.modals.push(a),this.zIndex++},g.prototype.destroy=function(){var a,c;return(c=this.currentView)?(c.destroy&&!c.isDestroyed?c.destroy():c.remove&&c.remove(),c.off("modal:destroy",this.destroy),this.modals.splice(b.indexOf(this.modals,c),1),this.zIndex--,this.currentView=this.modals[this.zIndex-1],a=b.last(this.modals),a&&(a.$el.removeAttr("style"),a.modalEl.addClass(""+a.prefix+"-modal--stacked-reverse"),b.delay(function(){return function(){return a.modalEl.removeClass(""+a.prefix+"-modal--stacked")}}(this),300),0!==this.zIndex&&a.delegateModalEvents()),0===this.zIndex&&d.$("body").css({overflow:"visible"}),this.triggerMethod("modal:destroy",c)):void 0},g.prototype.destroyAll=function(){var a,b,c,d,e;for(d=this.modals,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(this.destroy());return e},g}(e.Region),e.Modals=f,e.Modals})}).call(this); \ No newline at end of file diff --git a/backbone.marionette.modals.js b/backbone.marionette.modals.js index 9ff7a89..c642200 100644 --- a/backbone.marionette.modals.js +++ b/backbone.marionette.modals.js @@ -30,6 +30,9 @@ options = {}; } this._ensureElement(); + Backbone.$('body').css({ + overflow: 'hidden' + }); if (this.modals.length > 0) { lastModal = _.last(this.modals); lastModal.modalEl.addClass("" + lastModal.prefix + "-view--stacked"); @@ -49,19 +52,17 @@ this.triggerMethod('swap', view); this.triggerMethod('show', view); Marionette.triggerMethodOn(view, 'show'); - if (this.modals.length > 0) { - _ref = this.modals; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - modalView = _ref[_i]; - modalView.$el.css({ - background: 'none' - }); - } + _ref = this.modals; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + modalView = _ref[_i]; + modalView.undelegateModalEvents(); } _ref1 = this.modals; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { modalView = _ref1[_j]; - modalView.undelegateModalEvents(); + modalView.$el.css({ + background: 'none' + }); } view.on('modal:destroy', this.destroy); this.modals.push(view); @@ -93,9 +94,15 @@ }; })(this), 300); if (this.zIndex !== 0) { - return lastModal.delegateModalEvents(); + lastModal.delegateModalEvents(); } } + if (this.zIndex === 0) { + Backbone.$('body').css({ + overflow: 'visible' + }); + } + return this.triggerMethod('modal:destroy', view); }; Modals.prototype.destroyAll = function() { diff --git a/backbone.modal-bundled-min.js b/backbone.modal-bundled-min.js index f51dc92..ad5b796 100644 --- a/backbone.modal-bundled-min.js +++ b/backbone.modal-bundled-min.js @@ -1 +1 @@ -(function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b={}.hasOwnProperty,c=function(a,c){function d(){this.constructor=a}for(var e in c)b.call(c,e)&&(a[e]=c[e]);return d.prototype=c.prototype,a.prototype=new d,a.__super__=c.prototype,a},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};!function(a){return"function"==typeof define&&define.amd?define(["underscore","backbone","exports"],a):"object"==typeof exports?a(require("underscore"),require("backbone"),exports):a(_,Backbone,{})}(function(b,e,f){return f=function(f){function g(){this.triggerCancel=a(this.triggerCancel,this),this.triggerSubmit=a(this.triggerSubmit,this),this.triggerView=a(this.triggerView,this),this.clickOutside=a(this.clickOutside,this),this.checkKey=a(this.checkKey,this),this.rendererCompleted=a(this.rendererCompleted,this),this.args=Array.prototype.slice.apply(arguments),e.View.prototype.constructor.apply(this,this.args),this.setUIElements()}return c(g,f),g.prototype.prefix="bbm",g.prototype.animate=!0,g.prototype.keyControl=!0,g.prototype.render=function(a){var c,d,f;return d=this.serializeData(),(!a||b.isEmpty(a))&&(a=0),this.$el.addClass(""+this.prefix+"-wrapper"),this.modalEl=e.$("
").addClass(""+this.prefix+"-modal"),this.template&&this.modalEl.html(this.template(d)),this.$el.html(this.modalEl),this.viewContainer?(this.viewContainerEl=this.modalEl.find(this.viewContainer),this.viewContainerEl.addClass(""+this.prefix+"-modal__views")):this.viewContainerEl=this.modalEl,$(":focus").blur(),(null!=(f=this.views)?f.length:void 0)>0&&this.openAt(a),"function"==typeof this.onRender&&this.onRender(),this.delegateModalEvents(),c=this.getOption("animate"),this.$el.fadeIn&&c?(this.modalEl.css({opacity:0}),this.$el.fadeIn({duration:100,complete:this.rendererCompleted})):(this.$el.show(),this.rendererCompleted()),this},g.prototype.rendererCompleted=function(){var a;return this.keyControl&&(e.$("body").on("keyup",this.checkKey),e.$("body").on("click",this.clickOutside)),this.modalEl.css({opacity:1}).addClass(""+this.prefix+"-modal--open"),"function"==typeof this.onShow&&this.onShow(),null!=(a=this.currentView)&&"function"==typeof a.onShow?a.onShow():void 0},g.prototype.setUIElements=function(){var a;if(this.template=this.getOption("template"),this.views=this.getOption("views"),null!=(a=this.views)&&(a.length=b.size(this.views)),this.viewContainer=this.getOption("viewContainer"),this.$el.hide(),b.isUndefined(this.template)&&b.isUndefined(this.views))throw new Error("No template or views defined for Backbone.Modal");if(this.template&&this.views&&b.isUndefined(this.viewContainer))throw new Error("No viewContainer defined for Backbone.Modal")},g.prototype.getOption=function(a){return a?this.options&&d.call(this.options,a)>=0&&null!=this.options[a]?this.options[a]:this[a]:void 0},g.prototype.serializeData=function(){var a;return a={},this.model&&(a=b.extend(a,this.model.toJSON())),this.collection&&(a=b.extend(a,{items:this.collection.toJSON()})),a},g.prototype.delegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!0,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.on("click",f,this.triggerSubmit),a&&this.$el.on("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.on(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.undelegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!1,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.off("click",f,this.triggerSubmit),a&&this.$el.off("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.off(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.checkKey=function(a){if(this.active)switch(a.keyCode){case 27:return this.triggerCancel(a);case 13:return this.triggerSubmit(a)}},g.prototype.clickOutside=function(a){return e.$(a.target).hasClass(""+this.prefix+"-wrapper")&&this.active?this.triggerCancel():void 0},g.prototype.buildView=function(a,c){var d;if(a)return c&&b.isFunction(c)&&(c=c()),b.isFunction(a)?(d=new a(c||this.args[0]),d instanceof e.View?{el:d.render().$el,view:d}:{el:a(c||this.args[0])}):{view:a,el:a.$el}},g.prototype.triggerView=function(a){var c,d,e,f,g,h,i;if(null!=a&&"function"==typeof a.preventDefault&&a.preventDefault(),f=a.data,d=this.buildView(f.view,f.viewOptions),this.currentView&&(this.previousView=this.currentView,!(null!=(i=f.openOptions)?i.skipSubmit:void 0))){if(("function"==typeof(g=this.previousView).beforeSubmit?g.beforeSubmit():void 0)===!1)return;"function"==typeof(h=this.previousView).submit&&h.submit()}this.currentView=d.view||d.el,c=0;for(e in this.views)f.view===this.views[e].view&&(this.currentIndex=c),c++;return f.onActive&&(b.isFunction(f.onActive)?f.onActive(this):b.isString(f.onActive)&&this[f.onActive].call(this,f)),this.shouldAnimate?this.animateToView(d.el):(this.shouldAnimate=!0,this.$(this.viewContainerEl).html(d.el))},g.prototype.animateToView=function(a){var b,c,d,f,g,h,i;return f={position:"relative",top:-9999,left:-9999},g=e.$("").css(f),g.html(this.$el.clone().css(f)),0!==e.$("tester").length?e.$("tester").replaceWith(g):e.$("body").append(g),b=g.find(this.viewContainer?this.viewContainer:"."+this.prefix+"-modal"),b.removeAttr("style"),d=b.outerHeight(),b.html(a),c=b.outerHeight(),d===c?(this.$(this.viewContainerEl).html(a),"function"==typeof(h=this.currentView).onShow&&h.onShow(),null!=(i=this.previousView)&&"function"==typeof i.destroy?i.destroy():void 0):(this.$(this.viewContainerEl).css({opacity:0}),this.$(this.viewContainerEl).animate({height:c},100,function(b){return function(){var c,d;return b.$(b.viewContainerEl).css({opacity:1}).removeAttr("style"),b.$(b.viewContainerEl).html(a),"function"==typeof(c=b.currentView).onShow&&c.onShow(),null!=(d=b.previousView)&&"function"==typeof d.destroy?d.destroy():void 0}}(this)))},g.prototype.triggerSubmit=function(a){var b,c;return null!=a&&a.preventDefault(),this.beforeSubmit&&this.beforeSubmit()===!1||this.currentView&&this.currentView.beforeSubmit&&this.currentView.beforeSubmit()===!1?void 0:this.submit||(null!=(b=this.currentView)?b.submit:void 0)||this.getOption("submitEl")?(null!=(c=this.currentView)&&"function"==typeof c.submit&&c.submit(),"function"==typeof this.submit&&this.submit(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy()):this.triggerCancel()},g.prototype.triggerCancel=function(a){return null!=a&&a.preventDefault(),this.beforeCancel&&this.beforeCancel()===!1?void 0:("function"==typeof this.cancel&&this.cancel(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy())},g.prototype.destroy=function(){var a,c;return e.$("body").off("keyup",this.checkKey),e.$("body").off("click",this.clickOutside),"function"==typeof this.onDestroy&&this.onDestroy(),this.shouldAnimate=!1,this.modalEl.addClass(""+this.prefix+"-modal--destroy"),c=function(a){return function(){var b;return null!=(b=a.currentView)&&"function"==typeof b.remove&&b.remove(),a.remove()}}(this),a=this.getOption("animate"),this.$el.fadeOut&&a?(this.$el.fadeOut({duration:200}),b.delay(function(){return c()},200)):c()},g.prototype.openAt=function(a){var c,d,e,f,g;b.isNumber(a)?c=a:b.isNumber(a._index)&&(c=a._index),e=0;for(f in this.views)if("length"!==f)if(b.isNumber(c))e===c&&(g=this.views[f]),e++;else if(b.isObject(a))for(d in this.views[f])a[d]===this.views[f][d]&&(g=this.views[f]);return g&&(this.currentIndex=b.indexOf(this.views,g),this.triggerView({data:b.extend(g,{openOptions:a})})),this},g.prototype.next=function(a){return null==a&&(a={}),this.currentIndex+10&&(d=b.last(this.modals),d.modalEl.addClass(""+d.prefix+"-view--stacked"),g=this.modals[this.modals.length-1],null!=g&&g.modalEl.removeClass(""+g.prefix+"-modal--stacked-reverse")),a.render(c),a.regionEnabled=!0,this.triggerMethod("before:swap",a),this.triggerMethod("before:show",a),e.triggerMethodOn(a,"before:show"),this.triggerMethod("swapOut",this.currentView),this.$el.append(a.el),this.currentView=a,this.triggerMethod("swap",a),this.triggerMethod("show",a),e.triggerMethodOn(a,"show"),this.modals.length>0)for(l=this.modals,h=0,j=l.length;j>h;h++)f=l[h],f.$el.css({background:"none"});for(m=this.modals,i=0,k=m.length;k>i;i++)f=m[i],f.undelegateModalEvents();return a.on("modal:destroy",this.destroy),this.modals.push(a),this.zIndex++},f.prototype.destroy=function(){var a,c;return(c=this.currentView)?(c.destroy&&!c.isDestroyed?c.destroy():c.remove&&c.remove(),c.off("modal:destroy",this.destroy),this.modals.splice(b.indexOf(this.modals,c),1),this.zIndex--,this.currentView=this.modals[this.zIndex-1],a=b.last(this.modals),a&&(a.$el.removeAttr("style"),a.modalEl.addClass(""+a.prefix+"-modal--stacked-reverse"),b.delay(function(){return function(){return a.modalEl.removeClass(""+a.prefix+"-modal--stacked")}}(this),300),0!==this.zIndex)?a.delegateModalEvents():void 0):void 0},f.prototype.destroyAll=function(){var a,b,c,d,e;for(d=this.modals,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(this.destroy());return e},f}(e.Region),e.Modals=f,e.Modals})}.call(this); \ No newline at end of file +(function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b={}.hasOwnProperty,c=function(a,c){function d(){this.constructor=a}for(var e in c)b.call(c,e)&&(a[e]=c[e]);return d.prototype=c.prototype,a.prototype=new d,a.__super__=c.prototype,a},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};!function(a){return"function"==typeof define&&define.amd?define(["underscore","backbone","exports"],a):"object"==typeof exports?a(require("underscore"),require("backbone"),exports):a(_,Backbone,{})}(function(b,e,f){return f=function(f){function g(){this.triggerCancel=a(this.triggerCancel,this),this.triggerSubmit=a(this.triggerSubmit,this),this.triggerView=a(this.triggerView,this),this.clickOutside=a(this.clickOutside,this),this.checkKey=a(this.checkKey,this),this.rendererCompleted=a(this.rendererCompleted,this),this.args=Array.prototype.slice.apply(arguments),e.View.prototype.constructor.apply(this,this.args),this.setUIElements()}return c(g,f),g.prototype.prefix="bbm",g.prototype.animate=!0,g.prototype.keyControl=!0,g.prototype.showViewOnRender=!0,g.prototype.render=function(a){var c,d;return c=this.serializeData(),(!a||b.isEmpty(a))&&(a=0),this.$el.addClass(""+this.prefix+"-wrapper"),this.modalEl=e.$("
").addClass(""+this.prefix+"-modal"),this.template&&this.modalEl.html(this.template(c)),this.$el.html(this.modalEl),this.viewContainer?(this.viewContainerEl=this.modalEl.find(this.viewContainer),this.viewContainerEl.addClass(""+this.prefix+"-modal__views")):this.viewContainerEl=this.modalEl,$(":focus").blur(),(null!=(d=this.views)?d.length:void 0)>0&&this.showViewOnRender&&this.openAt(a),"function"==typeof this.onRender&&this.onRender(),this.delegateModalEvents(),this.trigger("modal:open"),this.$el.fadeIn&&this.animate?(this.modalEl.css({opacity:0}),this.$el.fadeIn({duration:100,complete:this.rendererCompleted})):this.rendererCompleted(),this},g.prototype.rendererCompleted=function(){var a;return this.keyControl&&(e.$("body").on("keyup",this.checkKey),e.$("body").on("click",this.clickOutside)),this.modalEl.css({opacity:1}).addClass(""+this.prefix+"-modal--open"),"function"==typeof this.onShow&&this.onShow(),null!=(a=this.currentView)&&"function"==typeof a.onShow?a.onShow():void 0},g.prototype.setUIElements=function(){var a;if(this.template=this.getOption("template"),this.views=this.getOption("views"),null!=(a=this.views)&&(a.length=b.size(this.views)),this.viewContainer=this.getOption("viewContainer"),this.animate=this.getOption("animate"),b.isUndefined(this.template)&&b.isUndefined(this.views))throw new Error("No template or views defined for Backbone.Modal");if(this.template&&this.views&&b.isUndefined(this.viewContainer))throw new Error("No viewContainer defined for Backbone.Modal")},g.prototype.getOption=function(a){return a?this.options&&d.call(this.options,a)>=0&&null!=this.options[a]?this.options[a]:this[a]:void 0},g.prototype.serializeData=function(){var a;return a={},this.model&&(a=b.extend(a,this.model.toJSON())),this.collection&&(a=b.extend(a,{items:this.collection.toJSON()})),a},g.prototype.delegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!0,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.on("click",f,this.triggerSubmit),a&&this.$el.on("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.on(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.undelegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!1,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.off("click",f,this.triggerSubmit),a&&this.$el.off("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.off(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.checkKey=function(a){if(this.active)switch(a.keyCode){case 27:return this.triggerCancel(a);case 13:return this.triggerSubmit(a)}},g.prototype.clickOutside=function(a){return e.$(a.target).hasClass(""+this.prefix+"-wrapper")&&this.active?this.triggerCancel():void 0},g.prototype.buildView=function(a,c){var d;if(a)return c&&b.isFunction(c)&&(c=c()),b.isFunction(a)?(d=new a(c||this.args[0]),d instanceof e.View?{el:d.render().$el,view:d}:{el:a(c||this.args[0])}):{view:a,el:a.$el}},g.prototype.triggerView=function(a){var c,d,e,f,g,h,i;if(null!=a&&"function"==typeof a.preventDefault&&a.preventDefault(),f=a.data,d=this.buildView(f.view,f.viewOptions),this.currentView&&(this.previousView=this.currentView,!(null!=(i=f.openOptions)?i.skipSubmit:void 0))){if(("function"==typeof(g=this.previousView).beforeSubmit?g.beforeSubmit():void 0)===!1)return;"function"==typeof(h=this.previousView).submit&&h.submit()}this.currentView=d.view||d.el,c=0;for(e in this.views)f.view===this.views[e].view&&(this.currentIndex=c),c++;return f.onActive&&(b.isFunction(f.onActive)?f.onActive(this):b.isString(f.onActive)&&this[f.onActive].call(this,f)),this.shouldAnimate?this.animateToView(d.el):(this.shouldAnimate=!0,this.$(this.viewContainerEl).html(d.el))},g.prototype.animateToView=function(a){var b,c,d,f,g,h,i;return f={position:"relative",top:-9999,left:-9999},g=e.$("").css(f),g.html(this.$el.clone().css(f)),0!==e.$("tester").length?e.$("tester").replaceWith(g):e.$("body").append(g),b=g.find(this.viewContainer?this.viewContainer:"."+this.prefix+"-modal"),b.removeAttr("style"),d=b.outerHeight(),b.html(a),c=b.outerHeight(),d===c?(this.$(this.viewContainerEl).html(a),"function"==typeof(h=this.currentView).onShow&&h.onShow(),null!=(i=this.previousView)&&"function"==typeof i.destroy?i.destroy():void 0):this.animate?(this.$(this.viewContainerEl).css({opacity:0}),this.$(this.viewContainerEl).animate({height:c},100,function(b){return function(){var c,d;return b.$(b.viewContainerEl).css({opacity:1}).removeAttr("style"),b.$(b.viewContainerEl).html(a),"function"==typeof(c=b.currentView).onShow&&c.onShow(),null!=(d=b.previousView)&&"function"==typeof d.destroy?d.destroy():void 0}}(this))):this.$(this.viewContainerEl).css({height:c}).html(a)},g.prototype.triggerSubmit=function(a){var b,c;return null!=a&&a.preventDefault(),this.beforeSubmit&&this.beforeSubmit()===!1||this.currentView&&this.currentView.beforeSubmit&&this.currentView.beforeSubmit()===!1?void 0:this.submit||(null!=(b=this.currentView)?b.submit:void 0)||this.getOption("submitEl")?(null!=(c=this.currentView)&&"function"==typeof c.submit&&c.submit(),"function"==typeof this.submit&&this.submit(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy()):this.triggerCancel()},g.prototype.triggerCancel=function(a){return null!=a&&a.preventDefault(),this.beforeCancel&&this.beforeCancel()===!1?void 0:("function"==typeof this.cancel&&this.cancel(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy())},g.prototype.destroy=function(){var a;return e.$("body").off("keyup",this.checkKey),e.$("body").off("click",this.clickOutside),"function"==typeof this.onDestroy&&this.onDestroy(),this.shouldAnimate=!1,this.modalEl.addClass(""+this.prefix+"-modal--destroy"),a=function(a){return function(){var b;return null!=(b=a.currentView)&&"function"==typeof b.remove&&b.remove(),a.remove()}}(this),this.$el.fadeOut&&this.animate?(this.$el.fadeOut({duration:200}),b.delay(function(){return a()},200)):a()},g.prototype.openAt=function(a){var c,d,e,f,g;b.isNumber(a)?c=a:b.isNumber(a._index)&&(c=a._index),e=0;for(f in this.views)if("length"!==f)if(b.isNumber(c))e===c&&(g=this.views[f]),e++;else if(b.isObject(a))for(d in this.views[f])a[d]===this.views[f][d]&&(g=this.views[f]);return g&&(this.currentIndex=b.indexOf(this.views,g),this.triggerView({data:b.extend(g,{openOptions:a})})),this},g.prototype.next=function(a){return null==a&&(a={}),this.currentIndex+10&&(f=b.last(this.modals),f.modalEl.addClass(""+f.prefix+"-view--stacked"),h=this.modals[this.modals.length-1],null!=h&&h.modalEl.removeClass(""+h.prefix+"-modal--stacked-reverse")),a.render(c),a.regionEnabled=!0,this.triggerMethod("before:swap",a),this.triggerMethod("before:show",a),e.triggerMethodOn(a,"before:show"),this.triggerMethod("swapOut",this.currentView),this.$el.append(a.el),this.currentView=a,this.triggerMethod("swap",a),this.triggerMethod("show",a),e.triggerMethodOn(a,"show"),m=this.modals,i=0,k=m.length;k>i;i++)g=m[i],g.undelegateModalEvents();for(n=this.modals,j=0,l=n.length;l>j;j++)g=n[j],g.$el.css({background:"none"});return a.on("modal:destroy",this.destroy),this.modals.push(a),this.zIndex++},g.prototype.destroy=function(){var a,c;return(c=this.currentView)?(c.destroy&&!c.isDestroyed?c.destroy():c.remove&&c.remove(),c.off("modal:destroy",this.destroy),this.modals.splice(b.indexOf(this.modals,c),1),this.zIndex--,this.currentView=this.modals[this.zIndex-1],a=b.last(this.modals),a&&(a.$el.removeAttr("style"),a.modalEl.addClass(""+a.prefix+"-modal--stacked-reverse"),b.delay(function(){return function(){return a.modalEl.removeClass(""+a.prefix+"-modal--stacked")}}(this),300),0!==this.zIndex&&a.delegateModalEvents()),0===this.zIndex&&d.$("body").css({overflow:"visible"}),this.triggerMethod("modal:destroy",c)):void 0},g.prototype.destroyAll=function(){var a,b,c,d,e;for(d=this.modals,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(this.destroy());return e},g}(e.Region),e.Modals=f,e.Modals})}.call(this); \ No newline at end of file diff --git a/backbone.modal-bundled.js b/backbone.modal-bundled.js index 0c6e2a3..63777e6 100644 --- a/backbone.modal-bundled.js +++ b/backbone.modal-bundled.js @@ -22,6 +22,8 @@ Modal.prototype.keyControl = true; + Modal.prototype.showViewOnRender = true; + function Modal() { this.triggerCancel = __bind(this.triggerCancel, this); this.triggerSubmit = __bind(this.triggerSubmit, this); @@ -35,7 +37,7 @@ } Modal.prototype.render = function(options) { - var animate, data, _ref; + var data, _ref; data = this.serializeData(); if (!options || _.isEmpty(options)) { options = 0; @@ -53,15 +55,14 @@ this.viewContainerEl = this.modalEl; } $(':focus').blur(); - if (((_ref = this.views) != null ? _ref.length : void 0) > 0) { + if (((_ref = this.views) != null ? _ref.length : void 0) > 0 && this.showViewOnRender) { this.openAt(options); } if (typeof this.onRender === "function") { this.onRender(); } this.delegateModalEvents(); - animate = this.getOption('animate'); - if (this.$el.fadeIn && animate) { + if (this.$el.fadeIn && this.animate) { this.modalEl.css({ opacity: 0 }); @@ -70,7 +71,6 @@ complete: this.rendererCompleted }); } else { - this.$el.show(); this.rendererCompleted(); } return this; @@ -99,7 +99,7 @@ _ref.length = _.size(this.views); } this.viewContainer = this.getOption('viewContainer'); - this.$el.hide(); + this.animate = this.getOption('animate'); if (_.isUndefined(this.template) && _.isUndefined(this.views)) { throw new Error('No template or views defined for Backbone.Modal'); } @@ -300,24 +300,30 @@ } return (_ref = this.previousView) != null ? typeof _ref.destroy === "function" ? _ref.destroy() : void 0 : void 0; } else { - this.$(this.viewContainerEl).css({ - opacity: 0 - }); - return this.$(this.viewContainerEl).animate({ - height: newHeight - }, 100, (function(_this) { - return function() { - var _base1, _ref1; - _this.$(_this.viewContainerEl).css({ - opacity: 1 - }).removeAttr('style'); - _this.$(_this.viewContainerEl).html(view); - if (typeof (_base1 = _this.currentView).onShow === "function") { - _base1.onShow(); - } - return (_ref1 = _this.previousView) != null ? typeof _ref1.destroy === "function" ? _ref1.destroy() : void 0 : void 0; - }; - })(this)); + if (this.animate) { + this.$(this.viewContainerEl).css({ + opacity: 0 + }); + return this.$(this.viewContainerEl).animate({ + height: newHeight + }, 100, (function(_this) { + return function() { + var _base1, _ref1; + _this.$(_this.viewContainerEl).css({ + opacity: 1 + }).removeAttr('style'); + _this.$(_this.viewContainerEl).html(view); + if (typeof (_base1 = _this.currentView).onShow === "function") { + _base1.onShow(); + } + return (_ref1 = _this.previousView) != null ? typeof _ref1.destroy === "function" ? _ref1.destroy() : void 0 : void 0; + }; + })(this)); + } else { + return this.$(this.viewContainerEl).css({ + height: newHeight + }).html(view); + } } }; @@ -374,7 +380,7 @@ }; Modal.prototype.destroy = function() { - var animate, removeViews; + var removeViews; Backbone.$('body').off('keyup', this.checkKey); Backbone.$('body').off('click', this.clickOutside); if (typeof this.onDestroy === "function") { @@ -393,8 +399,7 @@ return _this.remove(); }; })(this); - animate = this.getOption('animate'); - if (this.$el.fadeOut && animate) { + if (this.$el.fadeOut && this.animate) { this.$el.fadeOut({ duration: 200 }); @@ -504,6 +509,9 @@ options = {}; } this._ensureElement(); + Backbone.$('body').css({ + overflow: 'hidden' + }); if (this.modals.length > 0) { lastModal = _.last(this.modals); lastModal.modalEl.addClass("" + lastModal.prefix + "-view--stacked"); @@ -523,19 +531,17 @@ this.triggerMethod('swap', view); this.triggerMethod('show', view); Marionette.triggerMethodOn(view, 'show'); - if (this.modals.length > 0) { - _ref = this.modals; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - modalView = _ref[_i]; - modalView.$el.css({ - background: 'none' - }); - } + _ref = this.modals; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + modalView = _ref[_i]; + modalView.undelegateModalEvents(); } _ref1 = this.modals; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { modalView = _ref1[_j]; - modalView.undelegateModalEvents(); + modalView.$el.css({ + background: 'none' + }); } view.on('modal:destroy', this.destroy); this.modals.push(view); @@ -567,9 +573,15 @@ }; })(this), 300); if (this.zIndex !== 0) { - return lastModal.delegateModalEvents(); + lastModal.delegateModalEvents(); } } + if (this.zIndex === 0) { + Backbone.$('body').css({ + overflow: 'visible' + }); + } + return this.triggerMethod('modal:destroy', view); }; Modals.prototype.destroyAll = function() { diff --git a/backbone.modal-min.js b/backbone.modal-min.js index 9184828..1554d94 100644 --- a/backbone.modal-min.js +++ b/backbone.modal-min.js @@ -1 +1 @@ -(function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b={}.hasOwnProperty,c=function(a,c){function d(){this.constructor=a}for(var e in c)b.call(c,e)&&(a[e]=c[e]);return d.prototype=c.prototype,a.prototype=new d,a.__super__=c.prototype,a},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};!function(a){return"function"==typeof define&&define.amd?define(["underscore","backbone","exports"],a):"object"==typeof exports?a(require("underscore"),require("backbone"),exports):a(_,Backbone,{})}(function(b,e,f){return f=function(f){function g(){this.triggerCancel=a(this.triggerCancel,this),this.triggerSubmit=a(this.triggerSubmit,this),this.triggerView=a(this.triggerView,this),this.clickOutside=a(this.clickOutside,this),this.checkKey=a(this.checkKey,this),this.rendererCompleted=a(this.rendererCompleted,this),this.args=Array.prototype.slice.apply(arguments),e.View.prototype.constructor.apply(this,this.args),this.setUIElements()}return c(g,f),g.prototype.prefix="bbm",g.prototype.animate=!0,g.prototype.keyControl=!0,g.prototype.render=function(a){var c,d,f;return d=this.serializeData(),(!a||b.isEmpty(a))&&(a=0),this.$el.addClass(""+this.prefix+"-wrapper"),this.modalEl=e.$("
").addClass(""+this.prefix+"-modal"),this.template&&this.modalEl.html(this.template(d)),this.$el.html(this.modalEl),this.viewContainer?(this.viewContainerEl=this.modalEl.find(this.viewContainer),this.viewContainerEl.addClass(""+this.prefix+"-modal__views")):this.viewContainerEl=this.modalEl,$(":focus").blur(),(null!=(f=this.views)?f.length:void 0)>0&&this.openAt(a),"function"==typeof this.onRender&&this.onRender(),this.delegateModalEvents(),c=this.getOption("animate"),this.$el.fadeIn&&c?(this.modalEl.css({opacity:0}),this.$el.fadeIn({duration:100,complete:this.rendererCompleted})):(this.$el.show(),this.rendererCompleted()),this},g.prototype.rendererCompleted=function(){var a;return this.keyControl&&(e.$("body").on("keyup",this.checkKey),e.$("body").on("click",this.clickOutside)),this.modalEl.css({opacity:1}).addClass(""+this.prefix+"-modal--open"),"function"==typeof this.onShow&&this.onShow(),null!=(a=this.currentView)&&"function"==typeof a.onShow?a.onShow():void 0},g.prototype.setUIElements=function(){var a;if(this.template=this.getOption("template"),this.views=this.getOption("views"),null!=(a=this.views)&&(a.length=b.size(this.views)),this.viewContainer=this.getOption("viewContainer"),this.$el.hide(),b.isUndefined(this.template)&&b.isUndefined(this.views))throw new Error("No template or views defined for Backbone.Modal");if(this.template&&this.views&&b.isUndefined(this.viewContainer))throw new Error("No viewContainer defined for Backbone.Modal")},g.prototype.getOption=function(a){return a?this.options&&d.call(this.options,a)>=0&&null!=this.options[a]?this.options[a]:this[a]:void 0},g.prototype.serializeData=function(){var a;return a={},this.model&&(a=b.extend(a,this.model.toJSON())),this.collection&&(a=b.extend(a,{items:this.collection.toJSON()})),a},g.prototype.delegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!0,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.on("click",f,this.triggerSubmit),a&&this.$el.on("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.on(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.undelegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!1,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.off("click",f,this.triggerSubmit),a&&this.$el.off("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.off(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.checkKey=function(a){if(this.active)switch(a.keyCode){case 27:return this.triggerCancel(a);case 13:return this.triggerSubmit(a)}},g.prototype.clickOutside=function(a){return e.$(a.target).hasClass(""+this.prefix+"-wrapper")&&this.active?this.triggerCancel():void 0},g.prototype.buildView=function(a,c){var d;if(a)return c&&b.isFunction(c)&&(c=c()),b.isFunction(a)?(d=new a(c||this.args[0]),d instanceof e.View?{el:d.render().$el,view:d}:{el:a(c||this.args[0])}):{view:a,el:a.$el}},g.prototype.triggerView=function(a){var c,d,e,f,g,h,i;if(null!=a&&"function"==typeof a.preventDefault&&a.preventDefault(),f=a.data,d=this.buildView(f.view,f.viewOptions),this.currentView&&(this.previousView=this.currentView,!(null!=(i=f.openOptions)?i.skipSubmit:void 0))){if(("function"==typeof(g=this.previousView).beforeSubmit?g.beforeSubmit():void 0)===!1)return;"function"==typeof(h=this.previousView).submit&&h.submit()}this.currentView=d.view||d.el,c=0;for(e in this.views)f.view===this.views[e].view&&(this.currentIndex=c),c++;return f.onActive&&(b.isFunction(f.onActive)?f.onActive(this):b.isString(f.onActive)&&this[f.onActive].call(this,f)),this.shouldAnimate?this.animateToView(d.el):(this.shouldAnimate=!0,this.$(this.viewContainerEl).html(d.el))},g.prototype.animateToView=function(a){var b,c,d,f,g,h,i;return f={position:"relative",top:-9999,left:-9999},g=e.$("").css(f),g.html(this.$el.clone().css(f)),0!==e.$("tester").length?e.$("tester").replaceWith(g):e.$("body").append(g),b=g.find(this.viewContainer?this.viewContainer:"."+this.prefix+"-modal"),b.removeAttr("style"),d=b.outerHeight(),b.html(a),c=b.outerHeight(),d===c?(this.$(this.viewContainerEl).html(a),"function"==typeof(h=this.currentView).onShow&&h.onShow(),null!=(i=this.previousView)&&"function"==typeof i.destroy?i.destroy():void 0):(this.$(this.viewContainerEl).css({opacity:0}),this.$(this.viewContainerEl).animate({height:c},100,function(b){return function(){var c,d;return b.$(b.viewContainerEl).css({opacity:1}).removeAttr("style"),b.$(b.viewContainerEl).html(a),"function"==typeof(c=b.currentView).onShow&&c.onShow(),null!=(d=b.previousView)&&"function"==typeof d.destroy?d.destroy():void 0}}(this)))},g.prototype.triggerSubmit=function(a){var b,c;return null!=a&&a.preventDefault(),this.beforeSubmit&&this.beforeSubmit()===!1||this.currentView&&this.currentView.beforeSubmit&&this.currentView.beforeSubmit()===!1?void 0:this.submit||(null!=(b=this.currentView)?b.submit:void 0)||this.getOption("submitEl")?(null!=(c=this.currentView)&&"function"==typeof c.submit&&c.submit(),"function"==typeof this.submit&&this.submit(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy()):this.triggerCancel()},g.prototype.triggerCancel=function(a){return null!=a&&a.preventDefault(),this.beforeCancel&&this.beforeCancel()===!1?void 0:("function"==typeof this.cancel&&this.cancel(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy())},g.prototype.destroy=function(){var a,c;return e.$("body").off("keyup",this.checkKey),e.$("body").off("click",this.clickOutside),"function"==typeof this.onDestroy&&this.onDestroy(),this.shouldAnimate=!1,this.modalEl.addClass(""+this.prefix+"-modal--destroy"),c=function(a){return function(){var b;return null!=(b=a.currentView)&&"function"==typeof b.remove&&b.remove(),a.remove()}}(this),a=this.getOption("animate"),this.$el.fadeOut&&a?(this.$el.fadeOut({duration:200}),b.delay(function(){return c()},200)):c()},g.prototype.openAt=function(a){var c,d,e,f,g;b.isNumber(a)?c=a:b.isNumber(a._index)&&(c=a._index),e=0;for(f in this.views)if("length"!==f)if(b.isNumber(c))e===c&&(g=this.views[f]),e++;else if(b.isObject(a))for(d in this.views[f])a[d]===this.views[f][d]&&(g=this.views[f]);return g&&(this.currentIndex=b.indexOf(this.views,g),this.triggerView({data:b.extend(g,{openOptions:a})})),this},g.prototype.next=function(a){return null==a&&(a={}),this.currentIndex+1b;b++)if(b in this&&this[b]===a)return b;return-1};!function(a){return"function"==typeof define&&define.amd?define(["underscore","backbone","exports"],a):"object"==typeof exports?a(require("underscore"),require("backbone"),exports):a(_,Backbone,{})}(function(b,e,f){return f=function(f){function g(){this.triggerCancel=a(this.triggerCancel,this),this.triggerSubmit=a(this.triggerSubmit,this),this.triggerView=a(this.triggerView,this),this.clickOutside=a(this.clickOutside,this),this.checkKey=a(this.checkKey,this),this.rendererCompleted=a(this.rendererCompleted,this),this.args=Array.prototype.slice.apply(arguments),e.View.prototype.constructor.apply(this,this.args),this.setUIElements()}return c(g,f),g.prototype.prefix="bbm",g.prototype.animate=!0,g.prototype.keyControl=!0,g.prototype.showViewOnRender=!0,g.prototype.render=function(a){var c,d;return c=this.serializeData(),(!a||b.isEmpty(a))&&(a=0),this.$el.addClass(""+this.prefix+"-wrapper"),this.modalEl=e.$("
").addClass(""+this.prefix+"-modal"),this.template&&this.modalEl.html(this.template(c)),this.$el.html(this.modalEl),this.viewContainer?(this.viewContainerEl=this.modalEl.find(this.viewContainer),this.viewContainerEl.addClass(""+this.prefix+"-modal__views")):this.viewContainerEl=this.modalEl,$(":focus").blur(),(null!=(d=this.views)?d.length:void 0)>0&&this.showViewOnRender&&this.openAt(a),"function"==typeof this.onRender&&this.onRender(),this.delegateModalEvents(),this.trigger("modal:open"),this.$el.fadeIn&&this.animate?(this.modalEl.css({opacity:0}),this.$el.fadeIn({duration:100,complete:this.rendererCompleted})):this.rendererCompleted(),this},g.prototype.rendererCompleted=function(){var a;return this.keyControl&&(e.$("body").on("keyup",this.checkKey),e.$("body").on("click",this.clickOutside)),this.modalEl.css({opacity:1}).addClass(""+this.prefix+"-modal--open"),"function"==typeof this.onShow&&this.onShow(),null!=(a=this.currentView)&&"function"==typeof a.onShow?a.onShow():void 0},g.prototype.setUIElements=function(){var a;if(this.template=this.getOption("template"),this.views=this.getOption("views"),null!=(a=this.views)&&(a.length=b.size(this.views)),this.viewContainer=this.getOption("viewContainer"),this.animate=this.getOption("animate"),b.isUndefined(this.template)&&b.isUndefined(this.views))throw new Error("No template or views defined for Backbone.Modal");if(this.template&&this.views&&b.isUndefined(this.viewContainer))throw new Error("No viewContainer defined for Backbone.Modal")},g.prototype.getOption=function(a){return a?this.options&&d.call(this.options,a)>=0&&null!=this.options[a]?this.options[a]:this[a]:void 0},g.prototype.serializeData=function(){var a;return a={},this.model&&(a=b.extend(a,this.model.toJSON())),this.collection&&(a=b.extend(a,{items:this.collection.toJSON()})),a},g.prototype.delegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!0,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.on("click",f,this.triggerSubmit),a&&this.$el.on("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.on(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.undelegateModalEvents=function(){var a,c,d,e,f,g,h;this.active=!1,a=this.getOption("cancelEl"),f=this.getOption("submitEl"),f&&this.$el.off("click",f,this.triggerSubmit),a&&this.$el.off("click",a,this.triggerCancel),h=[];for(c in this.views)b.isString(c)&&"length"!==c?(d=c.match(/^(\S+)\s*(.*)$/),g=d[1],e=d[2],h.push(this.$el.off(g,e,this.views[c],this.triggerView))):h.push(void 0);return h},g.prototype.checkKey=function(a){if(this.active)switch(a.keyCode){case 27:return this.triggerCancel(a);case 13:return this.triggerSubmit(a)}},g.prototype.clickOutside=function(a){return e.$(a.target).hasClass(""+this.prefix+"-wrapper")&&this.active?this.triggerCancel():void 0},g.prototype.buildView=function(a,c){var d;if(a)return c&&b.isFunction(c)&&(c=c()),b.isFunction(a)?(d=new a(c||this.args[0]),d instanceof e.View?{el:d.render().$el,view:d}:{el:a(c||this.args[0])}):{view:a,el:a.$el}},g.prototype.triggerView=function(a){var c,d,e,f,g,h,i;if(null!=a&&"function"==typeof a.preventDefault&&a.preventDefault(),f=a.data,d=this.buildView(f.view,f.viewOptions),this.currentView&&(this.previousView=this.currentView,!(null!=(i=f.openOptions)?i.skipSubmit:void 0))){if(("function"==typeof(g=this.previousView).beforeSubmit?g.beforeSubmit():void 0)===!1)return;"function"==typeof(h=this.previousView).submit&&h.submit()}this.currentView=d.view||d.el,c=0;for(e in this.views)f.view===this.views[e].view&&(this.currentIndex=c),c++;return f.onActive&&(b.isFunction(f.onActive)?f.onActive(this):b.isString(f.onActive)&&this[f.onActive].call(this,f)),this.shouldAnimate?this.animateToView(d.el):(this.shouldAnimate=!0,this.$(this.viewContainerEl).html(d.el))},g.prototype.animateToView=function(a){var b,c,d,f,g,h,i;return f={position:"relative",top:-9999,left:-9999},g=e.$("").css(f),g.html(this.$el.clone().css(f)),0!==e.$("tester").length?e.$("tester").replaceWith(g):e.$("body").append(g),b=g.find(this.viewContainer?this.viewContainer:"."+this.prefix+"-modal"),b.removeAttr("style"),d=b.outerHeight(),b.html(a),c=b.outerHeight(),d===c?(this.$(this.viewContainerEl).html(a),"function"==typeof(h=this.currentView).onShow&&h.onShow(),null!=(i=this.previousView)&&"function"==typeof i.destroy?i.destroy():void 0):this.animate?(this.$(this.viewContainerEl).css({opacity:0}),this.$(this.viewContainerEl).animate({height:c},100,function(b){return function(){var c,d;return b.$(b.viewContainerEl).css({opacity:1}).removeAttr("style"),b.$(b.viewContainerEl).html(a),"function"==typeof(c=b.currentView).onShow&&c.onShow(),null!=(d=b.previousView)&&"function"==typeof d.destroy?d.destroy():void 0}}(this))):this.$(this.viewContainerEl).css({height:c}).html(a)},g.prototype.triggerSubmit=function(a){var b,c;return null!=a&&a.preventDefault(),this.beforeSubmit&&this.beforeSubmit()===!1||this.currentView&&this.currentView.beforeSubmit&&this.currentView.beforeSubmit()===!1?void 0:this.submit||(null!=(b=this.currentView)?b.submit:void 0)||this.getOption("submitEl")?(null!=(c=this.currentView)&&"function"==typeof c.submit&&c.submit(),"function"==typeof this.submit&&this.submit(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy()):this.triggerCancel()},g.prototype.triggerCancel=function(a){return null!=a&&a.preventDefault(),this.beforeCancel&&this.beforeCancel()===!1?void 0:("function"==typeof this.cancel&&this.cancel(),this.regionEnabled?this.trigger("modal:destroy"):this.destroy())},g.prototype.destroy=function(){var a;return e.$("body").off("keyup",this.checkKey),e.$("body").off("click",this.clickOutside),"function"==typeof this.onDestroy&&this.onDestroy(),this.shouldAnimate=!1,this.modalEl.addClass(""+this.prefix+"-modal--destroy"),a=function(a){return function(){var b;return null!=(b=a.currentView)&&"function"==typeof b.remove&&b.remove(),a.remove()}}(this),this.$el.fadeOut&&this.animate?(this.$el.fadeOut({duration:200}),b.delay(function(){return a()},200)):a()},g.prototype.openAt=function(a){var c,d,e,f,g;b.isNumber(a)?c=a:b.isNumber(a._index)&&(c=a._index),e=0;for(f in this.views)if("length"!==f)if(b.isNumber(c))e===c&&(g=this.views[f]),e++;else if(b.isObject(a))for(d in this.views[f])a[d]===this.views[f][d]&&(g=this.views[f]);return g&&(this.currentIndex=b.indexOf(this.views,g),this.triggerView({data:b.extend(g,{openOptions:a})})),this},g.prototype.next=function(a){return null==a&&(a={}),this.currentIndex+1 0) { + if (((_ref = this.views) != null ? _ref.length : void 0) > 0 && this.showViewOnRender) { this.openAt(options); } if (typeof this.onRender === "function") { this.onRender(); } this.delegateModalEvents(); - animate = this.getOption('animate'); - if (this.$el.fadeIn && animate) { + if (this.$el.fadeIn && this.animate) { this.modalEl.css({ opacity: 0 }); @@ -70,7 +71,6 @@ complete: this.rendererCompleted }); } else { - this.$el.show(); this.rendererCompleted(); } return this; @@ -99,7 +99,7 @@ _ref.length = _.size(this.views); } this.viewContainer = this.getOption('viewContainer'); - this.$el.hide(); + this.animate = this.getOption('animate'); if (_.isUndefined(this.template) && _.isUndefined(this.views)) { throw new Error('No template or views defined for Backbone.Modal'); } @@ -300,24 +300,30 @@ } return (_ref = this.previousView) != null ? typeof _ref.destroy === "function" ? _ref.destroy() : void 0 : void 0; } else { - this.$(this.viewContainerEl).css({ - opacity: 0 - }); - return this.$(this.viewContainerEl).animate({ - height: newHeight - }, 100, (function(_this) { - return function() { - var _base1, _ref1; - _this.$(_this.viewContainerEl).css({ - opacity: 1 - }).removeAttr('style'); - _this.$(_this.viewContainerEl).html(view); - if (typeof (_base1 = _this.currentView).onShow === "function") { - _base1.onShow(); - } - return (_ref1 = _this.previousView) != null ? typeof _ref1.destroy === "function" ? _ref1.destroy() : void 0 : void 0; - }; - })(this)); + if (this.animate) { + this.$(this.viewContainerEl).css({ + opacity: 0 + }); + return this.$(this.viewContainerEl).animate({ + height: newHeight + }, 100, (function(_this) { + return function() { + var _base1, _ref1; + _this.$(_this.viewContainerEl).css({ + opacity: 1 + }).removeAttr('style'); + _this.$(_this.viewContainerEl).html(view); + if (typeof (_base1 = _this.currentView).onShow === "function") { + _base1.onShow(); + } + return (_ref1 = _this.previousView) != null ? typeof _ref1.destroy === "function" ? _ref1.destroy() : void 0 : void 0; + }; + })(this)); + } else { + return this.$(this.viewContainerEl).css({ + height: newHeight + }).html(view); + } } }; @@ -374,7 +380,7 @@ }; Modal.prototype.destroy = function() { - var animate, removeViews; + var removeViews; Backbone.$('body').off('keyup', this.checkKey); Backbone.$('body').off('click', this.clickOutside); if (typeof this.onDestroy === "function") { @@ -393,8 +399,7 @@ return _this.remove(); }; })(this); - animate = this.getOption('animate'); - if (this.$el.fadeOut && animate) { + if (this.$el.fadeOut && this.animate) { this.$el.fadeOut({ duration: 200 }); diff --git a/bower.json b/bower.json index f860cfc..cd4f7a4 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "backbone.modal", "main": "backbone.modal.js", - "version": "1.1.1", + "version": "1.1.2", "homepage": "https://github.com/awkward/backbone.modal", "authors": [ "David van Leeuwen " diff --git a/examples/2_tab_based.html b/examples/2_tab_based.html index fc63a5c..da7b4d0 100644 --- a/examples/2_tab_based.html +++ b/examples/2_tab_based.html @@ -7,7 +7,7 @@ Tab based modal - + @@ -113,4 +113,4 @@

Tab based example

- \ No newline at end of file + diff --git a/examples/3_stacked_modal_with_marionette.html b/examples/3_stacked_modal_with_marionette.html index fd0bce5..ca9dd7d 100644 --- a/examples/3_stacked_modal_with_marionette.html +++ b/examples/3_stacked_modal_with_marionette.html @@ -24,7 +24,6 @@ Open modal -
@@ -59,7 +58,7 @@

The stacked modal