Drag-and-drop panel shows just 2 rows by default
authorRichard Fairhurst <richard@systemeD.net>
Sat, 24 Mar 2012 15:57:30 +0000 (15:57 +0000)
committerRichard Fairhurst <richard@systemeD.net>
Sat, 24 Mar 2012 15:57:30 +0000 (15:57 +0000)
net/systemeD/potlatch2/panels/DragAndDropPanel.mxml

index 5b281b8..821c87f 100644 (file)
@@ -9,11 +9,13 @@
        <mx:Repeater id="dndRep" dataProvider="{MapFeatures.getInstance().getCategoriesForType('point')}" styleName="dndRepeater">
                <mx:HBox width="100%" styleName="dndPanelCategory">
                        <mx:Label text="{dndRep.currentItem.name}:" styleName="dndPanelCategoryLabel"/>
+                       <mx:Spacer width="100%" />
+                       <mx:Label text="" click="showHide(event)" />
                </mx:HBox>
 
                <mx:TileList dataProvider="{dndRep.currentItem.getFeaturesForType('point', true)}" width="100%" height="1"
-                            id="tl" dragEnabled="true" mouseDown="mouseDownHandler(event)" 
-                            rowHeight="32" columnWidth="100" updateComplete="resizePOIGrid(event)" styleName="dndPanelTileList">
+                            id="tl" dragEnabled="true" mouseDown="mouseDownHandler(event)" verticalScrollPolicy="off" 
+                            rowHeight="32" columnWidth="100" updateComplete="sizePOIGrid(event)" styleName="dndPanelTileList">
                        <mx:itemRenderer>
                                <fx:Component>
                                        <mx:HBox toolTip="{data.name}" horizontalScrollPolicy="off" >
        import mx.core.DragSource;
        import mx.managers.DragManager;
        import mx.controls.Image;
+       import mx.controls.Button;
+       import mx.controls.TileList;
+       import mx.controls.scrollClasses.*;
 
        [Bindable] public var dndPrompt:String="Add new points by dragging them onto the map";
 
+       private var inited:Object={};
+       private static const MINIMISED_ROWS:uint=2;             // we could perhaps set this by category (e.g. two for shopping, one for tourism...)
+       private static const MINIMISED:uint=0;
+       private static const MAXIMISED:uint=1;
+       private static const TOO_SMALL:uint=2;
+
+       /** Start POI drag-and-drop. */
        private function mouseDownHandler(event:MouseEvent):void {
+               if (event.target is ScrollThumb || event.target is Button) return;
+
                var item:Feature=Feature(TileList(event.currentTarget).selectedItem);
                var dragInitiator:HBox = event.currentTarget.itemToItemRenderer(item);
                var dragSource:DragSource = new DragSource();
                DragManager.doDrag(dragInitiator, dragSource, event, dragProxy);
        }
 
-       private function resizePOIGrid(event:Event):void {
-               var rows:Number=event.target.dataProvider.length/event.target.columnCount;
+       /** Handle user's click on show/hide label. */
+       public function showHide(event:Event):void {
+               // find out which tilelist was clicked
+               var theHBox:HBox=event.currentTarget.parent;
+               var pos:uint=theHBox.parent.getChildIndex(theHBox);
+               var tilelist:TileList=TileList(theHBox.parent.getChildAt(pos+1));
+               
+               // now adjust the number of rows
+               setHeight(tilelist,(isAtMaximumSize(tilelist)));
+       }
+
+       /** Set a POI grid to the correct size when it's created. Non-functional if called later. */
+       private function sizePOIGrid(event:Event):void {
+               var tilelist:TileList=TileList(event.target);
+               if (inited[tilelist]) { return; }
+               inited[tilelist]=true;
+               setHeight(tilelist,true);
+       }
+       
+       /** Set a POI grid to minimised or maximised state, and return the state it's been set to. */
+       private function setHeight(tilelist:TileList,minimise:Boolean):uint {
+               // Set required state
+               var rows:Number=rowsForAll(tilelist);
+               var state:uint;
+               if (rows<=MINIMISED_ROWS) { applyHeight(tilelist,rows); state=TOO_SMALL; }
+               else if (minimise) { applyHeight(tilelist,MINIMISED_ROWS); state=MINIMISED; }
+               else { applyHeight(tilelist,rows); state=MAXIMISED; }
+               
+               // Update the clickable label
+               var pos:uint=tilelist.parent.getChildIndex(tilelist);
+               var hbox:HBox=HBox(tilelist.parent.getChildAt(pos-1));
+               var label:Label=Label(hbox.getChildAt(2));
+               switch (state) {
+                       case TOO_SMALL: label.text=""; break;
+                       case MINIMISED: label.text="Show all"; break;
+                       case MAXIMISED: label.text="Collapse"; break;
+               }
+               return state;
+       }
+       
+       /** Called by setHeight to actually apply the row height to the Flex TileList object. */
+       private function applyHeight(tilelist:TileList,rows:uint):void {
+               tilelist.height=rows*(tilelist.rowHeight+1);
+       }
+       
+       /** Is the POI grid at its maximum size? */
+       private function isAtMaximumSize(tilelist:TileList):Boolean {
+               var size:Number=tilelist.height/(tilelist.rowHeight+1);
+               return (size==rowsForAll(tilelist));
+       }
+       
+       /** How many rows are required to show everything in this POI grid? */
+       private function rowsForAll(tilelist:TileList):Number {
+               var rows:Number=tilelist.dataProvider.length/tilelist.columnCount;
                if (rows!=Math.floor(rows)) { rows=Math.floor(rows+1); }
-               event.target.height=rows*(event.target.rowHeight+1);
+               return rows;
        }
 
        ]]></fx:Script>