Skip to content

Commit

Permalink
feat(modal): add .isShown() method to modal instances
Browse files Browse the repository at this point in the history
Closes #320
  • Loading branch information
ajoslin committed Feb 13, 2014
1 parent 110ff9f commit e106457
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
15 changes: 12 additions & 3 deletions js/ext/angular/src/service/ionicModal.js
Expand Up @@ -9,21 +9,24 @@ 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() {
});
}

if(!this.didInitEvents) {
var onHardwareBackButton = function() {
_this.hide();
self.hide();
};

self.scope.$on('$destroy', function() {
Expand All @@ -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);

Expand All @@ -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;
}
});

Expand Down
29 changes: 26 additions & 3 deletions js/ext/angular/test/service/ionicModal.unit.js
Expand Up @@ -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('<div class="modal">hello</div>');
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('<div class="modal">hello</div>');
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('<div class="modal"></div>');
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 = '<div class="modal"></div>';
var modalInstance = modal.fromTemplate(template);
Expand Down

0 comments on commit e106457

Please sign in to comment.