10
Jun
08

Disabling keyboard navigation on the Accordion container in Flex

The following example shows how you can disable keyboard navigation on the Flex Accordion container by extending the Accordion class and overriding the protected keyDownHandler() method.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/10/disabling-keyboard-navigation-on-the-accordion-container-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        xmlns:comps="comps.*"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <comps:MyAccordion id="accordion"
            width="100%"
            height="100%">
        <mx:VBox id="v1"
                label="One"
                width="100%"
                height="100%">
            <mx:Label text="One" />
        </mx:VBox>
        <mx:VBox id="v2"
                label="Two"
                width="100%"
                height="100%">
            <mx:Label text="Two" />
        </mx:VBox>
        <mx:VBox id="v3"
                label="Three"
                width="100%"
                height="100%">
            <mx:Label text="Three" />
        </mx:VBox>
        <mx:VBox id="v4"
                label="Four"
                width="100%"
                height="100%">
            <mx:Label text="Four" />
        </mx:VBox>
        <mx:VBox id="v5"
                label="Five"
                width="100%"
                height="100%">
               <mx:Label text="Five" />
        </mx:VBox>
    </comps:MyAccordion>

</mx:Application>

comps/MyAccordion.as

/**
 * http://blog.flexexamples.com/2008/06/10/disabling-keyboard-navigation-on-the-accordion-container-in-flex/
 */
package comps {
    import mx.containers.Accordion;
    import flash.events.KeyboardEvent;

    public class MyAccordion extends Accordion {
        public function MyAccordion() {
            super();
        }

        override protected function keyDownHandler(evt:KeyboardEvent):void {
        }
    }
}

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/10/disabling-keyboard-navigation-on-the-accordion-container-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.VBox;
            import mx.controls.Label;
            import comps.*;

            private var accordion:MyAccordion;
            private var v1:VBox;
            private var v2:VBox;
            private var v3:VBox;
            private var v4:VBox;
            private var v5:VBox;
            private var l1:Label;
            private var l2:Label;
            private var l3:Label;
            private var l4:Label;
            private var l5:Label;

            private function init():void {
                l1 = new Label();
                l1.text = "One";

                l2 = new Label();
                l2.text = "Two";

                l3 = new Label();
                l3.text = "Three";

                l4 = new Label();
                l4.text = "Four";

                l5 = new Label();
                l5.text = "Five";

                v1 = new VBox();
                v1.label = "One";
                v1.percentWidth = 100;
                v1.percentHeight = 100;
                v1.addChild(l1);

                v2 = new VBox();
                v2.label = "Two";
                v2.percentWidth = 100;
                v2.percentHeight = 100;
                v2.addChild(l2);

                v3 = new VBox();
                v3.label = "Three";
                v3.percentWidth = 100;
                v3.percentHeight = 100;
                v3.addChild(l3);

                v4 = new VBox();
                v4.label = "Four";
                v4.percentWidth = 100;
                v4.percentHeight = 100;
                v4.addChild(l4);

                v5 = new VBox();
                v5.label = "Five";
                v5.percentWidth = 100;
                v5.percentHeight = 100;
                v5.addChild(l5);

                accordion = new MyAccordion();
                accordion.percentWidth = 100;
                accordion.percentHeight = 100;
                accordion.addChild(v1);
                accordion.addChild(v2);
                accordion.addChild(v3);
                accordion.addChild(v4);
                accordion.addChild(v5);
                addChild(accordion);
            }
        ]]>
    </mx:Script>

</mx:Application>

