Jorge, te molesto de nuevo con una inquietud:
Hice un ejercicio, que por medio de una clase AS3, carga un calendario con fechas seleccionables (llamando un combobox y un numeric stepper de la biblioteca). Me gustaría saber como incorporar lo que me explicaste a éste, para que responda al cambio de mes en dicho calendario.
Este es la clase:
Código :
package
{
import flash.display.Sprite;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.events.Event;
import fl.controls.ComboBox;
import fl.data.DataProvider;
import fl.controls.NumericStepper;
public class calendar extends Sprite
{
//variables
private var yearPickerNS:NumericStepper; //numeric stepper to pick a year
private var months:Array = [
{label:"January", data:0},
{label:"February", data:1},
{label:"March", data:2},
{label:"April", data:3},
{label:"May", data:4},
{label:"June", data:5},
{label:"July", data:6},
{label:"August", data:7},
{label:"September", data:8},
{label:"October", data:9},
{label:"November", data:10},
{label:"December", data:11},
];
private var monthPickerCB:ComboBox; //combobox to pick a month
private var currDateTime:Date = new Date();
private var firstDay:Date = new Date(currDateTime.fullYear,currDateTime.month,1);
private var firstDayColumn:uint = firstDay.day;
private var daysOfMonths:Array = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
private var weekDays:Array = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
private var maxDays:uint;
private var cellW:Number; //cell width
private var cellP:Number; //cell padding
private var allDatesCells:Array = new Array();
private var dateCellFormat:TextFormat;
private var dayLabelTxtFmt:TextFormat;
private var comboBoxTextFormat:TextFormat = new TextFormat();
private var numericStepperTextFormat:TextFormat = new TextFormat();
public function calendar(fontFace:String = "Verdana", fontSize:int = 13,
cellWidth:Number = 30, padding:Number = 1,
originX:Number = 15, originY:Number = 25,
cbX:Number = 20, cbY:Number = 32,
nsX:Number = 27, nsY:Number = 32,
monthsRange:int = 39)
{
cellW = cellWidth;
cellP = padding;
setTextFormat(fontFace,fontSize);
makeDatesCellGrid(originX, originY);
makeDaysLabels(originX, originY);
monthPickerCB = new ComboBox();
monthSetup();
monthPicker(cbX, cbY);
yearPickerNS = new NumericStepper();
yearPicker(nsX, nsY, monthsRange);
}
private function setTextFormat(whichFont:String, size:int):void
{
//date text format
dateCellFormat = new TextFormat();
dateCellFormat.font = whichFont;
dateCellFormat.color = 0xFFFFFF;
dateCellFormat.size = size;
dateCellFormat.align = "center";
dateCellFormat.leftMargin = 2;
//day label text format
dayLabelTxtFmt = new TextFormat();
dayLabelTxtFmt.font = "Verdana";
dayLabelTxtFmt.color = 0x333333;
dayLabelTxtFmt.size = size;
dayLabelTxtFmt.bold="true";
//combobox text format
comboBoxTextFormat.font = "Verdana";
comboBoxTextFormat.color = 0xFFFFFF;
//numeric stepper text format
numericStepperTextFormat.font = "Verdana";
numericStepperTextFormat.color = 0xFFFFFF;
}
private function makeDatesCellGrid(cellXPos:Number, cellYPos:Number):void
{
//create grid of date cells
for (var i:int = 0; i<42; i++)
{
var dateCell:TextField = new TextField();
addChild(dateCell);
//position cells to form a grid (7*6=42)
dateCell.x = cellXPos+(cellW*(i-(Math.floor(i/7)*7)));
dateCell.y = cellYPos+(cellW*Math.floor(i/7));
//put all date cells into array for further access
allDatesCells.push(dateCell);
}
}
private function makeDaysLabels(cellXPos:Number, cellYPos:Number):void
{
//add week days names
for (var i:int = 0; i<7; i++)
{
var dayLabel:TextField = new TextField();
addChild(dayLabel);
dayLabel.selectable = false;
dayLabel.text = weekDays[i];
dayLabel.setTextFormat(dayLabelTxtFmt);
dayLabel.x = cellXPos+(cellW*i)+3;
dayLabel.y = cellYPos-20;
}
}
private function monthSetup():void
{
for (var i:int = 0; i<42; i++)
{
allDatesCells[i].text = "";
//decor all cells
allDatesCells[i].background = true;
allDatesCells[i].backgroundColor = 0x333333;
allDatesCells[i].border = true;
allDatesCells[i].borderColor = 0x151515;
allDatesCells[i].selectable = false;
allDatesCells[i].width = allDatesCells[i].height = cellW-cellP;
allDatesCells[i].setTextFormat(dateCellFormat);
}
arrangeDates();
prevMonthDates();
nextMonthDates();
}
private function arrangeDates():void
{
//get column number for first day of the month
if (firstDay.day == 0)
{
//when last day of previous month is on saturday then move to second row
firstDayColumn = firstDay.day+7;
}
else
{
firstDayColumn = firstDay.day;
}
//get max days for current month w.r.t. leap year if any
maxDays = (firstDay.getFullYear()%4 == 0&&firstDay.getMonth() == 1?29:daysOfMonths[firstDay.getMonth()]);
//put days for current month
for (var i:int=0; i<maxDays; i++)
{
allDatesCells[firstDayColumn+i].text = i+1;
allDatesCells[firstDayColumn+i].setTextFormat(dateCellFormat);
allDatesCells[firstDayColumn+i].alpha = 1;
//highlight today
if(firstDay.fullYear == currDateTime.fullYear&&firstDay.month == currDateTime.month)
{
if(allDatesCells[firstDayColumn+i].text == currDateTime.date)
{
allDatesCells[firstDayColumn+i].backgroundColor = 0xFF6600;
}
}
}
}
private function prevMonthDates():void
{
var prevMonthFirstDay:Date = new Date(firstDay.fullYear,firstDay.month,firstDay.date-1);
for(var i:int = firstDayColumn-1; i>=0; i--)
{
allDatesCells[i].text = prevMonthFirstDay.date-((firstDayColumn-1)-i);
allDatesCells[i].setTextFormat(dateCellFormat);
allDatesCells[i].alpha = 0.5;
}
}
private function nextMonthDates():void
{
for (var i:int = 1; i<(42-maxDays-(firstDayColumn-1)); i++)
{
allDatesCells[(firstDayColumn-1)+i+maxDays].text = i;
allDatesCells[(firstDayColumn-1)+i+maxDays].setTextFormat(dateCellFormat);
allDatesCells[(firstDayColumn-1)+i+maxDays].alpha = 0.5;
}
}
private function monthPicker(cbX:Number, cbY:Number):void
{
monthPickerCB.dataProvider = new DataProvider(months);
addChild(monthPickerCB);
//position combobox
monthPickerCB.x = (cellW*7)+cbX+5;
monthPickerCB.y = (cellW*5)+cbY;
monthPickerCB.selectedIndex = currDateTime.month;
monthPickerCB.addEventListener(Event.CHANGE, pickMonth);
//style combobox
monthPickerCB.textField.setStyle("textFormat", comboBoxTextFormat);
monthPickerCB.dropdown.setRendererStyle("textFormat", comboBoxTextFormat);
monthPickerCB.width = 100;
}
private function pickMonth(e:Event):void
{
firstDay.month = ComboBox(e.target).selectedItem.data;
monthSetup();
}
private function yearPicker(nsX:Number, nsY:Number, maxYrsRange:int):void
{
yearPickerNS.maximum = currDateTime.fullYear+maxYrsRange;
yearPickerNS.minimum = currDateTime.fullYear-maxYrsRange;
yearPickerNS.value = currDateTime.fullYear;
addChild(yearPickerNS);
//position numeric stepper
yearPickerNS.x = monthPickerCB.x+(nsX*4);
yearPickerNS.y = (cellW*5)+nsY;
//style numeric stepper
yearPickerNS.setStyle("textFormat", numericStepperTextFormat);
yearPickerNS.width = 70;
yearPickerNS.addEventListener(Event.CHANGE, pickYear);
}
private function pickYear(e:Event):void
{
firstDay.fullYear = e.target.value;
monthSetup();
}
}
}
Y el código en el .fla, por si acaso:
Código :
import flash.display.Sprite;
var myCalendar:Sprite = new calendar();
addChild(myCalendar);
Gracias.