-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscripts.js
268 lines (218 loc) · 7.33 KB
/
scripts.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
var app = {
addressInput: document.getElementsByClassName('input-address')[0],
devDummyData: '<li class="rep-card mdl-card mdl-shadow--2dp"><div class="rep-card-content"><div class="left-content"><h3 class="rep-name mdl-card-title">Hakeem Jeffries</h3><p class="rep-title">Representative for New York's 8th congressional district</p><p class="rep-affil">Democrat</p></div><div class="right-content"><img class="rep-image" src="https://theunitedstates.io/images/congress/225x275/J000294.jpg"/></div></div><a class="mdl-button call-button" href="tel:202-225-5936"><i class="material-icons mdl-list__item-icon mdl-color-text--white">phone</i>202-225-5936</a></li>',
emptyStateHTML: '<div class="empty-state">Sorry, we were unable to find any results for that address.</div>',
autocomplete: null,
baseCivicsURL: 'https://www.googleapis.com/civicinfo/v2',
/**
* Add tracking to the social share buttons
*/
bindSocialSharingClick: function() {
var buttons = document.getElementsByClassName('social-share');
for (var i = 0, length = buttons.length; i < length; i++) {
buttons[i].addEventListener('click', function(e) {
var shareMethod = e.currentTarget.getAttribute('share-method');
app._trackEvent({
eventAction: 'click',
eventCategory: 'Social Share Button',
eventLabel: shareMethod,
});
});
}
},
/**
* Set up google address autocomplete on the address input
*/
initAutocomplete: function() {
if (!google && !google.maps) {
return;
}
this.autocomplete = new google.maps.places.Autocomplete(this.addressInput, {
componentRestrictions: {
country: 'us',
}
});
this._geolocateUserForAutocomplete();
this.autocomplete.addListener('place_changed', this.searchRepresentativesByAddress.bind(this));
},
/**
* Find the users location and set it to the autocomplete
* for easier address searching on more local results
*/
_geolocateUserForAutocomplete: function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
app.autocomplete.setBounds(circle.getBounds());
});
}
},
/**
* Returns the formatted address from the autocomplete input
* @return {string}
*/
getAddressInputValue: function() {
var address = this.autocomplete && this.autocomplete.getPlace();
return (address && address.formatted_address)
? address.formatted_address
: this.addressInput.value
;
},
/**
* Gets the address from the autocomplete and search
* for the representatives for that area
*/
searchRepresentativesByAddress: function(e) {
var queryString = this._getRepSearchQueryString();
var request = new XMLHttpRequest();
// Don't search if input is empty
if (this.getAddressInputValue().length === 0) {
return false;
}
// var formattedAddress = this.getAddressInputValue();
// if (this.addressInput.value !== formattedAddress) {
// this.addressInput.value = formattedAddress;
// }
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
var matchesResults = request.responseText.match(/ocd-division\/country:us\/(?:state|district):\w+\/cd:\d+/);
// Handle the geographical weirdo DC
if (!matchesResults) {
matchesResults = request.responseText.match(/ocd-division\/country:us\/(?:state|district):\w+\//);
}
if (matchesResults) {
var dataString = matchesResults[0];
var state = dataString.match(/(?:state|district):(\w{2})/)[1];
var districtNum = (state === 'dc') ? 1 : parseInt(dataString.match(/\d+$/)[0], 10);
if (state && districtNum) {
this.getRepresentativeData(state, districtNum);
}
} else {
this._handleSearchError();
}
} else {
this._handleSearchError();
}
}
}.bind(app);
request.open('GET', this.baseCivicsURL + '/representatives?' + queryString);
request.send(null);
if (e && e.currentTarget) {
return false;
}
},
_getRepSearchQueryString: function() {
return this._encodeData({
key: 'AIzaSyAQmMQg6Ti1XSiWULzRqJIdLS4lwS6muig',
address: this.getAddressInputValue(),
fields: 'divisions',
includeOffices: false
});
},
_encodeData: function(data) {
return Object.keys(data).map(function(key) {
return [key, data[key]].map(encodeURIComponent).join("=");
}).join("&");
},
/**
* Searches for the representative data by state & district
* number and then renders the rep-cards on success
* @param {string} state
* @param {string} districtNum
*/
getRepresentativeData: function(state, districtNum) {
if (typeof state !== 'string' && typeof districtNum !== 'number') {
return;
}
state = state.toUpperCase();
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === 4) {
if(request.status === 200) {
this.renderRepresentativeCard(request.responseText);
} else {
this._handleSearchError();
}
}
}.bind(app);
request.open('GET', 'http://cdn.usecalltoaction.com/' + state + '-' + districtNum + '.html');
request.send(null);
},
/**
* Render the passed-in representative card HTML
* @param {string} templateString
*/
renderRepresentativeCard: function(templateString) {
templateString || (templateString = app.devDummyData);
var repContainer = document.getElementById('repContentContainer');
var repCardList = document.getElementById('repList');
// Remove hidden class
if (repContainer) {
repContainer.classList = '';
}
// Add rep-card to page
repCardList.innerHTML = templateString;
this._bindCallButtonClick(repCardList.getElementsByClassName('call-button')[0]);
},
_bindCallButtonClick: function(element) {
if (!element) {
return;
}
element.addEventListener('click', function(e) {
var button = e.currentTarget;
var telNumber = button.getAttribute('href');
// Remove all non-digit characters & change to an integer
if (telNumber) {
telNumber = parseInt(telNumber.replace(/\D/g, ''), 10);
}
app._trackEvent({
eventAction: 'click',
eventCategory: 'Call Button',
eventLabel: 'Congressional Rep',
eventValue: telNumber,
});
});
},
_handleSearchError: function() {
this.renderRepresentativeCard(this.emptyStateHTML);
},
_testRenderRepresentativeCard: function() {
this.renderRepresentativeCard(this.devDummyData);
},
/**
* Wrapper function for event tracking
* @param {object} eventOptions
* Possible values:
* {string} eventCategory *required
* {string} eventAction *required
* {string} eventLabel
* {integer} eventValue
*
* https://developers.google.com/analytics/devguides/collection/analyticsjs/events
*/
_trackEvent: function(eventOptions) {
if (typeof ga !== 'function' || !eventOptions ||
typeof eventOptions.eventCategory === 'undefined' ||
typeof eventOptions.eventAction === 'undefined'
) {
return;
}
if (!eventOptions.hitType) {
eventOptions.hitType = 'event';
}
ga('send', eventOptions);
},
};
document.addEventListener('DOMContentLoaded', function() {
app.addressInput = document.getElementsByClassName('input-address')[0];
app.initAutocomplete();
app.bindSocialSharingClick();
});