-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
296 lines (279 loc) · 7.96 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
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
// Richard Wen
// rrwen.dev@gmail.com
// (define) Widget definition
freeboard.loadWidgetPlugin({
// (define_info) Widget information
'type_name' : 'leaflet_realtime',
'display_name': 'Leaflet Map (Real Time)',
'description' : 'Create a real time <a href="http://leafletjs.com/" target="_blank">Leaflet</a> webmap widget by fetching and updating <a href="http://geojson.org/" target="_blank">GeoJSON</a> data from a <a href="https://en.wikipedia.org/wiki/URL" target="_blank">URL</a> with <a href="https://github.com/perliedman/leaflet-realtime" target="_blank">leaflet-realtime</a>.<br><br><b>Note:</b> This widget must be recreated if you wish to change the settings.',
'fill_size' : true,
// (define_scripts) Load external scripts before widget
'external_scripts': [
'https://unpkg.com/leaflet@1.3.1/dist/leaflet.css',
'https://unpkg.com/leaflet@1.3.1/dist/leaflet.js',
'https://cdnjs.cloudflare.com/ajax/libs/leaflet-realtime/2.1.1/leaflet-realtime.js',
'https://cdn.rawgit.com/stefanocudini/leaflet-search/a88dc07e/src/leaflet-search.css',
'https://cdn.rawgit.com/stefanocudini/leaflet-search/a88dc07e/src/leaflet-search.js'
],
// (define_settings) User interface settings
'settings' : [
{
name: 'id',
display_name: 'Map ID',
type: 'text',
default_value: 'leaflet-realtime'
},
{
name: 'url',
display_name: 'Data Source URL',
type: 'text',
default_value: 'https://wanderdrone.appspot.com/'
},
{
name: 'propertiesID',
display_name: 'Data Source Properties ID',
type: 'text',
default_value: 'id'
},
{
name: 'interval',
display_name: 'Refresh Interval (sec)',
type: 'number',
default_value: 3
},
{
name: 'height',
display_name: 'Widget Height',
type: 'number',
default_value: 8
},
{
name: 'radius',
display_name: 'Radius Style',
type: 'number',
default_value: 4
},
{
name: 'color',
display_name: 'Color Style',
type: 'text',
default_value: '#FFFFFF'
},
{
name: 'weight',
display_name: 'Weight Style',
type: 'number',
default_value: 0
},
{
name: 'opacity',
display_name: 'Opacity Style',
type: 'number',
default_value: 0
},
{
name: 'fillColor',
display_name: 'Fill Color Style',
type: 'text',
default_value: '#FF9900'
},
{
name: 'fillOpacity',
display_name: 'Fill Opacity Style',
type: 'number',
default_value: 1
},
{
name: 'basemap',
display_name: 'Base Map URL',
type: 'text',
default_value: 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png'
},
{
name: 'viewX',
display_name: 'Starting Latitude',
type: 'number',
default_value: 0
},
{
name: 'viewY',
display_name: 'Starting Longitude',
type: 'number',
default_value: 0
},
{
name: 'zoomPosition',
display_name: 'Zoom Control Position',
type: 'text',
default_value: 'topright'
},
{
name: 'zoom',
display_name: 'Zoom Level',
type: 'number',
default_value: 2
},
{
name: 'zoomMin',
display_name: 'Minimum Zoom',
type: 'number',
default_value: 2
},
{
name: 'zoomMax',
display_name: 'Maximum Zoom',
type: 'number',
default_value: 19
},
{
name: 'popup',
display_name: 'Popup',
type: "boolean",
default_value: true
},
{
name: 'search',
display_name: 'Search',
type: 'boolean',
default_value: true
},
{
name: 'searchPosition',
display_name: 'Search Position',
type: 'text',
default_value: 'topleft'
},
{
name: 'searchZoom',
display_name: 'Search Zoom',
type: 'number',
default_value: 17
},
{
name: 'searchHide',
display_name: 'Search Hide Point',
type: 'boolean',
default_value: false
}
],
// (define_instance) Create widget instance
newInstance: function(settings, callback) {
callback(new widget(settings));
}
});
// (widget) Widget implementation
var widget = function(settings) {
// (widget_vars) Widget variables
var self = this;
var current = settings;
current.popup = current.popup || true;
current.search = current.search || true;
current.searchZoom = current.searchZoom || 17;
current.searchPosition = current.searchPosition || 'topleft';
current.searchHide = current.searchHide || false;
current.zoomPosition = current.zoomPosition || 'topright';
var div = $('<div></div>');
var map, realtime;
// (widget_render) Render webmap
self.render = function(container) {
// (widget_render_html) Add div to widget
div.attr('id', current.id);
$(div).attr('style', 'width: 100%; height: 100vh; z-index: 1;');
$(container).append(div);
// (widget_render_remove) Remove non compatible tools
$(container).parent().find('[data-bind="pluginEditor: {operation: \'edit\', type: \'widget\'}"]').remove();
$(container).parent().find('.sub-section-tools').remove();
$(container).parent().parent().parent().find('[data-bind="pluginEditor: {operation: \'edit\', type: \'pane\'}"]').remove();
$(container).parent().parent().parent().find('[data-bind="pluginEditor: {operation: \'add\', type: \'widget\'}"]').remove();
// (widget_render_map) Create leaflet map
map = L.map(current.id, {zoomControl: false}).setView([current.viewX, current.viewY], current.zoom);
L.control.zoom({position: current.zoomPosition}).addTo(map);
// (widget_render_tiles) Create leaflet base tiles
L.tileLayer(current.basemap, {
minZoom: current.zoomMin,
maxZoom: current.zoomMax
}).addTo(map);
// (widget_render_search) Create search button
if (current.search) {
map.addControl(new L.Control.Search({
position: current.searchPosition,
collapsed: true,
url: 'http://nominatim.openstreetmap.org/search?format=json&q={s}',
jsonpParam: 'json_callback',
propertyName: 'display_name',
propertyLoc: ['lat','lon'],
hideMarkerOnCollapse: current.searchHide,
minLength: 2,
textPlaceholder: 'Search Location...',
marker: {
icon: false,
animate: true,
circle: {
radius: current.radius + 2,
weight: current.weight + 1,
color: current.fillColor,
fill: false
}
},
moveToLocation: function(latlng, title, map) {
map.setView(latlng, current.searchZoom);
}
}));
}
// (widget_render_realtime) Create realtime map layer
realtime = L.realtime({
url: current.url,
crossOrigin: true,
type: 'json'
},
{
interval: current.interval * 1000,
getFeatureId: function(feature) {
return feature.properties[current.propertiesID];
},
style: {
color: current.color,
weight: current.weight,
opacity: current.opacity,
fillColor: current.fillColor,
fillOpacity: current.fillOpacity
},
pointToLayer: function(feature, latlng) {
return L.circleMarker(latlng, {
radius: current.radius,
color: current.color,
weight: current.weight,
opacity: current.opacity,
fillColor: current.fillColor,
fillOpacity: current.fillOpacity
});
},
onEachFeature: function (feature, layer) {
if (current.popup && feature.properties) {
var popupHTML = '';
for (var k in feature.properties) {
if (feature.properties[k]) {
popupHTML += '<p style="word-wrap: break-word; white-space: normal;"><b>' + k + '</b>: ' + feature.properties[k] + '</p><br>';
}
}
layer.bindPopup(popupHTML);
}
}
}).addTo(map);
setTimeout(function () {
map.invalidateSize()
}, 250);
};
// (widget_height) Return expected height of widget
self.getHeight = function() {
return current.height;
};
// (widget_change) Settings changed - currently disabled
self.onSettingsChanged = function(changed) {};
// (widget_dispose) Remove and clean up widget
self.onDispose = function() {
realtime.stop();
map.remove();
$(div).remove();
};
};