hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/operations/view/agents/AgentsForm.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<utils:TitleWindowSaveCancel 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:utils="it.ht.rcs.console.utils.*"
                                                         creationComplete="init()"
                                                         save="onSave(event)"
                                                         title="{R.get('EDIT_AGENT')}">

    <fx:Script>
        <![CDATA[
            import it.ht.rcs.console.agent.model.Agent;
            import it.ht.rcs.console.events.SaveEvent;
            import it.ht.rcs.console.operation.model.Operation;
            import it.ht.rcs.console.target.model.Target;
            import it.ht.rcs.console.utils.AlertPopUp;

            import locale.R;

            import mx.events.CloseEvent;

            public static const EDIT:String='edit';

            [Bindable]
            public var operation:Operation;
            [Bindable]
            public var target:Target;
            [Bindable]
            public var agent:Agent;

            private function init():void
            {
                if (currentState == EDIT)
                {
                    for each (var t:Object in types)
                        if (t.label.toLowerCase() == agent.type)
                            type.selectedItem=t;

                    for each (var s:Object in statuses)
                        if (s.label.toLowerCase() == agent.status)
                            status.selectedItem=s;
                }

                agentName.setFocus();
            }

            private function onSave(event:SaveEvent):void
            {
                if (currentState == EDIT && getStatus() != agent.status)
                {
                    AlertPopUp.show(R.get('CONFIRM_AGENT_CLOSE', [agent.name]), R.get('CONFIRM'), AlertPopUp.YES | AlertPopUp.NO, null, function(e:CloseEvent):void
                    {
                        if (e.detail == AlertPopUp.YES)
                        {
                            doSave();
                            close();
                        }
                    }, null, AlertPopUp.NO);
                    return;
                }

                doSave();
                close();
            }

            private function getStatus():String
            {
                return status.selectedIndex == 0 ? 'open' : 'closed';
            }

            private function doSave():void
            {
                agent.name=agentName.text;
                agent.desc=desc.text;
                agent.type=type.selectedItem.label.toLowerCase();
                agent.status=getStatus();
            }
        ]]>
    </fx:Script>

    <utils:states>
        <s:State name="edit"/>
    </utils:states>

    <s:Form width="100%"
                    defaultButton="{saveButton}">

        <s:FormItem label="{R.get('NAME')}">
            <s:TextInput id="agentName"
                                     width="250"
                                     text="{agent.name}"
                                     restrict="A-Za-z0-9'\-_() "
                                     maxChars="20"/>
        </s:FormItem>

        <s:FormItem label="{R.get('DESCRIPTION')}">
            <s:TextArea id="desc"
                                    width="250"
                                    heightInLines="3"
                                    text="{agent.desc}"/>
        </s:FormItem>

        <s:FormItem label="{R.get('TYPE')}"
                                enabled="false">
            <s:DropDownList id="type"
                                            width="150"
                                            selectedIndex="0">
                <s:ArrayCollection id="types">
                    <fx:Object label="DESKTOP"/>
                    <fx:Object label="MOBILE"/>
                </s:ArrayCollection>
            </s:DropDownList>
        </s:FormItem>

        <s:FormItem label="{R.get('STATUS')}"
                                visible="{currentState == EDIT}"
                                includeInLayout="{currentState == EDIT}"
                                enabled="{currentState == EDIT &amp;&amp; agent.status == 'open'}">
            <s:DropDownList id="status"
                                            width="150"
                                            requireSelection="true">
                <s:ArrayCollection id="statuses">
                    <fx:Object label="OPEN"/>
                    <fx:Object label="CLOSED"/>
                </s:ArrayCollection>
            </s:DropDownList>
        </s:FormItem>

    </s:Form>

</utils:TitleWindowSaveCancel>