hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/entities/view/components/advanced/timeline/Tracker.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
                 xmlns:s="library://ns.adobe.com/flex/spark"
                 xmlns:mx="library://ns.adobe.com/flex/mx"
                 xmlns:timeline="it.ht.rcs.console.entities.view.components.advanced.timeline.*"  resize="onResize()">
    <fx:Metadata>
    [Event(name="dateChange", type="flash.events.Event")]
    [Event(name="ready", type="flash.events.Event")]
  </fx:Metadata>
    <fx:Script>
        <![CDATA[
            import it.ht.rcs.console.entities.model.PositionsFlow;

            import mx.collections.ArrayCollection;

            [Embed(source="img/mediaplayers/playMedium.png")]
            [Bindable]
            public var playIcon:Class;

            [Embed(source="img/mediaplayers/pauseMedium.png")]
            [Bindable]
            public var pauseIcon:Class;

            [Embed(source="img/mediaplayers/stopMedium.png")]
            [Bindable]
            public var stopIcon:Class;

            private var monthNames:Array=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

            [Bindable]
            public var currentDate:Date;
            [Bindable]
            public var startDate:Date;
            [Bindable]
            public var endDate:Date;

            [Bindable]
            public var currentItem:HourRenderer;
      
      private function onResize():void
      {
        scrollbar.visible=scrollbar.includeInLayout=timeline.width > this.width;
      }

            private function doPlay():void
            {
                if (timeline.isPlaying)
                {
                    timeline.doStop()
                }
                else
                {
                    timeline.doPlay()

                }
            }

            public function doStop():void
            {
                if (timeline.isPlaying)
                {
                    timeline.doStop()
                }
            }

            private function onTimelineStop():void
            {
                playBtn.toolTip="play"
                playBtn.setStyle("icon", playIcon)
            }

            private function onTimelinePlay():void
            {
                playBtn.toolTip="stop"
                playBtn.setStyle("icon", stopIcon)
            }

            private function doubleDigits(n:Number):String
            {
                if (n < 10)
                    return String("0" + n)
                else
                    return String(n)
            }

            public function populateMinutes(positions:ArrayCollection):void
            {
                var i:int;
                for (i=0; i < timeline.frames.length; i++)
                {
                    var h:HourRenderer=timeline.frames[i] as HourRenderer
                        //h.reset()
                }

                for (i=0; i < positions.length; i++)
                {
                    var d:Date=new Date()
                    d.time=positions.getItemAt(i).time * 1000;
                    var key:String=formatDate(d);
                    //("key: " + key)
                    var renderer:HourRenderer=timeline.hours[key] as HourRenderer;
                    if(renderer) renderer.populateMinute(positions.getItemAt(i) as PositionsFlow);
                }
            }

            public function populate(positions:ArrayCollection):void
            {
                var i:int;
                for (i=0; i < timeline.frames.length; i++)
                {
                    var h:HourRenderer=timeline.frames[i] as HourRenderer
                    h.reset()
                }
                for (i=0; i < positions.length; i++)
                {
                    var d:Date=new Date()
                    d.time=positions.getItemAt(i).time * 1000;
                    var key:String=formatDate(d);
                    //trace("key summary: " + key)
                    var renderer:HourRenderer=timeline.hours[key] as HourRenderer;
                    if(renderer) renderer.populate(positions.getItemAt(i) as PositionsFlow);
                }
            }

            public function reset():void
            {
                timeline.reset()
                //dispatchEvent(new Event("dateChange"));
            }

            public function draw():void //called when date range change
            {
                timeline.draw()
        onDateChange();
            }

            public function init():void
            {
                /*     currentDate=timeline.currentDate;
                    currentItem=null //?? TODO
                    dispatchEvent(new Event("dateChange")); */
                timeline.init()
            }

            private function onTimelineReady():void
            {
                dispatchEvent(new Event("ready"))
        scrollbar.visible=scrollbar.includeInLayout=timeline.width > this.width;
       
            }

            private function onDateChange():void
            {
                currentDate=timeline.currentDate;
                currentItem=timeline.currentItem;
                currentDateTxt.text=doubleDigits(timeline.currentDate.date) + " " + monthNames[timeline.currentDate.month] + " " + doubleDigits(timeline.currentDate.fullYear) + " " + doubleDigits(timeline.currentDate.hours) + ":" + doubleDigits(timeline.currentDate.minutes);
                dispatchEvent(new Event("dateChange"));
        scrollbar.visible=scrollbar.includeInLayout=timeline.width > this.width;
            }

            private function formatDate(d:Date):String
            {
                if (!d)
                    return "";
                return String(d.fullYear) + doubleDigits(d.month + 1) + doubleDigits(d.date) + doubleDigits(d.hours);
            }

            private function changeSpeed():void
            {
                timeline.t.delay=speed.value * 100
            }
        ]]>
    </fx:Script>

  <s:VGroup gap="4"
                        width="100%">


        <s:Group width="100%"
                         clipAndEnableScrolling="true"
                         height="136"
                         id="viewport">
            <s:Rect width="100%"
                            height="100%">
                <s:fill>
                    <s:BitmapFill source="@Embed('img/backgrounds/geomaptimeline.png')"
                                                fillMode="repeat"/>
                </s:fill>
            </s:Rect>

            <timeline:AdvancedTimeline width="100%"
                                                                 id="timeline"
                                                                 dateChange="onDateChange()"
                                                                 stop="onTimelineStop()"
                                                                 play="onTimelinePlay()"
                                                                 startDate="{startDate}"
                                                                 endDate="{endDate}"
                                                                 ready="onTimelineReady()"/>
        </s:Group>
        <s:HScrollBar viewport="{viewport}"
                                    width="100%"
                                    id="scrollbar"
                                    stepSize="30"
                  visible="{timeline.width>this.width}"
                  includeInLayout="{timeline.width>this.width}"/>
        <s:HGroup verticalAlign="middle">
            <s:Button label=""
                                click="doPlay()"
                                toolTip="play"
                                id="playBtn"
                                icon="{playIcon}"
                                width="24"
                                height="24"/>
            <s:Spacer width="30"/>
      <s:Label text="speed"/>
      <s:HSlider minimum="1"
                 maximum="5"
                 value="2.5"
                 stepSize="0.5"
                 liveDragging="true"
                 change="changeSpeed()"
                 id="speed"
                 showDataTip="false"
                 layoutDirection="rtl"/>
            
            <s:Spacer width="30"/>
      <s:Label id="currentDateTxt" fontWeight="bold"/>
        
        </s:HGroup>
    </s:VGroup>

</s:Group>