-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsubscriberapi.html
92 lines (82 loc) · 3.38 KB
/
subscriberapi.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
<!DOCTYPE html>
<html>
<head>
<title>Plain HTML base layer switcher</title>
<script type="text/javascript">
var viewer = null;
var el = null;
var _activeMapName = null;
function onBaseLayerChange(el) {
var baseLayerName = el.value;
if (_activeMapName && baseLayerName) {
// This is the raw payload for the redux action to set the active base layer
viewer.dispatch({
type: 'Map/SET_BASE_LAYER',
payload: {
mapName: _activeMapName,
layerName: baseLayerName
}
});
}
}
// Subscriber for the active map name state
var activeMapNameSubscriber = {
name: "active-map-name",
appStateSelector: function(state) {
return state.config.activeMapName
},
onNewState: function(mapName) {
_activeMapName = mapName;
document.getElementById("activeMap").innerHTML = _activeMapName;
}
};
// Subscriber for the base map layer list
var baseLayerSubscriber = {
name: "base-layers",
appStateSelector: function (state) {
if (state.config.activeMapName) {
return state.mapState[state.config.activeMapName].externalBaseLayers.filter(function (bl) { return bl.kind != "UTFGrid" });
}
return undefined;
},
onNewState: function (baseLayers) {
if (baseLayers.length == 0) {
el.innerHTML = "<strong>Your current map has no base layers</strong>";
} else {
el.innerHTML = baseLayers.map(function (baseLayer) {
return "<input type='radio' onchange='onBaseLayerChange(this)' name='base-layer' value='" + baseLayer.name + "' " + (baseLayer.visible === true ? " checked='checked' " : "") + " /> " + baseLayer.name
}).join("<br/>");
}
}
};
window.onload = function () {
//GetViewerInterface is the new API that Task Pane content
//can call to access the new IMapViewer interface
viewer = parent.parent.GetViewerInterface();
el = document.getElementById("switcher");
viewer.addSubscribers([
baseLayerSubscriber,
activeMapNameSubscriber
]);
console.log("Subscribers added")
};
window.onunload = function () {
//Make sure to do this, otherwise the viewer will be holding onto dangling handlers
//as the browser reclaims memory used by this page
viewer.removeSubscribers([
activeMapNameSubscriber.name,
baseLayerSubscriber.name
]);
console.log("Subscribers removed")
};
</script>
</head>
<body>
<h1>Subscriber API</h1>
<p>This page demonstrates usage of the new subscriber API to implement a base layer switcher</p>
<p>This example is best viewed with a Flexible Layout configured for multiple maps, with at least one of these maps having multiple external base layers</p>
<p>Active map is: <strong id="activeMap"></strong></p>
<div id="switcher">
</div>
</body>
</html>