dear Adobe, FUCK OFF
[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       import mx.events.IndexChangedEvent;
41       
42       private var settingSelection:Boolean = false;
43       private var _selectedType:Feature;
44       private var _limit:String;
45       
46       [Bindable(event="selectedType")]
47       public function get selectedType():Feature {
48           return _selectedType;
49       }
50       
51       [Bindable(event="limitChanged")]
52       public function get limit():String {
53           return _limit;
54       }
55       
56       private function itemSelected(event:Event):void {
57           _selectedType = Feature(TileList(event.currentTarget).selectedItem);
58           if ( !settingSelection )
59               dispatchEvent(new Event("selectedType"));
60       }
61       
62       public function setLimitTypes(type:String):void {
63           _limit = type;
64           dispatchEvent(new Event("limitChanged"));
65       }
66       
67       /**
68        * Set the selected feature displayed in the selector.
69        *
70        * The tab is switched to the first category the feature
71        * is part of.
72        *
73        */
74       public function setSelectedFeature(feature:Feature):void {
75           _selectedType = feature;
76           
77           // check whether stack built yet, if not we get called again when it's made
78           if ( categoryStack == null )
79               return;
80
81           if ( feature != null ) {
82               var primaryCategory:Category = feature.findFirstCategory();
83           
84               // set the tab to the selected item's category
85               // (we have to manually fire the IndexChangedEvent because Flex's ViewStack.as isn't competent 
86               //  enough to do it reliably. This is basically a direct crib from dispatchChangeEvent)
87               var index:int = MapFeatures.getInstance().getCategoriesForType(limit).indexOf(primaryCategory);
88               var former:int = categoryStack.selectedIndex;
89               if (index!=former) {
90                       var event:IndexChangedEvent = new IndexChangedEvent(IndexChangedEvent.CHANGE);
91                       event.oldIndex = former;
92                       event.newIndex = index;
93                       event.relatedObject = categoryStack.getChildAt(index);
94                       categoryStack.selectedIndex = index;
95                       categoryStack.dispatchEvent(event);
96               }
97           }
98           
99           // finalise the item selection
100           ensureSelection();
101       }
102       
103       /**
104        * Sets the selected feature on each category page to the current
105        * value of _selectedType. If there is no selected feature then all
106        * panels selection will be blanked.
107        *
108        * This function does not change the selected category tab.
109        *
110        * Called both from setSelectedFeature, and category tab completion
111        * (so that newly created tabs work as expected)
112        */   
113       private function ensureSelection():void {
114           settingSelection = true;
115           for (var i:Number = 0; i < categoryStack.numChildren; i++) {
116               var box:Box = Box(categoryStack.getChildAt(i));
117               if ( box == null || box.numChildren == 0 )
118                   continue;
119               var tileList:TileList = TileList(box.getChildAt(0));
120               var category:Category = Category(tileList.getRepeaterItem());
121               var selected:Boolean = false;
122               for each( var catFeature:Feature in category.features ) {
123                   if ( catFeature == _selectedType ) {
124                       tileList.selectedItem = _selectedType;
125                       selected = true;
126                   }
127               }
128               if ( !selected )
129                   tileList.selectedItem = null;
130           }
131           settingSelection = false;
132       }
133       
134   ]]></mx:Script>
135 </mx:VBox>
136