support GPX 1.1 too
[potlatch2.git] / net / systemeD / potlatch2 / CategorySelector.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:VBox
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         xmlns:halcyon="net.systemeD.halcyon.*"
5         backgroundColor="white" borderStyle="inset">
6
7   <mx:HBox horizontalGap="0"> 
8   <mx:ToggleButtonBar height="100%" dataProvider="{categoryStack}" direction="vertical" styleName="catToggleButtonBar"/>
9   <mx:ViewStack id="categoryStack" width="100%" height="100%"
10       change="_formerIndex=event.newIndex;" 
11       creationComplete="setSelectedFeature(_selectedType);">
12       <mx:Repeater id="catRep" dataProvider="{MapFeatures.getInstance().getCategoriesForType(limit)}">
13           <mx:VBox label="{catRep.currentItem.name}">
14           <mx:TileList dataProvider="{catRep.currentItem.getFeaturesForType(limit)}"
15                        width="100%" height="100%" change="itemSelected(event);" styleName="catToggleTiles"
16                        creationComplete="ensureSelection();">
17               <mx:itemRenderer>
18                   <mx:Component>
19                       <mx:VBox width="100" height="75"
20                           horizontalScrollPolicy="off" verticalScrollPolicy="off"
21                           horizontalAlign="center" verticalGap="0">
22                           <mx:Image source="{data.image}" height="100%" verticalAlign="middle"/>
23                           <mx:Text text="{data.name}"/>
24                       </mx:VBox>
25                   </mx:Component>
26               </mx:itemRenderer>
27           </mx:TileList>
28           </mx:VBox>
29       </mx:Repeater>
30   </mx:ViewStack>
31   </mx:HBox>
32   
33   <!-- mx:Label id="hoverInfo" text="Hover Info goes here"/>-->
34   
35   <mx:Script><![CDATA[
36       import net.systemeD.halcyon.connection.*;
37       import net.systemeD.potlatch2.mapfeatures.*;
38
39       import mx.controls.*;
40       import mx.containers.*;
41       import mx.events.IndexChangedEvent;
42       
43       private var settingSelection:Boolean = false;
44       private var _selectedType:Feature;
45       private var _limit:String;
46       private var _formerIndex:int=0;
47       
48       [Bindable(event="selectedType")]
49       public function get selectedType():Feature {
50           return _selectedType;
51       }
52       
53       [Bindable(event="limitChanged")]
54       public function get limit():String {
55           return _limit;
56       }
57       
58       private function itemSelected(event:Event):void {
59           _selectedType = Feature(TileList(event.currentTarget).selectedItem);
60           if ( !settingSelection )
61               dispatchEvent(new Event("selectedType"));
62       }
63       
64       public function setLimitTypes(type:String):void {
65           _limit = type;
66           dispatchEvent(new Event("limitChanged"));
67       }
68       
69       /**
70        * Set the selected feature displayed in the selector.
71        *
72        * The tab is switched to the first category the feature
73        * is part of.
74        *
75        */
76       public function setSelectedFeature(feature:Feature):void {
77           _selectedType = feature;
78           
79           // check whether stack built yet, if not we get called again when it's made
80           if ( categoryStack == null )
81               return;
82
83           // set the tab to the selected item's category
84           // (we have to manually fire the IndexChangedEvent because Flex's ViewStack.as isn't competent 
85           //  enough to do it reliably. This is basically a direct crib from dispatchChangeEvent)
86           var index:int=0;
87           if ( feature != null ) {
88               var primaryCategory:Category = feature.findFirstCategory();
89               index = MapFeatures.getInstance().getCategoriesForType(limit).indexOf(primaryCategory);
90               if (index==-1) { index=0; }       // not found, so just show the default for now
91           }
92           var event:IndexChangedEvent = new IndexChangedEvent(IndexChangedEvent.CHANGE);
93           event.oldIndex = _formerIndex;
94           event.newIndex = index;
95           event.relatedObject = categoryStack.getChildAt(index);
96           categoryStack.selectedIndex = index;
97           categoryStack.dispatchEvent(event);
98           _formerIndex=index;
99           
100           // finalise the item selection
101           ensureSelection();
102       }
103       public function setNoSelectedFeature():void {
104             _selectedType=null;
105           }
106       
107       /**
108        * Sets the selected feature on each category page to the current
109        * value of _selectedType. If there is no selected feature then all
110        * panels selection will be blanked.
111        *
112        * This function does not change the selected category tab.
113        *
114        * Called both from setSelectedFeature, and category tab completion
115        * (so that newly created tabs work as expected)
116        */   
117       private function ensureSelection():void {
118           settingSelection = true;
119           for (var i:Number = 0; i < categoryStack.numChildren; i++) {
120               var box:Box = Box(categoryStack.getChildAt(i));
121               if ( box == null || box.numChildren == 0 )
122                   continue;
123               var tileList:TileList = TileList(box.getChildAt(0));
124               var category:Category = Category(tileList.getRepeaterItem());
125               var selected:Boolean = false;
126               for each( var catFeature:Feature in category.features ) {
127                   if ( catFeature == _selectedType ) {
128                       tileList.selectedItem = _selectedType;
129                       selected = true;
130                   }
131               }
132               if ( !selected )
133                   tileList.selectedItem = null;
134           }
135           settingSelection = false;
136       }
137       
138   ]]></mx:Script>
139 </mx:VBox>
140