<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Layerlist selected item becomes visible watch in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553976#M85999</link>
    <description>&lt;P&gt;I didn't even think about using at allLayers. Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's how it turned out. Works just right.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;                // Watching for changes to the visible year layers in the Map 
                reactiveUtils.watch(
                  () =&amp;gt; view.map.allLayers.filter((layer) =&amp;gt; layer.visible),
                  (newVisibleLayers, oldVisibleLayers) =&amp;gt; {
                    const added = newVisibleLayers.filter((layer) =&amp;gt; !oldVisibleLayers.includes(layer));
                    const addedIndices = added.map((layer) =&amp;gt; map.allLayers.findIndex((l) =&amp;gt; l === layer));
                    // Destructure with a default value
                    const [yearChosenIndex = null] = addedIndices;
                    chosenPriorYearMapLayer = [];
                    if (yearChosenIndex &amp;gt;= 0)
                      chosenPriorYearMapLayer.push(yearChosenIndex);    
                  }
                );&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 30 Oct 2024 20:32:26 GMT</pubDate>
    <dc:creator>GregoryBologna</dc:creator>
    <dc:date>2024-10-30T20:32:26Z</dc:date>
    <item>
      <title>Layerlist selected item becomes visible watch</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553421#M85993</link>
      <description>&lt;P&gt;I have visibility watches on a&amp;nbsp;&lt;SPAN&gt;layerList, but I cannot get the watches to hit on the items in the layerList when an item is selected.&amp;nbsp;&lt;/SPAN&gt;I want to get the &lt;STRONG&gt;visible item index&lt;/STRONG&gt; when the item is chosen in item.layer.layers.items.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="GregoryBologna_1-1730225342531.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118369i762F6E495422517E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="GregoryBologna_1-1730225342531.png" alt="GregoryBologna_1-1730225342531.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="GregoryBologna_0-1730224985249.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118366i3FF6D6DCFFCA1EA2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="GregoryBologna_0-1730224985249.png" alt="GregoryBologna_0-1730224985249.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can output the items in a forEach inside the&amp;nbsp;&lt;SPAN&gt;listItemCreatedFunction, but I need to use a watch.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;layerListExpand = new Expand({
content: (layerList = new LayerList({
  view: view,
  listItemCreatedFunction: (event) =&amp;gt; {
	let item = event.item;
	let key = item.title.toUpperCase().replace(/ /g, '');
	if (item.layer.type === 'group' || item.layer.type === 'tile') {
	  if (key === 'AERIALS') {

		item.layer.layers.items?.forEach((item) =&amp;gt; {
		  console.log(item.title, item.visible);
		});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;reactiveUtils.when doesn't get hit for item.layer.layers.item&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;reactiveUtils.when(
  () =&amp;gt; item.layer.layers.items,  // I was selected and visible,
  (visible) =&amp;gt; {
     // do something here
	if (visible) console.log('visible');
  }
);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 18:18:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553421#M85993</guid>
      <dc:creator>GregoryBologna</dc:creator>
      <dc:date>2024-10-29T18:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: Layerlist selected item becomes visible watch</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553563#M85994</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/193359"&gt;@GregoryBologna&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P data-unlink="true"&gt;reactiveUtils &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-core-reactiveUtils.html#when" target="_self"&gt;when&lt;/A&gt; will only check for truthy values.&amp;nbsp; You would want to use a &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-core-reactiveUtils.html#watch" target="_self"&gt;watch&lt;/A&gt;.&amp;nbsp;&amp;nbsp; items isn't part of the public API.&amp;nbsp; You might run into issues.&amp;nbsp; This will also create a watch and a handle for each list item maybe unnecessarily?&amp;nbsp; Not sure on the use case here exactly.&lt;/P&gt;
&lt;P data-unlink="true"&gt;What I would suggest is set up a single watch on &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-Map.html#allLayers" target="_self"&gt;map.allLayers&lt;/A&gt; something like this.&amp;nbsp; You could expand the filter logic to include only certain layers or layer types if necessary.&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;// Watching for changes to the visible layers in the Map
reactiveUtils.watch(
  () =&amp;gt; view.map.allLayers.filter((layer) =&amp;gt; layer.visible),
  (newVisibleLayers, oldVisibleLayers) =&amp;gt; {
    const added = newVisibleLayers.filter(
      (layer) =&amp;gt; !oldVisibleLayers.includes(layer)
    );
    const removed = oldVisibleLayers.filter(
      (layer) =&amp;gt; !newVisibleLayers.includes(layer)
    );
    added.forEach((layer) =&amp;gt; console.log(layer.title, "is visible"));
    removed.forEach((layer) =&amp;gt; console.log(layer.title, "is not visible"));
  }
);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2024 21:24:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553563#M85994</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2024-10-29T21:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Layerlist selected item becomes visible watch</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553976#M85999</link>
      <description>&lt;P&gt;I didn't even think about using at allLayers. Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's how it turned out. Works just right.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;                // Watching for changes to the visible year layers in the Map 
                reactiveUtils.watch(
                  () =&amp;gt; view.map.allLayers.filter((layer) =&amp;gt; layer.visible),
                  (newVisibleLayers, oldVisibleLayers) =&amp;gt; {
                    const added = newVisibleLayers.filter((layer) =&amp;gt; !oldVisibleLayers.includes(layer));
                    const addedIndices = added.map((layer) =&amp;gt; map.allLayers.findIndex((l) =&amp;gt; l === layer));
                    // Destructure with a default value
                    const [yearChosenIndex = null] = addedIndices;
                    chosenPriorYearMapLayer = [];
                    if (yearChosenIndex &amp;gt;= 0)
                      chosenPriorYearMapLayer.push(yearChosenIndex);    
                  }
                );&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 20:32:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/layerlist-selected-item-becomes-visible-watch/m-p/1553976#M85999</guid>
      <dc:creator>GregoryBologna</dc:creator>
      <dc:date>2024-10-30T20:32:26Z</dc:date>
    </item>
  </channel>
</rss>

