1
- const toggleModeEl = document . getElementById ( "toggle-mode" ) ;
2
- const mapEl = document . querySelector ( "arcgis-map" ) ;
3
- const darkModeCss = document . getElementById ( "jsapi-mode-dark" ) ;
4
- const lightModeCss = document . getElementById ( "jsapi-mode-light" ) ;
5
- const liveRegion = document . getElementById ( "liveRegion" ) ;
6
- const searchEl = document . getElementById ( "search-el" ) ;
7
- const expandEl = document . getElementById ( "expand-el" ) ;
8
-
9
- let mode = "light" ;
10
-
11
- toggleModeEl . addEventListener ( "click" , handleModeChange ) ;
12
-
13
-
14
- // Initialize the mutation observer
15
- // Resource: https://developers.arcgis.com/javascript/latest/watch-for-changes/#using-a-mutation-observer
16
- const observer = new MutationObserver ( ( mutations , observer ) => {
17
- for ( let mutation of mutations ) {
18
- // Mutation observer change message
19
- const mutationMessage = `MutationObserver: ${ mutation . attributeName } has changed to ${ mutation . target [ mutation . attributeName ] } ` ;
20
- console . log ( mutationMessage ) ;
21
- // Set focus on the arcgis-search if the component is expanded
22
- // Else set focus on the arcgis-expand
23
- if ( mutation . target [ mutation . attributeName ] == true ) {
24
- searchEl . focusSearch ( ) ;
25
- } else {
26
- const expandEls = document . querySelectorAll ( ".esri-expand__toggle > calcite-action" ) ;
27
- expandEls [ 0 ] . setFocus ( ) ;
28
- }
1
+ require ( [
2
+ "esri/core/reactiveUtils"
3
+ ] , ( reactiveUtils ) => {
4
+
5
+ const toggleModeEl = document . getElementById ( "toggle-mode" ) ;
6
+ const mapEl = document . querySelector ( "arcgis-map" ) ;
7
+ const darkModeCss = document . getElementById ( "jsapi-mode-dark" ) ;
8
+ const lightModeCss = document . getElementById ( "jsapi-mode-light" ) ;
9
+ const searchEl = document . getElementById ( "search-el" ) ;
10
+ const expandEl = document . getElementById ( "expand-el" ) ;
11
+
12
+ let mode = "light" ;
13
+
14
+ toggleModeEl . addEventListener ( "click" , handleModeChange ) ;
15
+
16
+ // reactiveUtils to watch for when the popup is opened and closed
17
+ // Resource: https://developers.arcgis.com/javascript/latest/watch-for-changes/#watch-for-changes-in-the-api
18
+ mapEl . addEventListener ( "arcgisViewReadyChange" , ( ) => {
19
+ reactiveUtils . watch ( ( ) => mapEl . view . popup . visible , ( visible ) => {
20
+ if ( mapEl . view . popup . visible ) {
21
+ setTimeout ( ( ) => {
22
+ mapEl . view . popup . focus ( ) ;
23
+ } , 100 ) ;
24
+ } else {
25
+ searchEl . focusSearch ( ) ;
26
+ }
27
+ }
28
+ ) ;
29
+ } ) ;
30
+
31
+
32
+ // Initialize the mutation observer
33
+ // Resource: https://developers.arcgis.com/javascript/latest/watch-for-changes/#using-a-mutation-observer
34
+ const observer = new MutationObserver ( ( mutations , observer ) => {
35
+ for ( let mutation of mutations ) {
36
+ // Mutation observer change message
37
+ const mutationMessage = `MutationObserver: ${ mutation . attributeName } has changed to ${ mutation . target [ mutation . attributeName ] } ` ;
38
+ console . log ( mutationMessage ) ;
39
+ // Set focus on the arcgis-search if the component is expanded
40
+ // Else set focus on the arcgis-expand
41
+ if ( mutation . target [ mutation . attributeName ] == true ) {
42
+ searchEl . focusSearch ( ) ;
43
+ } else {
44
+ const expandEls = document . querySelectorAll ( ".esri-expand__toggle > calcite-action" ) ;
45
+ expandEls [ 0 ] . setFocus ( ) ;
46
+ }
47
+ }
48
+ } ) ;
49
+
50
+ // Start observing the arcgis-expand's "expanded" attribute
51
+ observer . observe ( expandEl , {
52
+ attributeFilter : [ "expanded" ]
53
+ } ) ;
54
+
55
+ function handleModeChange ( ) {
56
+ mode = mode === "dark" ? "light" : "dark" ;
57
+ const isDarkMode = mode === "dark" ;
58
+ darkModeCss . disabled = ! isDarkMode ;
59
+ lightModeCss . disabled = isDarkMode ;
60
+ toggleModeEl . icon = isDarkMode ? "moon" : "brightness" ;
61
+ document . body . className = isDarkMode ? "calcite-mode-dark" : "" ;
62
+
63
+ document . querySelectorAll ( `.calcite-mode-${ isDarkMode ? "light" : "dark" } ` ) . forEach ( node =>
64
+ node . classList . replace ( `calcite-mode-${ isDarkMode ? "light" : "dark" } ` , `calcite-mode-${ mode } ` )
65
+ ) ;
29
66
}
30
- } ) ;
31
-
32
- // Start observing the arcgis-expand's "expanded" attribute
33
- observer . observe ( expandEl , {
34
- attributeFilter : [ "expanded" ]
35
- } ) ;
36
-
37
-
38
- function handleModeChange ( ) {
39
- mode = mode === "dark" ? "light" : "dark" ;
40
- const isDarkMode = mode === "dark" ;
41
- darkModeCss . disabled = ! isDarkMode ;
42
- lightModeCss . disabled = isDarkMode ;
43
- toggleModeEl . icon = isDarkMode ? "moon" : "brightness" ;
44
- document . body . className = isDarkMode ? "calcite-mode-dark" : "" ;
45
-
46
- document . querySelectorAll ( `.calcite-mode-${ isDarkMode ? "light" : "dark" } ` ) . forEach ( node =>
47
- node . classList . replace ( `calcite-mode-${ isDarkMode ? "light" : "dark" } ` , `calcite-mode-${ mode } ` )
48
- ) ;
49
- }
67
+
68
+ } ) ;
0 commit comments