@@ -7,41 +7,47 @@ define([
7
7
"underscore" ,
8
8
"mage/template" ,
9
9
"text!ui/template/dialog/dialog-modal.html" ,
10
+ "text!ui/template/dialog/dialog-slide.html" ,
10
11
"jquery/ui" ,
11
12
"mage/translate"
12
- ] , function ( $ , _ , template , dialogTemplate ) {
13
+ ] , function ( $ , _ , template , modalTpl , slideTpl ) {
13
14
"use strict" ;
14
15
15
16
/**
16
- * Dialog Widget - this widget is a wrapper for the jQuery UI Dialog
17
+ * Dialog Widget
17
18
*/
18
19
$ . widget ( 'mage.dialog' , {
19
20
options : {
20
21
type : 'modal' ,
21
22
title : '' ,
22
- template : dialogTemplate ,
23
- buttons : [ {
24
- text : $ . mage . __ ( 'Ok' ) ,
25
- class : 'action-primary' ,
26
- click : function ( ) {
27
- this . closeDialog ( ) ;
28
- }
29
- } ] ,
30
- events : [ ] ,
31
23
dialogClass : '' ,
32
- dialogActiveClass : '_show' ,
24
+ modalTpl : modalTpl ,
25
+ slideTpl : slideTpl ,
26
+ dialogVisibleClass : '_show' ,
33
27
parentDialogClass : '_has-dialog' ,
34
- overlayClass : 'overlay_magento' ,
35
- responsiveClass : 'dialog-slide' ,
28
+ innerScrollClass : '_inner-scroll' ,
36
29
responsive : false ,
30
+ innerScroll : false ,
37
31
dialogBlock : '[data-role="dialog"]' ,
38
32
dialogCloseBtn : '[data-role="closeBtn"]' ,
39
33
dialogContent : '[data-role="content"]' ,
40
34
dialogAction : '[data-role="action"]' ,
41
35
appendTo : 'body' ,
42
- wrapperId : 'dialogs-wrapper'
36
+ wrapperClass : 'dialogs-wrapper' ,
37
+ overlayClass : 'overlay_magento' ,
38
+ responsiveClass : 'dialog-slide' ,
39
+ dialogLeftMargin : 45 ,
40
+ buttons : [ {
41
+ text : $ . mage . __ ( 'Ok' ) ,
42
+ class : '' ,
43
+ click : function ( ) {
44
+ this . closeDialog ( ) ;
45
+ }
46
+ } ]
43
47
} ,
44
-
48
+ /**
49
+ * Creates dialog widget.
50
+ */
45
51
_create : function ( ) {
46
52
this . options . transitionEvent = this . whichTransitionEvent ( ) ;
47
53
this . _createWrapper ( ) ;
@@ -52,14 +58,42 @@ define([
52
58
this . element . on ( 'openDialog' , _ . bind ( this . openDialog , this ) ) ;
53
59
this . element . on ( 'closeDialog' , _ . bind ( this . closeDialog , this ) ) ;
54
60
} ,
61
+ /**
62
+ * Returns element from dialog node.
63
+ * @return {Object } - element.
64
+ */
55
65
_getElem : function ( elem ) {
56
66
return this . dialog . find ( elem ) ;
57
67
} ,
68
+ /**
69
+ * Gets visible dialog count.
70
+ * * @return {Number } - visible dialog count.
71
+ */
72
+ _getVisibleCount : function ( ) {
73
+ return this . dialogWrapper . find ( '.' + this . options . dialogVisibleClass ) . length ;
74
+ } ,
75
+ /**
76
+ * Gets visible slide type dialog count.
77
+ * * @return {Number } - visible dialog count.
78
+ */
79
+ _getVisibleSlideCount : function ( ) {
80
+ var elems = this . dialogWrapper . find ( '[data-type="' + this . options . type + '"]' ) ;
81
+
82
+ return elems . filter ( '.' + this . options . dialogVisibleClass ) . length ;
83
+ } ,
58
84
openDialog : function ( ) {
85
+ var that = this ;
86
+
59
87
this . options . isOpen = true ;
60
88
this . _createOverlay ( ) ;
61
- this . dialog . show ( ) ;
62
- this . dialog . addClass ( this . options . dialogActiveClass ) ;
89
+ this . _setActive ( ) ;
90
+ this . dialog . one ( this . options . transitionEvent , function ( ) {
91
+ that . _trigger ( 'opened' ) ;
92
+ } ) ;
93
+ this . dialog . addClass ( this . options . dialogVisibleClass ) ;
94
+ if ( ! this . options . transitionEvent ) {
95
+ that . _trigger ( 'opened' ) ;
96
+ }
63
97
64
98
return this . element ;
65
99
} ,
@@ -68,91 +102,125 @@ define([
68
102
69
103
this . options . isOpen = false ;
70
104
this . dialog . one ( this . options . transitionEvent , function ( ) {
71
- that . dialog . removeClass ( that . options . dialogActiveClass ) ;
72
105
that . _close ( ) ;
73
106
} ) ;
74
- this . dialog . removeClass ( this . options . dialogActiveClass ) ;
107
+ this . dialog . removeClass ( this . options . dialogVisibleClass ) ;
75
108
if ( ! this . options . transitionEvent ) {
76
- that . dialog . removeClass ( this . options . dialogActiveClass ) ;
77
109
that . _close ( ) ;
78
110
}
79
111
80
112
return this . element ;
81
113
} ,
114
+ /**
115
+ * Helper for closeDialog function.
116
+ */
82
117
_close : function ( ) {
83
- this . dialog . hide ( ) ;
118
+ var trigger = _ . bind ( this . _trigger , this , 'closed' , this . dialog ) ;
119
+
84
120
this . _destroyOverlay ( ) ;
85
- this . _trigger ( 'dialogClosed' ) ;
121
+ this . _unsetActive ( ) ;
122
+ _ . defer ( trigger , this ) ;
123
+ } ,
124
+ /**
125
+ * Set z-index and margin for dialog and overlay.
126
+ */
127
+ _setActive : function ( ) {
128
+ var zIndex = this . dialog . zIndex ( ) ;
129
+
130
+ this . prevOverlayIndex = this . overlay . zIndex ( ) ;
131
+ this . dialog . zIndex ( zIndex + this . _getVisibleCount ( ) ) ;
132
+ this . overlay . zIndex ( zIndex + ( this . _getVisibleCount ( ) - 1 ) ) ;
133
+ if ( this . _getVisibleSlideCount ( ) ) {
134
+ this . dialog . css ( 'marginLeft' , this . options . dialogLeftMargin * this . _getVisibleSlideCount ( ) ) ;
135
+ }
136
+ } ,
137
+ /**
138
+ * Unset styles for dialog and set z-index for previous dialog.
139
+ */
140
+ _unsetActive : function ( ) {
141
+ this . dialog . removeAttr ( 'style' ) ;
142
+ this . overlay . zIndex ( this . prevOverlayIndex ) ;
86
143
} ,
144
+ /**
145
+ * Creates wrapper to hold all dialogs.
146
+ */
87
147
_createWrapper : function ( ) {
88
- this . dialogWrapper = $ ( '# ' + this . options . wrapperId ) ;
148
+ this . dialogWrapper = $ ( '. ' + this . options . wrapperClass ) ;
89
149
if ( ! this . dialogWrapper . length ) {
90
150
this . dialogWrapper = $ ( '<div></div>' )
91
- . attr ( 'id' , this . options . wrapperId )
151
+ . addClass ( this . options . wrapperClass )
92
152
. appendTo ( this . options . appendTo ) ;
93
153
}
94
154
} ,
155
+ /**
156
+ * Compile template and append to wrapper.
157
+ */
95
158
_renderDialog : function ( ) {
96
- this . dialog = $ ( template (
97
- this . options . template ,
159
+ $ ( template (
160
+ this . options [ this . options . type + 'Tpl' ] ,
98
161
{
99
162
data : this . options
100
163
} ) ) . appendTo ( this . dialogWrapper ) ;
101
-
164
+ this . dialog = this . dialogWrapper . find ( this . options . dialogBlock ) . last ( ) ;
102
165
this . element . show ( ) . appendTo ( this . _getElem ( this . options . dialogContent ) ) ;
103
166
} ,
167
+ /**
168
+ * Creates buttons pane.
169
+ */
104
170
_createButtons : function ( ) {
105
171
var that = this ;
106
172
107
173
this . buttons = this . _getElem ( this . options . dialogAction ) ;
108
174
_ . each ( this . options . buttons , function ( btn , key ) {
109
175
var button = that . buttons [ key ] ;
110
176
111
- $ ( button ) . on ( 'click' , _ . bind ( btn . click , that ) ) ;
177
+ $ ( button ) . on ( 'click' , _ . bind ( btn . click , button ) ) ;
112
178
} ) ;
113
179
} ,
180
+ /**
181
+ * Creates overlay, append it to wrapper, set previous click event on overlay.
182
+ */
114
183
_createOverlay : function ( ) {
115
184
var that = this ,
116
185
events ;
117
186
118
187
this . overlay = $ ( '.' + this . options . overlayClass ) ;
119
188
if ( ! this . overlay . length ) {
120
-
121
189
$ ( this . options . appendTo ) . addClass ( this . options . parentDialogClass ) ;
122
190
this . overlay = $ ( '<div></div>' )
123
191
. addClass ( this . options . overlayClass )
124
- . appendTo ( this . options . appendTo ) ;
125
- } else {
126
- var zIndex = this . overlay . zIndex ( ) ;
127
- this . overlay . zIndex ( zIndex + 1 ) ;
192
+ . appendTo ( this . dialogWrapper ) ;
128
193
}
129
- events = this . overlay . data ( 'events' ) ;
194
+
195
+ events = $ . _data ( this . overlay . get ( 0 ) , 'events' ) ;
130
196
if ( events ) {
131
197
this . prevOverlayHandler = events . click [ 0 ] . handler ;
132
198
}
133
199
this . overlay . unbind ( ) . on ( 'click' , function ( ) {
134
200
that . closeDialog ( ) ;
135
201
} ) ;
136
202
} ,
137
-
203
+ /**
204
+ * Destroy overlay.
205
+ */
138
206
_destroyOverlay : function ( ) {
139
- var dialogCount = this . dialogWrapper . find ( this . options . dialogBlock ) . filter ( this . option . dialogClass ) . length ;
207
+ var dialogCount = this . dialogWrapper . find ( '.' + this . options . dialogVisibleClass ) . length ;
140
208
141
209
if ( ! dialogCount ) {
142
-
143
210
$ ( this . options . appendTo ) . removeClass ( this . options . parentDialogClass ) ;
144
-
145
211
this . overlay . remove ( ) ;
146
212
this . overlay = null ;
213
+
147
214
} else {
148
- var zIndex = this . overlay . zIndex ( ) ;
149
- this . overlay . zIndex ( zIndex - 1 ) ;
150
215
this . overlay . unbind ( ) . on ( 'click' , this . prevOverlayHandler ) ;
151
216
}
152
217
} ,
218
+ /**
219
+ * Detects browser transition event.
220
+ */
153
221
whichTransitionEvent : function ( ) {
154
222
var transition ,
155
- el = document . createElement ( 'fakeelement ' ) ,
223
+ el = document . createElement ( 'element ' ) ,
156
224
transitions = {
157
225
'transition' : 'transitionend' ,
158
226
'OTransition' : 'oTransitionEnd' ,
@@ -169,4 +237,4 @@ define([
169
237
} ) ;
170
238
171
239
return $ . mage . dialog ;
172
- } ) ;
240
+ } ) ;
0 commit comments