Select to view content in your preferred language

Creating 'Don't show again' functionality on a popup

1734
4
Jump to solution
03-13-2014 09:32 AM
RebeccaWatson
Deactivated User
I have changed the LayerList widget so that the user can save and load layer preferences. I wanted to create a popup that opens when the widget loads that asks the user if they want to load their saved preferences or not - and that would have functionality similar to Robert's Splash widget so that they could tick a box and have their preference remembered.

Everything works but it won't run the LoadLayers function from the initTOC function (highlighted in the code below). Can anyone point me to a solution?
       private function initTOC(expandLayerItems:Boolean = false):void    {     toc.map = map;     toc.isMapServiceOnly = false; //gotta get this from the config file     toc.excludeLayers = getExcludeLayers();     toc.basemapLayers = getBasemapLayers();     toc.excludeGraphicsLayers = true;     toc.includeLegendItems = includeLegendItems;     toc.expandLayerItems = expandLayerItems;     wTemplate.addTitlebarButton("assets/images/i_save.png", "Save", saveLayers, false);     wTemplate.addTitlebarButton("assets/images/i_folder.png", "Load", loadLayers, false);          showwindowSO = SharedObject.getLocal(SHOWWINDOW);          if (showwindowSO)     {      var sWindow:String = showwindowSO.data[SHOWWINDOW] as String;      //is sWindow true? If it is then bWindow is true, if no then false      var bWindow:Boolean = (sWindow == "true")?true:false;      //Alert.show(bWindow.toString());      if(!bWindow)      {       window = CustomTitleWindow(PopUpManager.createPopUp(parent, CustomTitleWindow, true));       window.ellwidget = this;       PopUpManager.centerPopUp(window);       window.addEventListener("evtYes", yes);       window.addEventListener("evtNo", no);       }      else if (bWindow)      {       var alwaysLoad:String = showwindowSO.data[ALWAYSLOAD] as String;       var aLoad:Boolean = (alwaysLoad =="true")?true:false;       if (aLoad)       {        //*********this next command works fine***********        Alert.show("aLoad is " + aLoad);        //********but this one doesn't***********        loadLayers()       }             }     }     }        public function dontShowWindowAgain(value:Boolean):void    {     showwindowSO.data[SHOWWINDOW] = value.toString();     showwindowSO.flush();    }        public function alwaysLoadPrefs(value:Boolean):void    {     showwindowSO.data[ALWAYSLOAD] = value.toString();     showwindowSO.flush();    }        public function neverLoadPrefs(value:Boolean):void    {     showwindowSO.data[NEVERLOAD] = value.toString();     showwindowSO.flush();    }         private function yes(event:MouseEvent):void     {      Alert.show("The Yes request has been pressed");      loadLayers()     }        private function no(event:MouseEvent):void    {     Alert.show("The No request has been pressed");    }        private function getExcludeLayers():ArrayCollection    {     var result:ArrayCollection = getBasemapLayers();     if (configXML)     {      var layers:XMLList = configXML.excludelayer as XMLList;      for (var j:Number = 0; j < layers.length(); j++)      {       result.addItem(layers.toString());      }     }     return result;    }        private function getBasemapLayers():ArrayCollection    {     var result:ArrayCollection = new ArrayCollection();     if (configData && configData.basemaps)     {      for (var i:int = 0; i < configData.basemaps.length; i++)      {       result.addItem(configData.basemaps.label);      }     }     return result;    }        private function saveLayers():void    {     //create sharedObject     visLayersSO = SharedObject.getLocal("SavedPrefs");          //Array to store the visibility of the Layers     var layerNodes:Array = new Array();     //Read each node of the TOC     for each (var item:Object in toc.dataProvider)     {      //if the item is a TOC item      if (item is TocItem)           {            //and if it is a group layer            if (TocItem(item).isGroupLayer())             {              //and if the check box is checked              if (TocItem(item).visible == true)               {                //Store the name of the Layer in the array                layerNodes[layerNodes.length] = TocItem(item).label;               }              //Verify the status of the children of this Group Layer              FetchNextVisibleLayer(TocItem(item), layerNodes);             }             else             {        //The Toc Item should be visible              //The item may not have a parent; if it has a parent - it should be a group layer        if (TocItem(item).visible == true && (TocItem(item).parent == null                  || (TocItem(item).visible == true && TocItem(item).parent != null && TocItem(item).parent.isGroupLayer())))               {                //Store the name of the Layer in the array                 layerNodes[layerNodes.length] = TocItem(item).label;               }             }          }      }     //Store this Array in Shared Object     visLayersSO.data[VISLAYERS] = layerNodes;     visLayersSO.flush();          //If there is something in the saved layers then give user a message     if (visLayersSO.size > 0)     {      Alert.show("Your layers have been saved.")     }    }         private function loadLayers():void    {     //get the shared object     visLayersSO = SharedObject.getLocal("SavedPrefs");          //if there is something in the shared object     if (visLayersSO.size > 0)      {      //put the saved info into an array      var layerNodes:Array = visLayersSO.data[VISLAYERS];      var item:Object;      //run through the nodes in the TOC      for each (item in toc.dataProvider)      {       //if the item is a TOC Item       if (item is TocItem)       {        //If the name of the layer is present then it was checked on        if(layerNodes.indexOf(TocItem(item).label) > -1)        {         //Check the Layer Checkbox in the TOC         TocItem(item).visible = true;         }        //Verify the status of this layer's Children        FetchNextLayer(TocItem(item), layerNodes);       }      }     }     //if there is nothing in the shared object tell the user      else      {      Alert.show("You have no saved preferences");      }          //check for unchecked items in the TOC     for each (item in toc.dataProvider)     {      if (item is TocItem)      {       //if the layer's name is not present in the array it was not checked       if(layerNodes.indexOf(TocItem(item).label) == -1)       {        //Uncheck the Group Layer Checkbox         TocItem(item).visible = false;        }       //Go ahead to verify the status of the layer's Children       FetchNextLayerUncheck(TocItem(item), layerNodes);      }     }              }            //If the Layer is visible, store its name in the input array    //and go ahead to verify rest of the nodes      private function FetchNextVisibleLayer(tocItem:TocItem, layerNodes:Array):void      {        //Check each and every node of the input Group Layer     for each (var item:TocItem in tocItem.children)     {         //If it is a group layer      if(item.isGroupLayer())       {       //and it is visible       if (item.visible == true)        {         //store it in the array        layerNodes[layerNodes.length] = item.label;        }       //Since current group layer can have group layers in it,        //keep checking until all the nodes are checked        FetchNextVisibleLayer(item, layerNodes);      }          else          {        //We are checking all nodes but don't want to store the lengend symbology nodes       //so we check if the item is visible and if its parent is a group layer           //A valid layer's parent in this case is a group layer where as a Legend Node's parent is an ordinary layer       if (item.visible == true && item.parent.isGroupLayer())            {              layerNodes[layerNodes.length] = item.label;           }         }         }      }            private function FetchNextLayer(tocItem:TocItem, layerNodes:Array):void    {     //go through the children     for each (var item:TocItem in tocItem.children)     {      //if the name of that layer appears in the layerNodes array      if(layerNodes.indexOf(item.label) > -1)      {       //then set the item's visible property to be true       item.visible = true;      }      //Since a group layer can have group layers, keep checking until all the nodes are checked      FetchNextLayer(item, layerNodes);     }    }            //Uncheck the Layer's checkbox, if the input array doesn't contain the Layer's name     private function FetchNextLayerUncheck(tocItem:TocItem, layerNodes:Array):void    {     for each (var item:TocItem in tocItem.children)     {      //if the name of that layer doesn't appear in the layerNodes array      if(layerNodes.indexOf(item.label) == -1)      {       //set the item's visbile property to be false       item.visible = false;      }      //Since a group layer can have group layers inside, keep checking until all the nodes are checked      FetchNextLayerUncheck(item, layerNodes);     }    }       ]]>  </fx:Script>  
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Rebecca,

It looks like it may be a timing issue when I take your code and duplicate a sample.

if (aLoad)                         {                             setTimeout(loadLayers, 500);                         }

View solution in original post

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus
Rebecca,

It looks like it may be a timing issue when I take your code and duplicate a sample.

if (aLoad)                         {                             setTimeout(loadLayers, 500);                         }
0 Kudos
RebeccaWatson
Deactivated User
Thank you Robert, that works perfectly now. Is the issue here that the loadLayers function wasn't available to the initTOC yet? Is that why delaying running the function works?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Rebecca,

   No, it was the issue of the TOC not being done creating the nodes in the tree.
0 Kudos
RebeccaWatson
Deactivated User
Thanks Robert. I will look out for that in future.
0 Kudos