-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindyourcandidate.js
190 lines (164 loc) · 6.94 KB
/
findyourcandidate.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
//Candidate Finder v1.0
//Built with love by @sallypoulsen for the Tyee Solutions Society
//Uses the Open North Represent API @ https://represent.opennorth.ca/api/
//August 2015
$('document').ready(function(){
var postalCode; //user-inputted postal code
var riding; //riding name
var party; //party name
var name; //candidate name
var social; //object with social media links
var inputLength; //for validation
var Exp = /((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i; //alphanumeric checker
//not currently necessary, but am plannign ahead for adding options to the widget
var retrieveResults;
retrieveResults = 'http://thehousingfix.ca/candidates/';
//generate jQuery widget
widget_markup = '<aside class="widget-credit">Powered by <a href="http://opennorth.ca" target="_blank"><img src="http://dev.housingfix.tyeesolutions.org/images/logos/open_north_rev.png"/></a></aside>';
widget_markup = widget_markup + '<div class="widget-inputs">';
widget_markup = widget_markup + '<h3>Find federal candidates in your riding</h3>';
widget_markup = widget_markup + '<form id="home-form" name="home-form">';
widget_markup = widget_markup + '<input id="riding_pc" name="riding" type="text" class="widget_field form-control" placeholder="Enter your Postal Code">';
widget_markup = widget_markup + '<input id="btn" type="submit" value="Submit" formaction="'+ retrieveResults+'" /></form></div>';
$('.find-a-candidate').html(widget_markup);
//Validation
jQuery.validator.addMethod("cdnPostal", function(postal, element) {
return this.optional(element) ||
postal.match(/[a-zA-Z][0-9][a-zA-Z](-| |)[0-9][a-zA-Z][0-9]/);
}, "Please specify a valid postal code.");
$("#home-form").validate({
rules: {
riding: {
required: true,
cdnPostal: true
}
},
messages: {
riding: "Please enter a valid postal code"
}
});
//if request loads from an external source, retrieve the postal code parameters form the URL
function GetURLParameter(sParam){
//get the URL
var sPageURL = window.location.search.substring(1);
//split it up
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++){
var sParameterName = sURLVariables[i].split('=');
// if it matches the desired parameter, dump it into the function
if (sParameterName[0] == sParam){
return sParameterName[1];
}
}
}
//put the postal code in a variable
postalCode = GetURLParameter('riding');
//console.log(riding);
//URL-ify the postal code
function urlify( withSpaces ){
var str = withSpaces;
str = str.replace(/\s+/g, '-').toLowerCase();
return str;
}
//clear any cruft on each click
$('#candidates_info').empty();
$('#riding_info').empty();
// feed the Postal Code into the API
function getRidingInfo(postalCode){
//make it upper case
postalCode = postalCode.toUpperCase();
//strip the spaces
postalCode = postalCode.replace('+', '');
//console.log(postalCode);
//API request
$.ajax({
//find the endpoint for the request postal code
url: 'https://represent.opennorth.ca/postcodes/'+ postalCode+ '/',
data: {
format: 'json'
},
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
success: function(data) {
console.log(data);
//check to see if riding name has already been rendered; if not, print it
var alreadyPrinted;
alreadyPrinted = false;
jQuery.each(data.boundaries_centroid, function(key, value){
if (value.boundary_set_name == "Federal electoral district"){
if (alreadyPrinted == false){
$('#riding_info').append('Your Candidates for ' + value.name);
alreadyPrinted = true;
}
}
});
//print individual candidate info
jQuery.each(data.candidates_centroid, function(key, value){
switch(value.party_name) {
case 'NDP':
logo = '<img height="20" alt="'+value.party_name+'" src="/images/widget_logos/NDP-white.png"/>';
break;
case 'Conservative':
logo = '<img height="20" alt="'+value.party_name+'" src="/images/widget_logos/cons-white.png"/>';
break;
case 'Green Party':
logo = '<img height="20" alt="'+value.party_name+'" src="/images/widget_logos/green-white.png"/>';
break;
case 'Liberal':
logo = '<img height="20" alt="'+value.party_name+'" src="/images/widget_logos/liberal-white.png"/>';
break;
default:
logo = value.party_name;
break;
}
var candidateContents;
candidateContents = '<div class="individual candidate-'+ key +'">';
candidateContents = candidateContents + '<div class="candidate-top ' + urlify(value.party_name) +'">';
candidateContents = candidateContents + '<div class="party-logo">'+ logo +'</div>';
if (value.incumbent == true){
candidateContents = candidateContents + '<div class="incumbent"><span>Incumbent</span></div>';
}
candidateContents = candidateContents + '<img class="candidate-image" src="' + value.photo_url+ '"/>';
candidateContents = candidateContents + '<h4>' +value.name+ '</h4>';
candidateContents = candidateContents + '</div>';
candidateContents = candidateContents + '<div class="candidate-bottom">'
candidateContents = candidateContents + '<ul>';
if (value.personal_url != ""){
website = value.personal_url;
candidateContents = candidateContents + '<li><a target="_blank" href="'+ website + '">Website</a></li>';
}
if (value.email !=""){
email = value.email;
candidateContents = candidateContents + '<li><a href="mailto:'+ email + '">Email</a></li>';
}
if (typeof value.extra != 'undefined'){
if (typeof value.extra.twitter != 'undefined'){
twitter = value.extra.twitter;
candidateContents = candidateContents + '<li><a href="' + twitter+ '">Twitter</a></li>';
}
if (typeof value.extra.facebook != 'undefined'){
facebook = value.extra.facebook;
candidateContents = candidateContents + '<li><a href="' + facebook+ '">Facebook</a></li>';
}
}
candidateContents = candidateContents + '</ul>';
candidateContents = candidateContents + '</div>';
console.log(candidateContents);
if (key ==3){
console.log(key %3);
candidateContents = candidateContents + '</div><div class="row">';
}
//if candidate key is == 3, then throw in a </div><div class="row">
$('#candidates_info').append(candidateContents);
});
},
error: function() {
$('.riding_content').append('Sorry, there was an error with your submission. Please try again.');
},
});
}
if(typeof postalCode !== "undefined") {
getRidingInfo(postalCode);
}
});