Layer Toggle Button Widget Version 2.14 - 01/13/2020

198580
178
08-01-2016 09:43 AM
Labels (1)

Layer Toggle Button Widget Version 2.14 - 01/13/2020

This is just a simple widget to toggle a configured layer(s) on and off using a button. The layer(s) can be a whole map service layer or a specific sublayer(s) in a map service. This widget will sync with the LayerList widget. You can add multiple instances of this widget to onscreen place holders or in a widget controller like the Header controller in the Foldable theme.

Live Preview Site

List of the latest enhancements and changes:

  1. Fixed issue with group layers in 2.12 WAB.

List of Previous changes:

  1. Fixed issue with basemap being toggled when exclusive mode is used in 2.12 WAB.
  2. Full support for using this widget in the Launchpad themes Anchorbar controller is now supported.
  3. There is now an exclusive mode for the layer toggle. Meaning that all other layers will be toggled off if the layer is exclusive. Caution should be used when choosing this mode.
  4. There is now a zoom to layers extent option for the layer(s) toggles.
  5. There is now a event that can be listened for when a layer is toggled on. The event name is 'toggleChanged'.
  6. Widget now indicates the toggle state of the layer using the background color of the button.
  7. You can follow the directions in the downloads readMe.txt to set the initial state of the button to be toggled on.
  8. Now the widget support toggling multiple layers.
Labels (1)
Attachments
Comments

Katie,

   I think the issue may be that you do not need to check the "FMGlassType" parent node just the "Glass Type" node. Try that and let me know.

Hi Robert, 

Thank you so much for the quick response. That did do the trick!

I didn't even think to try that, I had just assumed that the layer wouldn't turn out without the parent node checked. Glad it works now!

Thanks again!

Hi Tim,

I know it's been awhile since this post but I am trying to tackle the exact same problem. I'm using the Layer toggle widget in the AnchorBarController of the Launchpad theme and can't figure out how to visualize which button is active. Did you ever find a solution? 

Thanks!

Katie

Katie,

Hi there. Sorry I actually gave up on this avenue after spending way too much time on it. I was mostly trying to avoid using the basemap gallery widget to toggle aerial layers since it is coordinate system dependent. I eventually reprojected all of our aerials into Web Mercator and used the gallery. I do still prefer this tool but just couldn’t spend any more time on it. Good luck!

Tim

Katie and Tim,

   I will be releasing the 2.11 version of this widget that is designed to work with the Launchpad theme Anchorbar controller. when you add this widget to the Anchorbar it will properly show the widgets state  and a tooltip for what click on the widget will do to the layers visible state. The one problem is the initial state of the widget reflecting the visible state of the layer on startup. If the configured layer is on by default then you just set the widget to open at start. The issue is that only one widget can be opened at start so if you have another layer toggle button and it needs to show as on/open then you are out of luck.

Here is a snapshot of the widget indication of layer state and tooltip showing what clicking the button will do.

Thanks for the heads up! This is great news.

I think the Layer Toggle Widget works really well with the Launchpad Theme. I'm trying to use multiple instances of the toggle widget in the AnchorBarController instead of a having a layer widget, and getting the widget to reflect which layer is active was the only hurdle. I really appreciate your work and constant improvements to this widget! Looking forward to the latest release.

Robert,

As always great work! I may yet make use of this widget. Thanks for the info. Look forward to trying it out.

Hello! This looks to be a very promising widget and will fill a great need for my application. I was able to add it to my portal environment, and then to my web application. I was also able to configure the widget to connect it to the layer I would like to toggle. However, when I click the widget it simply spins (circle moves around in the button) and then is stuck. No layers turn on/off and it does not stop spinning. Any ideas what the problem could be? Please let me know if you would like any other details. Thank you!!!

Arthur,

   What does your browsers web console say the issue is?

Thank you for responding! I could easily have messed something up in how I setup the widget. Do I need to change any code? Or just copy/paste the folder onto my web server?

I get 2 errors in the Console of my web browser (Chrome):

init.js:35 GET https://<we=server>/portal/apps/webappbuilder/stemapp/jimu.js/LayerStructure.js?wab_dv=2.6 net::ERR_ABORTED 404 (Not Found)

init.js:36 Error: scriptError                                 at g (init.js:10)
at HTMLScriptElement.<anonymous> (init.js:35)

Arthur,

  Your install is fine. What version of Portal are you using? If the Widget can not find LayerStructure.js that means you are trying to use this widget with an older version of Portal.

i was worried that could be it! we are using arcgis enterprise version 10.6. is that too old? is there a way to get a previous version of the widget that would work? i really only want to use the basic functionality: turn one layer on/off.

Arthur,

   Portal 10.6 uses WAB 2.6. So here is the old 2.6 version of LayerToggleButton widget.

Download 2.6 version

