Wednesday, November 5, 2008

Flex DateChooser Toggle

Upadte- 2/4/2009 3:11 PM
Added support for simulating the shift key as well. If allowDisjointSelection is set then each click will toggle a date like id did before.
If it switched off though now the first click will set a start date and the second click an end date so that a range can be selected via clicking.


This component changes the DateChooser class to add a toggle option. When set clicking on a date will toggle it in the same way that control-clicking does.

DateChooser.as
package
{
import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.DateChooser;
import mx.core.mx_internal;

use namespace mx_internal;

/**
* This custom DateChooser adds the ability to toggle dates on click or to select a range with two clicks instead of using
* keyboard modifiers. If allowDisjointSelection is set then clicking a date will toggle it on or off. If not then the first click
* will set the start date and the second click will set the end date. In this mode a third click will un-select the first range and
* set a new start date.
*
* @author Jeremy Pyne jeremy.pyne@gmail.com
*/
public class DateChooser extends mx.controls.DateChooser
{
public function DateChooser()
{
super();
}

override protected function createChildren():void
{
super.createChildren();

dateGrid.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, 1);
}

//----------------------------------
// toggleSelection
//----------------------------------

/**
* @private
* Storage for the allowMultipleSelection property.
*/
private var _toggleSelection:Boolean = false;


[Bindable("toggleSelectionChanged")]
[Inspectable(category="General", defaultValue="true")]

/**
* If true, specifies that selecting dates will toggle them. Simulates controll key down.
*
* @default false
* @helpid
* @tiptext Selections are toggled if true
*/
public function get toggleSelection():Boolean
{
return _toggleSelection;
}

/**
* @private
*/
public function set toggleSelection(value:Boolean):void
{
_toggleSelection = value;
dispatchEvent(new Event("toggleSelectionChanged"));
}


private function mouseUpHandler(event:MouseEvent):void
{
if(toggleSelection && allowMultipleSelection && event.shiftKey == false)
{
// If allowDisjointSelection is set then simulate the control key pressed.
if(allowDisjointSelection)
event.ctrlKey = true;
// If not and there is something selected.
else if(this.selectedRanges.length)
// Simulate the shift key down if the currect selectedRange is that of a single day.
event.shiftKey = this.selectedRanges[0].rangeStart.time == this.selectedRanges[0].rangeEnd.time;
}
}
}
}

No comments: