slight refactoring of controller states
[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       creationComplete="setSelectedFeature(_selectedType);">
11       <mx:Repeater id="catRep" dataProvider="{MapFeatures.getInstance().getCategoriesForType(limit)}">
12           <mx:VBox label="{catRep.currentItem.name}">
13           <mx:TileList dataProvider="{catRep.currentItem.getFeaturesForType(limit)}"
14                        width="100%" height="100%" change="itemSelected(event);"
15                        creationComplete="ensureSelection();">
16               <mx:itemRenderer>
17                   <mx:Component>
18                       <mx:VBox width="100" height="75"
19                           horizontalScrollPolicy="off" verticalScrollPolicy="off"
20                           horizontalAlign="center" verticalGap="0">
21                           <mx:Image source="{data.image}" height="100%" verticalAlign="middle"/>
22                           <mx:Text text="{data.name}"/>
23                       </mx:VBox>
24                   </mx:Component>
25               </mx:itemRenderer>
26           </mx:TileList>
27           </mx:VBox>
28       </mx:Repeater>
29   </mx:ViewStack>
30   </mx:HBox>
31   
32   <!-- mx:Label id="hoverInfo" text="Hover Info goes here"/>-->
33   
34   <mx:Script><![CDATA[
35       import net.systemeD.halcyon.connection.*;
36       import net.systemeD.potlatch2.mapfeatures.*;
37
38       import mx.controls.*;
39       import mx.containers.*;
40       
41       private var settingSelection:Boolean = false;
42       private var _selectedType:Feature;
43       private var _limit:String;
44       
45       [Bindable(event="selectedType")]
46       public function get selectedType():Feature {
47           return _selectedType;
48       }
49       
50       [Bindable(event="limitChanged")]
51       public function get limit():String {
52           return _limit;
53       }
54       
55       private function itemSelected(event:Event):void {
56           _selectedType = Feature(TileList(event.currentTarget).selectedItem);
57           if ( !settingSelection )
58               dispatchEvent(new Event("selectedType"));
59       }
60       
61       public function setLimitTypes(type:String):void {
62           _limit = type;
63           dispatchEvent(new Event("limitChanged"));
64       }
65       
66       /**
67        * Set the selected feature displayed in the selector.
68        *
69        * The tab is switched to the first category the feature
70        * is part of.
71        *
72        */
73       public function setSelectedFeature(feature:Feature):void {
74           _selectedType = feature;
75           
76           // check whether stack built yet, if not we get called again when it's made
77           if ( categoryStack == null )
78               return;
79
80           if ( feature != null ) {
81               var primaryCategory:Category = feature.findFirstCategory();
82           
83               // set the tab to the selected item's category
84               var index:int = MapFeatures.getInstance().getCategoriesForType(limit).indexOf(primaryCategory);
85               categoryStack.selectedIndex = index;
86           }
87           
88           // finalise the item selection
89           ensureSelection();
90       }
91       
92       /**
93        * Sets the selected feature on each category page to the current
94        * value of _selectedType. If there is no selected feature then all
95        * panels selection will be blanked.
96        *
97        * This function does not change the selected category tab.
98        *
99        * Called both from setSelectedFeature, and category tab completion
100        * (so that newly created tabs work as expected)
101        */   
102       private function ensureSelection():void {
103           settingSelection = true;
104           for (var i:Number = 0; i < categoryStack.numChildren; i++) {
105               var box:Box = Box(categoryStack.getChildAt(i));
106               if ( box == null || box.numChildren == 0 )
107                   continue;
108               var tileList:TileList = TileList(box.getChildAt(0));
109               var category:Category = Category(tileList.getRepeaterItem());
110               var selected:Boolean = false;
111               for each( var catFeature:Feature in category.features ) {
112                   if ( catFeature == _selectedType ) {
113                       tileList.selectedItem = _selectedType;
114                       selected = true;
115                   }
116               }
117               if ( !selected )
118                   tileList.selectedItem = null;
119           }
120           settingSelection = false;
121       }
122       
123   ]]></mx:Script>
124 </mx:VBox>
125