From e106457e61b5510a4a2e2a62a7015e8a2fb83313 Mon Sep 17 00:00:00 2001 From: Andy Joslin Date: Thu, 13 Feb 2014 14:42:32 -0500 Subject: [PATCH] feat(modal): add .isShown() method to modal instances Closes #320 --- js/ext/angular/src/service/ionicModal.js | 15 ++++++++-- .../angular/test/service/ionicModal.unit.js | 29 +++++++++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/js/ext/angular/src/service/ionicModal.js b/js/ext/angular/src/service/ionicModal.js index 4bef1881d0b..ec0283a69f5 100644 --- a/js/ext/angular/src/service/ionicModal.js +++ b/js/ext/angular/src/service/ionicModal.js @@ -9,13 +9,16 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv }, // Show the modal show: function() { - var _this = this; + var self = this; var element = angular.element(this.el); + + self._isShown = true; + if(!element.parent().length) { element.addClass(this.animation); $animate.enter(element, angular.element($document[0].body), null, function() { }); - ionic.views.Modal.prototype.show.call(_this); + ionic.views.Modal.prototype.show.call(self); } else { $animate.addClass(element, this.animation, function() { }); @@ -23,7 +26,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv if(!this.didInitEvents) { var onHardwareBackButton = function() { - _this.hide(); + self.hide(); }; self.scope.$on('$destroy', function() { @@ -41,6 +44,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv }, // Hide the modal hide: function() { + this._isShown = false; var element = angular.element(this.el); $animate.removeClass(element, this.animation); @@ -53,10 +57,15 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv remove: function() { var self = this, element = angular.element(this.el); + this._isShown = false; $animate.leave(angular.element(this.el), function() { self.scope.$parent.$broadcast('modal.removed', self); self.scope.$destroy(); }); + }, + + isShown: function() { + return !!this._isShown; } }); diff --git a/js/ext/angular/test/service/ionicModal.unit.js b/js/ext/angular/test/service/ionicModal.unit.js index e7637033442..b37c60ba089 100644 --- a/js/ext/angular/test/service/ionicModal.unit.js +++ b/js/ext/angular/test/service/ionicModal.unit.js @@ -32,13 +32,36 @@ describe('Ionic Modal', function() { }); timeout.flush(); + expect(done).toBe(true); + }); - waitsFor(function() { - return done; - }, "Modal should be loaded", 100); + it('should set isShown on show/hide', function() { + var m = modal.fromTemplate(''); + expect(m.isShown()).toBe(false); + m.show(); + expect(m.isShown()).toBe(true); + m.hide(); + expect(m.isShown()).toBe(false); + }); + it('should set isShown on remove', function() { + var m = modal.fromTemplate(''); + expect(m.isShown()).toBe(false); + m.show(); + expect(m.isShown()).toBe(true); + m.remove(); + expect(m.isShown()).toBe(false); }); + it('should animate leave and destroy scope on remove', inject(function($animate) { + var m = modal.fromTemplate(''); + spyOn($animate, 'leave').andCallFake(function(el, cb) { cb(); }); + spyOn(m.scope, '$destroy'); + m.remove(); + expect($animate.leave).toHaveBeenCalled(); + expect(m.scope.$destroy).toHaveBeenCalled(); + })); + it('Should close on hardware back button', function() { var template = ''; var modalInstance = modal.fromTemplate(template);