package lastfmvis { import flare.vis.legend.LegendItem; import flash.display.Sprite; import flash.events.MouseEvent; import flash.utils.Dictionary; public class RadioSwitch extends Sprite { private var _selected:LegendItem; private var _disabled:Dictionary = new Dictionary; private var _lastSelected:LegendItem; private var _oldFunctions:Array = []; private var _onClick:Function; private var _onRollover:Function; private var _onRollout:Function; private static var SELECTED_ALPHA:int = 1; private static var DESELECTED_ALPHA:Number = 0.3; private static var DISABLED_ALPHA:Number = 0.2; public function get selectedItemName():String { return _selected.name; } public function RadioSwitch(click:Function, rollover:Function, rollout:Function, names:Array) { // Create Tag/Artist switch // this.name = "Viewing"; var i:int = 0; _onClick = click; _onRollover = rollover; _onRollout = rollout; for each (var name:String in names) { var legendItem:LegendItem = new LegendItem(name); legendItem.name = name; legendItem.label.applyFormat(Last_fm_vis.FMT); legendItem.buttonMode = true; legendItem.mouseChildren = false; legendItem.margin = 3; legendItem.render(); if (i > 0) { var prev:LegendItem = LegendItem(this.getChildAt(i - 1)); legendItem.y = prev.y + prev.height; legendItem.alpha = DESELECTED_ALPHA; } legendItem.addEventListener(MouseEvent.ROLL_OVER, function(e:MouseEvent):void { if (_disabled[e.target]) { return; } LegendItem(e.target).alpha = SELECTED_ALPHA; if (_onRollover != null) _onRollover(e.target); } ); legendItem.addEventListener(MouseEvent.ROLL_OUT, function(e:MouseEvent):void { var item:LegendItem = LegendItem(e.target); if (_disabled[item]) { return; } if (item != _selected) item.alpha = DESELECTED_ALPHA; if (_onRollout != null) _onRollout(e.target); } ); legendItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { var item:LegendItem = LegendItem(e.target); if (_disabled[item]) { return; } _selected = item; for (var i:Number = 0; i < numChildren; i++) { LegendItem(getChildAt(i)).alpha = DESELECTED_ALPHA; LegendItem(getChildAt(i)).buttonMode = true; } LegendItem(e.target).alpha = SELECTED_ALPHA; LegendItem(e.target).buttonMode = false; if (_onClick != null) _onClick(e.target); // filterProperty = LegendItem(e.target).name; // onFilter(); } ); this.addChild(legendItem); i++; } // First item is selected initially _selected = LegendItem(this.getChildAt(0)); } public function selectItemAt(index:Number):Boolean { if (this.getChildAt(index)) { selectItem(LegendItem(getChildAt(index))) return true; } return false; } public function selectItemWithName(name:String):Boolean { if (this.getChildByName(name)) { selectItem(LegendItem(this.getChildByName(name))); return true; } return false; } public function disableItem(name:String):void { var child:LegendItem = LegendItem(getChildByName(name)); if (child) { child.buttonMode = false; child.alpha = DISABLED_ALPHA; _disabled[this.getChildByName(name)] = true; } } public function enableItem(name:String):void { var child:LegendItem = LegendItem(getChildByName(name)); if (child) { child.buttonMode = true; if (child == _selected) child.alpha = SELECTED_ALPHA; else child.alpha = DESELECTED_ALPHA; delete _disabled[this.getChildByName(name)]; } } public function disable():void { for (var i:int = 0; i < this.numChildren; i++) { disableItem(this.getChildAt(i).name); } } public function enable():void { for (var i:int = 0; i < this.numChildren; i++) { enableItem(this.getChildAt(i).name); } } public function disableFunctions():void { _oldFunctions[0] = _onClick; _oldFunctions[1] = _onRollover; _oldFunctions[2] = _onRollout; _onClick = null; _onRollover = null; _onRollout = null; } public function enableFunctions():void { if (_oldFunctions[0]) _onClick = _oldFunctions[0]; if (_oldFunctions[1]) _onRollover = _oldFunctions[1]; if (_oldFunctions[2]) _onRollout = _oldFunctions[2]; } private function selectItem(item:LegendItem):void { if (item.parent == this) item.dispatchEvent(new MouseEvent(MouseEvent.CLICK)); else trace("RadioSwitch: Attempted to select item that isn't a child of the RadioSwitch"); } } }