forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoder.js
More file actions
101 lines (89 loc) · 3.63 KB
/
geocoder.js
File metadata and controls
101 lines (89 loc) · 3.63 KB
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
/* global google */
const scriptjs = require('scriptjs');
const localize = require('./localize').localize;
const applyToAllElements = require('./utility').applyToAllElements;
const createElement = require('./utility').createElement;
const Client = require('../app/base/client');
const Geocoder = (() => {
let el_btn_validate,
el_error;
let validated = false;
const init = (form_id) => {
scriptjs('https://maps.googleapis.com/maps/api/js?key=AIzaSyAEha6-HeZuI95L9JWmX3m6o-AxQr_oFqU&libraries=places', 'gMaps');
const form = document.getElementById(form_id.split('#')[1]);
const addr_1 = '#address_line_1';
const addr_2 = '#address_line_2';
const city = '#address_city';
const state = '#address_state';
const postcode = '#address_postcode';
const residence = Client.get('residence');
const getValue = (id) => document.getElementById(id.split('#')[1]).value || '';
const getAddress = () => `${getValue(addr_1)} ${getValue(addr_2)}, ${getValue(city)}, ${getValue(state)} ${getValue(postcode)}, ${residence}`;
form.querySelector(city).addEventListener('change', () => {
if (getValue(addr_1).length && getValue(city).length && !validated) {
validator(getAddress()).then(() => {
validated = true;
});
}
});
el_error = form.querySelector('#geocode_error');
applyToAllElements(`${addr_1}, ${addr_2}, ${city}, ${postcode}`, (element) => {
element.addEventListener('keyup', () => {
if (!validated && !el_btn_validate) {
el_btn_validate = createElement('button', {
id : 'geocode_validate',
class: 'button-secondary',
text : localize('Validate address'),
});
el_btn_validate.addEventListener('click', (e) => {
e.preventDefault();
validator(getAddress()).then(() => {
validated = true;
});
});
el_error.parentNode.appendChild(el_btn_validate);
}
if (el_btn_validate) el_btn_validate.setVisibility(1);
el_error.setVisibility(0);
});
}, '', form);
return {
address: getAddress(),
};
};
const validate = (form_id) => {
const address = init(form_id).address;
validator(address).then(() => {
validated = true;
});
};
const validator = (address) => (
new Promise((resolve) => {
scriptjs.ready('gMaps', () => {
const geocoder = new google.maps.Geocoder();
geocoder.geocode({
address,
}, (result, status) => {
// Geocoding status reference:
// https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingStatusCodes
handleResponse(status);
resolve(status);
});
});
})
);
const handleResponse = (status) => {
if (/ZERO_RESULTS|INVALID_REQUEST/.test(status)) {
el_error.setVisibility(1);
if (el_btn_validate) el_btn_validate.setVisibility(0);
} else {
el_error.setVisibility(0);
if (el_btn_validate) el_btn_validate.setVisibility(0);
}
};
return {
init,
validate,
};
})();
module.exports = Geocoder;