/
index.js
221 lines (191 loc) · 6.58 KB
/
index.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
var Mustache = require('mustache')
var L = require('leaflet')
require('leaflet.markercluster')
function createGeoJSON (data, optionsJSON) {
var geoJSON = []
data.forEach(function (lineItem) {
var hasGeo = confirmGeo(lineItem)
if (hasGeo && !lineItem.lat && !lineItem.long) handleLatLong(lineItem)
if (lineItem.linestring || lineItem.multipolygon) hasGeo = true
if (!hasGeo) return
if (!optionsJSON) {
optionsJSON = makeupOptionObject(lineItem)
var optionObj = buildOptionObject(optionsJSON, lineItem)
} else {
optionObj = buildOptionObject(optionsJSON, lineItem)
}
var type = determineType(lineItem)
if (lineItem.polygon || lineItem.multipolygon || lineItem.linestring) {
var shapeFeature = shapeJSON(lineItem, type, optionObj)
geoJSON.push(shapeFeature)
} else {
var pointFeature = pointJSON(lineItem, type, optionObj)
geoJSON.push(pointFeature)
}
})
return geoJSON
}
function buildOptionObject (optionsJSON, lineItem) {
var newObj = {}
optionsJSON.forEach(function (option) {
newObj[option] = lineItem[option]
})
return newObj
}
function makeupOptionObject (lineItem) {
var options = []
for (var i in lineItem) {
options.push(i)
}
return options
}
function confirmGeo (lineItem) {
var hasGeo = false
if (lineItem.lat && lineItem.long || lineItem.polygon) hasGeo = true
if (lineItem.latitude && lineItem.longitude || lineItem.polygon) hasGeo = true
if (lineItem.geolatitude && lineItem.geolongitude || lineItem.polygon) hasGeo = true
return hasGeo
}
function handleLatLong (lineItem) {
if (lineItem.latitude && lineItem.longitude || lineItem.polygon) {
lineItem.lat = lineItem.latitude
lineItem.long = lineItem.longitude
delete lineItem.latitude
delete lineItem.longitude
return lineItem
}
if (lineItem.geolatitude && lineItem.geolongitude || lineItem.polygon) {
lineItem.lat = lineItem.geolatitude
lineItem.long = lineItem.geolongitude
delete lineItem.geolatitude
delete lineItem.geolongitude
return lineItem
}
}
function pointJSON (lineItem, type, optionObj) {
var pointFeature = {
type: 'Feature',
'geometry': {
'type': type,
'coordinates': [+lineItem.long, +lineItem.lat]
},
'properties': {
'color': lineItem.hexcolor || '#2196f3'
},
'opts': optionObj
}
return pointFeature
}
function divIcon (color) {
var markerHtmlStyles = 'background-color: #' + color.replace('#', '') + ';' +
'width: 2rem; height: 2rem; display: block; left: -1rem; top: -1rem; border: 1px solid #fff;' +
'position: relative; border-radius: 3rem 3rem 0; transform: rotate(45deg);'
var icon = L.divIcon({
className: 'div-icon',
iconAnchor: [0, 24],
labelAnchor: [-6, 0],
popupAnchor: [0, -36],
html: '<span style="' + markerHtmlStyles + '">'
})
return icon
}
function shapeJSON (lineItem, type, optionObj) {
var lowercaseType = type.toLowerCase()
var coords
if (type !== 'LineString') {
coords = JSON.parse('[[' + lineItem[lowercaseType] + ']]')
} else { coords = JSON.parse('[' + lineItem[lowercaseType] + ']') }
var shapeFeature = {
type: 'Feature',
'geometry': {
'type': type,
'coordinates': coords
},
'properties': {
'fillColor': lineItem.hexcolor,
'color': lineItem.hexcolor
},
'opts': optionObj
}
return shapeFeature
}
function determineType (lineItem) {
var type = ''
// TODO this is not actually verifying the content just the property
if (lineItem.lat && lineItem.long) type = 'Point'
if (lineItem.polygon) type = 'Polygon'
if (lineItem.multipolygon) type = 'MultiPolygon'
if (lineItem.linestring) type = 'LineString'
return type
}
// MAPS
function loadMap (mapOptions) {
if (!mapOptions.data) return // no data, no map
var map = L.map(mapOptions.mapDiv)
var tiles = mapOptions.tiles || 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'
var attribution = '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
L.tileLayer(tiles, {attribution: attribution}).addTo(map)
// Set behavior
map.touchZoom.disable()
map.doubleClickZoom.disable()
map.scrollWheelZoom.disable()
addMarkerLayer(map, mapOptions)
}
function makePopupTemplate (geoJSON) {
var allOptions = geoJSON[0].opts
var keys = []
for (var i in allOptions) keys.push(i)
var mustacheKeys = mustachify(keys)
var template = '<ul>'
var counter = mustacheKeys.length
mustacheKeys.forEach(function (key) {
counter--
if (counter === 0) template = template.concat(key, '</ul>')
else template = template.concat(key)
})
return template
}
function mustachify (array) {
var newArray = []
array.forEach(function (item) {
item = '<li><b>' + item + ':</b> {{' + item + '}}</li>'
newArray.push(item)
})
return newArray
}
function addMarkerLayer (map, mapOpts) {
// setting a color in options overides colors in spreadsheet
if (mapOpts.hexcolor) var iconColor = mapOpts.hexcolor
mapOpts.geoJSONincludes = mapOpts.geoJSONincludes || null // um?
var geoJSON = createGeoJSON(mapOpts.data, mapOpts.geoJSONincludes)
// if no popup template, create one
if (!mapOpts.template) mapOpts.template = makePopupTemplate(geoJSON)
var features = {'type': 'FeatureCollection', 'features': geoJSON}
if (mapOpts.cluster) var clusterGroup = new L.MarkerClusterGroup()
var layer = L.geoJson(features)
layer.eachLayer(function (marker) {
var popupContent = Mustache.render(mapOpts.template, marker.feature.opts)
marker.bindPopup(popupContent, {closeButton: false})
marker.setIcon(divIcon(iconColor || marker.feature.properties.color))
if (mapOpts.cluster) clusterGroup.addLayer(marker)
})
map.fitBounds(layer.getBounds())
if (mapOpts.cluster) {
map.addLayer(clusterGroup)
addClusterCSS(iconColor || '#2196f3')
} else layer.addTo(map)
}
function addClusterCSS (color) {
if (!color.match('#')) color += '#'
var css = '.marker-cluster-small, .marker-cluster-small div, .marker-cluster-medium,' +
'.marker-cluster-medium div, .marker-cluster-large, .marker-cluster-large div' +
'{background-color:' + color + ';} .marker-cluster {background-clip: padding-box; border-radius: 20px;}' +
'.marker-cluster div {width: 30px; height: 30px; margin-left: 5px; margin-top: 5px;' +
'text-align: center; border-radius: 15px; font: 12px "Helvetica Neue", Arial, Helvetica, sans-serif;}' +
'.marker-cluster span {line-height: 30px;}'
var style = document.createElement('style')
style.innerHTML = css
document.head.appendChild(style)
}
module.exports.createGeoJSON = createGeoJSON
module.exports.loadMap = loadMap