@@ -54,6 +54,61 @@ angular.module('ui.bootstrap.modal', [])
5454 } ;
5555 } )
5656
57+ /**
58+ * A helper, internal data structure that stores all references attached to key
59+ */
60+ . factory ( '$$multiMap' , function ( ) {
61+ return {
62+ createNew : function ( ) {
63+ var map = { } ;
64+
65+ return {
66+ entries : function ( ) {
67+ return Object . keys ( map ) . map ( function ( key ) {
68+ return {
69+ key : key ,
70+ value : map [ key ]
71+ } ;
72+ } ) ;
73+ } ,
74+ get : function ( key ) {
75+ return map [ key ] ;
76+ } ,
77+ hasKey : function ( key ) {
78+ return ! ! map [ key ] ;
79+ } ,
80+ keys : function ( ) {
81+ return Object . keys ( map ) ;
82+ } ,
83+ put : function ( key , value ) {
84+ if ( ! map [ key ] ) {
85+ map [ key ] = [ ] ;
86+ }
87+
88+ map [ key ] . push ( value ) ;
89+ } ,
90+ remove : function ( key , value ) {
91+ var values = map [ key ] ;
92+
93+ if ( ! values ) {
94+ return ;
95+ }
96+
97+ var idx = values . indexOf ( value ) ;
98+
99+ if ( idx !== - 1 ) {
100+ values . splice ( idx , 1 ) ;
101+ }
102+
103+ if ( ! values . length ) {
104+ delete map [ key ] ;
105+ }
106+ }
107+ } ;
108+ }
109+ } ;
110+ } )
111+
57112/**
58113 * A helper directive for the $modal service. It creates a backdrop element.
59114 */
@@ -220,10 +275,12 @@ angular.module('ui.bootstrap.modal', [])
220275 '$animate' , '$timeout' , '$document' , '$compile' , '$rootScope' ,
221276 '$q' ,
222277 '$injector' ,
278+ '$$multiMap' ,
223279 '$$stackedMap' ,
224280 function ( $animate , $timeout , $document , $compile , $rootScope ,
225281 $q ,
226282 $injector ,
283+ $$multiMap ,
227284 $$stackedMap ) {
228285 var $animateCss = null ;
229286
@@ -235,6 +292,7 @@ angular.module('ui.bootstrap.modal', [])
235292
236293 var backdropDomEl , backdropScope ;
237294 var openedWindows = $$stackedMap . createNew ( ) ;
295+ var openedClasses = $$multiMap . createNew ( ) ;
238296 var $modalStack = {
239297 NOW_CLOSING_EVENT : 'modal.stack.now-closing'
240298 } ;
@@ -264,15 +322,16 @@ angular.module('ui.bootstrap.modal', [])
264322 } ) ;
265323
266324 function removeModalWindow ( modalInstance , elementToReceiveFocus ) {
267-
268325 var body = $document . find ( 'body' ) . eq ( 0 ) ;
269326 var modalWindow = openedWindows . get ( modalInstance ) . value ;
270327
271328 //clean up the stack
272329 openedWindows . remove ( modalInstance ) ;
273330
274331 removeAfterAnimate ( modalWindow . modalDomEl , modalWindow . modalScope , function ( ) {
275- body . toggleClass ( modalWindow . openedClass || OPENED_MODAL_CLASS , openedWindows . length ( ) > 0 ) ;
332+ var modalBodyClass = modalWindow . openedClass || OPENED_MODAL_CLASS ;
333+ openedClasses . remove ( modalBodyClass , modalInstance ) ;
334+ body . toggleClass ( modalBodyClass , openedClasses . hasKey ( modalBodyClass ) ) ;
276335 } ) ;
277336 checkRemoveBackdrop ( ) ;
278337
@@ -377,7 +436,8 @@ angular.module('ui.bootstrap.modal', [])
377436 } ) ;
378437
379438 $modalStack . open = function ( modalInstance , modal ) {
380- var modalOpener = $document [ 0 ] . activeElement ;
439+ var modalOpener = $document [ 0 ] . activeElement ,
440+ modalBodyClass = modal . openedClass || OPENED_MODAL_CLASS ;
381441
382442 openedWindows . add ( modalInstance , {
383443 deferred : modal . deferred ,
@@ -388,6 +448,8 @@ angular.module('ui.bootstrap.modal', [])
388448 openedClass : modal . openedClass
389449 } ) ;
390450
451+ openedClasses . put ( modalBodyClass , modalInstance ) ;
452+
391453 var body = $document . find ( 'body' ) . eq ( 0 ) ,
392454 currBackdropIndex = backdropIndex ( ) ;
393455
@@ -419,7 +481,8 @@ angular.module('ui.bootstrap.modal', [])
419481 openedWindows . top ( ) . value . modalDomEl = modalDomEl ;
420482 openedWindows . top ( ) . value . modalOpener = modalOpener ;
421483 body . append ( modalDomEl ) ;
422- body . addClass ( modal . openedClass || OPENED_MODAL_CLASS ) ;
484+ body . addClass ( modalBodyClass ) ;
485+
423486 $modalStack . clearFocusListCache ( ) ;
424487 } ;
425488
0 commit comments