I use this to iterate over all components in several methods, with some methods building up results in the accumulator acc
(e.g., writing to a String, keeping a count of all, whatever). Iteration includes component chrome when useRaw
is true
.
/**
* Descends through subcomponents applying function
*/
public static function visitComponent(component:Object, fn:Function, useRaw:Boolean = false, acc:Object = null):Object {
var newAcc:Object = fn.call(null, component, acc);
if (component is mx.core.Container) {
var kids:mx.core.Container = mx.core.Container(component);
var childList:IChildList;
if (useRaw) {
childList = kids.rawChildren;
} else {
childList = kids;
}
for (var i:int = 0; i < childList.numChildren; ++i) {
visitComponent(childList.getChildAt(i), fn, useRaw, newAcc);
}
} else if (component is DisplayObjectContainer) {
var displayObjContainer:DisplayObjectContainer = component as DisplayObjectContainer;
for (var j:int = 0; j < displayObjContainer.numChildren; ++j) {
visitComponent(displayObjContainer.getChildAt(j), fn, useRaw, newAcc);
}
}
return newAcc;
}
/**
* Randomly resets backgroundColor of subcomponents
*/
public static function colorizeComponent(component:Object):void {
var fn:Function = function(c:Object, acc:Object):Object {
if (c is UIComponent) {
(c as UIComponent).setStyle("backgroundColor", Math.random() * uint.MAX_VALUE);
(c as UIComponent).setStyle("backgroundAlpha", 0.2);
}
return null;
}
visitComponent(component, fn);
}