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