-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.html
174 lines (147 loc) · 4.3 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Load visualizations via a webmap</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.3/esri/css/main.css">
<script src="https://js.arcgis.com/4.3/"></script>
<script>
require([
"esri/WebMap",
"esri/views/MapView",
"esri/widgets/Legend",
"esri/widgets/LayerList",
"dojo/domReady!"
], function(WebMap, MapView, Legend, LayerList) {
var map = new WebMap({
portalItem: {
id: "8bcfd58b039a4477a0eb734fe6c8d4fe"
}
});
var view = new MapView({
container: "viewDiv"
});
var rendererInfos;
map.load().then(function(){
rendererInfos = map.layers.map(function(layer){
return {
renderer: layer.renderer.clone(),
title: layer.title
};
});
rendererInfos.reverse();
var visibleLayer = map.layers.find(function(layer){
layer.title = map.portalItem.title;
return layer.visible;
});
map.layers.removeAll();
map.layers.add(visibleLayer);
view.map = map;
return view;
}).then(addWidgets);
function addWidgets(view){
var layerList = new LayerList({
view: view,
createActionsFunction: createLayerListActions
});
var legend = new Legend({
view: view
});
view.ui.add(layerList, "top-right");
view.ui.add(legend, "bottom-left");
layerList.on("trigger-action", fireAction);
}
function createLayerListActions(evt){
if(evt.item.title = map.portalItem.title){
evt.item.actionsOpen = true;
return rendererInfos.map(function(rendererInfo){
var actions = [];
actions.push({
title: rendererInfo.title,
className: "esri-icon-maps",
id: "change-renderer",
rendererTitle: rendererInfo.title
});
var renderer = rendererInfo.renderer;
var hasOpacityVV = renderer && renderer.visualVariables.some(function(variable){
return variable.type === "opacity";
});
if (hasOpacityVV){
actions.push({
title: "Toggle opacity",
className: "esri-icon-environment-settings",
id: "toggle-opacity",
rendererTitle: rendererInfo.title
});
}
return actions;
}).toArray();
}
}
function fireAction(evt){
var actionId = evt.action.id;
var actionTitle = evt.action.title;
if (evt.action.id === "change-renderer"){
toggleRenderer(actionTitle);
} else if (evt.action.id === "toggle-opacity"){
toggleOpacity(evt.action.rendererTitle);
} else {
console.log("Couldn't find a matching action.");
}
}
function toggleOpacity(title){
var layer = view.map.layers.getItemAt(0);
var matchingRendererInfo = rendererInfos.find(function(info){
return info.title === layerRendererTitle && info.title === title;
});
if(!matchingRendererInfo){
return;
}
var renderer = layer.renderer.clone();
if (hasOpacityVV(renderer)){
var opacityIndex;
// findIndex isn't IE compatable
renderer.visualVariables.forEach(function(variable, index){
if (variable.type === "opacity"){
opacityIndex = index;
}
});
renderer.visualVariables.splice(opacityIndex,1);
} else {
renderer = matchingRendererInfo.renderer;
}
layer.renderer = renderer;
}
function hasOpacityVV (renderer){
return renderer && renderer.visualVariables.some(function(variable){
return variable.type === "opacity";
});
}
var layerRendererTitle;
function toggleRenderer(title){
var matchingInfo = rendererInfos.find(function(info){
return info.title === title;
});
var renderer = matchingInfo.renderer.clone();
var layer = view.map.layers.getItemAt(0);
layer.renderer = renderer;
layerRendererTitle = matchingInfo.title;
}
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>