generated from EsriDevEvents/ds-2023-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
164 lines (144 loc) · 4.92 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
157
158
159
160
161
162
163
164
/** Declare element variables */
const toggleModeEl = document.getElementById("toggle-mode");
const toggleModalEl = document.getElementById("toggle-modal");
const modalEl = document.getElementById("modal");
const chipGroupEl = document.getElementById("chips");
const darkModeCss = document.getElementById("jsapi-mode-dark");
const lightModeCss = document.getElementById("jsapi-mode-light");
/** Declare expected type values */
const allTypes = [
"National park or forest",
"State park or forest",
"Regional park",
"County park",
"Local park",
];
/** Declare colors to assign to each type - these will also be used in CSS */
const typeColors = ["#c66a4a", "#7a81ff", "#3cccb4", "#0096ff", "#f260a1"];
/** Create a simple state object and set the default filter to allTypes */
const appState = {
types: allTypes,
mode: "light",
};
/** Maps SDK */
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/widgets/Home",
"esri/widgets/Locate",
], (Map, MapView, FeatureLayer, Home, Locate) =>
(async () => {
toggleModeEl.addEventListener("click", () => handleModeChange());
toggleModalEl.addEventListener("click", () => handleModalChange());
chipGroupEl.addEventListener("calciteChipGroupSelect", (event) =>
handleChipChange(event)
);
const layer = new FeatureLayer({
url: "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Parks/FeatureServer/0",
outFields: ["*"],
popupTemplate: createPopupTemplate(),
minScale: 0,
maxScale: 0,
renderer: {
type: "unique-value",
field: "FEATTYPE",
uniqueValueInfos: assignColorsToTypes(),
},
});
const map = new Map({
basemap: "topo-vector",
layers: [layer],
});
const view = new MapView({
container: "viewDiv",
map: map,
center: [-120, 45],
zoom: 4,
});
/** Ensure smaller features are visible on top of larger ones */
layer.orderBy = {
field: "SQMI",
order: "ascending",
};
const locateBtn = new Locate({ view: view });
const homeWidget = new Home({ view: view });
view.ui.add(homeWidget, "top-left");
view.ui.add(locateBtn, { position: "top-left" });
function createPopupTemplate() {
return {
title: "{NAME}",
content: "{SQMI} square miles, jurisdiction: {FEATTYPE}",
};
}
/** Assign the color values to expected type values */
function assignColorsToTypes() {
let uniqueValueInfos = [];
allTypes.forEach((type, index) => {
uniqueValueInfos.push({
value: type,
symbol: {
type: "simple-fill",
color: typeColors[index],
outline: { color: typeColors[index] },
},
});
});
return uniqueValueInfos;
}
/** Create the chips to represent each jurisdiction */
function createFilterChips() {
allTypes.forEach((item) => {
const chip = document.createElement("calcite-chip");
const simpleName = item.split(" ")[0];
chip.innerText = simpleName;
chip.value = item;
chip.selected = true;
chip.id = `chip-type-${simpleName.toLowerCase()}`;
chipGroupEl.appendChild(chip);
});
}
function createWhereArguments() {
let args = [];
const typesActive = appState.types.length > 0;
const featureTypes = typesActive ? appState.types : allTypes;
featureTypes.forEach((j) => args.push(`'${j}'`));
const filtered = ` AND (FEATTYPE = ${args.join(" OR FEATTYPE = ")})`;
const unfiltered = ` AND FEATTYPE != ${args.join(" AND FEATTYPE != ")}`;
const argString = typesActive ? filtered : unfiltered;
return argString;
}
async function handleLayerFilter() {
await view.whenLayerView(layer).then((featureLayerView) => {
const where = `NAME IS NOT NULL${createWhereArguments()}`;
featureLayerView.featureEffect = {
filter: { where },
excludedEffect: "opacity(30%) grayscale(100%)",
includedEffect: "opacity(100%)",
};
});
}
function handleChipChange(event) {
let items = [];
event.target.selectedItems.forEach((item) => items.push(item.value));
appState.types = items;
handleLayerFilter();
}
function handleModeChange() {
appState.mode = appState.mode === "dark" ? "light" : "dark";
const isDarkMode = appState.mode === "dark";
darkModeCss.disabled = !darkModeCss.disabled;
lightModeCss.disabled = !lightModeCss.disabled;
map.basemap = isDarkMode ? "streets-night-vector" : "topo-vector";
toggleModeEl.icon = isDarkMode ? "moon" : "brightness";
document.body.className = isDarkMode ? "calcite-mode-dark" : undefined;
}
function handleModalChange() {
modalEl.open = !modalEl.open;
}
function initializeApp() {
createFilterChips();
handleLayerFilter();
}
initializeApp();
})());