Skip to content

Instantly share code, notes, and snippets.

@burhanaksendir
Created December 14, 2014 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burhanaksendir/4832b818cd1a08181220 to your computer and use it in GitHub Desktop.
Save burhanaksendir/4832b818cd1a08181220 to your computer and use it in GitHub Desktop.
Flex Mobile : CheckBox iconItemRenderer
<?xml version="1.0" encoding="utf-8"?>
<s:IconItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Metadata>
[Event(name="checkBoxIconItemRendererChanged", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import spark.components.CheckBox;
public var checkBox:CheckBox;
private var checkBoxChanged:Boolean;
private var _checkBoxField:String;
private var _checkBoxFunction:Function;
private var _checkBoxIncludeInLayoutFunction:Function;
private var _checkBoxVisibleFunction:Function;
private var _checkBoxAlignment:String;
[Inspectable(category="General", enumeration="left,right", defaultValue="left")]
public function set checkBoxAlignment(value:String):void{
if(_checkBoxAlignment==value){
return;
}
_checkBoxAlignment=value;
checkBoxChanged;
invalidateProperties();
}
public function get checkBoxAlignment():String{
return _checkBoxAlignment;
}
public function get checkBoxFunction():Function{
return _checkBoxFunction;
}
public function get checkBoxField():String{
return _checkBoxField;
}
public function set checkBoxFunction(value:Function):void{
if(_checkBoxFunction==value){
return;
}
_checkBoxFunction=value;
checkBoxChanged=true;
invalidateProperties();
}
public function set checkBoxIncludeInLayoutFunction(value:Function):void{
if(_checkBoxIncludeInLayoutFunction==value){
return;
}
_checkBoxIncludeInLayoutFunction=value;
checkBoxChanged=true;
invalidateProperties();
}
public function get checkBoxIncludeInLayoutFunction():Function{
return _checkBoxIncludeInLayoutFunction;
}
public function set checkBoxVisibleFunction(value:Function):void{
if(_checkBoxVisibleFunction==value){
return;
}
checkBoxChanged=true;
_checkBoxVisibleFunction=value;
invalidateProperties();
}
public function get checkBoxVisibleFunction():Function{
return _checkBoxVisibleFunction;
}
public function set checkBoxField(value:String):void{
if(_checkBoxField==value){
return;
}
checkBoxChanged=true;
_checkBoxField=value;
invalidateProperties();
}
override public function set data(value:Object):void
{
checkBoxChanged=true;
super.data = value; //->invalidateProperties();
}
override protected function createChildren():void
{
super.createChildren();
checkBox = new CheckBox();
checkBox.width=32;
checkBox.height=32;
checkBox.scaleY=.5;
checkBox.scaleX=.5;
addChild(checkBox);
checkBox.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void{
event.stopImmediatePropagation();
dispatchEvent(new Event("checkBoxIconItemRendererChanged"));
});
}
override protected function measure():void
{
super.measure();
if(checkBox.includeInLayout){
measuredWidth+=getStyle("horizontalGap")+checkBox.width*checkBox.scaleY;
measuredHeight=Math.max(measuredHeight, checkBox.height*checkBox.scaleY);
}
}
override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number):void
{
if(!checkBox.includeInLayout){
super.layoutContents(unscaledWidth, unscaledHeight);
return;
}
var paddingLeft:Number = getStyle("paddingLeft");
var paddingRight:Number = getStyle("paddingRight");
var paddingTop:Number = getStyle("paddingTop");
var paddingBottom:Number = getStyle("paddingBottom");
var horizontalGap:Number = getStyle("horizontalGap");
var verticalAlign:String = getStyle("verticalAlign");
switch(_checkBoxAlignment){
case "right":
var myWidth:Number=paddingRight+checkBox.width*checkBox.scaleX
super.layoutContents(unscaledWidth-myWidth, unscaledHeight);
checkBox.x=unscaledWidth-myWidth;
break;
default :
setStyle("paddingLeft",paddingLeft+checkBox.width*checkBox.scaleX+horizontalGap);
super.layoutContents(unscaledWidth, unscaledHeight);
setStyle("paddingLeft",paddingLeft);
checkBox.x=paddingLeft;
break;
}
var vAlign:Number;
if (verticalAlign == "top")
vAlign = 0;
else if (verticalAlign == "bottom")
vAlign = 1;
else // if (verticalAlign == "middle")
vAlign = 0.5;
var viewHeight:Number = unscaledHeight - paddingTop - paddingBottom;
var checkBoxDisplayY:Number = Math.round(vAlign * (viewHeight - checkBox.height*checkBox.scaleY)) + paddingTop;
checkBox.y=checkBoxDisplayY;
}
override protected function commitProperties():void
{
super.commitProperties();
if(checkBoxChanged){
checkBoxChanged=false;
if(checkBoxIncludeInLayoutFunction!=null){
checkBox.includeInLayout = checkBoxIncludeInLayoutFunction(data);
}
if(checkBoxVisibleFunction!=null){
checkBox.visible=checkBoxVisibleFunction(data);
}
if(checkBox.includeInLayout){
if (checkBoxFunction != null)
{
checkBox.selected=checkBoxFunction(data);
}
else if (checkBoxField)
{
try
{
if (checkBoxField in data && data[checkBoxField] != null)
checkBox.selected=data[checkBoxField];
}
catch(e:Error)
{
trace(e.message);
}
}
}
}
invalidateSize();
invalidateDisplayList();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</s:IconItemRenderer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment