Don't let mysterious invisible popup block keypresses
[potlatch2.git] / net / systemeD / potlatch2 / CategorySelector.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:VBox
3         xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5         xmlns:halcyon="net.systemeD.halcyon.*"
6         backgroundColor="white" borderStyle="inset">
7
8   <mx:HBox horizontalGap="0">
9   <mx:ToggleButtonBar height="100%" dataProvider="{categoryStack}" direction="vertical" styleName="catToggleButtonBar"/>
10   <mx:ViewStack id="categoryStack" width="100%" height="100%"
11       change="_formerIndex=event.newIndex;"
12       creationComplete="setSelectedFeature(_selectedType);">
13       <mx:Repeater id="catRep" dataProvider="{MapFeatures.getInstance().getCategoriesForType(limit)}">
14           <mx:VBox label="{catRep.currentItem.name}">
15           <mx:TileList dataProvider="{catRep.currentItem.getFeaturesForType(limit)}"
16                        width="420" height="100%" change="itemSelected(event);" styleName="catToggleTiles"
17                        creationComplete="ensureSelection();">
18               <mx:itemRenderer>
19                   <fx:Component>
20                       <mx:VBox width="100" height="75"
21                           horizontalScrollPolicy="off" verticalScrollPolicy="off"
22                           horizontalAlign="center" verticalGap="0" toolTip="{data.description}">
23                           <mx:Image source="{data.image}" height="100%" verticalAlign="middle"/>
24                           <mx:Text text="{data.name}"/>
25                       </mx:VBox>
26                   </fx:Component>
27               </mx:itemRenderer>
28           </mx:TileList>
29           </mx:VBox>
30       </mx:Repeater>
31   </mx:ViewStack>
32   </mx:HBox>
33
34   <!-- mx:Label id="hoverInfo" text="Hover Info goes here"/>-->
35   <fx:Script><![CDATA[
36
37
38       import net.systemeD.halcyon.connection.*;
39       import net.systemeD.potlatch2.mapfeatures.*;
40
41       import mx.controls.*;
42       import mx.containers.*;
43       import mx.events.IndexChangedEvent;
44
45       /** Set briefly by ensureSelection() (presumably to avoid an event race condition). */
46       private var settingSelection:Boolean = false;
47       private var _selectedType:Feature;
48       private var _limit:String;
49       private var _formerIndex:int=0;
50
51       public var allowControllerKeyboardEvents:Boolean = true;
52
53       [Bindable(event="selectedType")]
54       /** The Feature type (eg, "residential road") currently selected. */
55       public function get selectedType():Feature {
56           return _selectedType;
57       }
58
59       [Bindable(event="limitChanged")]
60       /** The type of features currently shown ("area", "node"...) */
61       public function get limit():String {
62           return _limit;
63       }
64
65       /** Determine which feature was clicked on, and set that as the currently selected type. */
66       private function itemSelected(event:Event):void {
67           _selectedType = Feature(TileList(event.currentTarget).selectedItem);
68           if ( !settingSelection )
69               dispatchEvent(new Event("selectedType"));
70       }
71
72       /** Set the current type of Features that will be shown ("area", "node"... )*/
73       public function setLimitTypes(type:String):void {
74           _limit = type;
75           dispatchEvent(new Event("limitChanged"));
76       }
77
78       /**
79        * Set the selected feature displayed in the selector.
80        *
81        * The tab is switched to the first category the feature
82        * is part of.
83        *
84        * @param feature The feature to make selected.
85        */
86       public function setSelectedFeature(feature:Feature):void {
87           _selectedType = feature;
88
89           // check whether stack built yet, if not we get called again when it's made
90           if ( categoryStack == null )
91               return;
92
93           // set the tab to the selected item's category
94           // (we have to manually fire the IndexChangedEvent because Flex's ViewStack.as isn't competent
95           //  enough to do it reliably. This is basically a direct crib from dispatchChangeEvent)
96           var index:int=0;
97           if ( feature != null ) {
98               var primaryCategory:Category = feature.findFirstCategory();
99               index = MapFeatures.getInstance().getCategoriesForType(limit).indexOf(primaryCategory);
100               if (index==-1) { index=0; }       // not found, so just show the default for now
101           }
102           var event:IndexChangedEvent = new IndexChangedEvent(IndexChangedEvent.CHANGE);
103           event.oldIndex = _formerIndex;
104           event.newIndex = index;
105           event.relatedObject = categoryStack.getChildAt(index);
106           categoryStack.selectedIndex = index;
107           categoryStack.dispatchEvent(event);
108           _formerIndex=index;
109
110           // finalise the item selection
111           ensureSelection();
112       }
113       public function setNoSelectedFeature():void {
114             /*_selectedType=null;*/
115             setSelectedFeature(null);
116           }
117
118       /**
119        * Sets the selected feature on each category page to the current
120        * value of _selectedType. If there is no selected feature then all
121        * panels selection will be blanked.
122        *
123        * This function does not change the selected category tab.
124        *
125        * Called both from setSelectedFeature, and category tab completion
126        * (so that newly created tabs work as expected)
127        */
128       private function ensureSelection():void {
129           settingSelection = true;
130           for (var i:Number = 0; i < categoryStack.numChildren; i++) {
131               var box:Box = Box(categoryStack.getChildAt(i));
132               if ( box == null || box.numChildren == 0 )
133                   continue;
134               var tileList:TileList = TileList(box.getChildAt(0));
135               var category:Category = Category(tileList.getRepeaterItem());
136               var selected:Boolean = false;
137               for each( var catFeature:Feature in category.features ) {
138                   if ( catFeature == _selectedType ) {
139                       tileList.selectedItem = _selectedType;
140                       selected = true;
141                   }
142               }
143               if ( !selected )
144                   tileList.selectedItem = null;
145           }
146           settingSelection = false;
147       }
148
149   ]]></fx:Script>
150 </mx:VBox>
151