hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/accounting/view/groups/GroupSelector.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<s:VGroup 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:components="it.ht.rcs.console.accounting.view.components.*"
          creationComplete="init()" gap="-1" addedToStage="onAddedToStage(event)" removedFromStage="onRemovedFromStage(event)">
  
  <fx:Script>
    <![CDATA[
      import it.ht.rcs.console.accounting.controller.GroupManager;
      import it.ht.rcs.console.accounting.model.Group;
      
      import mx.collections.ArrayCollection;
      import mx.collections.ArrayList;
      import mx.collections.IList;
      import mx.collections.ListCollectionView;
      
      import spark.events.IndexChangeEvent;
      import spark.filters.DropShadowFilter;
      
      [Bindable] private var selectedDP:ArrayCollection;
      [Bindable] private var availableDP:ListCollectionView;
      
      
      public var groupsToAdd:ArrayCollection;
      public var groupsToRemove:ArrayCollection;
      
      private function init():void
      {
        //addBtn.mx_internal::getCurrentIcon()=null;
        groupsToAdd=new ArrayCollection;
        groupsToRemove=new ArrayCollection;
        availableDP = GroupManager.instance.getView(null, listFilterFunction);
        if (selectedDP == null)
          selectedDP = new ArrayCollection();
        
      }
      
      public function set dataProvider(dp:ArrayCollection):void { selectedDP = new ArrayCollection(dp.toArray()); } // Work on copy, otherwise changes are commited immediately!
      public function get dataProvider():ArrayCollection { return selectedDP; }
      
      private function addGroup(event:Event):void
      {
        var _id:String = availableGroups.selectedItem ? availableGroups.selectedItem._id : null;
        if (_id == null) return;
        selectedDP.addItem(_id);
        //add the selected group to the new groups list
        groupsToAdd.addItem(availableGroups.selectedItem)
        availableDP.refresh();
        //dump()
      }
      
      private function removeGroup(event:MouseEvent=null):void
      {
        var idx:int = selectedDP.getItemIndex(selectedGroups.selectedItem);
        
        var groupToRemove:Group= GroupManager.instance.getItem(selectedGroups.selectedItem as String);
          
        //add the selected group to the list of groups to remove
        if(groupsToAdd.getItemIndex(groupToRemove)==-1)
        //is not in the list to add - put in the list to remove
        {
          groupsToRemove.addItem(groupToRemove)
        }
        else
        {
          //is in the list to add - remove from that list
          var index:int=groupsToAdd.getItemIndex(groupToRemove)
          groupsToAdd.removeItemAt(index)
        }
        selectedDP.removeItemAt(idx);
        selectedGroups.selectedIndex = idx < selectedDP.length ? idx : idx - 1;
       
        availableDP.refresh();
        //dump()
      }
      
     /*  private function dump():void
      {
      var i:int;
       trace("=================")
       trace("TO ADD")
       for(var i=0;i<groupsToAdd.length;i++)
         trace(groupsToAdd.getItemAt(i).name)
       trace("TO REMOVE")
       for(var i=0;i<groupsToRemove.length;i++)
         trace(groupsToRemove.getItemAt(i).name)
      } */
      
      private function listFilterFunction(item:Object):Boolean
      {
        try {
          return selectedDP.getItemIndex(item._id) == -1;
        } catch (e:Error) {}
        return true;
      }
      
      private function groupLabelFunction(_id:Object):String
      {
        var group:Group = GroupManager.instance.getItem(_id as String);
        return group ? group.name : '';
      }
      
      private function onKeyUp(event:KeyboardEvent):void
      {
        if (event.keyCode == Keyboard.DELETE && selectedGroups.selectedItem)
          removeGroup();
      }
      
      protected function onAddedToStage(event:Event):void
      {
        stage.addEventListener(MouseEvent.CLICK, onStageClick)
        
      }
      
      protected function onRemovedFromStage(event:Event):void
      {
        popup.displayPopUp=false;
        stage.removeEventListener(MouseEvent.CLICK, onStageClick)
        
      }
      
      private function onStageClick(e:MouseEvent):void
      {
        if(e.target!=addBtn)
          popup.displayPopUp=false;
      }
      
    ]]>
  </fx:Script>
  
  <s:List id="selectedGroups" width="100%" height="100" minWidth="0" dataProvider="{selectedDP}"
          labelFunction="groupLabelFunction" keyUp="onKeyUp(event)" borderColor="#2d2d2d"/>
  
  <s:HGroup width="100%" gap="-1" horizontalAlign="right">

    <s:Button width="30" label="-" click="removeGroup(event)" cornerRadius="0"
              enabled="{selectedGroups.selectedItem != null}"/>
    <s:Button width="30" label="+"  cornerRadius="0" id="addBtn" click="{popup.displayPopUp=true}"/>
      <s:PopUpAnchor id="popup" popUpPosition="above">
        <s:List id="availableGroups" click="addGroup(event)" dataProvider="{availableDP}"
                labelField="name" textAlign="left" filters="{[dropShadow]}" maxHeight="200" maxWidth="200" width="200" height="200"/>
      </s:PopUpAnchor>

    
  </s:HGroup>
  
  <fx:Declarations>
    <s:DropShadowFilter id="dropShadow" blurX="20" blurY="20" alpha="0.25" distance="8" angle="90" color="#333333"/>
  </fx:Declarations>
  
</s:VGroup>