-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
365 lines (322 loc) · 13.4 KB
/
script.js
File metadata and controls
365 lines (322 loc) · 13.4 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// JavaScript Document
window.onload = function () {
//click functions for checkboxes
$("#education").click(function(){
filterSectors();
})
$("#environment").click(function(){
filterSectors();
})
$("#transportation").click(function(){
filterSectors();
})
$("#health").click(function(){
filterSectors();
})
$("#housing").click(function(){
filterSectors();
})
$("#jobs").click(function(){
filterSectors();
})
$("#leadership").click(function(){
filterSectors();
})
$("#redevelopment").click(function(){ /*changed*/
filterSectors();
})
$("#youthSector").click(function(){
filterSectors();
})
$("#crime").click(function(){
filterSectors();
})
//beneficiaries
$("#teens").click(function(){
filterSectors();
})
$("#under12").click(function(){
filterSectors();
})
$("#adults").click(function(){
filterSectors();
})
$("#seniors").click(function(){
filterSectors();
})
//click function for radio buttons
$('#menu input:radio').click(function(e){ //if any of input type radio inside menu is clicked
if($(e.target).attr('name') == "colorcode") //if the button that called this function is the colorcode button
$("#colorCode").toggle(); //toggle visibility of legend
loadCSS();
});
function filterSectors(){
//select all
//filter down by sector
sublayer.setSQL("SELECT * FROM " + tableName);
if($('#sectors input:checked').length != 0)
{
$('#sectors input:checked').each(function(){
if($(this).attr("id") == "education")//id is education
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%education%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%education%'");
}
if($(this).attr("id") == "redevelopment")//changed
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%redevelopment%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%redevelopment%'");
}
if($(this).attr("id") == "transportation")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%transportation%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%transportation%'");
}
if($(this).attr("id") == "environment")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%environment%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%environment%'");
}
if($(this).attr("id") == "health")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%health%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%health%'");
}
if($(this).attr("id") == "jobs")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%job%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%job%'");
}
if($(this).attr("id") == "leadership")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%leadership%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%leadership%'");
}
if($(this).attr("id") == "youthSector")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%youth%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%youth%'");
}
if($(this).attr("id") == "crime")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%crime%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%crime%'");
}
if($(this).attr("id") == "housing")
{
if(sublayer.getSQL().indexOf("WHERE") == -1) //returns -1 if WHERE is not in the string
sublayer.setSQL(sublayer.getSQL() + " WHERE (sector ILIKE '%housing%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%housing%'");
}
});
}
else
sublayer.setSQL("SELECT * FROM " + tableName + " WHERE (1 = 0");
sublayer.setSQL(sublayer.getSQL() + " OR sector ILIKE '%no%') AND ("); //if no data provided
if($('#beneficiaries input:checked').length != 0)
{
$('#beneficiaries input:checked').each(function(){
if($(this).attr("id") == "teens")
{
if(sublayer.getSQL().indexOf("(beneficiaries") == -1) //if this is the first one checked
sublayer.setSQL(sublayer.getSQL() + "beneficiaries ILIKE '%teens%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR beneficiaries ILIKE '%teens%'");
}
if($(this).attr("id") == "community") //changed
{
if(sublayer.getSQL().indexOf("(beneficiaries") == -1)
sublayer.setSQL(sublayer.getSQL() + "beneficiaries ILIKE '%community%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR beneficiaries ILIKE '%community%'");
}
if($(this).attr("id") == "adults")
{
if(sublayer.getSQL().indexOf("(beneficiaries") == -1)
sublayer.setSQL(sublayer.getSQL() + "beneficiaries ILIKE '%adults%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR beneficiaries ILIKE '%adults%'");
}
if($(this).attr("id") == "under12")
{
if(sublayer.getSQL().indexOf("(beneficiaries") == -1)
sublayer.setSQL(sublayer.getSQL() + "beneficiaries ILIKE '%under12%'");
else
{
sublayer.setSQL(sublayer.getSQL() + " OR beneficiaries ILIKE '%under12%'");
console.log("under12")
}
}
if($(this).attr("id") == "seniors")
{
if(sublayer.getSQL().indexOf("(beneficiaries") == -1)
sublayer.setSQL(sublayer.getSQL() + "beneficiaries ILIKE '%seniors%'");
else
sublayer.setSQL(sublayer.getSQL() + " OR beneficiaries ILIKE '%seniors%'");
}
});
}
else
sublayer.setSQL(sublayer.getSQL() + "1 = 0")
sublayer.setSQL(sublayer.getSQL() + " OR beneficiaries ILIKE '%no%')") //no data provided
console.log(sublayer.getSQL());
}//end function
function loadCSS(){
//load simple
cartocssStyle = $('#simple').text();
if($('#labelOn').is(':checked'))
cartocssStyle = cartocssStyle + $('#labels').text();
if($('#bubbleOn').is(':checked'))
cartocssStyle = cartocssStyle + $('#bubble').text();
if($('#colorOn').is(':checked')){
cartocssStyle = cartocssStyle + $('#category').text();
}
sublayer.setCartoCSS(cartocssStyle);
}
function showInfoPane(data){
$("#infoPane").css("display","block"); //not working. need to figure out another way to show/hide
//TODO: use "data" param to populate the info pane
$("#infoPaneText").multiline(
data.amount + "\n\n" +
data.beneficiaries + "\n\n" +
data.description + "\n\n"
);
}
function showTip(data,pos){
var str = data.beneficiaries;
$("#toolTip").css("display","block");
x=event.screenX;
y=event.screenY;
$("#toolTip").css("left",pos.x + 3);
$("#toolTip").css("bottom",$(window).height()-pos.y + 3);
$("#org").multiline(
data.org_name);
$("#projectTip").multiline( //changed
data.project);
if(data.amount == 1)
{
$("#amt").multiline(
"$0" + "\n");
}
else
{
$("#amt").multiline(
"$" + data.amount.formatMoney(0));
}
//end if (data.amount == 1)
$("#toolTipSector").multiline(
data.sector);
if(str.indexOf("under12") > -1)
{
str = str.replace("under12","youth under 12");
}
if(str.indexOf("teens") > -1)
{
str = str.replace("teens","youth 12-17 years");
}
$("#toolTipBenif").multiline(
str);
$("#toolTipFunder").multiline(data.funder);
//PassContent();
}
function hideTip(data){
$("#toolTip").css("display","none");
}
$.fn.multiline = function(text){ //function to create breaklines at the char sequence \n in jquery's .text() method
this.text(text);
this.html(this.html().replace(/\n/g,'<br/>'));
return this;
}
var tableName = "blank_westside_fund_recipients";
// Put layer data into a JS object
var layerSource = {
user_name: 'westsidegrants',
type: 'cartodb',
sublayers: [{
sql: "SELECT * FROM " + tableName, // select all rows ad columns from our table
cartocss: $("#simple").text() // find the style tag with #bubble above, returns a string of css
}]
}
var layerUrl = 'http://westsidegrants.cartodb.com/api/v2/viz/5c80eeb4-d681-11e4-9e11-0e853d047bba/viz.json'
// Instantiate new map object, place it in 'map' div
//map options
var map_object = L.map('map').setView([33.7580385,-84.4118949], 15);
//create a tile layer sourced from mapbox
L.tileLayer('https://{s}.tiles.mapbox.com/v4/westsideinvestmentmap.04952d46/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoid2VzdHNpZGVpbnZlc3RtZW50bWFwIiwiYSI6IlVsT3FoOW8ifQ.WLaP_N4kdsbXurwvdci12A').addTo(map_object);
// For storing the sublayer
var sublayer;
var layerOptions = {
legends: true
};
// Add data layer to your map
cartodb.createLayer(map_object,layerUrl) //layerURL as opposed to layerSource for now (see note below)
.addTo(map_object)
.on('done',function(layer) {
sublayer = layer.getSubLayer(0);
loadCSS();
sublayer.setInteractivity('project, funder, org_name, cartodb_id, amount, beneficiaries, description, sector, status, address, funding_ends, funding_starts, phone'); //allows for those columns to be manipulated in the infopane, column names match cartodb.com
sublayer.on('featureClick',function(e,latlng,pos,data){ //note: does not work unless you use layerURL (as opposed to layerSource)
//showInfoPane(data);
//handled in cartodb.com
PassContent(data);
});
sublayer.on('featureOver',function(e,latlng,pos,data){
showTip(data,pos);
});
sublayer.on('featureOut',function(e,latlng,pos,data){
hideTip(data);
});
})
.error(function(err) {
console.log("error: " + err);
});
} //end onload
Number.prototype.formatMoney = function(c, d, t){
var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
function PassContent(data){
// var test = document.getElementById('amt').innerHTML;
// document.getElementById('SectorLabelText').innerHTML = document.getElementById('sector').innerHTML;
// document.getElementById('orgname').innerHTML = document.getElementById('org-name').innerHTML;
// document.getElementById('projectName-pass').innerHTML = document.getElementById('project').innerHTML; //changed
// document.getElementById('description-pass').innerHTML = document.getElementById('description').innerHTML;
// document.getElementById('amountreceived').innerHTML = test;
// document.getElementById('participants').innerHTML = document.getElementById('beneficiaries').innerHTML;
// document.getElementById('funder-pass').innerHTML = document.getElementById('funder').innerHTML;
//Change1To0();
//setTimeout("PassContent();", 100);
var test = document.getElementById('amt').innerHTML;
document.getElementById('SectorLabelText').innerHTML = document.getElementById('toolTipSector').innerHTML;
document.getElementById('orgname').innerHTML = document.getElementById('org').innerHTML;
document.getElementById('projectName-pass').innerHTML = document.getElementById('projectTip').innerHTML;
document.getElementById('description-pass').innerHTML = data.description;
document.getElementById('amountreceived').innerHTML = test;
document.getElementById('participants').innerHTML = document.getElementById('toolTipBenif').innerHTML;
document.getElementById('funder-pass').innerHTML = document.getElementById('toolTipFunder').innerHTML;
}