-
Notifications
You must be signed in to change notification settings - Fork 16
/
App.js
156 lines (131 loc) · 4.39 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
$(document).ready(function() {
"use strict";
ko.bindingHandlers.option = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
ko.selectExtensions.writeValue(element, value);
}
};
$('[data-toggle="tooltip"]').tooltip();
var MapModel = function(data) {
var self = this;
// Flattens the layers object and returns all basemap datasets
var getAllBasemaps = function() {
var basemaps = [];
ko.utils.arrayMap(self.layers(), function(dataset) {
basemaps = basemaps.concat(dataset.datasets);
});
return basemaps;
};
this.layers = ko.observableArray(data);
this.map = L.map('map').setView([0, 0], 3);
this.basemap = null;
this.overlay = null;
this.selectedBasemap = ko.observable();
this.selectedOverlay = ko.observable();
this.showOverlay = ko.observable(false);
this.basemapLink = ko.observable();
this.mapCenter = ko.observable({lat: 0, lng: 0});
this.mapZoom = ko.observable(3);
// Update the observables when the map moves
this.map.on('move', function() {
self.mapCenter(self.map.getCenter());
self.mapZoom(self.map.getZoom());
});
this.nextBasemap = function() {
var basemaps = getAllBasemaps();
// Get the index of the currently selected basemaps
var selected = basemaps.indexOf(self.selectedBasemap());
// Wrap around to the first basemap if at the end
if (selected == basemaps.length - 1) {
selected = 0;
} else {
selected++;
}
self.selectedBasemap(basemaps[selected]);
};
this.prevBasemap = function() {
var basemaps = getAllBasemaps();
// Get the index of the currently selected basemaps
var selected = basemaps.indexOf(self.selectedBasemap());
if (selected === 0) {
selected = basemaps.length - 1;
} else {
selected--;
}
self.selectedBasemap(basemaps[selected]);
};
this.toggleOverlay = function() {
self.showOverlay(!self.showOverlay());
};
// Only show the overlay if enabled
this.showOverlay.subscribe(function(overlayStatus) {
if (self.overlay !== null) {
if (overlayStatus) {
self.overlay.setOpacity(1);
} else {
self.overlay.setOpacity(0);
}
}
});
// Set the basemap tile layer URL when changed
this.selectedBasemap.subscribe(function(newDataset) {
if (self.basemap !== null) {
self.basemap.setUrl(newDataset.endpoint);
} else {
self.basemap = L.tileLayer(newDataset.endpoint).addTo(self.map);
}
});
// Set the overlay URL if changed
this.selectedOverlay.subscribe(function(newDataset) {
if (self.overlay !== null) {
self.overlay.setUrl(newDataset.endpoint);
} else {
self.overlay = L.tileLayer(newDataset.endpoint);
// If not enabled - then hide it until it gets enabled
if (!self.showOverlay()) {
self.overlay.setOpacity(0);
}
self.overlay.addTo(self.map);
}
});
// Creates the javascript that will be sent to codepen or jsfiddle
this.createJS = ko.pureComputed(function() {
var center = self.mapCenter();
var zoom = self.mapZoom()
var jsString = "var map = L.map('map').setView([" + self.mapCenter().lat + ", " + self.mapCenter().lng + "], " + self.mapZoom() + ");\n";
jsString += "L.tileLayer('" + self.selectedBasemap().endpoint + "').addTo(map);\n";
if (self.showOverlay()) {
jsString += "L.tileLayer('" + self.selectedOverlay().endpoint + "').addTo(map);\n";
}
return jsString;
}, this);
// JSFiddle needs multiple input fields
this.jsFiddleData = ko.pureComputed(function() {
return {
html: '<div id="map"></div>',
js: self.createJS(),
dtd: 'html 5',
css: 'html, body, #map { width: 100%; height: 100%; margin: 0px; }',
resources: '',
};
}, this);
// While codepen needs a stringified JSON object for its properties
this.codePenData = ko.pureComputed(function() {
return JSON.stringify({
html: '<div id="map"></div>',
js: self.createJS(),
editors: '001',
css: 'html, body, #map { width: 100%; height: 100%; margin: 0px; }',
js_external: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js',
css_external: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css',
js_library: 'jquery'
});
}, this);
};
$.getJSON('tiles.json', function(data) {
ko.applyBindings(new MapModel(data));
}).fail(function() {
$('#error-box').toggle('hidden');
});
});