4
4
*/
5
5
define ( [
6
6
"jquery" ,
7
+ "underscore" ,
7
8
"mage/template" ,
8
9
"text!ui/template/dialog/dialog.html" ,
9
10
"jquery/ui"
10
- ] , function ( $ , template , dialogTemplate ) {
11
+ ] , function ( $ , _ , template , dialogTemplate ) {
11
12
"use strict" ;
12
13
13
14
/**
@@ -16,18 +17,25 @@ define([
16
17
$ . widget ( 'mage.dialog' , {
17
18
options : {
18
19
type : 'modal' ,
19
- title : null ,
20
- template : template ( dialogTemplate ) ,
21
- buttons : [ ] ,
20
+ title : '' ,
21
+ template : dialogTemplate ,
22
+ buttons : [ {
23
+ text : $ . mage . __ ( 'Ok' ) ,
24
+ 'class' : 'action-primary' ,
25
+ click : function ( ) {
26
+ this . closeDialog ( ) ;
27
+ }
28
+ } ] ,
22
29
events : [ ] ,
23
30
dialogClass : '' ,
24
31
dialogActiveClass : 'ui-dialog-active' ,
25
32
overlayClass : 'overlay_magento' ,
26
- dialogTitleSelector : '.ui- dialog-title ' ,
27
- dialogCloseBtnSelector : '.ui-dialog-titlebar-close ' ,
28
- dialogContentSelector : '.dialog- content' ,
29
- dialogActionsSelector : '.dialog-actions ' ,
33
+ dialogBlock : '[data-role=" dialog"] ' ,
34
+ dialogCloseBtn : '[data-role="closeBtn"] ' ,
35
+ dialogContent : '[data-role=" content"] ' ,
36
+ dialogAction : '[data-role="action"] ' ,
30
37
appendTo : 'body' ,
38
+ wrapperId : 'dialogs-wrapper' ,
31
39
position : {
32
40
modal : {
33
41
width : '75%' ,
@@ -40,101 +48,119 @@ define([
40
48
width : 'auto' ,
41
49
position : 'fixed' ,
42
50
top : '0' ,
43
- left : '100% ' ,
51
+ left : '148px ' ,
44
52
bottom : '0' ,
45
53
right : '0'
46
54
}
47
55
}
48
56
} ,
49
57
50
-
51
58
_create : function ( ) {
59
+ this . options . transitionEvent = this . whichTransitionEvent ( ) ;
52
60
this . _createWrapper ( ) ;
53
- this . _createTitlebar ( ) ;
61
+ this . _renderDialog ( ) ;
54
62
this . _createButtons ( ) ;
55
63
this . _style ( ) ;
56
- this . _insertContent ( ) ;
57
64
65
+ this . dialog . find ( this . options . dialogCloseBtn ) . on ( 'click' , _ . bind ( this . closeDialog , this ) ) ;
58
66
this . element . on ( 'openDialog' , _ . bind ( this . openDialog , this ) ) ;
59
67
this . element . on ( 'closeDialog' , _ . bind ( this . closeDialog , this ) ) ;
60
-
61
- return this . element ;
68
+ } ,
69
+ _getElem : function ( elem ) {
70
+ return this . dialog . find ( elem ) ;
62
71
} ,
63
72
openDialog : function ( ) {
64
73
this . _isOpen = true ;
65
-
66
74
this . _position ( ) ;
67
75
this . _createOverlay ( ) ;
68
- this . uiDialog . show ( ) ;
69
- this . uiDialog . addClass ( this . options . dialogActiveClass ) ;
70
- if ( this . options . type === 'slideOut' ) {
71
- this . uiDialog . animate ( {
72
- left : '148px'
73
- } , 300 ) ;
74
- }
76
+ this . dialog . show ( ) ;
77
+ this . dialog . addClass ( this . options . dialogActiveClass ) ;
78
+
79
+ return this . dialog ;
75
80
} ,
76
81
closeDialog : function ( ) {
77
82
var that = this ;
78
- this . _isOpen = false ;
79
83
80
- if ( this . options . type === 'slideOut' ) {
81
- this . uiDialog . animate ( {
82
- left : '100%'
83
- } , 300 , function ( ) {
84
- that . _destroyOverlay ( ) ;
85
- } ) ;
86
- } else {
87
- this . uiDialog . removeClass ( this . options . dialogActiveClass ) ;
84
+ this . _isOpen = false ;
85
+ this . dialog . one ( this . options . transitionEvent , function ( ) {
86
+ that . dialog . hide ( ) ;
87
+ that . _destroyOverlay ( ) ;
88
+ } ) ;
89
+ this . dialog . removeClass ( this . options . dialogActiveClass ) ;
90
+ if ( ! this . options . transitionEvent ) {
91
+ this . dialog . hide ( ) ;
88
92
this . _destroyOverlay ( ) ;
89
93
}
94
+
95
+ return this . dialog ;
90
96
} ,
91
97
_createWrapper : function ( ) {
92
- this . uiDialog = $ ( this . options . template ( { data : this . options } ) )
93
- . addClass ( this . options . dialogClass )
94
- . appendTo ( this . options . appendTo ) ;
95
- } ,
96
- _createTitlebar : function ( ) {
97
- this . uiDialog . find ( this . options . dialogTitleSelector ) . html ( this . options . title ) ;
98
- this . closeButton = this . uiDialog . find ( this . options . dialogCloseBtnSelector ) ;
99
- this . closeButton . on ( 'click' , _ . bind ( this . closeDialog , this ) ) ;
98
+ this . dialogWrapper = $ ( '#' + this . options . wrapperId ) ;
99
+
100
+ if ( ! this . dialogWrapper . length ) {
101
+ this . dialogWrapper = $ ( '<div></div>' )
102
+ . attr ( 'id' , this . options . wrapperId )
103
+ . appendTo ( this . options . appendTo ) ;
104
+ }
100
105
} ,
101
- _insertContent : function ( ) {
102
- this . content = this . uiDialog . find ( this . options . dialogContentSelector ) ;
103
- this . element
104
- . show ( )
105
- . appendTo ( this . content ) ;
106
+ _renderDialog : function ( ) {
107
+ this . dialog = $ ( template (
108
+ this . options . template ,
109
+ {
110
+ data : this . options
111
+ } ) ) . appendTo ( this . dialogWrapper ) ;
112
+
113
+ this . element . show ( ) . appendTo ( this . _getElem ( this . options . dialogContent ) ) ;
114
+ this . dialog . hide ( ) ;
106
115
} ,
107
116
_createButtons : function ( ) {
108
117
var that = this ;
109
118
110
- this . buttonsPane = this . uiDialog . find ( this . options . dialogActionsSelector ) ;
119
+ this . buttons = this . _getElem ( this . options . dialogAction ) ;
111
120
_ . each ( this . options . buttons , function ( btn , key ) {
112
- var button = that . buttonsPane . children ( ) [ key ] ;
121
+ var button = that . buttons [ key ] ;
113
122
114
- button . on ( 'click' , btn . click ) ;
123
+ button . on ( 'click' , _ . bind ( btn . click , that ) ) ;
115
124
} ) ;
116
125
} ,
117
126
_createOverlay : function ( ) {
118
- var that = this ;
127
+ var that = this ,
128
+ events ;
119
129
120
- document . body . style . overflow = 'hidden' ;
121
- this . overlay = $ ( '<div></div>' )
122
- . addClass ( this . options . overlayClass )
123
- . appendTo ( this . options . appendTo ) ;
124
- this . overlay . on ( 'click' , function ( ) {
130
+ this . overlay = $ ( '.' + this . options . overlayClass ) ;
131
+ if ( ! this . overlay . length ) {
132
+ document . body . style . overflow = 'hidden' ;
133
+ this . overlay = $ ( '<div></div>' )
134
+ . addClass ( this . options . overlayClass )
135
+ . appendTo ( this . options . appendTo ) ;
136
+ } else {
137
+ var zIndex = this . overlay . zIndex ( ) ;
138
+ this . overlay . zIndex ( zIndex + 1 ) ;
139
+ }
140
+ events = this . overlay . data ( 'events' ) ;
141
+ if ( events ) {
142
+ this . prevOverlayHandler = events . click [ 0 ] . handler ;
143
+ }
144
+ this . overlay . unbind ( ) . on ( 'click' , function ( ) {
125
145
that . closeDialog ( ) ;
126
146
} ) ;
127
147
} ,
128
148
129
149
_destroyOverlay : function ( ) {
130
- document . body . style . overflow = 'auto' ;
131
- if ( this . overlay ) {
150
+ var dialogCount = this . dialogWrapper . find ( this . options . dialogBlock ) . filter ( ':visible' ) . length ;
151
+
152
+ if ( ! dialogCount ) {
153
+ document . body . style . overflow = 'auto' ;
132
154
this . overlay . remove ( ) ;
133
155
this . overlay = null ;
156
+ } else {
157
+ var zIndex = this . overlay . zIndex ( ) ;
158
+ this . overlay . zIndex ( zIndex - 1 ) ;
159
+ this . overlay . unbind ( ) . on ( 'click' , this . prevOverlayHandler ) ;
134
160
}
135
161
} ,
136
162
_style : function ( ) {
137
- this . uiDialog . css ( {
163
+ this . dialog . css ( {
138
164
padding : '30px' ,
139
165
backgroundColor : '#fff' ,
140
166
zIndex : 1000
@@ -143,7 +169,23 @@ define([
143
169
_position : function ( ) {
144
170
var type = this . options . type ;
145
171
146
- this . uiDialog . css ( this . options . position [ type ] ) ;
172
+ this . dialog . css ( this . options . position [ type ] ) ;
173
+ } ,
174
+ whichTransitionEvent : function ( ) {
175
+ var t ,
176
+ el = document . createElement ( 'fakeelement' ) ,
177
+ transitions = {
178
+ 'transition' : 'transitionend' ,
179
+ 'OTransition' : 'oTransitionEnd' ,
180
+ 'MozTransition' : 'transitionend' ,
181
+ 'WebkitTransition' : 'webkitTransitionEnd'
182
+ } ;
183
+
184
+ for ( t in transitions ) {
185
+ if ( el . style [ t ] !== undefined && transitions . hasOwnProperty ( t ) ) {
186
+ return transitions [ t ] ;
187
+ }
188
+ }
147
189
}
148
190
} ) ;
149
191
0 commit comments