That did the trick!! Thank you so much for your work on this widget and your help with getting it working for me!!

Quick question. Is there a way to programatically change the icon image when toggling a layer? Something like this?

domClass.add(this.parentContainer, "jimu-state-selected");
this.icon = 'https://i.stack.imgur.com/Rwkrv.jpg');

That is possible.

var widg = registry.byId(parentWid);
html.setAttr(widg.iconNode, 'src', 'loading_circle.gif');

Worked like a charm.

var widg = registry.byId('dijit__WidgetBase_0');
html.setAttr(widg.iconNode, 'src', './widgets/LayerToggleButton/images/icon2.png');

Thanks!

M

When I set the property "isExclusive" for a widget in a Launchpad theme, I found out that the other LayerToggleButtons were not closed in the GUI although the layer disappeared in the map.

To fix this, I rewrote the onLaunchpadOpen() function and added another function:

      closeWidget: function(id) {
        this.abc = WidgetManager.getInstance().getWidgetsByName("AnchorBarController")[0];
        
          this.abc.widgetManager.closeWidget(id);
          this.abc._removeFromOpenedIds(id);
      },

     onLaunchpadOpen: function(){
        var lObjs = [];
        array.map(this.toggleLayerIds, lang.hitch(this, function(id){
          var lyrNode = this.layerStructure.getNodeById(id);
          lObjs.push(lyrNode);
        }));
        
        if(!lObjs[0].isToggledOn() && this.isExclusive){
          this.layerStructure.traversal(lang.hitch(this, function(layerNode) {
            layerNode.hide();
          }));
        }
        
        if (this.isExclusive) {
            var toggleBtnArr = query("div[data-widget-name='LayerToggleButton']");
            toggleBtnArr.forEach(lang.hitch(this, function(node){
              var parentWid = html.getAttr(node, 'widgetId');
              if (parentWid){
                  if (this.id !== node.id) {
                     this.closeWidget(parentWid);
                     return
                }
              }
            }));
        }
        
        array.map(lObjs, lang.hitch(this, function(lObj, index){
          onOff = (lObj.isToggledOn()) ? 'On' : 'Off';
          if(this.parentContainer && this.originalTitle){
            html.setAttr(this.parentContainer.tooltipDialog, 'content', this.originalTitle + ': ' + onOff);
          }
          if(onOff === 'Off' && index === 0 && lObj._layerInfo && lObj._layerInfo.layerObject && lObj._layerInfo.layerObject.fullExtent){
            if(this.zoomToLayer){
              this.map.setExtent(lObj._layerInfo.layerObject.fullExtent.expand(1.2));
            }
          }
          if(!lObj.isVisible()){
            lObj.show();
            topic.publish('toggleChanged', lObj._layerInfo._visible, lObj);
          }
        }));
      },

Luc,

   Thanks. I forgot to handle that. I appreciate you sharing your code.

Thanks for this update! This is a great widget. I do have one more request though. After clicking the toggle, what would be the best approach to having a loading layer type progress indicator appear until the toggled layer appears?

Susan,

Did you ever find a remedy to this? I find the same thing happening. The toggle changes each layer that it is set for, to opposite of how they are set in the layers list. It would be really neat if the toggle, when clicked, would just ensure that all of the layers are turned on (or off when it is disabled.) Still, a nice widget as always.

I need to toggle between 3 layers. Can this toggle widget accomplish this?

Yes

Is the toggle button just clicked 3 times to cycle between all layers?

No you add three instances of the widget.

Is it easy to change this? My intended workflow is:

1. see first layer

2. click toggle see 2nd layer

3. click toggle and see 3rd layer

Nope that is not a supported workflow or an easy change to the code.

Have another idea other than having a few toggle buttons?

Toggling makes my basemap disappear. Do you have any idea why that would happen? It's only when I have the checkbox for the layer being exclusive checked. Thanks.

Earl,

   You must not be using a standard basemap then. How is your basemap added to your map?

I changed the basemap in the web map from Bing maps to the World Imagery and it still gave me the same problem.

Never seen this before. When you configured the Layer Toggle Widget is the list of map layer is your basemap listed there?

In WAB I go to where the widget is configured and under the part where you choose the layers to be toggled, there is no basemap option available. I use IE and Portal 10.4. Maybe these could cause issues?

Earl,

  When you say Portal 10.4 does that mean you are using a WAB app coming from your Portal?

The web map that I use in AppBuilder is from Portal and we will have the production application on Portal. I have to sign in to Portal when I start WAB.

That is likely the issue then. Portal 10.4 uses WAB 1.4

https://enterprise.arcgis.com/en/portal/latest/use/about-versions.htm

This widget is built for WAB 2.8 or higher, meaning Portal 10.6.1 or greater.

I see. I'll keep that in mind in the future. Thanks Robert!

I'm using WAB 2.12 and seeing the same behavior.  The basemap is not in the widget settings to be selected.  When Exclusive is true, the basemap is removed along with the layers that should go. Downloaded Layer Toggle Button widget on 8/16/2019.

Any ideas?

Strange... It never use to do that. OK the fix will hopefully be out for that today.

David,

  Version 2.12 now available.

There appears to be a bug in this version. If I click off the first toggle (Traffic Camera layers), then toggle it on again, all the other visible layers turn off. This doesn't happen with the other toggles.

Ken,

   That is because that toggle button is setup to use the "Exclusive" mode of this widget.

Ah, I overlooked that option. Thanks!

Hi Robert,

thank you for this great widget. I would really like to use it but I am getting error while clicking at the widget icon.

My layer structure is as follows:

Map service

   floor1 (group)

         layer1

         layer2

         layer3

   floor2 (group)

         layer1

         layer2

         layer3

   floor3 (group)

         layer1

         layer2

         layer3

I want to obviously to exclusivly toggle the floor groups. But when I toggle it, it switch off all the sublayers in group.

If I set to toggle even the sublayers together with the group, I get the error below.

I can observe this behaviour in Portal WAB (10.7.1) as well as in WAB DE (2.12, 2.13).

Console error:

fail to open widget LayerToggleButton. p@https://js.arcgis.com/3.29/init.js:121:447
dojo/on/</l.parse@https://js.arcgis.com/3.29/init.js:124:217
l@https://js.arcgis.com/3.29/init.js:123:165
dojo/on/</l.once@https://js.arcgis.com/3.29/init.js:123:386
setBusyIndicator@https://pcjsarata.arcdata.cz/floor_switcher/widgets/LayerToggleButton/Widget.js?wab_dv=2.13:214:14
toggleLayer/<@https://pcjsarata.arcdata.cz/floor_switcher/widgets/LayerToggleButton/Widget.js?wab_dv=2.13:303:18
hitch/<@https://js.arcgis.com/3.29/init.js:64:337
map@https://js.arcgis.com/3.29/init.js:72:238
toggleLayer@https://pcjsarata.arcdata.cz/floor_switcher/widgets/LayerToggleButton/Widget.js?wab_dv=2.13:300:15
onOpen@https://pcjsarata.arcdata.cz/floor_switcher/widgets/LayerToggleButton/Widget.js?wab_dv=2.13:267:16
openWidget@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/WidgetManager.js?wab_dv=2.13:549:18
_postWidgetStartup@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/WidgetManager.js?wab_dv=2.13:1158:12
_hitchArgs/<@https://js.arcgis.com/3.29/init.js:64:15
c@https://js.arcgis.com/3.29/init.js:120:235
openWidget@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/WidgetManager.js?wab_dv=2.13:540:18
getWidgetMarginBox@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/WidgetManager.js?wab_dv=2.13:259:12
getOffPanelWidgetPosition@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/OnScreenWidgetIcon.js?wab_dv=2.13:190:37
switchToOpen/<@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/OnScreenWidgetIcon.js?wab_dv=2.13:146:35
hitch/<@https://js.arcgis.com/3.29/init.js:64:337
l@https://js.arcgis.com/3.29/init.js:108:249
k@https://js.arcgis.com/3.29/init.js:108:175
e/this.resolve@https://js.arcgis.com/3.29/init.js:110:268
loadWidget/</</<@https://pcjsarata.arcdata.cz/floor_switcher/jimu.js/WidgetManager.js?wab_dv=2.13:131:21
hitch/<@https://js.arcgis.com/3.29/init.js:64:337

Do you have any idea what is going on?

Thank you,

Jan Sarata

Jan,

   What version of the widget are you using? The error line numbers are not matching up to the latest version of this widgets code...

Hi Robert,

thank you for your response.

I downloaded 2.12 version attached to your comment from 28 August of 2019 in this thread.

The console error appears only when I configure to toggle the layers (layer1, layer2, layer3) within the group (floor).

If I configure to toggle only the groups (floors) it is working fine except it turns off the sublayers within the group. So I end up with correct group (floor) turned on but all its sublayers are off.

Jan

Jan,

   The widget will toggle layer exactly as they are configured in the widget. So if you want to toggle group1 then it has to be checked in the widgets settings but if you have nothing in that group checked in the widgets settings then those sub layer will be turned off in that group if they are currently on. So if you want group1 to toggle and layer1 and layer2 toggle then you have to check each of those in the widgets settings.

Hi Robert,

thanks for your prompt response.

I think I followed the scenario you suggest -> see the screenshot attached

But then when I click the toggle button I am getting the console error mentioned in my previous comment.

Jan,

   OK, I will look into this further.

Jan,

   OK. I see that the issue is the group layer. I will be creating a fix for this and releasing a new version soon.

Version history
Last update:
‎08-01-2016 09:43 AM
Updated by: