-
Notifications
You must be signed in to change notification settings - Fork 0
/
currentViewer.html
610 lines (557 loc) · 20.8 KB
/
currentViewer.html
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>Stream Service Viewer</title>
<link
rel="stylesheet"
href="https://js.arcgis.com/4.29/esri/themes/light/main.css"
/>
<link rel="stylesheet" href="./streamServiceViewerStyles.css" />
<script src="https://js.arcgis.com/4.29/"></script>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/widgets/BasemapGallery",
"esri/layers/StreamLayer",
"esri/Graphic",
"esri/PopupTemplate",
"esri/widgets/Sketch/SketchViewModel",
"esri/layers/GraphicsLayer",
"esri/widgets/Expand",
"esri/widgets/LayerList",
"esri/widgets/Legend",
"esri/core/reactiveUtils"
], function (
Map,
MapView,
BasemapGallery,
StreamLayer,
Graphic,
PopupTemplate,
SketchViewModel,
GraphicsLayer,
Expand,
LayerList,
Legend,
reactiveUtils
) {
let map,
mapView,
sketchViewModel,
sketchViewModelLayer,
streamLayer,
streamLayerView,
spatialFilter,
expressionFilter,
isConnectingFromConnectCall;
function init() {
map = new Map({
basemap: "gray-vector"
});
mapView = new MapView({
map: map,
container: "map"
});
const bgExpand = new Expand({
view: mapView,
content: new BasemapGallery({ view: mapView })
});
const layerListExpand = new Expand({
view: mapView,
content: new LayerList({ view: mapView }),
expanded: false
});
const legendExpand = new Expand({
view: mapView,
content: new Legend({ view: mapView })
});
mapView.ui.add(bgExpand, "top-left");
mapView.ui.add(layerListExpand, "top-right");
mapView.ui.add(legendExpand, "bottom-left");
sketchViewModelLayer = new GraphicsLayer();
map.add(sketchViewModelLayer);
sketchViewModel = new SketchViewModel({
layer: sketchViewModelLayer,
view: mapView,
defaultUpdateOptions: {
enableRotation: false,
toggleToolOnClick: false
}
});
sketchViewModel.on("create", function (obj) {
switch (obj.state) {
case "complete":
spatialFilter = obj.graphic.geometry.extent;
drawSpatialFilter(spatialFilter);
applySpatialFilter(spatialFilter);
enableButton("clearSpatialFilter");
break;
default:
break;
}
});
//connect click events to UI buttons
const headerElements =
document.getElementsByClassName("config-header");
for (let i = 0; i < headerElements.length; i++) {
headerElements[i].addEventListener("click", function (e) {
if (e.target.type !== "checkbox") {
const sectionWrapper = e.target.closest("section");
sectionWrapper.classList.toggle("section-hidden");
}
});
}
document
.getElementsByClassName("collapser")[0]
.addEventListener("click", togglePanel);
document
.getElementById("toggleStreamLayer")
.addEventListener("click", toggleStreamLayer);
document
.getElementById("pauseResume")
.addEventListener("click", toggleSubscription);
document
.getElementById("applyWhereClause")
.addEventListener("click", applyWhereClause);
document
.getElementById("applySpatialFilter")
.addEventListener("click", sketchSpatialFilter);
document
.getElementById("clearSpatialFilter")
.addEventListener("click", clearSpatialFilter);
document
.getElementById("connectDisconnect")
.addEventListener("click", toggleConnection);
document
.getElementById("clearPreviousObservations")
.addEventListener("click", function () {
if (streamLayer) {
streamLayer.sendMessageToClient({
type: "clear"
});
}
});
document
.getElementById("switchViewer")
.addEventListener("click", function () {
window.location.href = "./classicViewer.html";
});
document
.getElementById("clear")
.addEventListener("click", function () {
document.getElementById("panelMessage").innerText = "";
});
}
/*************************************************
*
* Functions to update the UI
*
*************************************************/
function togglePanel() {
document
.getElementsByTagName("body")[0]
.classList.toggle("panel-collapsed");
}
function createToaster(message, type) {
const msg =
type === "success"
? document.createTextNode("\u2713 " + message)
: document.createTextNode("\u2716 " + message);
const element = document.createElement("span");
element.appendChild(msg);
document.getElementById("panelMessage").prepend(element);
}
function setPauseResumeButtonToPause() {
document.getElementById("pauseResume").innerText =
"Pause";
document.getElementById("pauseResume").title =
"Pause the connection to stop new observations from being applied until resuming. Data will be received but will not update the layer view until resumed.";
}
function setPauseResumeButtonToResume() {
document.getElementById("pauseResume").innerText =
"Resume";
document.getElementById("pauseResume").title =
"Resume the connection, apply any observations received while paused, and update the layer view with new observations.";
}
function setConnectDisconnectButtonToConnect() {
document.getElementById("connectDisconnect").innerText =
"Connect";
document.getElementById("connectDisconnect").title =
"Connect to the stream service to stream new observations.";
}
function setConnectDisconnectButtonToDisconnect() {
document.getElementById("connectDisconnect").innerText =
"Disconnect";
document.getElementById("connectDisconnect").title =
"Disconnect from the stream service. New observations will not be streamed until reconnecting.";
}
function disableButton(buttonId) {
document.getElementById(buttonId)
.classList.add("esri-button--disabled");
}
function enableButton(buttonId) {
document.getElementById(buttonId)
.classList.remove("esri-button--disabled");
}
/*************************************************
*
* Functions to add and remove Stream Layer
*
*************************************************/
function toggleSubscription() {
if (
streamLayerView &&
streamLayerView.connectionStatus === "paused"
) {
streamLayerView.resume();
setPauseResumeButtonToPause();
} else {
streamLayerView.pause();
setPauseResumeButtonToResume();
}
}
function toggleConnection() {
if (streamLayerView) {
if (streamLayerView.connectionStatus === "disconnected") {
streamLayerView.connect();
/*
* The button is disabled and relevant text is displayed
* to prevent a bug where the user can click disconnect while
* disconnected. Handling for when the layer is connected is
* in the layer's connection status change handler.
*/
document.getElementById("connectDisconnect").innerText =
"Connecting";
disableButton("connectDisconnect");
isConnectingFromConnectCall = true;
} else {
streamLayerView.disconnect();
setConnectDisconnectButtonToConnect();
// Previous observations cannot be cleared when disconnected.
disableButton("clearPreviousObservations");
// Pausing / resuming a disconnected stream layer has no effect,
// so, disable the button and set it up so the user can
// subscribe again
setPauseResumeButtonToResume();
disableButton("pauseResume");
}
}
}
function toggleStreamLayer() {
streamLayer ? removeStreamLayer() : addStreamLayer();
}
function addStreamLayer() {
//url to stream service
const svcUrl = document.getElementById("streamServiceUrl").value;
//construct Stream Layer
streamLayer = new StreamLayer({
url: svcUrl,
definitionExpression: expressionFilter,
geometryDefinition: spatialFilter,
purgeOptions: { displayCount: 10000 }
});
streamLayer.createPopupTemplate();
//Add layer to map
map.add(streamLayer);
//When layer loads, register listeners for layer events and add layer to map
mapView.whenLayerView(streamLayer).then(function (layerView) {
streamLayerView = layerView;
onLayerAdded();
layerView.on("message-received", onMessage);
layerView.watch("connectionError", function (error) {
removeStreamLayer();
createToaster("Error connecting to the Stream Service", "error");
});
reactiveUtils.watch(
() => layerView.connectionStatus,
() => onConnectionStatusChange(layerView.connectionStatus)
)
streamLayer.popupTemplate = streamLayer.createPopupTemplate();
});
}
function removeStreamLayer() {
if (streamLayer) {
map.remove(streamLayer);
streamLayer.destroy();
streamLayer = null;
onLayerRemoved();
}
}
/*********************************************************
*
* Stream layer event handlers
*
*********************************************************/
function onLayerAdded() {
document.getElementById("toggleStreamLayer").innerText =
"Remove Stream Layer";
enableButton("pauseResume");
enableButton("clearPreviousObservations");
enableButton("connectDisconnect");
}
function onLayerRemoved() {
document.getElementById("toggleStreamLayer").innerText =
"Add Stream Layer";
disableButton("pauseResume");
setPauseResumeButtonToPause();
disableButton("clearPreviousObservations");
disableButton("connectDisconnect");
setConnectDisconnectButtonToDisconnect();
}
function onMessage(msg) {
if (msg.error) {
createToaster(msg.error[0], "error");
} else if (msg.filter) {
if (
msg.filter.hasOwnProperty("where") ||
msg.filter.hasOwnProperty("geometry")
) {
createToaster(
"Successfully updated the filter. \nAttribute Filter: " +
(msg.filter.where || "") +
"\nSpatial Filter: " +
(msg.filter.geometry
? typeof msg.filter.geometry === "object"
? JSON.stringify(msg.filter.geometry)
: msg.filter.geometry
: ""),
"success"
);
} else {
createToaster(
"Successfully updated the filter. \n\nAttribute Filter: \nSpatial Filter: ",
"success"
);
}
}
}
function onConnectionStatusChange(connectionStatus) {
/*
* This will fire if the user has clicked the "connect" button after a
* disconnect. This will prevent a bug from occurring where the
* disconnect button can be clicked while still disconnected.
*/
if (isConnectingFromConnectCall && connectionStatus === "connected") {
setConnectDisconnectButtonToDisconnect();
enableButton("connectDisconnect");
enableButton("clearPreviousObservations");
setPauseResumeButtonToPause();
enableButton("pauseResume");
// Reapplying the websocket filters is required since this is a
// new websocket connection
if (document.getElementById("useSendMessageToSocket").checked) {
applyWebsocketFiltersOnReconnect();
}
isConnectingFromConnectCall = false;
}
}
/************************************************
*
* Functions to set and clear spatial/expression filter
*
************************************************/
function applyWhereClause() {
expressionFilter = document.getElementById("expressionFilter").value;
if (streamLayer) {
if (document.getElementById("useSendMessageToSocket").checked) {
streamLayer.sendMessageToSocket({
filter: { where: expressionFilter }
});
} else {
streamLayer.definitionExpression = expressionFilter;
// Adding the "toaster" line message explicitly is required for this case,
// since there will be no websocket message-received event fired, as opposed to the
// sendMessageToSocket case.
createToaster(
"Successfully updated the filter. \nAttribute Filter: " + expressionFilter,
"success"
);
}
}
}
function applySpatialFilter(geometry) {
if (streamLayer) {
if (document.getElementById("useSendMessageToSocket").checked) {
streamLayer.sendMessageToSocket({
filter: { geometry: JSON.stringify(geometry) }
});
} else {
streamLayer.geometryDefinition = geometry;
// Adding the "toaster" line message explicitly is required for this case,
// since there will be no websocket message-received event fired, as opposed to the
// sendMessageToSocket case.
createToaster(
"Successfully updated the filter. \nSpatial Filter: " + JSON.stringify(geometry),
"success"
);
}
}
}
function sketchSpatialFilter() {
sketchViewModel.create("rectangle");
}
function clearSpatialFilter() {
spatialFilter = null;
sketchViewModelLayer.removeAll();
if (streamLayer) {
if (document.getElementById("useSendMessageToSocket").checked) {
streamLayer.sendMessageToSocket({ filter: { geometry: null } });
} else {
streamLayer.geometryDefinition = null;
// Adding the "toaster" line message explicitly is required for this case,
// since there will be no websocket message-received event fired, as opposed to the
// sendMessageToSocket case.
createToaster(
"Successfully updated the filter. \nSpatial Filter: ",
"success"
);
}
}
disableButton("clearSpatialFilter");
}
function drawSpatialFilter(geometry) {
//update map graphic to show current spatial filter
if (geometry) {
sketchViewModelLayer.removeAll();
sketchViewModelLayer.add(
new Graphic({
geometry: geometry,
symbol: {
type: "simple-fill",
style: "none",
outline: {
color: [5, 112, 176],
width: 2
}
}
})
);
}
}
function applyWebsocketFiltersOnReconnect() {
expressionFilter = document.getElementById("expressionFilter").value;
if (expressionFilter) {
applyWhereClause();
}
if (sketchViewModelLayer.graphics.length > 0) {
const graphic = sketchViewModelLayer.graphics.at(0);
applySpatialFilter(graphic.geometry.extent);
}
}
init();
});
</script>
</head>
<body class="flat">
<div id="map"></div>
<div class="collapser">
<i id="collapserIcon" class="flat-chevron-right"></i>
<i id="expanderIcon" class="flat-chevron-left"></i>
</div>
<div id="controls">
<section>
<div class="config-header">
<h2>Layers</h2>
<div class="header-actions-group">
<i id="collapsedPane" class="fa flat-chevron-down"></i>
<i id="expandedPane" class="fa flat-chevron-up"></i>
</div>
</div>
<div class="config-content">
<div class="input-group input-group-full input-group-flex">
<input
type="text"
id="streamServiceUrl"
class="esri-input"
placeholder="Stream layer url"
value="https://geoeventsample1.esri.com/server/rest/services/FAAStream/StreamServer"
/>
</div>
<div class="input-group input-group-full">
<button id="toggleStreamLayer" class="esri-button">
Add Stream Layer
</button>
</div>
<div class="input-group input-group-full">
<button
id="connectDisconnect"
class="esri-button esri-button--disabled"
title="Disconnect from the stream service. New observations will not be streamed until reconnecting."
>
Disconnect
</button>
</div>
<div class="input-group input-group-full">
<button
id="pauseResume"
class="esri-button esri-button--disabled"
title="Pause the connection to stop new observations from being applied until resuming. Data will be received but will not update the layer view until resumed."
>
Pause
</button>
</div>
<div class="input-group input-group-full">
<button
id="clearPreviousObservations"
class="esri-button esri-button--disabled"
>
Clear Previous Observations
</button>
</div>
<div class="input-group input-group-full input-group-flex">
<input
id="expressionFilter"
class="esri-input"
type="text"
placeholder="Attribute Filter (where clause)"
/>
<button id="applyWhereClause" class="esri-button">Apply</button>
</div>
<div class="input-group">
<div class="input-row">
<button id="applySpatialFilter" class="prev-element esri-button">
Apply Spatial Filter
</button>
<button
id="clearSpatialFilter"
class="esri-button esri-button--disabled"
>
Clear Spatial Filter
</button>
</div>
</div>
<div class="input-group input-group-full">
<input
id="useSendMessageToSocket"
class="esri-input"
type="checkbox"
/>
<label
for="useSendMessageToSocket"
title="When unchecked, the app logic will clear previous observations when setting new filters."
>
Use sendMessageToSocket API
</label>
</div>
</div>
</section>
<section>
<div class="config-content">
<div id="panelWrapper">
<h4 id="panelHeading">Messages <span id="clear">clear</span></h4>
<p id="panelMessage"></p>
</div>
</div>
</section>
<section
style="position: absolute; bottom: 0; right: 0; padding: 5px 10px"
>
<button id="switchViewer" class="esri-button">
Switch to Classic Viewer
</button>
</section>
</div>
</body>
</html>