@@ -11,10 +11,11 @@ define([
11
11
'underscore' ,
12
12
'mage/template' ,
13
13
'text!ui/template/modal/modal-prompt-content.html' ,
14
+ 'Magento_Ui/js/lib/key-codes' ,
14
15
'jquery-ui-modules/widget' ,
15
16
'Magento_Ui/js/modal/modal' ,
16
17
'mage/translate'
17
- ] , function ( $ , _ , template , promptContentTmpl ) {
18
+ ] , function ( $ , _ , template , promptContentTmpl , keyCodes ) {
18
19
'use strict' ;
19
20
20
21
$ . widget ( 'mage.prompt' , $ . mage . modal , {
@@ -64,13 +65,31 @@ define([
64
65
click : function ( ) {
65
66
this . closeModal ( true ) ;
66
67
}
67
- } ]
68
+ } ] ,
69
+ keyEventHandlers : {
70
+
71
+ /**
72
+ * Escape key press handler,
73
+ * close modal window
74
+ * @param {Object } event - event
75
+ */
76
+ escapeKey : function ( event ) {
77
+ if ( this . options . isOpen && this . modal . find ( document . activeElement ) . length ||
78
+ this . options . isOpen && this . modal [ 0 ] === document . activeElement ) {
79
+ this . _close ( ) ;
80
+ }
81
+ }
82
+ }
68
83
} ,
69
84
70
85
/**
71
86
* Create widget.
72
87
*/
73
88
_create : function ( ) {
89
+ _ . bindAll (
90
+ this ,
91
+ 'keyEventSwitcher'
92
+ ) ;
74
93
this . options . focus = this . options . promptField ;
75
94
this . options . validation = this . options . validation && this . options . validationRules . length ;
76
95
this . _super ( ) ;
@@ -118,6 +137,18 @@ define([
118
137
return formTemplate ;
119
138
} ,
120
139
140
+ /**
141
+ * Listener key events.
142
+ * Call handler function if it exists
143
+ */
144
+ keyEventSwitcher : function ( event ) {
145
+ var key = keyCodes [ event . keyCode ] ;
146
+
147
+ if ( this . options . keyEventHandlers . hasOwnProperty ( key ) ) {
148
+ this . options . keyEventHandlers [ key ] . apply ( this , arguments ) ;
149
+ }
150
+ } ,
151
+
121
152
/**
122
153
* Remove widget
123
154
*/
@@ -177,3 +208,4 @@ define([
177
208
return $ ( '<div class="prompt-message"></div>' ) . html ( config . content ) . prompt ( config ) ;
178
209
} ;
179
210
} ) ;
211
+
0 commit comments