forked from lazychino/college-track
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
196 lines (171 loc) · 5.74 KB
/
app.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
window.app = (function($) {
var API_KEY = ' wNItlTM01zprvFa8K62Tu3LJj';
var app = {};
var get_field_values = function(data, fields, f){
if(!f){ f = {}; }
data.forEach(function(item){
for(var i=0,e=fields.length; i<e; ++i){
var field=fields[i];
var fentry = f[field] || (f[field]={});
if(!fentry[item[field]]){ fentry[item[field]]=1; }
}
});
for(var i=0,e=fields.length; i<e; ++i){
var field=fields[i];
var fentry=f[field];
var farr=[];
for(var j in fentry){
farr.push(j);
}
f[field]=farr;
}
return f;
};
app.init = function(map, filters_toolbar){
app.map = map;
app.filters_toolbar = $(filters_toolbar);
app.load_data();
};
app.load_data = function(){
// var url = 'https://data.pr.gov/resource/admit.json?$app_token=wNItlTM01zprvFa8K62Tu3LJj';
var url = '/sample-data.json';
$.get('recintos.json', function(recintos) {
app.recintos = recintos;
$.get(url, function(data){
app.set_data(data);
});
});
};
app.set_data = function(data) {
app.data = data;
get_field_values(data, [
"igs",
"genero",
"program",
"calendario",
"gpa",
"institucion_de_procedencia",
"campus",
], app.fields={});
app.make_filters();
app.drawData(app.data);
};
app.make_filters = function(){
app.filters={};
app.filters_toolbar.empty();
for(var f in app.fields){
app.filters_toolbar.append(app.make_filter_ctrl(f));
}
app.filters_toolbar.removeClass('hidden');
};
app.make_filter_ctrl = function(f){
var vals = app.fields[f];
var el = $(
'<div class="btn-group">' +
'<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">' +
f + ' <span class="caret"></span>' +
'</button>' +
'<ul class="dropdown-menu" role="menu">' +
'</ul>' +
'</div>'
);
el.find('.dropdown-menu').click(function(e){if($(this).is(':visible')){e.stopPropagation();}});
var dd = el.find('.dropdown-menu');
vals.forEach(function(value){
app.set_filter(f, value, true, true);
var item=$('<li></li>').append(
$('<label></label>').append(
$('<input type="checkbox" checked="checked" />').change(function(){
app.set_filter(f, value, $(this).is(':checked'));
})
).append(value)
).appendTo(dd);
});
return el;
};
app.set_filter = function(filter, value, checked, ignore){
if(!app.filters[filter]){app.filters[filter]={};}
app.filters[filter][value] = checked;
if(!ignore){
var filtered = app.filter_data(app.data, app.filters);
app.clear_map(app.data);
app.drawData(filtered);
}
};
app.filter_data = function(data, filters){
var filtered = data.filter(function(item){
for(var f in filters){
var filter = filters[f];
if(filter && !filter[item[f]]){
return false;
}
}
return true;
});
return filtered;
};
app.drawData = function(data) {
Object.keys(app.recintos).forEach(app.addUprMarker.bind(app));
data.forEach(app.addHighSchoolMaker.bind(app));
// console.log(Object.keys(app.recintos));
};
app.clear_map = function(data) {
for(var i=0, e=data.length; i < e; ++i){
if(data[i].marker){
data[i].marker.setMap(null);
data[i].marker = null;
}
if(data[i].arrow){
data[i].arrow.setMap(null);
data[i].arrow = null;
}
}
};
app.addHighSchoolMaker = function(item) {
item.marker = new google.maps.Marker({
position: item.location = new google.maps.LatLng(+item.location_1.latitude, +item.location_1.longitude),
map: app.map,
title: item.institucion_de_procedencia,
icon: {
anchor: new google.maps.Point(16,16),
url: '/img/green4.png',
},
});
item.arrow = app.addArrow({
lat: +item.location_1.latitude, lon:+item.location_1.longitude
}, app.recintos[item.campus]);
};
app.addUprMarker = function(id) {
var item = app.recintos[id];
item.marker = new google.maps.Marker({
position: item.location = new google.maps.LatLng(item.lat, item.lon),
map: app.map,
title: item.name,
icon: {
anchor: new google.maps.Point(16,16),
url: '/img/torre2.png',
},
});
};
app.addArrow = function(hs, upr) {
var lineSymbol = {
path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW
};
var lineCoordinates = [
new google.maps.LatLng(hs.lat, hs.lon),
new google.maps.LatLng(upr.lat, upr.lon)
];
var line = new google.maps.Polyline({
path: lineCoordinates,
icons: [{
icon: lineSymbol,
offset: '100%'
}],
map: app.map,
strokeColor: "#575757",
strokeWeight: 1
});
return line;
};
return app;
})($);