hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/audit/view/filters/AbstractHeaderRenderer.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<s:DefaultGridHeaderRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                                                         xmlns:s="library://ns.adobe.com/flex/spark"
                                                         xmlns:mx="library://ns.adobe.com/flex/mx"
                                                         addedToStage="onAddedToStage()"
                                                         click="clickHandler(event)"
                                                         removedFromStage="onRemovedFromStage()"
                                                         useHandCursor="true"
                                                         buttonMode="true">
  
  <fx:Script>
        <![CDATA[
            import it.ht.rcs.console.events.FilterEvent;

            import mx.core.FlexGlobals;
            import mx.events.FlexMouseEvent;
            import mx.managers.PopUpManager;

            import spark.components.CheckBox;
            import spark.components.HGroup;

            private static var currentPopup:AbstractFilterPopup;

            [Bindable]
            private var popup:AbstractFilterPopup;

            public var popupFactory:IFactory;

            [Bindable]
            public var mandatory:Boolean=false;

            public var filter:Object;

            private function onAddedToStage():void
            {

                popup=popupFactory.newInstance();
                popup.property=column.dataField;
                popup.filter=filter;
                PopUpManager.addPopUp(popup, this.owner, false);

                showFilter.selected=filter.hasOwnProperty(column.dataField) || mandatory;

                FlexGlobals.topLevelApplication.addEventListener(FilterEvent.RESET_FILTER, resetFilter);
                FlexGlobals.topLevelApplication.addEventListener(FilterEvent.FILTER_VALUES_CHANGED, filterValuesChanged);

            }

            private function onRemovedFromStage():void
            {
                FlexGlobals.topLevelApplication.removeEventListener(FilterEvent.RESET_FILTER, resetFilter);
                FlexGlobals.topLevelApplication.removeEventListener(FilterEvent.FILTER_VALUES_CHANGED, filterValuesChanged);
                PopUpManager.removePopUp(popup);
            }

            private function resetFilter(event:FilterEvent):void
            {
                showFilter.selected=mandatory || false;
                changeHandler(false);
            }

            private function filterValuesChanged(event:FilterEvent):void
            {
                if (event.filterValues.hasOwnProperty(popup.property))
                    popup.filterValues=event.filterValues[popup.property];
            }

            override public function prepare(hasBeenRecycled:Boolean):void
            {
                super.prepare(hasBeenRecycled);
                height=22;
                (labelDisplayGroup.parent as HGroup).top=2;
                labelDisplay.left=20;
                labelDisplay.top=1;
                labelDisplayGroup.addElementAt(showFilter, 0);
            }

            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                super.updateDisplayList(unscaledWidth, unscaledHeight);
                if (popup && popup.visible)
                    centerPopup();
            }

            private function centerPopup():void
            {
                var center:Point=localToGlobal(new Point(x, y));
                popup.x=center.x - x;
                popup.y=center.y + height;
                if (popup.x + popup.width > owner.width)
                    popup.x=center.x - x - popup.width + width + 1;
            }

            private function clickHandler(event:MouseEvent):void
            {

                if (currentPopup && currentPopup.visible)
                {
                    currentPopup.removeEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, clickHandler);
                    currentPopup.commit(); //COMMIT FILTER!!!!
                    if (currentPopup === popup)
                    {
                        currentPopup=null;
                        return;
                    }
                }

                centerPopup();
                popup.visible=true;
                popup.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, clickHandler);
                currentPopup=popup;

            }

            // Triggered only when a filter is set (ie. when switching from selected to deselected state)
            // because the checkbox is disabled when there's no filter
            private function changeHandler(fireEvent:Boolean=true):void
            {
                popup.reset(fireEvent);
                popup.visible=false;
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:CheckBox id="showFilter"
                                change="changeHandler()"
                                enabled="{!mandatory &amp;&amp; showFilter.selected}"
                                selected="{mandatory || popup.hasFilter}"
                                skinClass="it.ht.rcs.console.skins.FilterCheckBoxSkin"/>
    </fx:Declarations>

</s:DefaultGridHeaderRenderer>