-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
852 lines (716 loc) · 23.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// storage engine
localforage.config({
driver: localforage.LOCALSTORAGE,
name: 'secooraPointEntry',
storeName: 'secoora_point_entry'
});
// @TODO: default layers get loaded/stored in localforage if no key already in localforage
var cScale = d3.schemeDark2;
cScale[0] = "darkgray"; // Bathy
cScale[1] = "#d95f02"; // HF Radar
cScale[2] = "#66a61e"; // Regional Stations
cScale[3] = "#e6ab02"; // SECOORA Funded
cScale[4] = "midnightblue"; // 2016 Glider Tracks
cScale[5] = "darkviolet";
cScale[6] = "#a6761d";
cScale[7] = "#666666";
var colorScale = d3.scaleOrdinal(cScale),
userColorScale = d3.scaleOrdinal(d3.schemeSet1);
// default layers
var defaultLayers = [
{name: 'Bathymetry', url: "se_bathy.geojson", visible: true},
{name: 'HF Radar', url: "https://raw.githubusercontent.com/SECOORA/static_assets/master/hfradar/hfradar.geojson", visible: true},
{name: 'Regional Stations', url: "https://raw.githubusercontent.com/SECOORA/static_assets/master/stations/regional/stations.geojson", visible: true},
{name: 'SECOORA Funded', url: "https://raw.githubusercontent.com/SECOORA/static_assets/master/stations/assets/stations.geojson", visible: false},
{name: '2016 Glider Tracks', url: 'https://raw.githubusercontent.com/SECOORA/static_assets/master/gliders/tracks/gliders_2016/gliders_2016.geojson', visible: true},
{name: 'Glider Tracks', url: "https://raw.githubusercontent.com/SECOORA/static_assets/master/glider_tracks/gliders_from_georefd.geojson", visible: true},
{name: 'Glider Tracks (triangle)', url: "https://raw.githubusercontent.com/SECOORA/static_assets/master/glider_tracks/georef_glider_triangles.geojson", visible: true},
//{name: 'Gliders', url: "https://raw.githubusercontent.com/SECOORA/static_assets/master/gliders/gliders.geojson", visible: true},
]
// leaflet
var map = L.map('mapid').setView([30.676, -80.134], 6);
//var layer = new L.StamenTileLayer("terrain-background");
//var attribution = '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>';
//var layer = L.tileLayer('http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png', {attribution: attribution})
var layer = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',{ attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="https://carto.com/attributions">CARTO</a>' });
map.addLayer(layer);
// add default layers
var q = d3.queue();
defaultLayers.forEach(function(e) {
q.defer(d3.json, e.url);
});
q.awaitAll(function(error, data) {
//console.log(data);
// zip back up with original data
var newData = _.map(defaultLayers, function(d, i) {
return _.assign(_.clone(d), {
color: colorScale(i),
layer: L.geoJson(data[i], {
pointToLayer: function(feature, latlng) {
if (feature.properties.status === "Decommissioned") {
return null;
}
return L.circleMarker(latlng, {
radius: 3,
fillColor: colorScale(i),
color: colorScale(i),
weight: 1,
opacity: 1,
fillOpacity: 0.5
});
},
style: {
color: colorScale(i),
weight: 1,
opacity: 0.5,
},
onEachFeature: function(feature, layer) {
setPopup(feature.properties, layer);
}
})});
});
newData.forEach(function(e) {
e.layer.addTo(map);
});
// add to left hand controls
var cards = d3.select('.layerbar__predefined .list-group')
.selectAll('li')
.data(newData)
.enter()
.append('li')
.attr('class', 'list-group-item')
.on('click', function(d) {
d3.select(this).classed('disabled', d.visible);
if (d.visible) {
map.removeLayer(d.layer);
d.visible = false;
reorderLayers();
} else {
map.addLayer(d.layer);
d.visible = true;
reorderLayers();
}
});
cards.append('div')
.attr('class', 'tag tag-default tag-pill float-xs-right layercard__swatch')
.style('background-color', function(d) { return d.color })
.html(" ");
cards.append('div')
.attr('class', 'layercard__text')
.text(function(d) { return d.name });
reorderLayers();
});
function dataToGeoJson(data) {
var features = _.map(data, function(d) {
var props = _.omit(d, ['lon', 'lat']);
return {
type: "Feature",
properties: props,
geometry: {
type: "Point",
coordinates: [
d.lon,
d.lat
]
}
}
});
return {
type: "FeatureCollection",
features: features
}
}
function save(dataItem) {
// set in userLayers
if (dataItem.index === undefined) {
dataItem.index = window.userLayers.length;
}
window.userLayers[dataItem.index] = dataItem;
return _save();
}
/**
* Saves whatever is currently in userLayers
*/
function _save() {
// strip presentation attributes from userLayers
var saveValue = _.map(window.userLayers, function(d) {
return _.omit(d, ['layer', 'color', 'index']);
});
// store in localstorage
return localforage.setItem('user-layers', saveValue);
}
function loadUserLayers(value) {
window.userLayers = _.map(value, function(d, i) {
var gj = dataToGeoJson(d.data),
color = userColorScale(i);
// always assign color/visible/index, index will change!
_.assign(d, {
color: color,
visible: d.visible !== false,
index: i,
});
// only create/assign layer if we don't already have one
if (d.layer === undefined) {
d.layer = L.geoJson(gj, {
pointToLayer: function(feature, latlng) {
return L.circleMarker(latlng, {
radius: 5,
fillColor: color,
weight: 1,
opacity: 1,
fillOpacity: 1
});
},
style: {
color: color
},
onEachFeature: function(feature, layer) {
setPopup(feature.properties, layer);
}
});
}
return d;
});
// add to left hand controls
var selCards = d3.select('.layerbar__user__layers')
.selectAll('li')
.data(window.userLayers, function(d) {
return d.name;
});
// delete if necessary
selCards.exit()
.remove();
// remove map layers on anything in exit selection
// @TODO: peeks too much into internals
selCards.exit()._groups[0].forEach(function(e) {
map.removeLayer(e.__data__.layer);
});
var cards = selCards.enter()
.append('li')
.attr('class', 'list-group-item')
.on('click', function(d) {
d3.selectAll('.layerbar__user__layers .list-group-item-info').classed('list-group-item-info', false);
d3.select(this).classed('list-group-item-info', true);
// enable buttons
$('.editor__toolbar button').attr('disabled', false);
// add handler for export
$('.editor__toolbar__export').off('click');
$('.editor__toolbar__export').click(function() {
var gj = dataToGeoJson(d.data),
gjs = JSON.stringify(gj, null, ' ');
$('.export-modal__geojson').val(gjs);
$('.export-modal__link').attr('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(gjs));
$('.export-modal__link').attr('download', d.name + ".geojson");
$('#export-modal').modal();
});
// sizeup editor pane
$('.editor').css('flex-basis', '40%');
// add handler for delete
$('.editor__toolbar__delete').off('click');
$('.editor__toolbar__delete').click(function() {
if (window.confirm("Do you really want to delete layer " + d.name + "?")) {
// remove from userLayers, store in localforage
window.userLayers.splice(d.index, 1);
_save(); // we're not updating or adding so just use internal save current
// run through updateLayers
loadUserLayers(window.userLayers);
// close editor
closeEditorPane();
}
});
// define data controller for editor view
// interfaces with localforage
var controller = {
loadData: function(filter) {
//console.log("LOADING", d.data);
return _.map(d.data, function(dd, ii) {
return _.assign({_idx: ii}, dd);
});
},
insertItem: function(item) {
//console.log("INSERT", item);
// append new item to d
var newIdx = d.data.length;
d.data[newIdx] = _.clone(item);
// store in localstorage
return save(d).then(function() {
item._idx = newIdx;
return item;
});
},
updateItem: function(item) {
//console.log("UPDATE", item);
var idx = item._idx,
newItem = _.omit(item, ['_idx']);
d.data[idx] = newItem;
// store in localstorage
return save(d).then(function() {
return item;
});
},
deleteItem: function(item) {
//console.log("DELETE", item);
var idx = item._idx;
d.data.splice(idx, 1);
// store in localstorage
return save(d);
}
};
// extend jsgrid with decimal type
var DecimalField = function(config) {
jsGrid.Field.call(this, config);
};
DecimalField.prototype = new jsGrid.NumberField({
filterValue: function() {
return parseFloat(this.filterControl.val());
},
insertValue: function() {
return parseFloat(this.insertControl.val());
},
editValue: function() {
return parseFloat(this.editControl.val());
},
editTemplate: function(value) {
var template = jsGrid.NumberField.prototype.editTemplate.call(this, value);
template.on('keydown', null, this._grid, function(e) {
if (e.which === 13) {
e.data.updateItem();
} else if (e.keyCode === 27) {
e.data.cancelEdit();
}
});
return template;
},
insertTemplate: function(value) {
var template = jsGrid.NumberField.prototype.insertTemplate.call(this, value);
template.on('keydown', null, this._grid, function(e) {
var grid = e.data;
if (e.which === 13) {
grid.insertItem().done(function() {
grid.clearInsert();
});
} else if (e.keyCode === 27) {
$('.' + jsGrid.ControlField.prototype.insertModeButtonClass).click();
}
});
return template;
}
});
jsGrid.fields.decimal = DecimalField;
function genEnterEditor(value) {
var template = jsGrid.TextField.prototype.editTemplate.call(this, value);
template.on('keydown', null, this._grid, function(e) {
if (e.which === 13) {
e.data.updateItem();
} else if (e.keyCode === 27) {
e.data.cancelEdit();
}
});
return template;
}
function genInsertTemplate(value) {
var template = jsGrid.TextField.prototype.insertTemplate.call(this, value);
template.on('keydown', null, this._grid, function(e) {
var grid = e.data;
if (e.which === 13) {
grid.insertItem().done(function() {
grid.clearInsert();
});
} else if (e.keyCode === 27) {
$('.' + jsGrid.ControlField.prototype.insertModeButtonClass).click();
}
});
return template;
}
// default schema
var defaultSchema = [
{ name: "name", type: "text", editTemplate: genEnterEditor, insertTemplate: genInsertTemplate},
{ name: "lat", type: "decimal" },
{ name: "lon", type: "decimal" },
]
// grab any additional fields
var schema = defaultSchema.concat(_.map(_.filter(_.uniq(_.flatten(_.map(d.data, function(dd) {
return _.keys(dd);
}))), function(dd) {
return _.map(defaultSchema, 'name').indexOf(dd) == -1;
}), function(dd) {
return {
name: dd,
type: "text",
editTemplate: genEnterEditor,
insertTemplate: genInsertTemplate
}
}));
schema.push({type: "control"});
$('.editor__grid').jsGrid({
width: "100%",
height: "200px",
inserting: true,
editing: true,
sorting: false,
paging: false,
autoload: true,
fields: schema,
controller: controller,
onItemInserted: function(args) {
// new layer for new item
var item = args.item,
lat = parseFloat(item.lat) || 0,
lon = parseFloat(item.lon) || 0,
latlng = new L.LatLng(lat, lon),
marker = L.circleMarker(latlng, {
radius: 5,
fillColor: d.color,
color: d.color,
weight: 1,
opacity: 1,
fillOpacity: 1
});
setPopup(_.omit(item, ['lat', 'lon', '_idx']), marker);
d.layer.addLayer(marker);
},
onItemUpdated: function(args) {
// adjust marker
var item = args.item,
lat = parseFloat(item.lat) || 0,
lon = parseFloat(item.lon) || 0,
latlng = new L.LatLng(lat, lon),
marker = d.layer.getLayers()[item._idx];
marker.setLatLng(latlng);
setPopup(_.omit(item, ['lat', 'lon', '_idx']), marker);
},
onItemDeleted: function(args) {
// remove marker!
var item = args.item,
marker = d.layer.getLayers()[item._idx];
d.layer.removeLayer(marker);
}
});
});
// add map layers on anything in enter selection
// @TODO: peeks too much into internals
cards._groups[0].forEach(function(e) {
e.__data__.layer.addTo(map);
});
cards.append('div')
.attr('class', 'tag tag-default tag-pill float-xs-right layercard__swatch')
.style('background-color', function(d) { return d.color })
.html(' ')
.on('click', function(d) {
d3.event.stopPropagation();
d3.select(this.parentNode).classed('disabled', d.visible);
if (d.visible) {
map.removeLayer(d.layer);
d.visible = false;
reorderLayers();
} else {
map.addLayer(d.layer);
d.visible = true;
reorderLayers();
}
});
cards.append('div')
.attr('class', 'layercard__text')
.text(function(d) { return d.name });
reorderLayers();
}
// LOAD USER DEFINED LAYERS FROM LOCALSTORAGE
localforage.getItem('user-layers')
.then(function(value) {
if (value === null) {
value = [];
}
window.userLayers = value;
return window.userLayers;
})
.then(loadUserLayers)
.catch(function(err) {
console.error(err)
});
$('.layerbar__user__add').click(function() {
$('#new-modal').modal();
});
// attempt to parse value and determine format
// returns an object
// {
// type: 'geojson' | 'csv' | 'unknown',
// error: ... // only set if there was an issue. type will be set to what we thought it might be
// data: ...
// }
function tryParse(val) {
var err = null;
// first, try parsing it as JSON as that's more strict.
try {
var gjo = JSON.parse(val);
return {
type: 'geojson',
data: gjo
}
} catch (e) {
// should we bother parsing with CSV?
if (val.trim().startsWith("{")) {
return {
type: 'geojson',
error: e,
data: null
}
}
}
var csvParsed = Papa.parse(val, {
header: true,
dynamicTyping: true
});
// how to validate csv?
return {
type: 'csv',
data: csvParsed
}
// TODO
return {
type: 'unknown',
error: 'Could not determine format of contents',
data: null
}
}
function findSimilar(fields, name) {
var weights = _.map(fields, function(d) {
return {
name: d,
weight: jaro_winkler.distance(name, d)
};
});
// 0.6 arbitrarily chosen!
var filtered = _.reject(_.sortBy(weights, 'weight').reverse(), function(o) { return o.weight < 0.6 });
if (filtered.length == 0) {
return null;
}
return filtered[0].name;
}
function parseHint() {
var val = $('#new-modal__contents').val(),
parsedData = tryParse(val);
setHint(parsedData.type);
}
// set the csv/geojson hint label
// can pass null to clear it
function setHint(type) {
$('#new-modal .btn-group label').removeClass('active btn-success');
if (type !== null) {
$('#format-' + type).parent().addClass('active btn-success');
}
}
$('#new-modal__contents').on('change keyup paste', _.debounce(parseHint, 500));
$('#new-modal__name').one('keyup paste', function() {
$(this).data('edited', 'true');
});
$('#new-modal__file').on('change', function() {
var file = $(this).get(0).files[0],
name = $('#new-modal__name').val();
// set hint if we can figure it out
if (file.name.toLowerCase().endsWith(".csv")) {
setHint('csv');
} else if (file.name.toLowerCase().endsWith('json')) {
setHint('geojson');
} else {
setHint(null);
}
// set name if never been set
if ($('#new-modal__name').data('edited') !== 'true') {
var dotIndex = file.name.lastIndexOf('.');
if (dotIndex != -1) {
var newName = file.name.slice(0, dotIndex),
finalName = findAvailableLayerName(newName);
$('#new-modal__name').val(finalName);
}
}
});
$('.new-modal__import').click(function() {
// kill any previous dangers/alerts
$('#new-modal').find('.form-control-feedback').remove();
$('#new-modal').find('.has-danger').removeClass('has-danger');
$('#new-modal').find('.form-control-danger').removeClass('form-control-danger');
var contents = $('#new-modal__contents').val(),
file = $('#new-modal__file').get(0).files[0],
parsedData = null;
// if the contents pane visible, parse and immediatly call doImport
if ($('#tab-paste').hasClass('active')) {
if (contents.trim().length == 0) {
contents = {};
}
parsedData = tryParse(contents);
doImport(parsedData);
} else if ($('#tab-file').hasClass('active')) {
if (file === undefined) {
$('#new-modal__file').parent().addClass('has-danger')
.append('<div class="form-control-feedback">File is required</div>');
return;
}
if (file.name.toLowerCase().endsWith(".csv")) {
Papa.parse(file, {
header: true,
dynamicTyping: true,
complete: function(pd, oldF) {
doImport({
type: 'csv',
data: pd
});
}
});
} else if (file.name.toLowerCase().endsWith('json')) {
var reader = new FileReader();
reader.onload = function(event) {
parsedData = tryParse(event.target.result);
if (parsedData.type == 'geojson') {
doImport(parsedData);
} else {
$('#new-modal__file').parent().addClass('has-danger')
.append('<div class="form-control-feedback">Expected geojson format</div>');
return;
}
};
reader.readAsText(file);
} else {
$('#new-modal__file').parent().addClass('has-danger')
.append('<div class="form-control-feedback">File must end in json or .csv!</div>');
}
}
});
function doImport(parsedData) {
var invalid = false,
name = $('#new-modal__name').val(),
layerData = null;
// if user didn't enter a name, it's an error
if (!name) {
$('#new-modal__name').parent().addClass('has-danger')
.append('<div class="form-control-feedback">Name is required</div>');
$('#new-modal__name').addClass('form-control-danger');
invalid = true;
}
if (parsedData.hasOwnProperty('error')) {
$('#new-modal__contents').parent().addClass('has-danger')
.append('<div class="form-control-feedback">' + 'Could not parse: ' + parsedData.error + '</div>');
$('#new-modal__contents').addClass('form-control-danger');
invalid = true;
}
if (invalid) return;
if (parsedData.type == 'geojson') {
layerData = _.map(parsedData.data.features, function(d, i) {
return _.assign({
name: d.properties.name || "Item " + (i+1),
lat: d.geometry.coordinates[1],
lon: d.geometry.coordinates[0]
}, d.properties);
})
} else if (parsedData.type == 'csv') {
// find lat/lon
var latName = findSimilar(parsedData.data.meta.fields, 'lat'),
lonName = findSimilar(parsedData.data.meta.fields, 'lon');
console.debug("Lat field:", latName, "Lon field:", lonName);
if (latName === null || lonName === null) {
$('#new-modal').one('hidden.bs.modal', function(e) {
window.alert("Could not find lat (" + latName + ") or lon (" + lonName + "), blank values used.");
});
}
// transform data
layerData = _.map(parsedData.data.data, function(d, i) {
return _.assign({
name: d.name || "Item " + (i+1),
lat: d[latName] || 0.0,
lon: d[lonName] || 0.0
}, _.omit(d, [latName, lonName]));
});
}
var layer = {
name: name,
visible: true,
data: layerData
};
// save to localforage
save(layer).then(function() {
// reupdate UI
loadUserLayers(window.userLayers);
});
closeEditorPane();
$('#new-modal').modal('hide');
}
function closeEditorPane() {
// shrink editor pane
$('.editor').css('flex-basis', '50px');
// disable toolbar buttons, remove handlers
$('.editor__toolbar__export').attr('disabled', true);
$('.editor__toolbar__delete').attr('disabled', true);
$('.editor__toolbar__export').off('click');
$('.editor__toolbar__delete').off('click');
// remove jsGrid
if ($('.editor__grid').has('div').length) {
$('.editor__grid').jsGrid("destroy");
}
// remove any editing highlight from user pane list items
d3.selectAll('.layerbar__user__layers .list-group-item-info').classed('list-group-item-info', false);
}
$('.editor .close').click(closeEditorPane);
function findAvailableLayerName(prefix) {
var curNames = _.map(window.userLayers, 'name'),
name = '';
// start with prefix itself
if (curNames.indexOf(prefix) == -1) {
return prefix;
}
for (var i = 2; i < 500; i++) {
name = prefix + " " + i;
if (curNames.indexOf(name) == -1)
break;
}
return name;
}
$('#new-modal').on('show.bs.modal', function(e) {
var name = findAvailableLayerName("Unnamed Layer");
if (!name) {
throw "500 names?!?";
}
$('#new-modal__name').val(name);
setHint(null);
});
$('#new-modal').on('shown.bs.modal', function(e) {
$('#new-modal__name').select().focus();
});
$('#new-modal__contents').focus(function() {
$(this).select();
});
$('.export-modal__geojson').focus(function() {
$(this).select();
});
$('#new-modal, #export-modal').on('hidden.bs.modal', function(e) {
var form = $(this).find('form');
form[0].reset();
// kill any dangers/alerts
$('#new-modal').find('.form-control-feedback').remove();
$('#new-modal').find('.has-danger').removeClass('has-danger');
$('#new-modal').find('.form-control-danger').removeClass('form-control-danger');
});
function reorderLayers() {
// reverse go through layers in predefined, send them to back
var predefined = d3.select('.layerbar__predefined .list-group').selectAll('li').data();
predefined.reverse();
predefined.forEach(function(d) {
if (d.visible) {
d.layer.bringToBack();
}
});
// go through layers in userlayers, send them to front (in order)
window.userLayers.forEach(function(d) {
if (d.visible) {
d.layer.bringToFront();
}
});
}
function setPopup(properties, layer) {
var desc = "";
for (var key in properties) {
desc = desc + "<tr><td>" + key + "</td><td>" + properties[key] + "</td></tr>\n";
}
if (layer) {
layer.bindPopup("<table class='map-popup-table'>" + desc + "</table>");
}
return desc;
}