1
+ function launchAddressLookup ( type , key , searchFor , hideFields , biasTowards , placeholder ) {
2
+ const woocommercefields = {
3
+ billing : {
4
+ field : "swiftcomplete_billing_address_autocomplete" ,
5
+ populateLineFormat : [
6
+ { field : "billing_company" , format : "Company" } ,
7
+ { field : "billing_address_1" , format : "BuildingName, BuildingNumber SecondaryRoad, Road" } ,
8
+ { field : "billing_address_2" , format : "SubBuilding" } ,
9
+ { field : "billing_city" , format : "TertiaryLocality, SecondaryLocality, PRIMARYLOCALITY" } ,
10
+ { field : "billing_state" , format : "" } ,
11
+ { field : "billing_postcode" , format : "POSTCODE" } ,
12
+ { field : "swiftcomplete_what3words" , format : "what3words" } ,
13
+ ]
14
+ } ,
15
+ shipping : {
16
+ field : "swiftcomplete_shipping_address_autocomplete" ,
17
+ populateLineFormat : [
18
+ { field : "shipping_company" , format : "Company" } ,
19
+ { field : "shipping_address_1" , format : "BuildingName, BuildingNumber SecondaryRoad, Road" } ,
20
+ { field : "shipping_address_2" , format : "SubBuilding" } ,
21
+ { field : "shipping_city" , format : "TertiaryLocality, SecondaryLocality, PRIMARYLOCALITY" } ,
22
+ { field : "shipping_state" , format : "" } ,
23
+ { field : "shipping_postcode" , format : "POSTCODE" } ,
24
+ { field : "swiftcomplete_what3words" , format : "what3words" }
25
+ ]
26
+ }
27
+ }
28
+
29
+ function initialiseSwiftcomplete ( ) {
30
+ swiftcomplete . runWhenReady ( function ( ) {
31
+ if ( document . getElementById ( woocommercefields [ type ] . field ) ) {
32
+ swiftcomplete . controls [ type ] = new swiftcomplete . PlaceAutoComplete ( {
33
+ key,
34
+ searchFor : searchFor ,
35
+ field : document . getElementById ( woocommercefields [ type ] . field ) ,
36
+ emptyQueryMode : 'prompt' ,
37
+ promptText : placeholder ,
38
+ noResultsText : 'No addresses found - click here to enter your address manually' ,
39
+ manualEntryText : 'Can\'t find your address? Click here to enter manually' ,
40
+ populateLineFormat : woocommercefields [ type ] . populateLineFormat . map ( f => ( {
41
+ field : document . getElementById ( f . field ) ,
42
+ format : f . format
43
+ } ) )
44
+ } ) ;
45
+
46
+ swiftcomplete . controls [ type ] . biasTowards ( biasTowards ) ;
47
+
48
+ var addressFields = [
49
+ { container : document . getElementById ( type + '_company_field' ) , field : document . getElementById ( type + '_company' ) } ,
50
+ { container : document . getElementById ( type + '_address_1_field' ) , field : document . getElementById ( type + '_address_1' ) } ,
51
+ { container : document . getElementById ( type + '_address_2_field' ) , field : document . getElementById ( type + '_address_2' ) } ,
52
+ { container : document . getElementById ( type + '_city_field' ) , field : document . getElementById ( type + '_city' ) } ,
53
+ { container : document . getElementById ( type + '_state_field' ) , field : document . getElementById ( type + '_state' ) } ,
54
+ { container : document . getElementById ( type + '_postcode_field' ) , field : document . getElementById ( type + '_postcode' ) }
55
+ ] ;
56
+
57
+ document . getElementById ( woocommercefields [ type ] . field ) . addEventListener ( 'swiftcomplete:place:selected' , function ( e ) {
58
+ for ( var i = 0 ; i < addressFields . length ; i ++ )
59
+ addressFields [ i ] . container . style . display = 'block' ;
60
+ } , false ) ;
61
+
62
+ document . getElementById ( woocommercefields [ type ] . field ) . addEventListener ( 'swiftcomplete:place:manualentry' , function ( e ) {
63
+ for ( var i = 0 ; i < addressFields . length ; i ++ ) {
64
+ document . getElementById ( 'swiftcomplete_' + type + '_address_autocomplete_field' ) . style . display = 'none' ;
65
+ addressFields [ i ] . container . style . display = 'block' ;
66
+ }
67
+ } , false ) ;
68
+
69
+ jQuery ( function ( $ ) {
70
+ showOrHideFields ( type , addressFields , hideFields , $ ( 'select[name=' + type + '_country]' ) . val ( ) ) ;
71
+
72
+ $ ( document . body ) . on ( 'change' , 'select[name=' + type + '_country]' , function ( ) {
73
+ showOrHideFields ( type , addressFields , hideFields , $ ( this ) . val ( ) ) ;
74
+ } ) ;
75
+ } ) ;
76
+ }
77
+ } ) ;
78
+ }
79
+
80
+ jQuery ( document ) . ready ( initialiseSwiftcomplete )
81
+ }
82
+
83
+ function showOrHideFields ( type , addressFields , hideFields , countryCode ) {
84
+ swiftcomplete . controls [ type ] . setCountries ( countryCode ) ;
85
+
86
+ var fieldsVisible = true ;
87
+
88
+ if ( hideFields ) {
89
+ var addressValuesExist = false ;
90
+
91
+ try {
92
+ for ( var i = 0 ; i < addressFields . length ; i ++ ) {
93
+ if ( ! addressFields [ i ] . container || ! addressFields [ i ] . field ) {
94
+ addressFields . splice ( i , 1 ) ;
95
+ i -- ;
96
+ continue ;
97
+ }
98
+
99
+ if ( addressFields [ i ] . field . value . length > 0 )
100
+ addressValuesExist = true ;
101
+ }
102
+ } catch ( err ) {
103
+ addressValuesExist = true ;
104
+ fieldsVisible = true ;
105
+ }
106
+
107
+ if ( swiftcomplete . controls [ type ] . hasAddressAutocompleteCoverageForCountry ( countryCode ) && ! addressValuesExist )
108
+ fieldsVisible = false ;
109
+
110
+ for ( var i = 0 ; i < addressFields . length ; i ++ )
111
+ addressFields [ i ] . container . style . display = fieldsVisible ? 'block' : 'none' ;
112
+ }
113
+
114
+ document . getElementById ( 'swiftcomplete_' + type + '_address_autocomplete_field' ) . style . display = ( swiftcomplete . controls [ type ] . hasAddressAutocompleteCoverageForCountry ( countryCode ) ? 'block' : 'none' ) ;
115
+ }
0 commit comments