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