免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1531 | 回复: 0
打印 上一主题 下一主题

mx.styles.CSSStyleDeclaration [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:54 |只看该作者 |倒序浏览
1.概述
       继承    EventDispatcher->Object
       子类    AdvancedDataGridColumn, DataGridColumn
类的作用
       CSSStyleDeclaration 类表示一组 CSS 样式规则。MXML 编译器在和 Flex 应用程序关联的 CSS 文件中为每个选择器自动生成一个 CSSStyleDeclaration 对象。

CSS 规则--类型选择器,如
      Button { color: #FF0000 }
影响 Button 类的每个实例;如 Button 之类的选择器称为类型选择器,不能以点开头。

CSS 规则--类选择器,如
      .redButton { color: #FF0000 }
只影响其 styleName 属性设置为“.redButton”的组件;如 .redButton 之类的选择器称为类选择器,必须以点开头。

2.常见属性和方法
2.1 clearStyle()方法
       格式:public function clearStyle(styleProp:String):void
       功能:清除该 CSSStyleDeclaration 上的样式属性。这和将样式值设置为 undefined 相同。
       参数
              styleProp:String — 样式属性的名称。

2.2 getStyle()方法     
       格式:public function getStyle(styleProp:String):*
       功能:获取指定样式属性的值,它完全由该 CSSStyleDeclaration 确定。
       参数
              styleProp:String — 样式属性的名称。
       返回值
              返回值可以为任何类型。值 null、""、false、NaN 和 0 都是有效的样式值(如果设置样式),或 undefined(如果未设置样式)。 可以使用 StyleManager.isValidStyleValue() 方法来测试返回的值。

2.3 setStyle()方法     
       格式:public function setStyle(styleProp:String, newValue:*):void
       功能:在该 CSSStyleDeclaration 上设置样式属性。
       参数
              styleProp:String — 样式属性的名称。
              newValue:*  — 样式属性的值。该值可以为任何类型。值 null、""、false、NaN 和0 都是有效的样式值


附件:源代码
  1. package mx.styles
  2. {

  3. import flash.display.DisplayObject;
  4. import flash.events.EventDispatcher;
  5. import flash.utils.Dictionary;

  6. import mx.core.Singleton;
  7. import mx.core.mx_internal;
  8. import mx.events.FlexChangeEvent;
  9. import mx.managers.ISystemManager;
  10. import mx.managers.SystemManagerGlobals;
  11. import mx.utils.ObjectUtil;

  12. use namespace mx_internal;

  13. /**
  14.  * The CSSStyleDeclaration class represents a set of CSS style rules.
  15.  * The MXML compiler automatically generates one CSSStyleDeclaration object
  16.  * for each selector in the CSS files associated with a Flex application.
  17.  *
  18.  * <p>A CSS rule such as
  19.  * <pre>
  20.  * Button { color: #FF0000 }
  21.  * </pre>
  22.  * affects every instance of the Button class;
  23.  * a selector like <code>Button</code> is called a type selector
  24.  * and must not start with a dot.</p>
  25.  *
  26.  * <p>A CSS rule such as
  27.  * <pre>
  28.  * .redButton { color: #FF0000 }
  29.  * </pre>
  30.  * affects only components whose <code>styleName</code> property
  31.  * is set to <code>"redButton"</code>;
  32.  * a selector like <code>.redButton</code> is called a class selector
  33.  * and must start with a dot.</p>
  34.  *
  35.  * <p>You can access the autogenerated CSSStyleDeclaration objects
  36.  * using the <code>StyleManager.getStyleDeclaration()</code> method,
  37.  * passing it either a type selector
  38.  * <pre>
  39.  * var buttonDeclaration:CSSStyleDeclaration =
  40.  * StyleManager.getStyleDeclaration("Button");
  41.  * </pre>
  42.  * or a class selector
  43.  * <pre>
  44.  * var redButtonStyleDeclaration:CSSStyleDeclaration =
  45.  * StyleManager.getStyleDeclaration(".redButton");
  46.  * </pre>
  47.  * </p>
  48.  *
  49.  * <p>You can use the <code>getStyle()</code>, <code>setStyle()</code>,
  50.  * and <code>clearStyle()</code> methods to get, set, and clear
  51.  * style properties on a CSSStyleDeclaration.</p>
  52.  *
  53.  * <p>You can also create and install a CSSStyleDeclaration at run time
  54.  * using the <code>StyleManager.setStyleDeclaration()</code> method:
  55.  * <pre>
  56.  * var newStyleDeclaration:CSSStyleDeclaration = new CSSStyleDeclaration(".bigMargins");
  57.  * newStyleDeclaration.defaultFactory = function():void
  58.  * {
  59.  * leftMargin = 50;
  60.  * rightMargin = 50;
  61.  * }
  62.  * StyleManager.setStyleDeclaration(".bigMargins", newStyleDeclaration, true);
  63.  * </pre>
  64.  * </p>
  65.  *
  66.  * @see mx.core.UIComponent
  67.  * @see mx.styles.StyleManager
  68.  */
  69. public class CSSStyleDeclaration extends EventDispatcher
  70. {
  71.     include "../core/Version.as";

  72.     //--------------------------------------------------------------------------
  73.     //
  74.     // Class constants
  75.     //
  76.     //--------------------------------------------------------------------------

  77.     /**
  78.      * @private
  79.      */
  80.     private static const NOT_A_COLOR:uint = 0xFFFFFFFF;

  81.     /**
  82.      * @private
  83.      */
  84.     private static const FILTERMAP_PROP:String = "__reserved__filterMap";
  85.     
  86.     //--------------------------------------------------------------------------
  87.     //
  88.     // Constructor
  89.     //
  90.     //--------------------------------------------------------------------------

  91.     /**
  92.      * Constructor.
  93.      *
  94.      * @param selector - If the selector is a CSSSelector then advanced
  95.      * CSS selectors are supported. If a String is used for the selector then
  96.      * only simple CSS selectors are supported. If the String starts with a
  97.      * dot it is interpreted as a universal class selector, otherwise it must
  98.      * represent a simple type selector. If not null, this CSSStyleDeclaration
  99.      * will be registered with StyleManager.
  100.      *
  101.      * @param styleManager - The style manager to set this declaration into. If the
  102.      * styleManager is null the top-level style manager will be used.
  103.      *
  104.      * @param autoRegisterWithStyleManager - If true set the selector in the styleManager. The selector
  105.      * will only be set if both <code>selector</code> and <code>styleManager</code> are
  106.      * both non-null.
  107.      *
  108.      * @langversion 3.0
  109.      * @playerversion Flash 9
  110.      * @playerversion AIR 1.1
  111.      * @productversion Flex 3
  112.      */
  113.     public function CSSStyleDeclaration(selector:Object=null, styleManager:IStyleManager2=null, autoRegisterWithStyleManager:Boolean = true)
  114.     {
  115.         super();

  116.         // Do not reference StyleManager directly because this is a bootstrap class
  117.         if (!styleManager)
  118.             styleManager = Singleton.getInstance("mx.styles::IStyleManager2") as IStyleManager2;

  119.         this.styleManager = styleManager;

  120.         if (selector)
  121.         {
  122.             if (selector is CSSSelector)
  123.             {
  124.                 this.selector = selector as CSSSelector;
  125.             }
  126.             else
  127.             {
  128.                 // Otherwise, a legacy Flex 3 String selector was provided
  129.                 selectorString = selector.toString();
  130.             }

  131.             if (autoRegisterWithStyleManager)
  132.                 styleManager.setStyleDeclaration(selectorString, this, false);
  133.         }

  134.     }

  135.     //--------------------------------------------------------------------------
  136.     //
  137.     // Variables
  138.     //
  139.     //--------------------------------------------------------------------------

  140.     /**
  141.      * @private
  142.      * This Dictionary keeps track of all the style name/value objects
  143.      * produced from this CSSStyleDeclaration and already inserted into
  144.      * prototype chains. Whenever this CSSStyleDeclaration's overrides object
  145.      * is updated by setStyle(), these clone objects must also be updated.
  146.      */
  147.     private var clones:Dictionary = new Dictionary(true);

  148.     /**
  149.      * @private
  150.      * The number of CSS selectors pointing to this CSSStyleDeclaration.
  151.      * It will be greater than 0 if this CSSStyleDeclaration has been
  152.      * installed in the StyleManager.styles table by
  153.      * StyleManager.setStyleDeclaration().
  154.      */
  155.     mx_internal var selectorRefCount:int = 0;

  156.     /**
  157.      * @private
  158.      * Array that specifies the names of the events declared
  159.      * by this CSS style declaration.
  160.      * This Array is used by the <code>StyleProtoChain.initObject()</code>
  161.      * method to register the effect events with the Effect manager.
  162.      */
  163.     mx_internal var effects:Array;

  164.     /**
  165.      * @private
  166.      * reference to StyleManager
  167.      */
  168.     private var styleManager:IStyleManager2;

  169.     //--------------------------------------------------------------------------
  170.     //
  171.     // Properties
  172.     //
  173.     //--------------------------------------------------------------------------
  174.     
  175.     //----------------------------------
  176.     // defaultFactory
  177.     //----------------------------------

  178.     private var _defaultFactory:Function;
  179.     
  180.     [Inspectable(environment="none")]
  181.     
  182.     /**
  183.      * This function, if it isn't <code>null</code>,
  184.      * is usually autogenerated by the MXML compiler.
  185.      * It produce copies of a plain Object, such as
  186.      * <code>{ leftMargin: 10, rightMargin: 10 }</code>,
  187.      * containing name/value pairs for style properties; the object is used
  188.      * to build a node of the prototype chain for looking up style properties.
  189.      *
  190.      * <p>If this CSSStyleDeclaration is owned by a UIComponent
  191.      * written in MXML, this function encodes the style attributes
  192.      * that were specified on the root tag of the component definition.</p>
  193.      *
  194.      * <p>If the UIComponent was written in ActionScript,
  195.      * this property is <code>null</code>.</p>
  196.      *
  197.      * @langversion 3.0
  198.      * @playerversion Flash 9
  199.      * @playerversion AIR 1.1
  200.      * @productversion Flex 3
  201.      */
  202.     public function get defaultFactory():Function
  203.     {
  204.         return _defaultFactory;
  205.     }
  206.     
  207.     /**
  208.      * @private
  209.      */
  210.     public function set defaultFactory(f:Function):void
  211.     {
  212.         _defaultFactory = f;
  213.     }
  214.     
  215.     //----------------------------------
  216.     // factory
  217.     //----------------------------------

  218.     private var _factory:Function;

  219.     [Inspectable(environment="none")]
  220.     
  221.     /**
  222.      * This function, if it isn't <code>null</code>,
  223.      * is usually autogenerated by the MXML compiler.
  224.      * It produce copies of a plain Object, such as
  225.      * <code>{ leftMargin: 10, rightMargin: 10 }</code>,
  226.      * containing name/value pairs for style properties; the object is used
  227.      * to build a node of the prototype chain for looking up style properties.
  228.      *
  229.      * <p>If this CSSStyleDeclaration is owned by a UIComponent,
  230.      * this function encodes the style attributes that were specified in MXML
  231.      * for an instance of that component.</p>
  232.      *
  233.      * @langversion 3.0
  234.      * @playerversion Flash 9
  235.      * @playerversion AIR 1.1
  236.      * @productversion Flex 3
  237.      */
  238.     public function get factory():Function
  239.     {
  240.         return _factory;
  241.     }
  242.     
  243.     /**
  244.      * @private
  245.      */
  246.     public function set factory(f:Function):void
  247.     {
  248.         _factory = f;
  249.     }

  250.     //----------------------------------
  251.     // overrides
  252.     //----------------------------------

  253.     private var _overrides:Object;

  254.     /**
  255.      * If the <code>setStyle()</code> method is called on a UIComponent or CSSStyleDeclaration
  256.      * at run time, this object stores the name/value pairs that were set;
  257.      * they override the name/value pairs in the objects produced by
  258.      * the methods specified by the <code>defaultFactory</code> and
  259.      * <code>factory</code> properties.
  260.      *
  261.      * @langversion 3.0
  262.      * @playerversion Flash 9
  263.      * @playerversion AIR 1.1
  264.      * @productversion Flex 3
  265.      */
  266.     public function get overrides():Object
  267.     {
  268.         return _overrides;
  269.     }
  270.     
  271.     /**
  272.      * @private
  273.      */
  274.     public function set overrides(o:Object):void
  275.     {
  276.         _overrides = o;
  277.     }
  278.     
  279.     //----------------------------------
  280.     // selector
  281.     //----------------------------------

  282.     private var _selector:CSSSelector;

  283.     /**
  284.      * This property is the base selector of a potential chain of selectors
  285.      * and conditions that are used to match CSS style declarations to
  286.      * components.
  287.      *
  288.      * @langversion 3.0
  289.      * @playerversion Flash 10
  290.      * @playerversion AIR 1.5
  291.      * @productversion Flex 4
  292.      */
  293.     public function get selector():CSSSelector
  294.     {
  295.         return _selector;
  296.     }

  297.     public function set selector(value:CSSSelector):void
  298.     {
  299.         _selector = value;
  300.         _selectorString = null;
  301.     }

  302.     //----------------------------------
  303.     // selectorString
  304.     //----------------------------------

  305.     private var _selectorString:String;

  306.     /**
  307.      * Legacy support for setting a Flex 3 styled selector string after
  308.      * the construction of a style declaration. Only universal class selectors
  309.      * or simple type selectors are supported. Note that this style declaration
  310.      * is not automatically registered with the StyleManager when using this
  311.      * API.
  312.      *
  313.      * @langversion 3.0
  314.      * @playerversion Flash 10
  315.      * @playerversion AIR 1.5
  316.      * @productversion Flex 4
  317.      */
  318.     mx_internal function get selectorString():String
  319.     {
  320.         if (_selectorString == null && _selector != null)
  321.             _selectorString = _selector.toString();

  322.         return _selectorString;
  323.     }

  324.     mx_internal function set selectorString(value:String):void
  325.     {
  326.         // For the legacy API, the first argument is either a simple
  327.         // type selector or a universal class selector
  328.         if (value.charAt(0) == ".")
  329.         {
  330.             var condition:CSSCondition = new CSSCondition(CSSConditionKind.CLASS, value.substr(1));
  331.             _selector = new CSSSelector("", [condition]);
  332.         }
  333.         else
  334.         {
  335.             _selector = new CSSSelector(value);
  336.         }

  337.         _selectorString = value;
  338.     }

  339.     //----------------------------------
  340.     // specificity
  341.     //----------------------------------

  342.     /**
  343.      * Determines the order of precedence when applying multiple style
  344.      * declarations to a component. If style declarations are of equal
  345.      * precedence, the last one wins.
  346.      *
  347.      * @langversion 3.0
  348.      * @playerversion Flash 10
  349.      * @playerversion AIR 1.5
  350.      * @productversion Flex 4
  351.      */
  352.     public function get specificity():int
  353.     {
  354.         return _selector ? _selector.specificity : 0;
  355.     }

  356.     //----------------------------------
  357.     // subject
  358.     //----------------------------------

  359.     /**
  360.      * The subject describes the name of a component that may be a potential
  361.      * match for this style declaration. The subject is determined as right
  362.      * most simple type selector in a potential chain of selectors.
  363.      *
  364.      * @langversion 3.0
  365.      * @playerversion Flash 10
  366.      * @playerversion AIR 1.5
  367.      * @productversion Flex 4
  368.      */
  369.     public function get subject():String
  370.     {
  371.         if (_selector != null)
  372.         {
  373.             // Check for an implicit universal selector which omits *
  374.             // for the subject but includes conditions.
  375.             if (_selector.subject == "" && _selector.conditions)
  376.                 return "*";
  377.             else
  378.                 return _selector.subject;
  379.         }

  380.         return null;
  381.     }

  382.     //--------------------------------------------------------------------------
  383.     //
  384.     // Methods
  385.     //
  386.     //--------------------------------------------------------------------------

  387.     /**
  388.      * @private
  389.      * Determines whether the selector chain for this style declaration makes
  390.      * use of a pseudo condition.
  391.      */
  392.     mx_internal function getPseudoCondition():String
  393.     {
  394.         return (selector != null) ? selector.getPseudoCondition() : null;
  395.     }

  396.     /**
  397.      * @private
  398.      * Determines whether this style declaration has an advanced selector.
  399.      */
  400.     mx_internal function isAdvanced():Boolean
  401.     {
  402.         if (selector != null)
  403.         {
  404.             if (selector.ancestor)
  405.             {
  406.                 return true;
  407.             }
  408.             else if (selector.conditions)
  409.             {
  410.                 if (subject != "*" && subject != "global")
  411.                 {
  412.                     return true;
  413.                 }

  414.                 for each (var condition:CSSCondition in selector.conditions)
  415.                 {
  416.                     if (condition.kind != CSSConditionKind.CLASS)
  417.                     {
  418.                         return true;
  419.                     }
  420.                 }
  421.             }
  422.         }

  423.         return false;
  424.     }

  425.     /**
  426.      * Determines whether this style declaration applies to the given component
  427.      * based on a match of the selector chain.
  428.      *
  429.      * @param object The component to match the style declaration against.
  430.      *
  431.      * @return true if this style declaration applies to the component,
  432.      * otherwise false.
  433.      *
  434.      * @langversion 3.0
  435.      * @playerversion Flash 10
  436.      * @playerversion AIR 1.5
  437.      * @productversion Flex 4
  438.      */
  439.     public function matchesStyleClient(object:IAdvancedStyleClient):Boolean
  440.     {
  441.         return (selector != null) ? selector.matchesStyleClient(object) : false;
  442.     }

  443.     /**
  444.      * Determine if the properties of this style declaration are the same as the the properties of a specified
  445.      * style declaration.
  446.      *
  447.      * @param styleDeclaration the style declaration to compare.
  448.      *
  449.      * @return true if the styleDeclaration is considered equal to this declaration.
  450.      */
  451.     mx_internal function equals(styleDeclaration:CSSStyleDeclaration):Boolean
  452.     {
  453.         if (styleDeclaration == null)
  454.             return false;
  455.         
  456.         // test in order of most likey to be different.
  457.         
  458.         var obj:Object; // loop variable
  459.         
  460.         // overrides
  461.         if (ObjectUtil.compare(overrides, styleDeclaration.overrides) != 0)
  462.             return false;

  463.         // factory
  464.         if ((factory == null && styleDeclaration.factory != null) ||
  465.             (factory != null && styleDeclaration.factory == null))
  466.             return false;

  467.         if (factory != null)
  468.         {
  469.             if (ObjectUtil.compare(new factory(), new styleDeclaration.factory()) != 0)
  470.                 return false;
  471.         }
  472.         
  473.         // defaultFactory
  474.         if ((defaultFactory == null && styleDeclaration.defaultFactory != null) ||
  475.             (defaultFactory != null && styleDeclaration.defaultFactory == null))
  476.             return false;
  477.         
  478.         if (defaultFactory != null)
  479.         {
  480.             if (ObjectUtil.compare(new defaultFactory(),
  481.                                    new styleDeclaration.defaultFactory()) != 0)
  482.                 return false;
  483.         }
  484.         
  485.         // effects
  486.         if (ObjectUtil.compare(effects, styleDeclaration.mx_internal::effects))
  487.             return false;
  488.                 
  489.         return true;
  490.     }
  491.     

  492.     /**
  493.      * Gets the value for a specified style property,
  494.      * as determined solely by this CSSStyleDeclaration.
  495.      *
  496.      * <p>The returned value may be of any type.</p>
  497.      *
  498.      * <p>The values <code>null</code>, <code>""</code>, <code>false</code>,
  499.      * <code>NaN</code>, and <code>0</code> are all valid style values,
  500.      * but the value <code>undefined</code> is not; it indicates that
  501.      * the specified style is not set on this CSSStyleDeclaration.
  502.      * You can use the method <code>StyleManager.isValidStyleValue()</code>
  503.      * to test the value that is returned.</p>
  504.      *
  505.      * @param styleProp The name of the style property.
  506.      *
  507.      * @return The value of the specified style property if set,
  508.      * or <code>undefined</code> if not.
  509.      *
  510.      * @langversion 3.0
  511.      * @playerversion Flash 9
  512.      * @playerversion AIR 1.1
  513.      * @productversion Flex 3
  514.      */
  515.     public function getStyle(styleProp:String):*
  516.     {
  517.         var o:*;
  518.         var v:*;

  519.         // First look in the overrides, in case setStyle()
  520.         // has been called on this CSSStyleDeclaration.
  521.         if (overrides)
  522.         {
  523.             // If the property exists in our overrides, but
  524.             // has 'undefined' as its value, it has been
  525.             // cleared from this stylesheet so return
  526.             // undefined.
  527.             if (styleProp in overrides &&
  528.                 overrides[styleProp] === undefined)
  529.                 return undefined;
  530.                 
  531.             v = overrides[styleProp];
  532.             if (v !== undefined) // must use !==
  533.                 return v;
  534.         }

  535.         // Next look in the style object that this CSSStyleDeclaration's
  536.         // factory function produces; it contains styles that
  537.         // were specified in an instance tag of an MXML component
  538.         // (if this CSSStyleDeclaration is attached to a UIComponent).
  539.         if (factory != null)
  540.         {
  541.             factory.prototype = {};
  542.             o = new factory();
  543.             v = o[styleProp];
  544.             if (v !== undefined) // must use !==
  545.                 return v;
  546.         }

  547.         // Next look in the style object that this CSSStyleDeclaration's
  548.         // defaultFactory function produces; it contains styles that
  549.         // were specified on the root tag of an MXML component.
  550.         if (defaultFactory != null)
  551.         {
  552.             defaultFactory.prototype = {};
  553.             o = new defaultFactory();
  554.             v = o[styleProp];
  555.             if (v !== undefined) // must use !==
  556.                 return v;
  557.         }

  558.         // Return undefined if the style isn't specified
  559.         // in any of these three places.
  560.         return undefined;
  561.     }

  562.     /**
  563.      * Sets a style property on this CSSStyleDeclaration.
  564.      *
  565.      * @param styleProp The name of the style property.
  566.      *
  567.      * @param newValue The value of the style property.
  568.      * The value may be of any type.
  569.      * The values <code>null</code>, <code>""</code>, <code>false</code>,
  570.      * <code>NaN</code>, and <code>0</code> are all valid style values,
  571.      * but the value <code>undefined</code> is not.
  572.      * Setting a style property to the value <code>undefined</code>
  573.      * is the same as calling the <code>clearStyle()</code> method.
  574.      *
  575.      * @langversion 3.0
  576.      * @playerversion Flash 9
  577.      * @playerversion AIR 1.1
  578.      * @productversion Flex 3
  579.      */
  580.     public function setStyle(styleProp:String, newValue:*):void
  581.     {
  582.         var oldValue:Object = getStyle(styleProp);
  583.         var regenerate:Boolean = false;

  584.         // If this CSSStyleDeclaration didn't previously have a factory,
  585.         // defaultFactory, or overrides object, then this CSSStyleDeclaration
  586.         // hasn't been added to anyone's proto chain. In that case, we
  587.         // need to regenerate everyone's proto chain.
  588.         if (selectorRefCount > 0 &&
  589.             factory == null &&
  590.             defaultFactory == null &&
  591.             !overrides &&
  592.             (oldValue !== newValue)) // must be !==
  593.         {
  594.             regenerate = true;
  595.         }
  596.         
  597.         if (newValue !== undefined) // must be !==
  598.         {
  599.             setLocalStyle(styleProp, newValue);
  600.         }
  601.         else
  602.         {
  603.             if (newValue == oldValue)
  604.                 return;
  605.             setLocalStyle(styleProp, newValue);
  606.         }

  607.         var sms:Array = SystemManagerGlobals.topLevelSystemManagers;
  608.         var n:int = sms.length;
  609.         var i:int;

  610.         // Type as Object to avoid dependency on SystemManager.
  611.         var sm:ISystemManager;
  612.         var cm:Object;

  613.         if (regenerate)
  614.         {
  615.             // Regenerate all the proto chains
  616.             // for all objects in the application.
  617.             for (i = 0; i < n; i++)
  618.             {
  619.                 sm = sms[i];
  620.                 cm = sm.getImplementation("mx.managers::ISystemManagerChildManager");
  621.                 cm.regenerateStyleCache(true);
  622.             }
  623.         }

  624.         for (i = 0; i < n; i++)
  625.         {
  626.             sm = sms[i];
  627.             cm = sm.getImplementation("mx.managers::ISystemManagerChildManager");
  628.             cm.notifyStyleChangeInChildren(styleProp, true);
  629.         }
  630.     }
  631.     
  632.     /**
  633.      * @private
  634.      * Sets a style property on this CSSStyleDeclaration.
  635.      *
  636.      * @param styleProp The name of the style property.
  637.      *
  638.      * @param newValue The value of the style property.
  639.      * The value may be of any type.
  640.      * The values <code>null</code>, <code>""</code>, <code>false</code>,
  641.      * <code>NaN</code>, and <code>0</code> are all valid style values,
  642.      * but the value <code>undefined</code> is not.
  643.      * Setting a style property to the value <code>undefined</code>
  644.      * is the same as calling <code>clearStyle()</code>.
  645.      */
  646.     mx_internal function setLocalStyle(styleProp:String, value:*):void
  647.     {
  648.         var oldValue:Object = getStyle(styleProp);

  649.         // If setting to undefined, clear the style attribute.
  650.         if (value === undefined) // must use ===
  651.         {
  652.             clearStyleAttr(styleProp);
  653.             return;
  654.         }

  655.         var o:Object;

  656.         // If the value is a String of the form "#FFFFFF" or "red",
  657.         // then convert it to a RGB color uint (e.g.: 0xFFFFFF).
  658.         if (value is String)
  659.         {
  660.             if (!styleManager)
  661.                 styleManager = Singleton.getInstance("mx.styles::IStyleManager2") as IStyleManager2;
  662.             var colorNumber:Number = styleManager.getColorName(value);
  663.             if (colorNumber != NOT_A_COLOR)
  664.                 value = colorNumber;
  665.         }

  666.         // If the new value for styleProp is different from the one returned
  667.         // from the defaultFactory function, then store the new value on the
  668.         // overrides object. That way, future clones will get the new value.
  669.         if (defaultFactory != null)
  670.         {
  671.             o = new defaultFactory();
  672.             if (o[styleProp] !== value) // must use !==
  673.             {
  674.                 if (!overrides)
  675.                     overrides = {};
  676.                 overrides[styleProp] = value;
  677.             }
  678.             else if (overrides)
  679.             {
  680.                 delete overrides[styleProp];
  681.             }
  682.         }

  683.         // If the new value for styleProp is different from the one returned
  684.         // from the factory function, then store the new value on the
  685.         // overrides object. That way, future clones will get the new value.
  686.         if (factory != null)
  687.         {
  688.             o = new factory();
  689.             if (o[styleProp] !== value) // must use !==
  690.             {
  691.                 if (!overrides)
  692.                     overrides = {};
  693.                 overrides[styleProp] = value;
  694.             }
  695.             else if (overrides)
  696.             {
  697.                 delete overrides[styleProp];
  698.             }
  699.         }

  700.         if (defaultFactory == null && factory == null)
  701.         {
  702.             if (!overrides)
  703.                 overrides = {};
  704.             overrides[styleProp] = value;
  705.         }

  706.         // Update all clones of this style sheet.
  707.         updateClones(styleProp, value);
  708.         
  709.     }
  710.     
  711.     /**
  712.      * Clears a style property on this CSSStyleDeclaration.
  713.      *
  714.      * This is the same as setting the style value to <code>undefined</code>.
  715.      *
  716.      * @param styleProp The name of the style property.
  717.      *
  718.      * @langversion 3.0
  719.      * @playerversion Flash 9
  720.      * @playerversion AIR 1.1
  721.      * @productversion Flex 3
  722.      */
  723.     public function clearStyle(styleProp:String):void
  724.     {
  725.         public::setStyle(styleProp, undefined);
  726.     }

  727.     /**
  728.      * @private
  729.      */
  730.     mx_internal function createProtoChainRoot():Object
  731.     {
  732.         var root:Object = {};

  733.         // If there's a defaultFactory for this style sheet,
  734.         // then add the object it produces to the root.
  735.         if (defaultFactory != null)
  736.         {
  737.             defaultFactory.prototype = root;
  738.             root = new defaultFactory();
  739.         }

  740.         // If there's a factory for this style sheet,
  741.         // then add the object it produces to the root.
  742.         if (factory != null)
  743.         {
  744.             factory.prototype = root;
  745.             root = new factory();
  746.         }

  747.         clones[ root ] = 1;
  748.         
  749.         return root;
  750.     }

  751.     /**
  752.      * @private
  753.      *
  754.      * The order of nodes in the prototype chain:
  755.      *
  756.      * 1. parent style default factories
  757.      * 2. this default factory
  758.      * 3. parent style factories
  759.      * 4. parent style overrides
  760.      * 5. this factory
  761.      * 6. this overrides
  762.      *
  763.      * Where a parent style is a style with the same selector as this
  764.      * style but in a parent style manager.
  765.      *
  766.      */
  767.     mx_internal function addStyleToProtoChain(chain:Object,
  768.                                          target:DisplayObject,
  769.                                          filterMap:Object = null):Object
  770.     {
  771.         var nodeAddedToChain:Boolean = false;
  772.         var originalChain:Object = chain;
  773.         
  774.         // Get a list of parent style declarations for this selector.
  775.         var parentStyleDeclarations:Vector.<CSSStyleDeclaration> = new Vector.<CSSStyleDeclaration>();
  776.         var styleParent:IStyleManager2 = styleManager.parent;
  777.         while (styleParent)
  778.         {
  779.             var parentStyle:CSSStyleDeclaration = styleParent.getStyleDeclaration(selectorString);
  780.             if (parentStyle)
  781.                 parentStyleDeclarations.unshift(parentStyle);

  782.             styleParent = styleParent.parent;
  783.         }

  784.         // #1. Add parent's default styles. Topmost parent is added to the chain first.
  785.         for each (var style:CSSStyleDeclaration in parentStyleDeclarations)
  786.         {
  787.             // If there's a defaultFactory for this style sheet,
  788.             // then add the object it produces to the chain.
  789.             if (style.defaultFactory != null)
  790.                 chain = style.addDefaultStyleToProtoChain(chain, target, filterMap);
  791.         }
  792.         
  793.         // #2. Add this style's defaultFactory to the proto chain.
  794.         if (defaultFactory != null)
  795.             chain = addDefaultStyleToProtoChain(chain, target, filterMap);

  796.         // #3 and #4. Add parent's factory styles and overrides.
  797.         var addedParentStyleToProtoChain:Boolean = false;
  798.         for each (style in parentStyleDeclarations)
  799.         {
  800.             if (style.factory != null || style.overrides != null)
  801.             {
  802.                 chain = style.addFactoryAndOverrideStylesToProtoChain(chain, target, filterMap);
  803.                 addedParentStyleToProtoChain = true;
  804.             }
  805.         }
  806.         
  807.         // #5 and #6. Add this factory style and overrides.
  808.         var inChain:Object = chain;
  809.         if (factory != null || overrides != null)
  810.         {
  811.             chain = addFactoryAndOverrideStylesToProtoChain(chain, target, filterMap);
  812.             if (inChain != chain)
  813.                 nodeAddedToChain = true;
  814.         }
  815.         
  816.         // Here we check if we need to add an empty node to the chain for clone
  817.         // purposes. If there are parent nodes between this defaultFactory and
  818.         // this factory, then we can't use the defaultFactory node as the clone
  819.         // since overrides could get blocked by parent styles.
  820.         // First we check if we have a defaultFactory and we didn't add a factory
  821.         // or override node to the chain. If we have a factory or override node
  822.         // then we will just use that.
  823.         if (defaultFactory != null && !nodeAddedToChain)
  824.         {
  825.             // Now we know we have a default factory node and no factory or override
  826.             // nodes. We can use the default factory as a clone on the chain if there
  827.             // are no parent styles below it on the proto chain.
  828.             // Otherwise create an empty node so overrides and be added later.
  829.             if (addedParentStyleToProtoChain)
  830.             {
  831.                 // There are parent styles so create an empty node.
  832.                 var emptyObjectFactory:Function = function():void
  833.                 {
  834.                 };
  835.                 emptyObjectFactory.prototype = chain;
  836.                 chain = new emptyObjectFactory();
  837.             }
  838.             
  839.             nodeAddedToChain = true;
  840.         }
  841.         
  842.         if (nodeAddedToChain)
  843.             clones[chain] = 1;

  844.         return chain;
  845.     }

  846.     /**
  847.      * @private
  848.      */
  849.     mx_internal function addDefaultStyleToProtoChain(chain:Object,
  850.                                             target:DisplayObject,
  851.                                             filterMap:Object = null):Object
  852.     {
  853.         // If there's a defaultFactory for this style sheet,
  854.         // then add the object it produces to the chain.
  855.         if (defaultFactory != null)
  856.         {
  857.             var originalChain:Object = chain;
  858.             if (filterMap)
  859.             {
  860.                 chain = {};
  861.             }
  862.             
  863.             defaultFactory.prototype = chain;
  864.             chain = new defaultFactory();

  865.             if (filterMap)
  866.                 chain = applyFilter(originalChain, chain, filterMap);
  867.         }
  868.         
  869.         return chain;
  870.     }
  871.     
  872.     /**
  873.      * @private
  874.      */
  875.     mx_internal function addFactoryAndOverrideStylesToProtoChain(chain:Object,
  876.                                                 target:DisplayObject,
  877.                                                 filterMap:Object = null):Object
  878.     {
  879.         var originalChain:Object = chain;
  880.         if (filterMap)
  881.         {
  882.             chain = {};
  883.         }
  884.         
  885.         // If there's a factory for this style sheet,
  886.         // then add the object it produces to the chain.
  887.         var objectFactory:Object = null;
  888.         if (factory != null)
  889.         {
  890.             objectFactory = new factory();
  891.             factory.prototype = chain;
  892.             chain = new factory();
  893.         }
  894.         
  895.         // If someone has called setStyle() on this CSSStyleDeclaration,
  896.         // then some of the values returned from the factory are
  897.         // out-of-date. Overwrite them with the up-to-date values.
  898.         if (overrides)
  899.         {
  900.             // Before we add our overrides to the object at the head of
  901.             // the chain, make sure that we added an object at the head
  902.             // of the chain.
  903.             if (factory == null)
  904.             {
  905.                 var emptyObjectFactory:Function = function():void
  906.                 {
  907.                 };
  908.                 emptyObjectFactory.prototype = chain;
  909.                 chain = new emptyObjectFactory();
  910.             }
  911.             
  912.             for (var p:String in overrides)
  913.             {
  914.                 if (overrides[p] === undefined)
  915.                     delete chain[p];
  916.                 else
  917.                     chain[p] = overrides[p];
  918.             }
  919.         }

  920.         if (filterMap)
  921.         {
  922.             if (factory != null || overrides)
  923.                 chain = applyFilter(originalChain, chain, filterMap);
  924.             else
  925.                 chain = originalChain;
  926.         }
  927.         
  928.         if (factory != null || overrides)
  929.             clones[chain] = 1;
  930.         
  931.         return chain;
  932.     }

  933.     
  934.     /**
  935.      * @private
  936.      */
  937.     mx_internal function applyFilter(originalChain:Object, chain:Object, filterMap:Object):Object
  938.     {
  939.         var filteredChain:Object = {};
  940.         // Create an object on the head of the chain using the original chain
  941.         var filterObjectFactory:Function = function():void
  942.         {
  943.         };
  944.         filterObjectFactory.prototype = originalChain;
  945.         filteredChain = new filterObjectFactory();
  946.         
  947.         for (var i:String in chain)
  948.         {
  949.             if (filterMap[i] != null)
  950.             {
  951.                 filteredChain[filterMap[i]] = chain[i];
  952.             }
  953.         }
  954.         
  955.         chain = filteredChain;
  956.         chain[FILTERMAP_PROP] = filterMap;

  957.         return chain;
  958.     }
  959.     
  960.     /**
  961.      * @private
  962.      */
  963.     mx_internal function clearOverride(styleProp:String):void
  964.     {
  965.         if (overrides && overrides[styleProp] !== undefined)
  966.             delete overrides[styleProp];
  967.     }

  968.     /**
  969.      * @private
  970.      */
  971.     private function clearStyleAttr(styleProp:String):void
  972.     {
  973.         // Put "undefined" into our overrides Array
  974.         if (!overrides)
  975.             overrides = {};
  976.         overrides[styleProp] = undefined;
  977.         
  978.         // Remove the property from all our clones
  979.         for (var clone:* in clones)
  980.         {
  981.             delete clone[styleProp];
  982.         }
  983.     }
  984.     
  985.     /**
  986.      * @private
  987.      */
  988.     mx_internal function updateClones(styleProp:String, value:*):void
  989.     {
  990.         // Update all clones of this style sheet.
  991.         for (var clone:* in clones)
  992.         {
  993.             var cloneFilter:Object = clone[FILTERMAP_PROP];
  994.             if (cloneFilter)
  995.             {
  996.                 if (cloneFilter[styleProp] != null)
  997.                 {
  998.                     clone[cloneFilter[styleProp]] = value;
  999.                 }
  1000.             }
  1001.             else
  1002.             {
  1003.                 clone[styleProp] = value;
  1004.             }
  1005.         }
  1006.     }
  1007.     
  1008. }

  1009. }

参考文献
1.http://222.27.111.131/flexhelp/mx/styles/CSSStyleDeclaration.html
2.http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/framework/src/mx/styles/CSSStyleDeclaration.as
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP