8
8
'jquery/ui' ,
9
9
'mage/dropdown' ,
10
10
'mage/template'
11
- ] , function ( $ , _ ) {
11
+ ] , function ( $ ) {
12
12
'use strict' ;
13
13
14
14
$ . widget ( 'mage.addToCart' , {
@@ -18,23 +18,24 @@ define([
18
18
singleOpenDropDown : true ,
19
19
dialog : { } , // Options for mage/dropdown
20
20
dialogDelay : 500 , // Delay in ms after resize dropdown shown again
21
+ origin : '' , //Required, type of popup: 'msrp', 'tier' or 'info' popup
21
22
22
23
// Selectors
23
24
cartForm : '.form.map.checkout' ,
24
25
msrpLabelId : '#map-popup-msrp' ,
25
26
priceLabelId : '#map-popup-price' ,
26
27
popUpAttr : '[data-role=msrp-popup-template]' ,
27
- cartButtonId : '' , // better to be cartButton
28
- popupId : '' , // better to be popup
28
+ popupCartButtonId : '#map-popup-button' ,
29
+ paypalCheckoutButons : '[data-action=checkout-form-submit]' ,
30
+ popupId : '' ,
29
31
realPrice : '' ,
30
32
msrpPrice : '' ,
31
- helpLinkId : '' , // better to be helpLink
33
+ helpLinkId : '' ,
32
34
addToCartButton : '' ,
33
35
34
36
// Text options
35
37
productName : '' ,
36
38
addToCartUrl : ''
37
-
38
39
} ,
39
40
40
41
openDropDown : null ,
@@ -62,61 +63,149 @@ define([
62
63
* @private
63
64
*/
64
65
_create : function ( ) {
65
- var tierOptions ;
66
-
67
- this . popupDOM = $ ( this . options . popUpAttr ) [ 0 ] ;
68
- this . infoPopupDOM = $ ( '[data-role=msrp-info-template]' ) [ 0 ] ;
69
-
70
- if ( this . options . popupId ) {
71
- $ ( 'body' ) . append ( $ ( this . popupDOM ) . html ( ) ) ;
72
- this . $popup = $ ( $ ( this . popupDOM ) . html ( ) ) ;
73
-
74
- $ ( this . options . popupId ) . on ( 'click' , function ( e ) {
75
- this . popUpOptions . position . of = $ ( e . target ) ;
76
- this . $popup . find ( this . options . msrpLabelId ) . html ( this . options . msrpPrice ) ;
77
- this . $popup . find ( this . options . priceLabelId ) . html ( this . options . realPrice ) ;
78
- this . $popup . dropdownDialog ( this . popUpOptions ) . dropdownDialog ( 'open' ) ;
79
-
80
- this . $popup . find ( 'button' ) . on ( 'click' , function ( ) {
81
- if ( this . options . addToCartButton ) {
82
- $ ( this . options . addToCartButton ) . click ( ) ;
83
- }
84
- } . bind ( this ) ) ;
85
- this . _toggle ( this . $popup ) ;
86
- } . bind ( this ) ) ;
66
+ if ( this . options . origin === 'msrp' ) {
67
+ this . initMsrpPopup ( ) ;
68
+ } else if ( this . options . origin === 'info' ) {
69
+ this . initInfoPopup ( ) ;
70
+ } else if ( this . options . origin === 'tier' ) {
71
+ this . initTierPopup ( ) ;
87
72
}
73
+ } ,
74
+
75
+ /**
76
+ * Init msrp popup
77
+ * @private
78
+ */
79
+ initMsrpPopup : function ( ) {
80
+ var popupDOM = $ ( this . options . popUpAttr ) [ 0 ] ,
81
+ $msrpPopup = $ ( $ ( popupDOM ) . html ( ) ) . clone ( ) ;
88
82
89
- if ( this . options . helpLinkId ) {
90
- this . $infoPopup = $ ( this . infoPopupDOM . innerText ) . appendTo ( 'body' ) ;
91
- $ ( this . options . helpLinkId ) . on ( 'click' , function ( e ) {
92
- this . popUpOptions . position . of = $ ( e . target ) ;
93
- this . $infoPopup . dropdownDialog ( this . popUpOptions ) . dropdownDialog ( 'open' ) ;
94
- this . _toggle ( this . $infoPopup ) ;
95
- } . bind ( this ) ) ;
83
+ $msrpPopup . find ( this . options . productIdInput ) . val ( this . options . productId ) ;
84
+ $ ( 'body' ) . append ( $msrpPopup ) ;
85
+ $msrpPopup . trigger ( 'contentUpdated' ) ;
86
+
87
+ $msrpPopup . find ( 'button' ) . on ( 'click' , function ( ev ) {
88
+ ev . preventDefault ( ) ;
89
+ this . handleMsrpAddToCart ( ) ;
90
+ } . bind ( this ) ) ;
91
+
92
+ $msrpPopup . find ( this . options . paypalCheckoutButons ) . on ( 'click' ,
93
+ this . handleMsrpPaypalCheckout . bind ( this ) ) ;
94
+
95
+ $ ( this . options . popupId ) . on ( 'click' , function ( e ) {
96
+ this . popUpOptions . position . of = $ ( e . target ) ;
97
+ $msrpPopup . find ( this . options . msrpLabelId ) . html ( this . options . msrpPrice ) ;
98
+ $msrpPopup . find ( this . options . priceLabelId ) . html ( this . options . realPrice ) ;
99
+ $msrpPopup . dropdownDialog ( this . popUpOptions ) . dropdownDialog ( 'open' ) ;
100
+ this . _toggle ( $msrpPopup ) ;
101
+ } . bind ( this ) ) ;
102
+
103
+ this . $popup = $msrpPopup ;
104
+ } ,
105
+
106
+ /**
107
+ * Init info popup
108
+ * @private
109
+ */
110
+ initInfoPopup : function ( ) {
111
+ var infoPopupDOM = $ ( '[data-role=msrp-info-template]' ) [ 0 ] ,
112
+ $infoPopup = $ ( infoPopupDOM . innerText ) . appendTo ( 'body' ) ;
113
+
114
+ $ ( this . options . helpLinkId ) . on ( 'click' , function ( e ) {
115
+ this . popUpOptions . position . of = $ ( e . target ) ;
116
+ $infoPopup . dropdownDialog ( this . popUpOptions ) . dropdownDialog ( 'open' ) ;
117
+ this . _toggle ( $infoPopup ) ;
118
+ } . bind ( this ) ) ;
119
+
120
+ this . $popup = $infoPopup ;
121
+ } ,
122
+
123
+ /**
124
+ * Init tier price popup
125
+ * @private
126
+ */
127
+ initTierPopup : function ( ) {
128
+ var tierOptions = JSON . parse ( $ ( this . options . attr ) . attr ( 'data-tier-price' ) ) ,
129
+ popupDOM = $ ( this . options . popUpAttr ) [ 0 ] ,
130
+ $tierPopup = $ ( popupDOM . innerText ) . appendTo ( 'body' ) ;
131
+
132
+ $tierPopup . find ( this . options . productIdInput ) . val ( this . options . productId ) ;
133
+ this . popUpOptions . position . of = $ ( this . options . helpLinkId ) ;
134
+
135
+ $tierPopup . find ( 'button' ) . on ( 'click' , function ( ev ) {
136
+ ev . preventDefault ( ) ;
137
+ this . handleTierAddToCart ( tierOptions ) ;
138
+ } . bind ( this ) ) ;
139
+
140
+ $tierPopup . find ( this . options . paypalCheckoutButons ) . on ( 'click' , function ( ) {
141
+ this . handleTierPaypalCheckout ( tierOptions ) ;
142
+ } . bind ( this ) ) ;
143
+
144
+ $ ( this . options . attr ) . on ( 'click' , function ( e ) {
145
+ this . popUpOptions . position . of = $ ( e . target ) ;
146
+ $tierPopup . find ( this . options . msrpLabelId ) . html ( tierOptions . msrp ) ;
147
+ $tierPopup . find ( this . options . priceLabelId ) . html ( tierOptions . price ) ;
148
+ $tierPopup . dropdownDialog ( this . popUpOptions ) . dropdownDialog ( 'open' ) ;
149
+ this . _toggle ( $tierPopup ) ;
150
+ } . bind ( this ) ) ;
151
+
152
+ this . $popup = $tierPopup ;
153
+ } ,
154
+
155
+ /**
156
+ * handle 'AddToCart' click on Msrp popup
157
+ *
158
+ * @private
159
+ */
160
+ handleMsrpAddToCart : function ( ) {
161
+ if ( this . options . addToCartButton ) {
162
+ $ ( this . options . addToCartButton ) . click ( ) ;
163
+ this . closePopup ( this . $popup ) ;
164
+ }
165
+ } ,
166
+
167
+ /**
168
+ * handle 'paypal checkout buttons' click on Msrp popup
169
+ *
170
+ * @private
171
+ */
172
+ handleMsrpPaypalCheckout : function ( ) {
173
+ this . closePopup ( this . $popup ) ;
174
+ } ,
175
+
176
+ /**
177
+ * handle 'AddToCart' click on Tier popup
178
+ *
179
+ * @param {Object } tierOptions
180
+ * @private
181
+ */
182
+ handleTierAddToCart : function ( tierOptions ) {
183
+ if ( this . options . addToCartButton &&
184
+ this . options . inputQty && ! isNaN ( tierOptions . qty )
185
+ ) {
186
+ $ ( this . options . inputQty ) . val ( tierOptions . qty ) ;
187
+ $ ( this . options . addToCartButton ) . click ( ) ;
188
+ this . closePopup ( this . $popup ) ;
96
189
}
190
+ } ,
97
191
98
- if ( this . options . attr ) {
99
- this . popupDOM = $ ( this . options . popUpAttr ) [ 0 ] ;
100
- this . $popup = $ ( this . popupDOM . innerText ) . appendTo ( 'body' ) ;
101
- this . popUpOptions . position . of = $ ( this . options . helpLinkId ) ;
102
- $ ( this . options . attr ) . on ( 'click' , function ( e ) {
103
- this . popUpOptions . position . of = $ ( e . target ) ;
104
- tierOptions = JSON . parse ( $ ( e . target ) . attr ( 'data-tier-price' ) ) ;
105
- this . $popup . find ( this . options . msrpLabelId ) . html ( tierOptions . msrp ) ;
106
- this . $popup . find ( this . options . priceLabelId ) . html ( tierOptions . price ) ;
107
- this . $popup . find ( 'button' ) . on ( 'click' , function ( e ) {
108
- e . preventDefault ( ) ;
109
- this . $popup . find ( 'form' ) . attr ( 'action' , tierOptions . addToCartUrl ) . submit ( ) ;
110
- } . bind ( this ) ) ;
111
- this . $popup . dropdownDialog ( this . popUpOptions ) . dropdownDialog ( 'open' ) ;
112
- this . _toggle ( this . $popup ) ;
113
- } . bind ( this ) ) ;
192
+ /**
193
+ * handle 'paypal checkout buttons' click on Tier popup
194
+ *
195
+ * @param {Object } tierOptions
196
+ * @private
197
+ */
198
+ handleTierPaypalCheckout : function ( tierOptions ) {
199
+ if ( this . options . inputQty && ! isNaN ( tierOptions . qty )
200
+ ) {
201
+ $ ( this . options . inputQty ) . val ( tierOptions . qty ) ;
202
+ this . closePopup ( this . $popup ) ;
114
203
}
115
204
} ,
116
205
117
206
/**
118
207
*
119
- * @param $elem
208
+ * @param { HTMLElement } $elem
120
209
* @private
121
210
*/
122
211
_toggle : function ( $elem ) {
@@ -127,12 +216,12 @@ define([
127
216
} . bind ( this ) ) ;
128
217
$ ( window ) . on ( 'resize' , function ( ) {
129
218
this . closePopup ( $elem ) ;
130
- } . bind ( this ) )
219
+ } . bind ( this ) ) ;
131
220
} ,
132
221
133
222
/**
134
223
*
135
- * @param $elem
224
+ * @param { HTMLElement } $elem
136
225
*/
137
226
closePopup : function ( $elem ) {
138
227
$elem . dropdownDialog ( 'close' ) ;
0 commit comments