package { import dw.cm.content.IXmlConfigurable; import dw.tween.dTween; import dw.tween.props; import dw.tween.tint; import dw.ui.basic.ListboxItemBase; import dw.ui.basic.UIItemEvent; import fl.motion.easing.Quadratic; import flash.display.MovieClip; import flash.events.Event; import flash.events.MouseEvent; import flash.net.FileReference; import flash.net.URLRequest; import flash.net.navigateToURL; import flash.text.TextField; //REPRESENTS AN ITEM IN THE PLAYLIST LISTBOX public class PlaylistListboxItem extends ListboxItemBase implements IXmlConfigurable { public static var DOWNLOADABLE:String = "downloadable"; public static var BUYABLE:String = "buyable"; public static var NONE:String = "none"; protected var _xmlData:XML; public var title:TextField; public var download:MovieClip; public var buytrack:MovieClip; public var background:MovieClip; protected var _over:Boolean = false; protected var _donwloadButtonState:String = NONE; protected var fileRef:FileReference = new FileReference(); public function PlaylistListboxItem() { super(); title.mouseEnabled = false; //download.textfield.autoSize = TextFieldAutoSize.CENTER; download.buttonMode = true; buytrack.buttonMode = true; download.useHandCursor = true; buytrack.useHandCursor = true; (download["textfield"] as TextField).mouseEnabled = false; (buytrack["textfield"] as TextField).mouseEnabled = false; addEventListener(Event.ADDED_TO_STAGE, evthndlAddedToStage); dTween(background).apply([new tint(_selected?0x333333:0x131313,1)]); dTween(download).apply([new props({alpha:0})]); dTween(buytrack).apply([new props({alpha:0})]); dTween(title).apply([new tint(0x7e7e7e,1)]); addEventListener(UIItemEvent.ITEM_SELECT_REQUEST, evthndlSelectRequest); addEventListener(UIItemEvent.ITEM_UNSELECTED, evthndlUnselected); } public function get xmlData():XML { return _xmlData; } public function set xmlData(v:XML):void { _xmlData = v; title.text = _xmlData.@artist + " - " + _xmlData.@track; switch(_xmlData.@downloadable.toString()) { case "true": _donwloadButtonState = DOWNLOADABLE; download.addEventListener(MouseEvent.CLICK, evthndlDownload); buytrack.visible = false; download.visible = true; break; case "false": _donwloadButtonState = NONE; download.visible = false; buytrack.visible = false; break; case "buy": _donwloadButtonState = BUYABLE; download.visible = false; buytrack.visible = true; buytrack.addEventListener(MouseEvent.CLICK, evthndlDownload); break; default: _donwloadButtonState = NONE; buytrack.visible = false; download.visible = false; } } //HANDLER FOR THE DOWNLOAD BUTTON PUSH EVENT //CHECKS IF THE URL CONTAINS THE HTTP PREFIX, IF NOT, IT ADDS IT.. ADD HERE MORE LOGIC IF YOU HAVE TO SUPPORT HTTPS (IE. ALSO CHECK FOR HTTPS) //DEPENDING ON WHERE YOUR MP3S ARE LOCATED YOU MIGHT NEED TO ALSO ADD A XML CROSS DOMAIN POLICY FILE ON THAT SERVER, CHECK LIVEDOCS FOR THIS protected function evthndlDownload(e:MouseEvent):void { var url:String; e.stopImmediatePropagation(); if(_donwloadButtonState == DOWNLOADABLE) { //filereference.download requires that the protocol IS http or https, local download does not work!!!! url = _xmlData.@url; var baseUrl:String = root.loaderInfo.url; if(baseUrl.substr(0,4).toLowerCase()=="http") { if(url.substr(0,1) == ".") { var lastSlash:int = baseUrl.lastIndexOf("/"); baseUrl = baseUrl.substring(0, lastSlash+1); url = baseUrl + url.substring(2); } //if(url.substr(0,7) != "http://") url = "http://"+url; var req:URLRequest = new URLRequest(url); try { fileRef.download(req); } catch (e:Error) { trace("Downlading failed: "+e.message); } } } if(_donwloadButtonState == BUYABLE) { url = _xmlData.@buyUrl; if(url.substr(0,7) != "http://") url = "http://"+url; var request:URLRequest = new URLRequest(url); try { navigateToURL(request, "_blank"); } catch (e:Error) { trace("Error occurred!"); } } } protected function evthndlAddedToStage(e:Event):void { stage.addEventListener(Event.MOUSE_LEAVE, evthndlRollOut); addEventListener(MouseEvent.ROLL_OUT, evthndlRollOut); addEventListener(MouseEvent.ROLL_OVER, evthndlRollOver); } //CHANGE COLOR TINT HERE AND IN THE CONSTRUCTOR protected function evthndlRollOut(e:Event):void { dTween(background).ease(Quadratic.easeIn).to(0.2,[new tint(_selected?0x272727:0x131313,1)]); if(_donwloadButtonState == BUYABLE) { dTween(buytrack).ease(Quadratic.easeIn).to(0.2,[new props({alpha:0})]); } else if(_donwloadButtonState == DOWNLOADABLE) { dTween(download).ease(Quadratic.easeIn).to(0.2,[new props({alpha:0})]); } _over = false; } //CHAGNE COLOR TINT HERE protected function evthndlRollOver(e:Event):void { dTween(background).ease(Quadratic.easeOut).to(0.2,[new tint(_selected?0x272727:0x232323,1)]); if(_donwloadButtonState == DOWNLOADABLE) { dTween(download).ease(Quadratic.easeIn).to(0.2,[new props({alpha:1})]); } else if(_donwloadButtonState == BUYABLE) { dTween(buytrack).ease(Quadratic.easeIn).to(0.2,[new props({alpha:1})]); } _over = true; } protected function evthndlSelectRequest(e:UIItemEvent):void { dTween(title).ease(Quadratic.easeOut).to(0.2,[new tint(0x7e7e7e,0)]); dTween(background).ease(Quadratic.easeOut).to(0.2,[new tint(0x272727,1)]); buttonMode = false; } protected function evthndlUnselected(e:UIItemEvent):void { dTween(title).ease(Quadratic.easeOut).to(0.2,[new tint(0x7e7e7e,1)]); dTween(background).ease(Quadratic.easeIn).to(0.2,[new tint(0x131313,0)]); buttonMode = true; } public override function dispose():void { super.dispose(); download.removeEventListener(MouseEvent.CLICK, evthndlDownload); buytrack.removeEventListener(MouseEvent.CLICK, evthndlDownload); removeEventListener(Event.ADDED_TO_STAGE, evthndlAddedToStage); removeEventListener(MouseEvent.ROLL_OUT, evthndlRollOut); removeEventListener(MouseEvent.ROLL_OVER, evthndlRollOver); removeEventListener(UIItemEvent.ITEM_SELECT_REQUEST, evthndlSelectRequest); removeEventListener(UIItemEvent.ITEM_UNSELECTED, evthndlUnselected); if(stage != null) stage.removeEventListener(Event.MOUSE_LEAVE, evthndlRollOut); } } }