This is a major update to my previous add/remove layers widget. In the old version, map layers would not be recognized by Experience Builder as valid datasources. In this version, FeatureLayers added to this widget will be converted to datasources and should operate within the Experience Builder framework as if they were added from the official Add Data Widget. This means you will have access to the Add To Table and Export Actions, the Select Widget and the Edit Widget. (I have not tested with the Edit Widget, but it should work.) Note: Any widget which requires setup in their Settings Panel to operate will not be able to pick up these layers.
How it works:
Layers will be added to the map starting at the highest available index of your map layer. FeatureLayers will be converted to a datasource and re-added to the map with the prefix 'custom_' added to their ids. (If you need to access these layers for other custom widgets, their permanent id will be `custom_${layer.mapId}`. MapImageLayers will not have their ids changed.) The originally added FeatureLayer will then be removed from the map. This adding and removing layers and the dependency on the order promises are resolved will result in a pseudo-random layer order. After the amount of delay designated in the Settings Panel, the layers will sort themselves based on the order designated in the groups starting at the index number also designated in the Settings Panel. If the layers are loaded as single layers, they will not re-sort.
If your user has loaded their layers with the layer groups and they use the drag-and-drop reordering in the Map Layers Widget, the Default Layer Order button will instantly restore the order designated by your groups.
How to use:
Open /src/components/layers.ts and replace the dummy data with the layers and groups you want to use. I recommend setting your Map Widget to a WebMap with only a basemap and no layer data and loading all of your data from this widget. By default, groups[0] will appear selected on load, you may wish to set this to null or a different group. If you chose to have a group selected on load, each layer in that group should be set to activeOnLoad: true. Every layer must be designated as a FeatureLayer or a MapImageLayer by using the layerType property and setting it to layerType.feature or layerType.mapImage. In the Settings Panel, use the dropdown menu to connect this widget to a Map Widget.
If you are loading all of your operational layers from this widget or you want your layers added below any WebMap layers, set the Start Adding Layers At Index value to 0. Higher numbers will cause your layers to paint on top of any WebMap layers with a lower index. Out of range values could cause unexpected results. Set the Reordering Delay to slightly longer than you expect your most complicated group change (largest amount of layers and/or data being added/removed at once) to take. The Default Layer Order button can be switched off if you do not want to show this option to your users.
