Flex actionscript extending DateChooser, events in calendar

Posted by Nemi on Stack Overflow See other posts from Stack Overflow or by Nemi
Published on 2010-03-29T02:26:02Z Indexed on 2010/03/29 2:33 UTC
Read the original article Hit count: 686

Filed under:
|
|
|
|

ExtendedDateChooser class is great solution for simple event calendar used in my flex project. You can find it if google for "Adding-Calendar-Event-Entries-to-the-Flex-DateChooser-Component" with a link of updated solution in comments of the post. I posted files below.

Problem in that calendar is text events are missing when month is changed.

Is there updateCompleted event in Actionscript just like in dateChooser flex component? Like in:

<mx:DateChooser id="dc" updateCompleted="goThroughDateChooserCalendarLayoutAndSetEventsInCalendarAgain()"</mx>

When scroll event is added, which is available in Actionscript, it gets dispatched but after updateDisplayList() is fired, so didn't manage to answer, why are calendar events erased?

Any suggestions, what to add in code, maybe override some function?

ExtendedDateChooserClass.mxml

   <?xml version='1.0' encoding="utf-8"?>

   <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:mycomp="cyberslingers.controls.*" 
    layout="absolute"  
   creationComplete="init()">

   <mx:Script>


<![CDATA[
    import cyberslingers.controls.ExtendedDateChooser;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.controls.Alert;

public var mycal:ExtendedDateChooser = new ExtendedDateChooser();

    // collection to hold date, data and label
    [Bindable]
    public var dateCollection:XMLList = new XMLList();

    private function init():void
    {
        eventList.send();
    }

    private function readCollection(event:ResultEvent):void
    {
        dateCollection = event.result.calendarevent;

        //Position and size the calendar
        mycal.width = 400;
        mycal.height = 400;
        //Add the data from the XML file to the calendar
        mycal.dateCollection = dateCollection;
        //Add the calendar to the canvas
        this.addChild(mycal);
    }

    private function readFaultHandler(event:FaultEvent):void
    {
        Alert.show(event.fault.message, "Could not load data");
    }
]]>
</mx:Script>

<mx:HTTPService id="eventList"
      url="data.xml"
      resultFormat="e4x"
      result="readCollection(event);"
      fault="readFaultHandler(event);"/>

 </mx:Application>

ExtendedDateChooser.as

package cyberslingers.controls
{
import flash.events.Event;
import flash.events.TextEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.CalendarLayout;
import mx.controls.DateChooser;
import mx.core.UITextField;
import mx.events.FlexEvent;



public class ExtendedDateChooser extends DateChooser
{
    public function ExtendedDateChooser()
    {
        super();
        this.addEventListener(TextEvent.LINK, linkHandler);
        this.addEventListener(FlexEvent.CREATION_COMPLETE, addEvents);
    }

    //datasource
    public var dateCollection:XMLList = new XMLList();

    //--------------------------------------
    //  Add events
    //--------------------------------------
    /**
     * Loop through calendar control and add event links
     * @param e
     */ 
    private function addEvents(e:Event):void
    {
        // loop through all the calendar children
        for(var i:uint = 0; i < this.numChildren; i++)
        {
            var calendarObj:Object = this.getChildAt(i);

            // find the CalendarLayout object
            if(calendarObj.hasOwnProperty("className"))
            {
                if(calendarObj.className == "CalendarLayout")
                {
                    var cal:CalendarLayout = CalendarLayout(calendarObj);

                    // loop through all the CalendarLayout children
                    for(var j:uint = 0; j < cal.numChildren; j++)
                    {
                        var dateLabel:Object = cal.getChildAt(j);

                        // find all UITextFields
                        if(dateLabel.hasOwnProperty("text"))
                        {
                            var day:UITextField = UITextField(dateLabel);

                            var dayHTML:String = day.text;
                            day.selectable = true;
                            day.wordWrap = true;
                            day.multiline = true;
                            day.styleName = "EventLabel";

                            //TODO: passing date as string is not ideal, tough to validate
                            //Make sure to add one to month since it is zero based
                            var eventArray:Array = dateHelper((this.displayedMonth+1) + "/" + dateLabel.text + "/" + this.displayedYear);
                            if(eventArray.length > 0)
                            {

                                for(var k:uint = 0; k < eventArray.length; k++)
                                {
                                    dayHTML += "<br><A HREF='event:" + eventArray[k].data + "' TARGET=''>" + eventArray[k].label + "</A>";
                                }
                                day.htmlText = dayHTML;
                            }
                        }
                    }
                }
            }
        }
    }

    //--------------------------------------
    //  Events
    //--------------------------------------
    /**
     * Handle clicking text link
     * @param e
     */ 
    private function linkHandler(event:TextEvent):void
    {
        // What do we want to do when user clicks an entry?
        Alert.show("selected: " + event.text);
    }

    //--------------------------------------
    //  Helpers
    //--------------------------------------
    /**
     * Build array of events for current date
     * @param string - current date
     * 
     */ 
    private function dateHelper(renderedDate:String):Array
    {
        var result:Array = new Array();
        for(var i:uint = 0; i < dateCollection.length(); i++)
        {
            if(dateCollection[i].date == renderedDate)
            {
                result.push(dateCollection[i]);
            }
        }
        return result;
    }

}
}

data.xml

<?xml version="1.0" encoding="utf-8"?>
<rss>

<calendarevent>
<date>8/22/2009</date>
<data>This is  a test 1</data>
<label>Stephens Test 1</label>
</calendarevent>

<calendarevent>
<date>8/23/2009</date>
<data>This is  a test 2</data>
<label>Stephens Test 2</label>
</calendarevent>

</rss>

© Stack Overflow or respective owner

Related posts about flex

Related posts about flex3