5 Responses to “Disabling keyboard navigation on the Accordion container in Flex”


  1. 1 Nick Jun 16th, 2008 at 2:01 pm

    Hey Peter,

    Just wanted to say thank you again! This worked out perfectly for what I needed it for.

    Thanks,

    - Nick

  2. 2 Yon Oct 8th, 2008 at 2:17 pm

    I made an accordion some time ago on kind of this line, I extended the accordion, however i didnt wanted to disable keyboard, just wanted to disable navigation to disabled headers, but still be able to use both mouse and keyboard to navigate.

    I use a recursive function to achieve this, however it acts funny if u disable all headers (but why disable all headers anyways..)

    package renders {
    	import mx.events.IndexChangedEvent;
    	import flash.events.KeyboardEvent;
    	import mx.containers.Accordion;
    	import mx.core.mx_internal;
    	import flash.events.Event;
    	import flash.ui.Keyboard;
    
    	public class PracticalAccordion extends Accordion {
    		private var _focusedIndex:int = -1;
    
    		public function PracticalAccordion() {
    			super();
    		}
    
    	    private function drawHeaderFocus(headerIndex:int, isFocused:Boolean):void {
    	        if (headerIndex != -1)
    	            getHeaderAt(headerIndex).drawFocus(isFocused);
    	    }
    
            override protected function keyDownHandler(event:KeyboardEvent):void {
    	        if (event.target != this)
    	            return;
    
    	        var prevValue:int = selectedIndex;
    	        if(event.keyCode != Keyboard.SPACE && event.keyCode != Keyboard.ENTER)
    	        	drawHeaderFocus(_focusedIndex, false);
    	        getIndexPlus(event, 0, prevValue);
    	        if(event.keyCode != Keyboard.SPACE && event.keyCode != Keyboard.ENTER)
                    drawHeaderFocus(_focusedIndex, true);
                event.stopPropagation();
                if(event.keyCode != Keyboard.DOWN && event.keyCode != Keyboard.UP && event.keyCode != Keyboard.LEFT && event.keyCode != Keyboard.RIGHT)
                	dispatchChangeEvent(prevValue, selectedIndex, event);
            }
    
            private function getIndexPlus (event:KeyboardEvent, loop:int, prevValue:int):void {
    	        switch (event.keyCode) {
    	            case Keyboard.PAGE_DOWN: {
    	                _focusedIndex = selectedIndex = (selectedIndex  0 ? selectedIndex - 1 : numChildren - 1);
    	                break;
    	            }
    	            case Keyboard.HOME: {
    	                _focusedIndex = selectedIndex = 0;
    	                break;
    	            }
    	            case Keyboard.END: {
    	                _focusedIndex = selectedIndex = numChildren - 1;
    	                break;
    	            }
    	            case Keyboard.DOWN:
    	            case Keyboard.RIGHT: {
    	                _focusedIndex = (_focusedIndex  0 ? _focusedIndex - 1 : numChildren - 1);
    	                break;
    	            }
    	            case Keyboard.SPACE:
    	            case Keyboard.ENTER: {
    	                if (_focusedIndex != selectedIndex) {
    	                    selectedIndex = _focusedIndex;
    	                }
    	                break;
    	            }
    	        }
    
              if (_focusedIndex != selectedIndex) {
                	if(!getHeaderAt(selectedIndex).enabled || !getHeaderAt(_focusedIndex).enabled)
                		if(loop <= numChildren)
                			getIndexPlus(event, loop + 1, prevValue);
    /*             		else {
                			_focusedIndex = prevValue;
                			set selectedIndex(prevValue);
                		} */
               	} else
    	           	if(!getHeaderAt(selectedIndex).enabled)
    	           		if(loop <= numChildren)
    	            		getIndexPlus(event, loop + 1, prevValue);
    /* 					else {
                			_focusedIndex = prevValue;
                			set selectedIndex(prevValue)
         				} */
            }
    
    	    private function dispatchChangeEvent(oldIndex:int,
    	                                         newIndex:int,
    	                                         cause:Event = null):void {
    	        var indexChangeEvent:IndexChangedEvent =
    	            new IndexChangedEvent(IndexChangedEvent.CHANGE);
    	        indexChangeEvent.oldIndex = oldIndex;
    	        indexChangeEvent.newIndex = newIndex;
    	        indexChangeEvent.relatedObject = getChildAt(newIndex);
    	        indexChangeEvent.triggerEvent = cause;
    	        dispatchEvent(indexChangeEvent);
    	    }
    	}
    }
    
  3. 3 thomary Nov 7th, 2008 at 8:03 am

    I was looking for code to open myform with the accordion totally collapsed. In other words, all accordion pages are not open. The default is to have the first page opened.

    I looked through Adobe forums, adobe class for accordion and around the internet.
    I think I need something like:
    my_acc.getChildAt(my_acc.numChildren -1);

    I tried:

    cfformitem type=”script”
    function formOnLoad(){
    var theChild_obj:Object = {};
    var theChild_obj:Object = info.getChildAt(info.numChildren -1);
    }
    /cfformitem
    But this did not work.
    I am using CFusion MX 7 with cfform format=”Flash”
    Any help would be greatly appreciated.
    Thanks, Thomary

  4. 4 thomary Nov 7th, 2008 at 9:56 am

    I used a work around. added a blank page and defaulted to that one.

    Thanks for all these pages.
    I found a lot of useful pages here.

  5. 5 yon Dec 1st, 2008 at 11:54 am

    I created a blog, i plan to post articles about flex and coding as soon as i have some free time
    By now i have the code i posted above (without errors, since i didnt parsed it to be digested by the postin machine in here..) with a working example.
    You can read it here
    http://faithoncode.wordpress.com/2008/11/11/flex-accordionheaders/

    Regards

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed