hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/entities/view/EntityForm.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.*"
                                                         xmlns:components="it.ht.rcs.console.entities.view.components.*"
                                                         creationComplete="init()"
                                                         save="onSave(event)"
                                                         title="{currentState == CREATE ? R.get('NEW_ENTITY') : R.get('EDIT_ENTITY')}">

    <fx:Script>
        <![CDATA[
      import it.ht.rcs.console.accounting.controller.OperationManager;
      import it.ht.rcs.console.entities.controller.EntityManager;
      import it.ht.rcs.console.entities.model.Entity;
      import it.ht.rcs.console.entities.model.Position;
      import it.ht.rcs.console.entities.model.Position_attr;
      import it.ht.rcs.console.entities.view.graph.LinkGraph;
      import it.ht.rcs.console.events.SaveEvent;
      import it.ht.rcs.console.operation.model.Operation;
      import it.ht.rcs.console.target.controller.TargetManager;
      import it.ht.rcs.console.target.model.Target;
      import it.ht.rcs.console.utils.AlertPopUp;
      
      import locale.R;
      
      import mx.collections.ArrayCollection;
      import mx.events.CloseEvent;
      import mx.managers.PopUpManager;

            [Bindable]
            public var operation:Operation;
            [Bindable]
            public var entity:Entity;
      
      public var graph:LinkGraph

            public static const CREATE:String='create';
            public static const EDIT:String='edit';
            public static const CLOSE:String='close';
            [Bindable]
            private var types:ArrayCollection;


            private function init():void
            {
                entityName.setFocus();
                entityName.text=entity.name;
                desc.text=entity.desc;
                if (currentState == EDIT)
                {
                    types=new ArrayCollection(["PERSON", "TARGET"])
            
          if(!Console.currentSession.user.is_admin_targets())
          {
            types.removeItemAt(1)// no target
          }

                    typeContainer.visible=typeContainer.includeInLayout=entity.type == "person" && Console.currentSession.user.is_admin_targets();
                    type.selectedIndex=0;
                    if (entity.type == "position")
                    {
                        positionEditor.visible=positionEditor.includeInLayout=true;
                        positionEditor.set(entity);
                        PopUpManager.centerPopUp(this);
                    }
                }

                if (currentState == CREATE)
                {
                    typeContainer.visible=typeContainer.includeInLayout=true;
                    type.selectedIndex=0;
                    types=new ArrayCollection(["PERSON", "POSITION", "TARGET", "VIRTUAL"])
            if(!Console.currentSession.user.is_admin_targets())
            {
              types.removeItemAt(2)// no target
            }
                    if (entity.type == "position")
                    {
                        positionEditor.visible=positionEditor.includeInLayout=true;
                        positionEditor.reset();
                        type.visible=type.includeInLayout=false;
                        type.selectedIndex=1;
                        PopUpManager.centerPopUp(this);
                    }
          if (entity.type == "group")
          {
            typeContainer.visible=typeContainer.includeInLayout=false;
          }
                }
            }

            private function onSave(event:SaveEvent):void
            {

                doSave();
                dispatchEvent(new Event("update"))
                if (currentState == CREATE)
                {
                    if (entity.type != "target")
                    {
            if(entity.type=="group")
            {
              var children:Array=new Array()
              for(var i:int=0;i<graph.vis.selection.length;i++)
              {
                children.push(graph.vis.selection[i].data._id)
              }
                entity.children=new ArrayCollection(children)
                graph.vis.groupElements({name:     entityName.text, type:"group"},graph.vis.selection)
            }
                        EntityManager.instance.addEntity(entity, operation, function(e:Entity):void
                        {
                            entity=e;  
                            close();
                            dispatchEvent(new Event("close"))
                        });
                    }
                    else
                    {
                        trace("SAVING A TARGET")
            var t:Target=new Target()
            t.name = entityName.text;
            t.desc = desc.text;
            t.status = 'open';
           
            TargetManager.instance.addTarget(t,operation,function(t:Target):void
            {
             trace("target created")
              close();
              dispatchEvent(new Event("close"))
            });
                    }
                }
                else
                {
                    close();
          dispatchEvent(new Event("close"))
                }
            }

            private function doSave():void
            {
                entity.name=entityName.text;
                entity.desc=desc.text;
        
        
                if (currentState == CREATE)
                {
          if(entity.type!="group")
                    entity.type=type.selectedItem.toLowerCase();
                    if (entity.type == "position")
                    {
                        entity.position=new Position();
                        entity.position.latitude=String(positionEditor.position.latitude);
                        entity.position.longitude=String(positionEditor.position.longitude);
                        entity.position_attr=new Position_attr();
                        entity.position_attr.accuracy=String(positionEditor.position.accuracy);
                    }
                }
        else if(currentState == EDIT && entity.type=="person" && type.selectedItem=="TARGET")
        {
         
          EntityManager.instance.promoteToTarget(entity._id, function():void
          {
           EntityManager.instance.refresh()
          
          });
        }
        
                else if (currentState == EDIT && entity.type == "position") //problems updating
                {
                    if (entity.position)
                    {
                        entity.position.latitude=String(positionEditor.position.latitude);
                        entity.position.longitude=String(positionEditor.position.longitude);
                    }
                    if (entity.position_attr)
                    {
                        entity.position_attr.accuracy=String(positionEditor.position.accuracy);
                    }
                    var newPos:Position=new Position()
                    newPos.latitude=entity.position.latitude;
                    newPos.longitude=entity.position.longitude;
                    var newAttr:Position_attr=new Position_attr();
                    newAttr.accuracy=entity.position_attr.accuracy;
                    //
                    EntityManager.instance.updatePosition(entity, newPos, newAttr)
                }
        
        
            }

            private function onTypeChange():void
            {
                if (type.selectedItem == "POSITION")
                {
                    positionEditor.visible=positionEditor.includeInLayout=true;
                    positionEditor.reset();
                }
                else
                {
                    positionEditor.visible=positionEditor.includeInLayout=false;
                }
                PopUpManager.centerPopUp(this);
            }

            private function promoteToTarget():void
            {

            }
        ]]>
    </fx:Script>

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

    <s:VGroup horizontalAlign="center">

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

            <s:FormItem label="{R.get('NAME')}">
                <s:TextInput id="entityName"
                                         width="250"
                                         text="{entity.name}"
                                         maxChars="20"/>
            </s:FormItem>

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

            <s:FormItem label="{R.get('TYPE')}"
                                    id="typeContainer">
                <s:DropDownList id="type"
                                                width="150"
                                                requireSelection="true"
                                                change="onTypeChange()"
                                                dataProvider="{types}">

                </s:DropDownList>
            </s:FormItem>


        </s:Form>
        <components:PositionEditor id="positionEditor"
                                                             visible="false"
                                                             includeInLayout="false"
                                                             horizontalAlign="center"/>

    </s:VGroup>
</utils:TitleWindowSaveCancel>