-
Notifications
You must be signed in to change notification settings - Fork 0
/
addressfinder_salesforce_au.js
119 lines (105 loc) · 4.73 KB
/
addressfinder_salesforce_au.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*!
* The AddressFinder for Salesforce plugin adds autocomplete capability for VisualForce pages.
*
* For more information view the README here:
* https://github.com/AbleTech/addressfinder-salesforce/blob/master/README.md
*
* VERSION 1.0.2
*
* Copyright (c) 2016 Abletech
*/
(function(d,w){
const VERSION = '1.1.0';
/**************************** CONFIGURATION ****************************/
// AddressFinder license key.
var afKey = 'INSERT_LICENSE_KEY_HERE',
// AddressFinder country details (supports NZ and AU country codes).
countryCode = 'AU',
countryName = 'Australia',
// address field elements - update these IDs to match the address fields on your page
// (the street input element is used as the address search field)
streetId = 'INSERT_STREET_FIELD_ID_HERE',
cityId = 'INSERT_CITY_FIELD_ID_HERE',
provinceId = 'INSERT_PROVINCE_FIELD_ID_HERE',
postcodeId = 'INSERT_POSTCODE_FIELD_ID_HERE',
countryId = 'INSERT_COUNTRY_FIELD_ID_HERE',
// enable/disable debug mode (displays errors as JS alerts, else logs to console)
debug = true;
/***********************************************************************/
/*********************** PRIVATE FUNCTIONS ****************************/
/*
* Sets the value of the input field corresponding to a given element id.
* If the corresponding field is not found an error message is either
* displayed in a JS alert or, if debug mode is disabled, logged to console.
*/
var _setFieldValue = function(elementId, value) {
// find the matching field
var field = d.getElementById(elementId);
// If the matching field exists
if (field) {
// update field value
field.value = value;
return;
}
// The field has not been found, log errors
var errorMessage = 'AddressFinder Error: '
+ 'Attempted to update value for field that could not be found.\n'
+ '\nField ID: ' + elementId
+ '\nValue: ' + value;
if (debug) {
alert(errorMessage);
} else if (w.console) {
console.log(errorMessage);
}
};
/*
* This callback function invokes the AF widget and binds it to the streetField
* If the user selects a result, then it formats the address response data for
* use within the page's form.
*/
var _initAF = function() {
var streetField = d.getElementById(streetId),
clientAgentVersion = `Salesforce/${VERSION}`,
widget = new AddressFinder.Widget(streetField, afKey, countryCode, {ca: clientAgentVersion});
var _formatAddressFields = function(address, metaData){
// country is hardcoded to match the scope of the widget
_setFieldValue(countryId, countryName);
// get full address string from widget result
var addressComponents = address.split(', ');
var componentCount = addressComponents.length;
// separate address components into city/postcode and street address
var cityStatePostcode = addressComponents[componentCount - 1].split(' ');
var streetAddress = addressComponents.slice(0, componentCount - 1).join('\n');
// populate street field
_setFieldValue(streetId, streetAddress);
// populate city, state and postcode fields
var cspLength = cityStatePostcode.length;
var city = cityStatePostcode.slice(0, cspLength - 2).join(' ');
var state = cityStatePostcode[cspLength - 2];
var postcode = cityStatePostcode[cspLength - 1];
_setFieldValue(cityId, city);
_setFieldValue(provinceId, state);
_setFieldValue(postcodeId, postcode);
// remove focus from street field
streetField.blur();
};
widget.on('result:select', _formatAddressFields);
};
/*
* This function is called when the window DOMContentLoaded event fires.
* It adds the AddressFinder widget script, and when it loads, calls _initAF().
*/
var _addScript = function() {
var s = d.createElement('script');
s.src = 'https://api.addressfinder.io/assets/v3/widget.js';
s.async = 1;
s.onload = _initAF;
d.body.appendChild(s);
};
/***********************************************************************/
/*
* Add the AF widget when DOM content has loaded.
* The widget (when downloaded) will then call the initAF function
*/
w.addEventListener('DOMContentLoaded', _addScript);
})(document, window);