/
slippymap.js
272 lines (228 loc) · 9.25 KB
/
slippymap.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
const nextOnResponse = require("stimsrv/controller/nextOnResponse");
const valOrFunc = require("stimsrv/util/valOrFunc");
const resource = require("stimsrv/util/resource");
let pick = require("stimsrv/util/pickProperties");
const DEFAULTS = {
name: "slippymap", // task name
description: "Interactive (slippy) map",
tiles: null, // tiles must be specified by experiment!
initialPosition: [0,0], // array or f(context)
initialZoom: 6, // number or f(context)
initialBounds: null, // if defined, takes precedence over initialPosition and initialZoom, array or f(context)
initialBoundsOptions: {animate: false}, // object or f(context)
minZoom: 0, // number or f(context)
maxZoom: 20, // number or f(context)
interaction: true, // bool or f(context)
synchronize: false, // bool or f(context)
synchronizeMode: "centerZoom", // "centerZoom" or "bounds"
synchronizeEventType: "mapmove",
mapInterfaces: ["display","monitor"], // interfaces to show the map on, array or f(context)
auxInterfaces: {}, // further interfaces to show, object or f(context)
leafletOptions: {}, // options passed to leaflet, object or f(context)
// callback functions
// each of those will be called with "map" and "context" as additional parameters
// after internal handling (setup of map, map synchronization)
initialize: null, // (parent, stimsrv, map, context) => {...}
render: null, // (condition, map, context) => {...}
event: null, // (type, data, map, context) => {...}
controller: nextOnResponse(),
}
const taskContextKeys = ["mapInterfaces","auxInterfaces"];
const rendererContextKeys = ["tiles","initialZoom","initialPosition","initialBounds","initialBoundsOptions","minZoom","maxZoom","interaction","synchronize","leafletOptions"];
// I don't see an easy way to get those programmatically, so extracted all map options from Leaflet docs
const leafletMapOptions = [
"attributionControl","zoomControl","closePopupOnClick","zoomSnap","zoomDelta",
"boxZoom","doubleClickZoom","dragging","crs","minZoom","maxZoom","maxBounds",
"zoomAnimation","zoomAnimationThreshold","fadeAnimation","markerZoomAnimation",
"inertia","inertiaDeceleration","inertiaMaxSpeed","easeLinearity","maxBoundsViscosity",
"keyboard","keyboardPanDelta","scrollWheelZoom","wheelDebounceTime","wheelPxPerZoomLevel",
"tap","tapTolerance","touchZoom","bounceAtZoomLimits"
];
let resources = resource("slippymap", "resources", __dirname);
let slippyMapRenderer = function(config, context) {
config = valOrFunc.properties(Object.assign({}, DEFAULTS, config), rendererContextKeys, context);
if (!(config.tiles?.tileURL)) {
throw new Error("Slippymap renderer: config.tiles.tileURL must be specified!");
}
// Leaflet reference
let L = null;
let map = null;
let sync = {ongoing: false};
let duringUserMovement = false;
return {
initialize: function(parent, stimsrv) {
if (!L) L = require("leaflet");
parent.innerHTML = `<link rel="stylesheet" href="${resource.url("slippymap")}/leaflet.css"/> `;
let document = parent.ownerDocument;
let mapEl = document.createElement("div");
mapEl.className = "slippymap";
mapEl.id = "slippymap";
mapEl.style.width = "100%";
mapEl.style.height = "100%";
parent.appendChild(mapEl);
let mapOptions = Object.assign({}, config.leafletOptions); //pick(config, leafletMapOptions);
if (!config.interaction) {
Object.assign(mapOptions, {
boxZoom: false,
doubleClickZoom: false,
dragging: false,
zoomControl: false,
scrollWheelZoom: false,
touchZoom: false,
keyboard: false
});
}
map = L.map('slippymap', mapOptions);
L.tileLayer(config.tiles.tileURL, config.tiles).addTo(map);
map.attributionControl.setPrefix("");
if (config.initialBounds) {
map.fitBounds(config.initialBounds, config.initialBoundsOptions);
}
else {
// Leaflet is sensitive to order of zoom/pan commands
// to test see http://jsfiddle.net/sx5gtwa7/3/
if (config.initialZoom && typeof config.initialZoom == "number") {
map.setZoom(config.initialZoom);
}
if (config.initialPosition && Array.isArray(config.initialPosition)) {
map.setView(config.initialPosition);
}
}
if (config.synchronize) {
function sendMapSyncEvent() {
let data = null;
if (config.synchronizeMode == "bounds") {
let bounds = map.getBounds();
data = {
bounds: [[bounds.getNorth(), bounds.getEast()],[bounds.getSouth(), bounds.getWest()]]
};
}
else {
let pos = map.getCenter();
data = {
center: {lat: pos.lat, lng: pos.lng},
zoom: map.getZoom()
};
}
stimsrv.event(config.synchronizeEventType, data);
}
const debugui = false;
let duringWheel = false;
// Leaflet events fire liberally also during programmatically triggered changes
// not sure how to best detect real user interaction
let lastUpdateTime = 0;
// this does not work on the map, use DOM element instead
mapEl.addEventListener("touchstart", function(event) {
if (debugui) console.log("TOUCHSTART");
duringUserMovement = true;
});
mapEl.addEventListener("wheel", function(event) {
if (debugui) console.log("TOUCHSTART");
duringUserMovement = true;
duringWheel = true;
});
map.on("mousedown", function(event) {
if (debugui) console.log("MOUSEDOWN");
duringUserMovement = true;
});
map.on("movestart", function(event) {
if (debugui) console.log("MOVESTART");
});
map.on("move", function(event) {
if (debugui) console.log("MOVE");
if (duringUserMovement && !sync.ongoing && Date.now() - lastUpdateTime > 100) {
sendMapSyncEvent();
lastUpdateTime = Date.now();
}
});
map.on("moveend", function(event) {
if (debugui) console.log("MOVEEND");
if (duringUserMovement && !sync.ongoing) {
sendMapSyncEvent();
}
});
map.on("zoomend", function(event) {
if (debugui) console.log("ZOOMEND");
if (duringUserMovement && !sync.ongoing) {
sendMapSyncEvent();
}
if (duringWheel) {
// wait until after listeners have been completed
setTimeout(() => {
duringWheel = false;
duringUserMovement = false;
}, 1);
}
});
map.on("mouseup", function(event) {
if (debugui) console.log("MOUSEUP");
duringUserMovement = false;
});
map.addUserEventListener = function(type, callback) {
map.addEventListener(type, function(event) {
if (duringUserMovement && !sync.ongoing) {
callback(event);
}
});
}
}
if (config.initialize) config.initialize(parent, stimsrv, map, context);
},
render: function(condition) {
if (config.render) {
config.render(condition, map, context);
}
else {
if (condition.mapPosition) {
map.setView(condition.mapCenter, condition.mapZoom);
}
else if (condition.mapZoom) {
map.setZoom(condition.mapZoom);
}
}
//console.log("Centering map on " + condition.initialPosition);
},
event: function(type, data) {
if (config.synchronize && type == config.synchronizeEventType) {
if (!duringUserMovement) {
// store local copy in closure, to ensure that only last event can reset globally
sync = {ongoing: true};
let _sync = sync;
if (data.bounds) {
map.on("moveend", () => { _sync.ongoing = false; });
map.fitBounds(data.bounds); //, {animate: false});
}
if (data.center && data.zoom) {
map.on("moveend", () => { _sync.ongoing = false; });
map.setView(data.center, data.zoom); //, {animate: false});
}
}
}
if (config.event) config.event(type, data, map, context);
},
getMap: () => map,
resources: resources
}
}
function slippyMapTask(config) {
config = Object.assign({}, DEFAULTS, config);
return {
name: config.name,
description: config.description,
frontend: context => {
config = valOrFunc.properties(config, taskContextKeys, context);
let renderer = slippyMapRenderer(config, context);
let uis = valOrFunc(config.auxInterfaces, context);
for (let ui of valOrFunc(config.mapInterfaces, context)) {
uis[ui] = renderer;
}
return {
interfaces: uis
};
},
controller: config.controller,
resources: resources
}
}
slippyMapTask.renderer = slippyMapRenderer;
module.exports = slippyMapTask;