@@ -38,8 +38,7 @@ define([
3838 shippingAutocomplete : null ,
3939 billingAutocomplete : null ,
4040 billingStreetFound : false ,
41- billingFunction : null ,
42- shippingFunctions : null
41+ billingFunction : null
4342 } ,
4443
4544 /**
@@ -48,97 +47,8 @@ define([
4847 */
4948 _create : function ( ) {
5049 var self = this ;
51- this . _initShippingAutocomplete ( ) ;
5250
53- $ ( document ) . on ( 'change' , 'select[name="billing_address_id"]' , function ( ) {
54- if ( $ ( this ) . find ( 'option:last' ) . prop ( 'selected' ) ) {
55- self . _initBillingAutocomplete ( ) ;
56- }
57- } ) ;
58-
59- $ ( document ) . on ( 'click' , 'input[name="billing-address-same-as-shipping"], .action-edit-address' , function ( ) {
60- self . _initBillingAutocomplete ( ) ;
61- } ) ;
62- } ,
63-
64- /**
65- * Fill in shipping address
66- *
67- * @private
68- */
69- _fillInShippingAddress ( ) {
70- // Get the place details from the autocomplete object.
71- var place = this . options . shippingAutocomplete . getPlace ( ) ;
72- // Get each component of the address from the place details
73- // and fill the corresponding field on the form.
74- for ( var i = place . address_components . length - 1 ; i >= 0 ; i -- ) {
75- var addressType = place . address_components [ i ] . types [ 0 ] ;
76- var long_name = place . address_components [ i ] . long_name ;
77- var short_name = place . address_components [ i ] . short_name ;
78- if ( this . options . componentForm [ addressType ] && long_name ) {
79- if ( this . options . componentForm [ addressType ] == 'country' ) {
80- $ ( '#shipping-new-address-form select[name="country_id"]' ) . val ( short_name ) . trigger ( 'change' ) ;
81- } else if ( this . options . componentForm [ addressType ] == 'region' ) {
82- $ ( '#shipping-new-address-form input[name="region"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
83- if ( $ ( '#shipping-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) ) {
84- $ ( '#shipping-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) . prop ( 'selected' , true ) . trigger ( 'change' ) ;
85- }
86- } else if ( this . options . componentForm [ addressType ] == 'route' ) {
87- $ ( '#shipping-new-address-form input[name="street[0]"]' ) . val ( long_name ) . trigger ( 'keyup' ) . trigger ( 'keyup' ) ;
88- } else if ( this . options . componentForm [ addressType ] == 'street_1' ) {
89- $ ( '#shipping-new-address-form input[name="street[0]"]' ) . val ( long_name + ' ' + $ ( '#shipping-new-address-form input[name="street[0]"]' ) . val ( ) ) . trigger ( 'keyup' ) ;
90- } else if ( this . options . componentForm [ addressType ] == 'zip' ) {
91- $ ( '#shipping-new-address-form input[name="postcode"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
92- } else {
93- $ ( '#shipping-new-address-form input[name="' + this . options . componentForm [ addressType ] + '"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
94- }
95- }
96- }
97- } ,
98-
99- /**
100- * Fill in billing address
101- *
102- * @private
103- */
104- _fillInBillingAddress ( ) {
105- // Get the place details from the autocomplete object.
106- var place = this . options . billingAutocomplete . getPlace ( ) ;
107- console . log ( place ) ;
108- // Get each component of the address from the place details
109- // and fill the corresponding field on the form.
110- for ( var i = place . address_components . length - 1 ; i >= 0 ; i -- ) {
111- var addressType = place . address_components [ i ] . types [ 0 ] ;
112- var long_name = place . address_components [ i ] . long_name ;
113- var short_name = place . address_components [ i ] . short_name ;
114- if ( this . options . componentForm [ addressType ] && long_name ) {
115- if ( this . options . componentForm [ addressType ] == 'country' ) {
116- $ ( '#billing-new-address-form select[name="country_id"]' ) . val ( short_name ) . trigger ( 'change' ) ;
117- } else if ( this . options . componentForm [ addressType ] == 'region' ) {
118- $ ( '#billing-new-address-form input[name="region"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
119- if ( $ ( '#billing-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) ) {
120- $ ( '#billing-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) . prop ( 'selected' , true ) . trigger ( 'change' ) ;
121- }
122- } else if ( this . options . componentForm [ addressType ] == 'route' ) {
123- $ ( '#billing-new-address-form input[name="street[0]"]' ) . val ( long_name ) . trigger ( 'keyup' ) . trigger ( 'keyup' ) ;
124- } else if ( this . options . componentForm [ addressType ] == 'street_1' ) {
125- $ ( '#billing-new-address-form input[name="street[0]"]' ) . val ( long_name + ' ' + $ ( '#billing-new-address-form input[name="street[0]"]' ) . val ( ) ) . trigger ( 'keyup' ) ;
126- } else if ( this . options . componentForm [ addressType ] == 'zip' ) {
127- $ ( '#billing-new-address-form input[name="postcode"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
128- } else {
129- $ ( '#billing-new-address-form input[name="' + this . options . componentForm [ addressType ] + '"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
130- }
131- }
132- }
133- } ,
134-
135- /**
136- * Get place for shipping address
137- *
138- * @private
139- */
140- _initShippingAutocomplete ( ) {
141- var self = this ;
51+ // Fill in shipping address
14252 this . options . shippingFunctions = setInterval ( function ( ) {
14353 var street = $ ( '#shipping-new-address-form' ) . find ( 'input[name="street[0]"]' ) [ 0 ] ;
14454 if ( street ) {
@@ -149,7 +59,33 @@ define([
14959 self . options . shippingAutocomplete . inputId = street . id ;
15060 self . options . shippingAutocomplete . setComponentRestrictions ( { 'country' : self . _getCountriesCodeArray ( ) } ) ;
15161 google . maps . event . addListener ( self . options . shippingAutocomplete , 'place_changed' , function ( ) {
152- self . _fillInShippingAddress ( ) ;
62+ // Get the place details from the autocomplete object.
63+ var place = self . options . shippingAutocomplete . getPlace ( ) ;
64+ // Get each component of the address from the place details
65+ // and fill the corresponding field on the form.
66+ for ( var i = place . address_components . length - 1 ; i >= 0 ; i -- ) {
67+ var addressType = place . address_components [ i ] . types [ 0 ] ;
68+ var long_name = place . address_components [ i ] . long_name ;
69+ var short_name = place . address_components [ i ] . short_name ;
70+ if ( self . options . componentForm [ addressType ] && long_name ) {
71+ if ( self . options . componentForm [ addressType ] == 'country' ) {
72+ $ ( '#shipping-new-address-form select[name="country_id"]' ) . val ( short_name ) . trigger ( 'change' ) ;
73+ } else if ( self . options . componentForm [ addressType ] == 'region' ) {
74+ $ ( '#shipping-new-address-form input[name="region"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
75+ if ( $ ( '#shipping-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) ) {
76+ $ ( '#shipping-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) . prop ( 'selected' , true ) . trigger ( 'change' ) ;
77+ }
78+ } else if ( self . options . componentForm [ addressType ] == 'route' ) {
79+ $ ( '#shipping-new-address-form input[name="street[0]"]' ) . val ( long_name ) . trigger ( 'keyup' ) . trigger ( 'keyup' ) ;
80+ } else if ( self . options . componentForm [ addressType ] == 'street_1' ) {
81+ $ ( '#shipping-new-address-form input[name="street[0]"]' ) . val ( long_name + ' ' + $ ( '#shipping-new-address-form input[name="street[0]"]' ) . val ( ) ) . trigger ( 'keyup' ) ;
82+ } else if ( self . options . componentForm [ addressType ] == 'zip' ) {
83+ $ ( '#shipping-new-address-form input[name="postcode"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
84+ } else {
85+ $ ( '#shipping-new-address-form input[name="' + self . options . componentForm [ addressType ] + '"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
86+ }
87+ }
88+ }
15389 } ) ;
15490 clearInterval ( self . options . shippingFunctions ) ;
15591 }
@@ -158,14 +94,26 @@ define([
15894 clearInterval ( self . options . shippingFunctions ) ;
15995 }
16096 } , 2000 ) ;
97+
98+ $ ( document ) . on ( 'change' , 'select[name="billing_address_id"]' , function ( ) {
99+ if ( $ ( this ) . find ( 'option:last' ) . prop ( 'selected' ) ) {
100+ self . _fillInBillingAddress ( ) ;
101+ }
102+ } ) ;
103+
104+ $ ( document ) . on ( 'click' , 'input[name="billing-address-same-as-shipping"], .action-edit-address' , function ( ) {
105+ if ( $ ( this ) . find ( 'option:last' ) . prop ( 'selected' ) ) {
106+ self . _fillInBillingAddress ( ) ;
107+ }
108+ } ) ;
161109 } ,
162110
163111 /**
164- * Get place for billing address
112+ * Fill in billing address
165113 *
166114 * @private
167115 */
168- _initBillingAutocomplete ( ) {
116+ _fillInBillingAddress ( ) {
169117 var self = this ;
170118 if ( ! this . options . billingStreetFound ) {
171119 this . options . billingFunctions = setInterval ( function ( ) {
@@ -178,7 +126,33 @@ define([
178126 self . options . billingAutocomplete . inputId = street . id ;
179127 self . options . billingAutocomplete . setComponentRestrictions ( { 'country' : self . _getCountriesCodeArray ( ) } ) ;
180128 google . maps . event . addListener ( self . options . billingAutocomplete , 'place_changed' , function ( ) {
181- self . _fillInBillingAddress ( ) ;
129+ // Get the place details from the autocomplete object.
130+ var place = self . options . billingAutocomplete . getPlace ( ) ;
131+ // Get each component of the address from the place details
132+ // and fill the corresponding field on the form.
133+ for ( var i = place . address_components . length - 1 ; i >= 0 ; i -- ) {
134+ var addressType = place . address_components [ i ] . types [ 0 ] ;
135+ var long_name = place . address_components [ i ] . long_name ;
136+ var short_name = place . address_components [ i ] . short_name ;
137+ if ( self . options . componentForm [ addressType ] && long_name ) {
138+ if ( self . options . componentForm [ addressType ] == 'country' ) {
139+ $ ( '#billing-new-address-form select[name="country_id"]' ) . val ( short_name ) . trigger ( 'change' ) ;
140+ } else if ( self . options . componentForm [ addressType ] == 'region' ) {
141+ $ ( '#billing-new-address-form input[name="region"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
142+ if ( $ ( '#billing-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) ) {
143+ $ ( '#billing-new-address-form select[name="region_id"] option:contains(' + long_name + ')' ) . prop ( 'selected' , true ) . trigger ( 'change' ) ;
144+ }
145+ } else if ( self . options . componentForm [ addressType ] == 'route' ) {
146+ $ ( '#billing-new-address-form input[name="street[0]"]' ) . val ( long_name ) . trigger ( 'keyup' ) . trigger ( 'keyup' ) ;
147+ } else if ( self . options . componentForm [ addressType ] == 'street_1' ) {
148+ $ ( '#billing-new-address-form input[name="street[0]"]' ) . val ( long_name + ' ' + $ ( '#billing-new-address-form input[name="street[0]"]' ) . val ( ) ) . trigger ( 'keyup' ) ;
149+ } else if ( self . options . componentForm [ addressType ] == 'zip' ) {
150+ $ ( '#billing-new-address-form input[name="postcode"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
151+ } else {
152+ $ ( '#billing-new-address-form input[name="' + self . options . componentForm [ addressType ] + '"]' ) . val ( long_name ) . trigger ( 'keyup' ) ;
153+ }
154+ }
155+ }
182156 } ) ;
183157 clearInterval ( self . options . billingFunctions ) ;
184158 self . options . billingStreetFound = true ;
0 commit comments