Skip to content

Commit 7d076bd

Browse files
committed
feat(modal): Create a modal backdrop wrapper w/ internal modal directive, closes #605
1 parent dcbe378 commit 7d076bd

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

Diff for: js/ext/angular/src/directive/ionicModal.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('ionic.ui.modal', [])
5+
6+
/*
7+
* We don't document the ionModal directive, we instead document
8+
* the $ionicModal service
9+
*/
10+
.directive('ionModal', [function() {
11+
return {
12+
restrict: 'E',
13+
transclude: true,
14+
replace: true,
15+
template: '<div class="modal-backdrop">' +
16+
'<div class="modal-wrapper" ng-transclude></div>' +
17+
'</div>'
18+
};
19+
}]);
20+
21+
})();

Diff for: js/ext/angular/src/service/ionicModal.js

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.service.platform', 'ngAnimate'])
1+
angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.service.platform', 'ionic.ui.modal'])
22

33

4-
.factory('$ionicModal', ['$rootScope', '$document', '$compile', '$animate', '$q', '$timeout', '$ionicPlatform', '$ionicTemplateLoader', function($rootScope, $document, $compile, $animate, $q, $timeout, $ionicPlatform, $ionicTemplateLoader) {
4+
.factory('$ionicModal', ['$rootScope', '$document', '$compile', '$timeout', '$ionicPlatform', '$ionicTemplateLoader',
5+
function( $rootScope, $document, $compile, $timeout, $ionicPlatform, $ionicTemplateLoader) {
6+
57
var ModalView = ionic.views.Modal.inherit({
68
initialize: function(opts) {
79
ionic.views.Modal.prototype.initialize.call(this, opts);
810
this.animation = opts.animation || 'slide-in-up';
911
},
12+
1013
// Show the modal
1114
show: function() {
1215
var self = this;
13-
var element = angular.element(self.el);
16+
var modalEl = angular.element(self.modalEl);
1417

15-
document.body.classList.add('modal-open');
18+
$document[0].body.classList.add('modal-open');
1619

1720
self._isShown = true;
1821

19-
if(!element.parent().length) {
20-
self.el.classList.add(self.animation);
22+
if(!self.el.parentElement) {
23+
modalEl.addClass(self.animation);
2124
$document[0].body.appendChild(self.el);
2225
}
2326

24-
element.addClass('ng-enter active');
25-
element.removeClass('ng-leave ng-leave-active');
27+
modalEl.addClass('ng-enter active')
28+
.removeClass('ng-leave ng-leave-active');
2629

2730
$timeout(function(){
28-
element.addClass('ng-enter-active');
31+
modalEl.addClass('ng-enter-active');
2932
self.scope.$parent && self.scope.$parent.$broadcast('modal.shown');
3033
}, 20);
3134

@@ -34,25 +37,26 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
3437
}, 200);
3538

3639
},
40+
3741
// Hide the modal
3842
hide: function() {
3943
this._isShown = false;
40-
var element = angular.element(this.el);
44+
var modalEl = angular.element(this.modalEl);
4145

42-
element.addClass('ng-leave');
46+
modalEl.addClass('ng-leave');
4347

4448
$timeout(function(){
45-
element.addClass('ng-leave-active');
46-
element.removeClass('ng-enter ng-enter-active active');
49+
modalEl.addClass('ng-leave-active')
50+
.removeClass('ng-enter ng-enter-active active');
4751
}, 20);
4852

4953
$timeout(function(){
50-
document.body.classList.remove('modal-open');
51-
}, 400);
54+
$document[0].body.classList.remove('modal-open');
55+
}, 350);
5256

5357
ionic.views.Modal.prototype.hide.call(this);
5458

55-
this.scope.$parent.$broadcast('modal.hidden');
59+
this.scope.$parent && this.scope.$parent.$broadcast('modal.hidden');
5660

5761
this._deregisterBackButton && this._deregisterBackButton();
5862
},
@@ -61,12 +65,12 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
6165
remove: function() {
6266
var self = this;
6367
self.hide();
64-
self.scope.$parent.$broadcast('modal.removed');
68+
self.scope.$parent && self.scope.$parent.$broadcast('modal.removed');
6569

6670
$timeout(function(){
6771
self.scope.$destroy();
6872
self.el && self.el.parentElement && self.el.parentElement.removeChild(self.el);
69-
}, 1000);
73+
}, 750);
7074
},
7175

7276
isShown: function() {
@@ -79,9 +83,10 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
7983
var scope = options.scope && options.scope.$new() || $rootScope.$new(true);
8084

8185
// Compile the template
82-
var element = $compile(templateString)(scope);
86+
var element = $compile('<ion-modal>' + templateString + '</ion-modal>')(scope);
8387

8488
options.el = element[0];
89+
options.modalEl = options.el.querySelector('.modal');
8590
var modal = new ModalView(options);
8691

8792
modal.scope = scope;

Diff for: js/ext/angular/test/service/ionicModal.unit.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ describe('Ionic Modal', function() {
1818
var template = '<div class="modal"></div>';
1919
var modalInstance = modal.fromTemplate(template);
2020
modalInstance.show();
21-
expect(modalInstance.el.classList.contains('modal')).toBe(true);
22-
expect(modalInstance.el.classList.contains('slide-in-up')).toBe(true);
21+
expect(modalInstance.el.classList.contains('modal-backdrop')).toBe(true);
22+
expect(modalInstance.modalEl.classList.contains('modal')).toBe(true);
23+
expect(modalInstance.modalEl.classList.contains('slide-in-up')).toBe(true);
2324
});
2425

2526
it('Should show for dynamic template', function() {
@@ -30,8 +31,9 @@ describe('Ionic Modal', function() {
3031
var modalInstance = modal.fromTemplateUrl('modal.html', function(modalInstance) {
3132
done = true;
3233
modalInstance.show();
33-
expect(modalInstance.el.classList.contains('modal')).toBe(true);
34-
expect(modalInstance.el.classList.contains('active')).toBe(true);
34+
expect(modalInstance.el.classList.contains('modal-backdrop')).toBe(true);
35+
expect(modalInstance.modalEl.classList.contains('modal')).toBe(true);
36+
expect(modalInstance.modalEl.classList.contains('active')).toBe(true);
3537
});
3638

3739
timeout.flush();

0 commit comments

Comments
 (0)