1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 2005-2006 Adobe Systems Incorporated
5//  All Rights Reserved.
6//
7//  NOTICE: Adobe permits you to use, modify, and distribute this file
8//  in accordance with the terms of the license agreement accompanying it.
9//
10////////////////////////////////////////////////////////////////////////////////
11
12package spark.effects
13{
14import mx.core.IVisualElement;
15import mx.core.IVisualElementContainer;
16import mx.core.mx_internal;
17import mx.effects.Effect;
18import mx.effects.effectClasses.PropertyChanges;
19import spark.effects.supportClasses.RemoveActionInstance;
20
21use namespace mx_internal;
22
23//--------------------------------------
24//  Excluded APIs
25//--------------------------------------
26
27[Exclude(name="duration", kind="property")]
28
29/**
30 *  The RemoveAction class defines an action effect that corresponds
31 *  to the RemoveChild property of a view state definition.
32 *  You use a RemoveAction effect within a transition definition
33 *  to control when the view state change defined by a RemoveChild property
34 *  occurs during the transition.
35 *
36 *  @mxml
37 *
38 *  <p>The <code>&lt;s:RemoveAction&gt;</code> tag
39 *  inherits all of the tag attributes of its superclass,
40 *  and adds the following tag attributes:</p>
41
42 *  <pre>
43 *  &lt;s:RemoveAction
44 *    <b>Properties</b>
45 *    id="ID"
46 *  /&gt;
47 *  </pre>
48 *
49 *  @see spark.effects.supportClasses.RemoveActionInstance
50 *  @see mx.states.RemoveChild
51 *
52 *  @langversion 3.0
53 *  @playerversion Flash 10
54 *  @playerversion AIR 1.5
55 *  @productversion Flex 4
56 */
57public class RemoveAction extends Effect
58{
59    include "../core/Version.as";
60
61    //--------------------------------------------------------------------------
62    //
63    //  Class constants
64    //
65    //--------------------------------------------------------------------------
66
67    /**
68     *  @private
69     */
70    private static var AFFECTED_PROPERTIES:Array = [ "parent", "index" ];
71
72    //--------------------------------------------------------------------------
73    //
74    //  Constructor
75    //
76    //--------------------------------------------------------------------------
77
78    /**
79     *  Constructor.
80     *
81     *  @param target The Object to animate with this effect.
82     *
83     *  @langversion 3.0
84     *  @playerversion Flash 10
85     *  @playerversion AIR 1.5
86     *  @productversion Flex 4
87     */
88    public function RemoveAction(target:Object = null)
89    {
90        super(target);
91        duration = 0;
92        instanceClass = RemoveActionInstance;
93    }
94
95    //--------------------------------------------------------------------------
96    //
97    //  Overridden methods
98    //
99    //--------------------------------------------------------------------------
100
101    /**
102     *  @private
103     */
104    override public function getAffectedProperties():Array /* of String */
105    {
106        return AFFECTED_PROPERTIES;
107    }
108
109    /**
110     *  @private
111     */
112    private function propChangesSortHandler(
113                    first:PropertyChanges,
114                    second:PropertyChanges):Number
115    {
116        if (first.start.index > second.start.index)
117            return 1;
118        else if (first.start.index < second.start.index)
119            return -1;
120
121        return 0;
122    }
123
124    /**
125     *  @private
126     */
127    override mx_internal function applyStartValues(propChanges:Array,
128                                              targets:Array):void
129    {
130        if (propChanges)
131            propChanges.sort(propChangesSortHandler);
132
133        super.applyStartValues(propChanges, targets);
134    }
135
136    /**
137     *  @private
138     */
139    override protected function getValueFromTarget(target:Object,
140                                                   property:String):*
141    {
142        var container:* = target.parent;
143        if (property == "index")
144            return container ?
145                    ((container is IVisualElementContainer) ?
146                    IVisualElementContainer(container).getElementIndex(target as IVisualElement) : container.getChildIndex(target))
147                : 0;
148
149        return super.getValueFromTarget(target, property);
150    }
151
152    /**
153     *  @private
154     */
155    override protected function applyValueToTarget(target:Object,
156                                                   property:String,
157                                                   value:*,
158                                                   props:Object):void
159    {
160        if (property == "parent" && value)
161        {
162            if (target.parent == null)
163            {
164                if (value is IVisualElementContainer)
165                    IVisualElementContainer(value).addElementAt(target as IVisualElement, Math.min(props.index,
166                        IVisualElementContainer(value).numElements));
167                else
168                    value.addChildAt(target, Math.min(props.index,
169                        value.numChildren));
170            }
171        }
172
173        // Ignore index - it's applied along with parent
174    }
175}
176
177}
178