2015-03-13 8 views
0

Я использую Drop-Down с несколькими проверками, который не запускает первое событие Change. Последующие события срабатывают должным образом. Когда я выбираю «Выбрать все» или любую другую опцию, первый раз, когда он не вызывает событие.Сменить обработчик, не запускающий первый раз в Flex

use namespace mx_internal; 

[Style(name="selectAllBorderAlpha", type="Number", inherit="no", theme="spark, mobile", minValue="0.0", maxValue="1.0")] 
[Style(name="selectAllBorderColor", type="uint", format="Color", inherit="no", theme="spark, mobile")] 
[Style(name="selectAllBorderVisible", type="Boolean", inherit="no", theme="spark, mobile")] 
[Style(name="selectAllBackgroundColor", type="uint", format="Color", inherit="no", theme="spark, mobile")] 
[Style(name="selectAllBackgroundAlpha", type="Number", inherit="no", theme="spark, mobile", minValue="0.0", maxValue="1.0")] 

[IconFile("DropDownList.png")] 

[DiscouragedForProfile("mobileDevice")] 
public class CheckBoxDropDownList extends CheckBoxDropDownListBase 
{ 
    public function CheckBoxDropDownList() 
    { 
    super(); 
    addEventListener(IndexChangeEvent.CHANGE, indexChangeHandler); 
    } 

    protected function indexChangeHandler(event:IndexChangeEvent):void 
    { 
    selectedAll = false; 
    } 

    [SkinPart(required="false")] 
    public var selectAllCheckBox:CheckBox; 

    [SkinPart(required="false")] 
    public var selectAllHitArea:UIComponent; 

    [SkinPart(required="false")] 
    public var labelDisplay:IDisplayText; 

    private var labelChanged:Boolean = false; 
    private var labelDisplayExplicitWidth:Number; 
    private var labelDisplayExplicitHeight:Number; 
    private var sizeSetByTypicalItem:Boolean; 

    override public function get baselinePosition():Number 
    { 
    return getBaselinePositionForPart(labelDisplay as IVisualElement); 
    } 

    private var _prompt:String = ""; 

    [Inspectable(category="General", defaultValue="")] 

    public function get prompt():String 
    { 
    return _prompt; 
    } 

    public function set prompt(value:String):void 
    { 
    if (_prompt == value) 
     return; 

    _prompt = value; 
    labelChanged = true; 
    invalidateProperties(); 
    } 

    [Inspectable(category="Data")] 

    override public function set typicalItem(value:Object):void 
    { 
    super.typicalItem = value; 
    invalidateSize(); 
    } 

    override protected function commitProperties():void 
    { 
    super.commitProperties();   

    if (labelChanged) 
    { 
     labelChanged = false; 
     updateLabelDisplay(); 
    } 

    if (selectedAllChanged) 
    { 
     selectedAllChanged = false; 

     if (selectAllCheckBox) 
     { 
     selectAllCheckBox.selected = _selectedAll; 
     this.dispatchEvent(new Event("selectAllChanged")); 
     } 
     invalidateList(); 
    } 
    } 

    override protected function partAdded(partName:String, instance:Object):void 
    { 
    super.partAdded(partName, instance); 

    if (instance == labelDisplay) 
    { 
     labelChanged = true; 
     invalidateProperties(); 
    } 

    if (instance == selectAllCheckBox) 
    { 
     selectedAllChanged = true; 
     invalidateProperties(); 
    } 

    if (instance == selectAllHitArea) 
    { 
     selectAllHitArea.addEventListener(MouseEvent.CLICK, selectAllHitArea_clickHandler); 
    } 
    } 

    override protected function partRemoved(partName:String, instance:Object):void 
    { 
    super.partRemoved(partName, instance); 

    if (instance == selectAllHitArea) 
    { 
     selectAllHitArea.removeEventListener(MouseEvent.CLICK, selectAllHitArea_clickHandler); 
    } 
    } 

    protected function selectAllHitArea_clickHandler(event:MouseEvent):void 
    { 
    if (selectAllCheckBox) 
     selectedAll = !selectAllCheckBox.selected; 
    } 

    private var _selectedAll:Boolean = false; 
    private var selectedAllChanged:Boolean; 

    public function get selectedAll():Boolean 
    { 
    return _selectedAll; 
    } 

    public function set selectedAll(value:Boolean):void 
    { 
    if (value == _selectedAll) 
     return; 

    _selectedAll = value;   
    selectedAllChanged = true; 
    labelChanged = true; 

    selectedIndices = Vector.<int>([]); 
    //setSelectedItem(undefined, false); 

    invalidateProperties(); 
    } 

    public function setSelectedIndices(selValues:Array):void 
    { 
    if (this.dataProvider == null) { 
     return; 
    } 

    var selIndices:Vector.<int> = new Vector.<int>(); 

    if (selValues == null || selValues.length == 0) 
    { 
     this.selectedAll = true; 
     return; 
    } 

    for(var i:int=0; i < this.dataProvider.length; i++) 
    { 
     for(var j:int=0; j < selValues.length; j++) 
     { 
     var obj:Object = this.dataProvider.getItemAt(i); 

     if(selValues[j] == obj.value || selValues[j] == obj.label) 
     { 
      selIndices.push(i); 
      break; 
     } 
     } 
    } 

    if (selIndices.length == 0) 
    { 
     this.selectedAll = true; 
    } 
    else 
    { 
     this.selectedAll = false; 
     this.selectedIndices = selIndices; 
    } 
    } 

