Skip to content

Commit 0286828

Browse files
committed
fix(modal): closing breaks on missing scope, 1.4
- supports Angular 1.4 Fixes angular-ui#3787 Fixes angular-ui#3806 Fixes angular-ui#3873 Closes angular-ui#3888
1 parent dd4f3cc commit 0286828

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

Diff for: src/modal/modal.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ angular.module('ui.bootstrap.modal', [])
7878
}
7979
}])
8080

81-
.directive('modalWindow', ['$modalStack', '$q', function ($modalStack, $q) {
81+
.directive('modalWindow', [
82+
'$modalStack', '$q', '$animate',
83+
function ($modalStack , $q , $animate) {
8284
return {
8385
restrict: 'EA',
8486
scope: {
85-
index: '@',
86-
animate: '='
87+
index: '@'
8788
},
8889
replace: true,
8990
transclude: true,
@@ -119,8 +120,14 @@ angular.module('ui.bootstrap.modal', [])
119120
});
120121

121122
modalRenderDeferObj.promise.then(function () {
122-
// trigger CSS transitions
123-
scope.animate = true;
123+
if (attrs.modalInClass) {
124+
$animate.addClass(element, attrs.modalInClass);
125+
126+
scope.$on($modalStack.WINDOW_CLOSING_EVENT, function (e, setIsAsync) {
127+
var done = setIsAsync();
128+
$animate.removeClass(element, attrs.modalInClass).then(done);
129+
});
130+
}
124131

125132
var inputsWithAutofocus = element[0].querySelectorAll('[autofocus]');
126133
/**
@@ -169,14 +176,21 @@ angular.module('ui.bootstrap.modal', [])
169176
};
170177
})
171178

172-
.factory('$modalStack', ['$animate', '$timeout', '$document', '$compile', '$rootScope', '$$stackedMap',
173-
function ($animate, $timeout, $document, $compile, $rootScope, $$stackedMap) {
179+
.factory('$modalStack', [
180+
'$animate', '$timeout', '$document', '$compile', '$rootScope',
181+
'$q',
182+
'$$stackedMap',
183+
function ($animate , $timeout , $document , $compile , $rootScope ,
184+
$q,
185+
$$stackedMap) {
174186

175187
var OPENED_MODAL_CLASS = 'modal-open';
176188

177189
var backdropDomEl, backdropScope;
178190
var openedWindows = $$stackedMap.createNew();
179-
var $modalStack = {};
191+
var $modalStack = {
192+
WINDOW_CLOSING_EVENT: 'modal.stack.window-closing'
193+
};
180194

181195
function backdropIndex() {
182196
var topBackdropIndex = -1;
@@ -203,8 +217,25 @@ angular.module('ui.bootstrap.modal', [])
203217
//clean up the stack
204218
openedWindows.remove(modalInstance);
205219

220+
var closingDeferred;
221+
var closingPromise;
222+
var setIsAsync = function () {
223+
if (!closingDeferred) {
224+
closingDeferred = $q.defer();
225+
closingPromise = closingDeferred.promise;
226+
}
227+
228+
return function () {
229+
closingDeferred.resolve();
230+
};
231+
};
232+
modalWindow.modalScope.$broadcast($modalStack.WINDOW_CLOSING_EVENT, setIsAsync);
233+
206234
//remove window DOM element
207-
removeAfterAnimate(modalWindow.modalDomEl, modalWindow.modalScope, function() {
235+
$q.when(closingPromise).then(function() {
236+
modalWindow.modalDomEl.remove();
237+
modalWindow.modalScope.$destroy();
238+
208239
body.toggleClass(OPENED_MODAL_CLASS, openedWindows.length() > 0);
209240
checkRemoveBackdrop();
210241
});

Diff for: src/modal/test/modal.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ describe('$modal', function () {
147147

148148
function close(modal, result, noFlush) {
149149
var closed = modal.close(result);
150+
$rootScope.$digest();
150151
if (!noFlush) {
151152
$timeout.flush();
152153
}
153-
$rootScope.$digest();
154154
return closed;
155155
}
156156

157157
function dismiss(modal, reason, noFlush) {
158158
var closed = modal.dismiss(reason);
159+
$rootScope.$digest();
159160
if (!noFlush) {
160161
$timeout.flush();
161162
}
162-
$rootScope.$digest();
163163
return closed;
164164
}
165165

Diff for: template/modal/window.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div modal-render="{{$isRendered}}" tabindex="-1" role="dialog" class="modal"
22
modal-animation-class="fade"
3-
ng-class="{in: animate}" ng-style="{'z-index': 1050 + index*10, display: 'block'}" ng-click="close($event)">
3+
modal-in-class="in"
4+
ng-style="{'z-index': 1050 + index*10, display: 'block'}" ng-click="close($event)">
45
<div class="modal-dialog" ng-class="size ? 'modal-' + size : ''"><div class="modal-content" modal-transclude></div></div>
56
</div>

0 commit comments

Comments
 (0)