33var __ = require ( 'underscore' ) ,
44 domUtils = require ( '../utils/dom' ) ,
55 loadTemplate = require ( '../utils/loadTemplate' ) ,
6- baseVw = require ( './baseVw' ) ;
6+ baseVw = require ( './baseVw' ) ,
7+ baseModal ;
78
8- module . exports = baseVw . extend ( {
9+ baseModal = baseVw . extend ( {
910 constructor : function ( options ) {
1011 var events = { } ,
1112 args = Array . prototype . slice . call ( arguments ) ,
@@ -36,18 +37,27 @@ module.exports = baseVw.extend({
3637 events [ 'click .js-modal-close' ] = '__closeClick' ;
3738 this . events = __ . extend ( { } , events , this . events || { } ) ;
3839
39- if ( typeof this . constructor . __modalsOpen === 'undefined' ) {
40- this . constructor . __modalsOpen = 0 ;
41- }
40+ if ( typeof baseModal . __openModals === 'undefined' ) {
41+ baseModal . __openModals = [ ] ;
42+ }
43+
44+ this . $html = baseModal . $html = baseModal . $html || $ ( 'html' ) ;
45+ this . $container = baseModal . $container = baseModal . $container || $ ( '#contentFrame' ) ;
4246
43- this . $html = this . constructor . $html = this . constructor . $html || $ ( 'html' ) ;
44- this . $container = this . constructor . $container = this . constructor . $container || $ ( '#contentFrame' ) ;
47+ if ( ! baseModal . __docKeyPressHandlerBound ) {
48+ $ ( document ) . on ( 'keyup' , baseModal . __onDocKeypress ) ;
49+ baseModal . __docKeyPressHandlerBound = true ;
50+ }
4551
4652 baseVw . prototype . constructor . apply ( this , args ) ;
4753 } ,
4854
4955 __onDocKeypress : function ( e ) {
50- if ( this . __options . dismissOnEscPress && e . keyCode === 27 ) {
56+ var openModals = baseModal . __openModals ,
57+ topModal = this . __getTopModal ( ) ;
58+
59+ if ( this . __options . dismissOnEscPress && e . keyCode === 27 &&
60+ topModal && topModal === this ) {
5161 this . close ( ) ;
5262 }
5363 } ,
@@ -62,6 +72,27 @@ module.exports = baseVw.extend({
6272 this . close ( ) ;
6373 } ,
6474
75+ __getTopModal : function ( ) {
76+ var openModals = baseModal . __openModals . slice ( ) ;
77+
78+ openModals = openModals . map ( ( modal , i ) => {
79+ return { modal : modal , index : i } ;
80+ } ) ;
81+
82+ openModals = openModals . sort ( ( a , b ) => {
83+ var aZindex = parseInt ( window . getComputedStyle ( a . modal . el ) . zIndex ) || 0 ,
84+ bZindex = parseInt ( window . getComputedStyle ( b . modal . el ) . zIndex ) || 0 ;
85+
86+ if ( aZindex === bZindex ) {
87+ return ( a . index < b . index ) ? - 1 : ( a . index > b . index ) ? 1 : 0 ;
88+ } else {
89+ return ( aZindex < bZindex ) ? - 1 : ( aZindex > bZindex ) ? 1 : 0 ;
90+ }
91+ } ) ;
92+
93+ return openModals [ openModals . length - 1 ] && openModals [ openModals . length - 1 ] . modal ;
94+ } ,
95+
6596 isOpen : function ( ) {
6697 return this . _open ;
6798 } ,
@@ -70,8 +101,8 @@ module.exports = baseVw.extend({
70101 if ( ! domUtils . isInPage ( this . el ) ) {
71102 this . $html . addClass ( 'modalOpen' ) ;
72103 this . $container . append ( this . el ) ;
73- this . constructor . __modalsOpen += 1 ;
74- $ ( document ) . on ( 'keyup' , this . __onDocKeypress ) ;
104+ baseModal . __openModals . push ( this ) ;
105+ baseModal . __topModal = this . __getTopModal ( ) ;
75106 this . _open = true ;
76107 this . trigger ( 'open' ) ;
77108 window . obEventBus . trigger ( 'modal-open' , { modal : this } ) ;
@@ -81,10 +112,13 @@ module.exports = baseVw.extend({
81112 } ,
82113
83114 close : function ( ) {
115+ var modalIndex ;
116+
84117 if ( domUtils . isInPage ( this . el ) ) {
85- this . constructor . __modalsOpen -= 1 ;
86- ! this . constructor . __modalsOpen && this . $html . removeClass ( 'modalOpen' ) ;
87- $ ( document ) . off ( 'keyup' , this . __onDocKeypress ) ;
118+ modalIndex = baseModal . __openModals . indexOf ( this ) ;
119+ modalIndex >= 0 && baseModal . __openModals . splice ( modalIndex , 1 ) ;
120+ ! baseModal . __openModals . length && this . $html . removeClass ( 'modalOpen' ) ;
121+ baseModal . __topModal = this . __getTopModal ( ) ;
88122 this . $container [ 0 ] . removeChild ( this . el ) ;
89123 this . _open = false ;
90124 this . trigger ( 'close' ) ;
@@ -131,3 +165,14 @@ module.exports = baseVw.extend({
131165 return this ;
132166 }
133167} ) ;
168+
169+ baseModal . __onDocKeypress = function ( e ) {
170+ var topModal ;
171+
172+ if ( e . keyCode === 27 && ( topModal = baseModal . __topModal ) &&
173+ topModal . __options . dismissOnEscPress ) {
174+ topModal . close ( ) ;
175+ }
176+ } ;
177+
178+ module . exports = baseModal ;
0 commit comments