    override protected function item_mouseDownHandler(event:MouseEvent):void 
    { 
    if (selectedAll) 
    { 
     selectedAll = false; 

     var newIndex:int 
     if (event.currentTarget is IItemRenderer) 
     newIndex = IItemRenderer(event.currentTarget).itemIndex; 
     else 
     newIndex = dataGroup.getElementIndex(event.currentTarget as IVisualElement); 

     var arr:Array = dataProvider.toArray() 
     arr.splice(newIndex, 1); 
     selectedItems = Vector.<Object>(arr); 

     return; 
    } 
    super.item_mouseDownHandler(event); 

    // if all items are selected, then unselect them and check the "Select All" checkbox. 
    if (selectedItems.length == dataProvider.length) 
    { 
     selectedAll = true; 
     selectedIndex = -1; 
    } 
    } 

    override protected function dropDownController_closeHandler(event:DropDownEvent):void 
    { 
    super.dropDownController_closeHandler(event); 

    // Automatically selected all items if no items are selected when closing the dropDown. 
    if (selectedItems.length == 0 && !selectedAll) 
     selectedAll = true; 
    } 

    override protected function measure():void 
    { 
    var labelComp:TextBase = labelDisplay as TextBase; 

    // If typicalItem is set, then use it for measurement 
    if (labelComp && typicalItem != null) 
    { 
     // Save the labelDisplay's dimensions in case we clear out typicalItem 
     if (!sizeSetByTypicalItem) 
     { 
     labelDisplayExplicitWidth = labelComp.explicitWidth; 
     labelDisplayExplicitHeight = labelComp.explicitHeight; 
     sizeSetByTypicalItem = true; 
     } 

     labelComp.explicitWidth = NaN; 
     labelComp.explicitHeight = NaN; 

     // Swap in the typicalItem into the labelDisplay 
     updateLabelDisplay(typicalItem); 
     UIComponentGlobals.layoutManager.validateClient(skin, true); 

     // Force the labelDisplay to be sized to the measured size 
     labelComp.width = labelComp.measuredWidth; 
     labelComp.height = labelComp.measuredHeight; 

     // Set the labelDisplay back to selectedItem 
     updateLabelDisplay(); 
    } 
    else if (labelComp && sizeSetByTypicalItem && typicalItem == null) 
    { 
     // Restore the labelDisplay to its original size 
     labelComp.width = labelDisplayExplicitWidth; 
     labelComp.height = labelDisplayExplicitHeight; 
     sizeSetByTypicalItem = false; 
    } 
    super.measure(); 
    } 

    override mx_internal function updateLabelDisplay(displayItem:* = undefined):void 
    { 
    if (labelDisplay) 
    { 
     if (displayItem == undefined) 
     { 
     if (selectedItems != null && selectedItems.length > 1) 
      displayItem = VectorUtils.vectorToArray(selectedItems, Object); 
     else 
      displayItem = selectedItem; 
     } 

     if (displayItem != null && displayItem != undefined) 
     if (displayItem is Array) 
     { 
      this.toolTip = selectedItemsToLabel(displayItem, labelField, labelFunction); 
      labelDisplay.text = (displayItem as Array).length + " selected"; 
     } 
     else 
     { 
      this.toolTip = null;      
      labelDisplay.text = selectedItemsToLabel(displayItem, labelField, labelFunction); 
     } 
     else if (selectedAll) 
     labelDisplay.text = "All"; 
     else 
     labelDisplay.text = prompt; 
    } 
    } 

    private function invalidateList():void 
    { 
    if (dataGroup == null) 
     return; 

    for each (var itemIndex:int in dataGroup.getItemIndicesInView()) 
    { 
     var renderer:UIComponent = dataGroup.getElementAt(itemIndex) as UIComponent; 
     if (renderer) 
     renderer.invalidateDisplayList(); 
    } 
    } 

    private function selectedItemsToLabel(item:Object, labelField:String=null, labelFunction:Function=null):String 
    { 
    if (labelFunction != null) 
     return labelFunction(item); 

    var collection:ICollectionView = null; 
    if (item is Array) 
    { 
     collection = new ArrayCollection(item as Array); 
    } 
    else if (item is ICollectionView) 
    { 
     collection = ICollectionView(item); 
    } 
    else if (item is IList) 
    { 
     collection = new ListCollectionView(IList(item)); 
    } 

    if (collection != null) 
    { 
     var itemLabels:Array = []; 
     for each (var obj:Object in collection) 
     { 
     itemLabels.push(obj[labelField]); 
     } 
     return itemLabels.join(", "); 
    } 
    return LabelUtil.itemToLabel(item, labelField, labelFunction); 
    } 

    public function get selectedValues():Array 
    { 
    var arr:Array = []; 

    if(selectedItems != null && selectedItems.length > 0) 
    { 
     for each (var obj:Object in selectedItems) 
     arr.push(obj.value); 
    } 
    return arr; 
    } 

    public function get selectedLabels():Array 
    { 
    var arr:Array = []; 

    if(selectedItems != null && selectedItems.length > 0) 
    { 
     for each (var obj:Object in selectedItems) 
     arr.push(obj.label); 
    } 
    return arr; 
    } 
} 
+0

Я переместил заявление вопрос к вершине, переписал его можно было легко и понятно, и упростили его немного. Затем я переписал название немного, чтобы соответствовать. Наконец, я удалил все комментарии и большинство других комментариев, так как они, похоже, не были особенно полезны. Тем не менее, есть еще слишком много кода, поэтому, пожалуйста, [обрезать то, что осталось много] (http://stackoverflow.com/help/mcve). Кроме того, у вас появляются конфликтующие метки для flex3 и flex4.5; оставьте только тот, который вы используете. –

ответ

0

Попробуйте вызвать супер() после addEventListener() в функции конструктора

+0

Я попытался, но он не работает – user1942215

+0

Вы всегда можете отправить его вручную, вызвав 'dispatchEvent (новый IndexChangeEvent (IndexChangeEvent.CHANGE));' – Harrison