Skip to content

Instantly share code, notes, and snippets.

@maxan
Last active January 2, 2016 06:08
Show Gist options
  • Save maxan/8261295 to your computer and use it in GitHub Desktop.
Save maxan/8261295 to your computer and use it in GitHub Desktop.
Competence date selector.
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" initialize="{startComponente_Initialize(event)}"
creationComplete="{createComponent_CreationComplete(event)}"
width="232">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable]
private var _listYearDataProvider:ArrayCollection = new ArrayCollection();
[Bindable]
private var _listMonthDataProvider:ArrayCollection = new ArrayCollection();
private var _loadNowDate:Boolean = true;
private var _yearsAhead:int = 0;
[Bindable]
/**
* @private
*/
public function get yearsAhead():int
{
return _yearsAhead;
}
/**
* Indicates the number of years ahead that will be shown in years list.
*/
public function set yearsAhead(value:int):void
{
_yearsAhead = value;
}
[Bindable]
/**
* @private
*/
public function get loadWithNowDate():Boolean
{
return _loadNowDate;
}
/**
* Load the date selector with now date.
*/
public function set loadWithNowDate(value:Boolean):void
{
_loadNowDate = value;
}
/**
* Get the selected competence date (string format).
*/
public function getStringDateCompetence():String
{
var addZero:Boolean = String(_listMonth.selectedItem.MonthNumber).length < 2;
return (addZero ? "0" : "") + _listMonth.selectedItem.MonthNumber + "/" + _listYear.selectedItem;
}
/**
* Get the selected competence date (date format).
*/
public function getDateCompetence():Date
{
return new Date(_listYear.selectedItem, _listMonth.selectedItem.MonthNumber - 1, 1);
}
/**
* Load initial values and subcomponents used by DateSelector.
*
* @param event Event object with aditional information about the initialize event.
*/
protected function startComponente_Initialize(event:FlexEvent):void
{
_listYearDataProvider = generateListYear(5, _yearsAhead);
_listMonthDataProvider = generateListMonth();
}
/**
* Config internal components according to properties.
*
* @param event event object with aditional information about the creation event.
*/
protected function createComponent_CreationComplete(event:FlexEvent):void
{
if (_loadNowDate)
{
_listYear.selectedIndex = yearsAhead > 0 ? yearsAhead : 0;
_listMonth.selectedIndex = (new Date()).month;
}
}
/**
* Generate the list of the months.
*/
private function generateListMonth():ArrayCollection
{
var months:ArrayCollection = new ArrayCollection();
const MONTHS:Object = [
{"MonthNumber":1, "MonthName":"Janeiro"},
{"MonthNumber":2, "MonthName":"Fevereiro"},
{"MonthNumber":3, "MonthName":"Março"},
{"MonthNumber":4, "MonthName":"Abril"},
{"MonthNumber":5, "MonthName":"Maio"},
{"MonthNumber":6, "MonthName":"Junho"},
{"MonthNumber":7, "MonthName":"Julho"},
{"MonthNumber":8, "MonthName":"Agosto"},
{"MonthNumber":9, "MonthName":"Setembro"},
{"MonthNumber":10, "MonthName":"Outubro"},
{"MonthNumber":11, "MonthName":"Novembro"},
{"MonthNumber":12, "MonthName":"Dezembro"}
];
for each (var month:Object in MONTHS)
{
months.addItem(month);
}
return months;
}
/**
* Generate the list of the years.
*/
public function generateListYear(itemsNumber:Number, yearsAhead:Number = 0):ArrayCollection
{
var years:Array = new Array();
var currentYear:Number = (new Date()).fullYear;
for (var dec:Number = (currentYear + yearsAhead); dec >= ((currentYear + yearsAhead)-itemsNumber-1); dec--)
{
years.push(dec);
}
return new ArrayCollection(years);
}
]]>
</fx:Script>
<s:ComboBox id="_listMonth" dataProvider="{_listMonthDataProvider}" labelField="MonthName" width="65%"/>
<s:Label text="/" verticalAlign="middle" height="100%" fontSize="18"/>
<s:ComboBox id="_listYear" dataProvider="{_listYearDataProvider}" width="35%"/>
</s:HGroup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment