//Create a menu with a list of operational layers. Each menu item contains a check box //that allows users to toggle layer visibility. function addLayerList(layers, operational) { var layerList = buildLayerVisibleList(layers, operational); if (layerList.length > 0) { //create a menu of layers //layerList.reverse(); var menu = new dijit.Menu({ id: 'layerMenu' }); menu.addChild(new dijit.CheckedMenuItem({ label: "Elevation", checked: false, onChange: function () { var e1 = map.getLayer("Elevation"); e1.setVisibility(!e1.visible); var e2 = map2.getLayer("Elevation"); e2.setVisibility(!e2.visible); } })); menu.addChild(new dijit.CheckedMenuItem({ label: "Water Depth", checked: true, onChange: function (checked) { for (var i = 0; i < SBDLayers.length; i++) { SBDLayers.setVisibility(checked); var e2 = map2.getLayer(SBDLayers.id); e2.setVisibility(checked); } } })); menu.addChild(new dijit.CheckedMenuItem({ label: "Dune Susceptibility", checked: true, onChange: function (checked) { for (var i = 0; i < SBDELayers.length; i++) { SBDELayers.setVisibility(checked); var e2 = map2.getLayer(SBDELayers.id); e2.setVisibility(checked); } } })); dojo.forEach(layerList, function (layer) { menu.addChild(new dijit.CheckedMenuItem({ label: layer.title, checked: layer.visible, onChange: function (checked) { if (!operational) { var e2 = map2.getLayer(layer.layer.id); e2.setVisibility(checked); layer.layer.setVisibility(!layer.layer.visible); } else if (layer.layer.featureCollection) { //turn off all the layers in the feature collection even //though only the main layer is listed in the layer list dojo.forEach(layer.layer.featureCollection.layers, function (layer) { layer.layerObject.setVisibility(!layer.layerObject.visible); }); } else { layer.layer.setVisibility(!layer.layer.visible); } } })); }); var button = new dijit.form.DropDownButton({ label: i18n.tools.layers.label, id: "layerBtn", iconClass: "esriLayerIcon", title: i18n.tools.layers.title, dropDown: menu }); dojo.byId('webmap-toolbar-center').appendChild(button.domNode); } } //build a list of layers for the toggle layer list - this list //is slightly different than the legend because we don't want to list lines,points,areas etc for each //feature collection type. function buildLayerVisibleList(layers, operational) { var layerInfos = []; dojo.forEach(layers, function (mapLayer, index) { if (mapLayer.featureCollection && !mapLayer.layerObject) { if (mapLayer.featureCollection.layers) { //add the first layer in the layer collection... not all - when we turn off the layers we'll //turn them all off if (mapLayer.featureCollection.layers) { layerInfos.push({ "layer": mapLayer, "visible": mapLayer.visibility, "title": mapLayer.title }); } } } else if (mapLayer.layerObject) { layerInfos.push({ layer: mapLayer.layerObject, visible: mapLayer.layerObject.visible, title: mapLayer.title }); } else if (!operational) {//reallayers if (mapLayer.id.indexOf("SBD-") === -1 && mapLayer.id.indexOf("SBDE-") === -1 && mapLayer.id.indexOf("Elevation") === -1) { layerInfos.push({ "layer": mapLayer, "visible": mapLayer.visible, "title": mapLayer.title || mapLayer.displayname || mapLayer.name || mapLayer.folders[0].name }); } } }); return layerInfos.sort(function(a,b){ return (a.layer.id < b.layer.id) ? -1 : 1; }); }