refactoring and some bugfixes
[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"/>
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);"
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           }
91           var event:IndexChangedEvent = new IndexChangedEvent(IndexChangedEvent.CHANGE);
92           event.oldIndex = _formerIndex;
93           event.newIndex = index;
94           event.relatedObject = categoryStack.getChildAt(index);
95           categoryStack.selectedIndex = index;
96           categoryStack.dispatchEvent(event);
97           _formerIndex=index;
98           
99           // finalise the item selection
100           ensureSelection();
101       }
102       public function setNoSelectedFeature():void {
103             _selectedType=null;
104           }
105       
106       /**
107        * Sets the selected feature on each category page to the current
108        * value of _selectedType. If there is no selected feature then all
109        * panels selection will be blanked.
110        *
111        * This function does not change the selected category tab.
112        *
113        * Called both from setSelectedFeature, and category tab completion
114        * (so that newly created tabs work as expected)
115        */   
116       private function ensureSelection():void {
117           settingSelection = true;
118           for (var i:Number = 0; i < categoryStack.numChildren; i++) {
119               var box:Box = Box(categoryStack.getChildAt(i));
120               if ( box == null || box.numChildren == 0 )
121                   continue;
122               var tileList:TileList = TileList(box.getChildAt(0));
123               var category:Category = Category(tileList.getRepeaterItem());
124               var selected:Boolean = false;
125               for each( var catFeature:Feature in category.features ) {
126                   if ( catFeature == _selectedType ) {
127                       tileList.selectedItem = _selectedType;
128                       selected = true;
129                   }
130               }
131               if ( !selected )
132                   tileList.selectedItem = null;
133           }
134           settingSelection = false;
135       }
136       
137   ]]></mx:Script>
138 </mx:VBox>
139