-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
167 lines (124 loc) · 3.46 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
var fs = require('fs');
var Handlebars = require('handlebars');
var elClass = require('element-class');
var on = require('component-delegate').bind;
var movement = require('geolocation-stream')()
var Leaflet = require('leaflet');
require('mapbox.js');
var fastClick = require('fastclick');
fastClick(document.body);
var page = document.getElementById('page');
var mapEl = document.getElementById('map');
mapEl.style.height = (window.innerHeight - mapEl.offsetTop) + 'px';
var data = require('./data.json');
data = createImageArrays(data);
L.mapbox.accessToken = 'pk.eyJ1Ijoic2V0aHZpbmNlbnQiLCJhIjoiSXZZXzZnUSJ9.Nr_zKa-4Ztcmc1Ypl0k5nw';
/*
* pull in template for showing info about a location
*/
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/';
on(document.body, '.nav a', 'click', function (e) {
var id = e.target.id;
if (id === 'about-view') {
var content = fs.readFileSync('about.html', 'utf8');
modal(content);
}
else {
var content = templates.list({ locations: data });
modal(content);
}
});
window.onresize = function (e) {
var modal = document.querySelector('.modal');
if (modal) resizeModal();
};
/*
* add a marker to map from json data
*/
var markers = [];
data.forEach(addMarker);
function addMarker (row, i) {
var latlng = { lat: row['lat'], lng: row['long'] };
var content = templates.info(row);
var marker = L.marker(latlng, {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-color': '#aa3c3c'
})
});
markers.push(marker);
marker.on('click', function(e) {
modal(content);
});
}
var markerGroup = L.layerGroup(markers);
/*
* create map using mapbox plugin
*/
var streets = L.mapbox.tileLayer('sethvincent.de840f5b');
var map = L.map('map', {
center: [47.555, -122.252],
zoom: 15,
layers: [streets, markerGroup]
});
var location = L.marker([47, -122], {
icon: L.mapbox.marker.icon({
'marker-size': 'medium',
'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)
});
function modal (content) {
if (document.querySelector('.modal')) {
page.removeChild(document.querySelector('.modal'));
}
var modal = document.createElement('div');
modal.className = 'modal';
modal.innerHTML = content;
page.appendChild(modal);
resizeModal();
}
on(document.body, '#close-modal', 'click', function (e) {
var modal = document.querySelector('.modal');
page.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';
}
}
function createImageArrays (data) {
data.forEach(function (item) {
if (!item.photos) return;
var images = item.photos.split(',');
images.forEach(function(image, i) {
if (!images[i].match('http')) {
images.splice(i, 1);
}
else images[i] = images[i].replace(/ /g,'');
});
item.images = images;
});
return data;
}