forked from sewardparkstories/sewardparkstories.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
284 lines (217 loc) · 6.63 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
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
var fs = require('fs');
var page = require('page');
var Handlebars = require('handlebars');
var elClass = require('element-class');
var on = require('component-delegate').bind;
var movement = require('geolocation-stream')();
//var Scrollbar = require('scrollbar');
var Leaflet = require('leaflet');
require('mapbox.js');
var flatsheet = require('flatsheet-api-client')({
host: 'http://seward.flatsheet.io'
});
var fastClick = require('fastclick');
fastClick(document.body);
var main = document.getElementById('main');
var mapEl = document.getElementById('map');
flatsheet.sheet('cc13b010-b0e1-11e4-a8bf-61e0a2f359a1', function (err, sheet) {
var data = createImageArrays(sheet.rows);
var dataByID = {};
for (var i = 0, l = data.length; i < l; i++) {
data[i].id = slugify(data[i].title);
dataByID[data[i].id] = data[i];
}
L.mapbox.accessToken = 'pk.eyJ1Ijoic2V0aHZpbmNlbnQiLCJhIjoiSXZZXzZnUSJ9.Nr_zKa-4Ztcmc1Ypl0k5nw';
/*
* pull in templates. requires browserify with brfs as build step
*/
var templates = {};
templates.info = Handlebars.compile(
fs.readFileSync('info-template.html', 'utf8')
);
templates.list = Handlebars.compile(
fs.readFileSync('list-template.html', 'utf8')
);
/*
* set image path
*/
L.Icon.Default.imagePath = 'node_modules/leaflet/dist/images/';
/*
* categories
*/
var checkedCategories = [];
var menu = document.getElementById('filter');
categories = getAllCategories(data);
categories.forEach(function addCheckbox(item) {
var div = document.createElement('div');
elClass(div).add('filter-option');
var checkbox = document.createElement('input');
checkbox.className = 'categoryCheckbox';
checkbox.type = 'checkbox';
checkbox.name = item;
checkbox.value = item;
checkbox.checked = true;
checkbox.addEventListener('change', updateWithFilters);
var label = document.createElement('label');
label.appendChild(document.createTextNode(item));
label.htmlFor = item;
div.appendChild(checkbox);
div.appendChild(label);
menu.appendChild(div);
})
/*
* create map using mapbox plugin
*/
var streets = L.mapbox.tileLayer('sethvincent.de840f5b');
var map = L.map('map', {
center: [47.551398, -122.259555],
zoom: 14,
zoomControl: false,
layers: [streets]
});
new L.Control.Zoom({ position: 'topright' }).addTo(map);
var location = L.marker([47, -122], {
icon: L.mapbox.marker.icon({
'marker-size': 'small',
'marker-color': '#fa0'
})
}).addTo(map);
movement.on('data', function(data) {
location.setLatLng(new L.LatLng(data.coords.latitude, data.coords.longitude));
});
movement.on('error', function(err) {
//console.error(err)
});
var baseurl = window.location.origin;
page.base(baseurl+'/#')
page('/', function () {
page('/about');
});
page('/about', function (ctx) {
var content = fs.readFileSync('about.html', 'utf8');
modal(content);
});
page('/list', function (ctx) {
var content = templates.list({ locations: data });
modal(content);
});
page(baseurl, '/');
page('/:id', function (ctx) {
var row = dataByID[ctx.params.id];
var content = templates.info(row);
modal(content);
});
page();
on(document.body, 'a', 'click', function (e) {
if (elClass(e.target).has('ignore')) return;
var dest = e.target.hash.substring(1, e.target.hash.length);
page(dest);
});
window.onresize = function (e) {
var modal = document.querySelector('.modal');
if (modal) resizeModal();
};
/*
* add a marker to map from json data
*/
var markerGroup = new L.FeatureGroup();
data.forEach(addMarker);
updateWithFilters();
function addMarker (row, i) {
var latlng = { lat: row['lat'], lng: row['long'] };
var marker = L.marker(latlng, {
icon: L.mapbox.marker.icon({
'marker-size': 'small',
'marker-line-color': '#335966',
'marker-line-opacity': 1,
'marker-color': '#335966'
})
});
markerGroup.addLayer(marker);
marker.on('click', function(e) {
page('/'+row.id);
});
}
function modal (content) {
if (document.querySelector('.modal')) {
main.removeChild(document.querySelector('.modal'));
}
var modal = document.createElement('div');
modal.className = 'modal';
modal.innerHTML = content;
main.appendChild(modal);
resizeModal();
}
on(document.body, '#close-modal', 'click', function (e) {
var modal = document.querySelector('.modal');
main.removeChild(modal);
e.preventDefault();
});
function resizeModal () {
var content = document.querySelector('.modal-inner');
content.style.width = (window.innerWidth - 44) + 'px';
if (window.innerWidth < 470) {
content.style.height = window.innerHeight - 152 + 'px';
}
else {
content.style.height = window.innerHeight - 107 + 'px';
}
if (window.innerWidth > 800) {
content.style.width = window.innerWidth / 2 + 'px';
content.style.maxWidth = '500px';
}
}
function createImageArrays (data) {
data.forEach(function (item) {
if (!item['image_1']) return;
var images = [];
if (item['image_1'].length > 1) images[0] = item['image_1'];
if (item['image_2'].length > 1) images[1] = item['image_2'];
item.images = images;
});
return data;
}
function updateWithFilters() {
checkedCategories = [];
var categoryCheckboxes = document.getElementsByClassName('categoryCheckbox');
for (var i = 0; i < categoryCheckboxes.length; i++) {
if (categoryCheckboxes[i].checked) {
checkedCategories.push(categoryCheckboxes[i].value);
}
}
markerGroup.clearLayers();
map.removeLayer(markerGroup);
if (checkedCategories.length > 0) {
data.filter(filterByCategory).forEach(addMarker);
} else {
data.forEach(addMarker);
}
map.addLayer(markerGroup);
}
function filterByCategory(element) {
for (var i = 0; i < checkedCategories.length; i++) {
if (checkedCategories[i] === element.category) {
return true;
}
}
return false;
}
function containsCategory(categories, c) {
for (var i = 0; i < categories.length; i++) {
if (categories[i] === c) return true;
}
return false;
}
function getAllCategories(data) {
var categories = [];
for (var i = 0; i < data.length; i++) {
if (!containsCategory(categories, data[i].category)) {
categories.push(data[i].category);
}
}
return categories;
}
});
function slugify (title) {
return title.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-')
}