Skip to content

Instantly share code, notes, and snippets.

@camwest
Created July 18, 2009 05:27
Show Gist options
  • Select an option

  • Save camwest/149424 to your computer and use it in GitHub Desktop.

Select an option

Save camwest/149424 to your computer and use it in GitHub Desktop.
package com.yardstick.admin.ui.presenters
{
import assets.Config;
import com.yardstick.shared.events.AccountEvent;
import com.yardstick.shared.model.vos.Account;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.ProgressEvent;
import flash.net.FileReference;
import mx.controls.Alert;
import mx.controls.ProgressBar;
import mx.core.FlexGlobals;
import mx.events.CloseEvent;
public class GeneralViewPresentationModel extends EventDispatcher
{
//--------------------------------------------------------------------
// Public Constants
//--------------------------------------------------------------------
public static const NORMAL:String = "normal";
public static const LOADING:String = "loading";
public static const NOT_VALID:String = "notValid";
public static const UPLOADING:String = "uploading";
//--------------------------------------------------------------------
// Public Setters and Getters
//--------------------------------------------------------------------
//--------------state--------------------
private var _state:String = NORMAL;
[Bindable(Event="stateChange")] public function get state():String
{
return _state;
}
//--------------account--------------------
private var _account:Account;
private var _tempAccount:Account;
[Bindable(Event="accountChange")] public function get account():Account
{
return _account;
}
public function set account( account:Account ):void
{
if (account != null) {
_account = account;
_tempAccount = new Account( { account : account } );
dispatchEvent( new Event("accountChange") );
_state = NORMAL;
dispatchEvent( new Event("stateChange") );
}
}
//--------------imageURL--------------------
[Bindable(Event="accountChange")] public function get imageUrl():Object
{
return _account.image_url;
}
//--------------siteNameErrorString--------------------
private var _siteNameErrorString:String;
[Bindable(Event="validationChange")]
public function get siteNameErrorString():String
{
return _siteNameErrorString;
}
//--------------percentUploaded--------------------
public var bytesLoaded:Number;
public var bytesTotal:Number;
//-----------------------------------------------
// Constructor
//-----------------------------------------------
private var dispatcher:IEventDispatcher;
public function GeneralViewPresentationModel(dispatcher:IEventDispatcher)
{
this.dispatcher = dispatcher;
}
//--------------------------------------------------------------------
// Public Methods
//--------------------------------------------------------------------
// save .......................................................................
public function save():void
{
var event:AccountEvent = new AccountEvent(AccountEvent.UPDATE);
event.account = _tempAccount;
dispatcher.dispatchEvent(event);
_state = LOADING;
dispatchEvent( new Event("stateChange") );
}
//.......................................................................
public function updateName( value:String ):void
{
_tempAccount.name = value;
validate();
}
// cancel account ........................................................
public function cancelAccount():void
{
/* cancelling an account will delete all data and users and
also get rid of the subdomain. this happens immediately
so we need to present the user with a serious message */
var alert:Alert = Alert.show(
"Are you positive?",
"Cancel Account",
Alert.YES | Alert.NO,
FlexGlobals.topLevelApplication as Sprite,
cancelAccountListener,
Config.TRASH_IMAGE,
Alert.NO);
}
// change account image ..................................................
public function changeAccountImage():void
{
/* changing the account image involves opening up a dialog
box on the users machine and letting them select an image
after they select an image it will immediately start
uploading. After the image is uploaded it will be presented
in the preview window, if the user likes what they see they
can choose save changes which will send a multi-part form to
the server */
accountImageRef = new FileReference();
accountImageRef.addEventListener(Event.SELECT, selectAccountImageListener, false, 0, true);
accountImageRef.browse([Config.IMAGES_FILTER]);
}
// cancel uploading account image ........................................
public function cancelUploading():void
{
accountImageRef.cancel()
_state = NORMAL;
dispatchEvent( new Event("stateChange") );
}
// reset the progress bar after a succesfull upload ........................................
public function resetProgressBar(progress:ProgressBar):void
{
progress.setProgress(0, 100);
bytesLoaded = 0;
bytesTotal = 0;
}
//--------------------------------------------------------------------
// Private Methods
//--------------------------------------------------------------------
private function validate():void
{
_state = NORMAL;
_siteNameErrorString = "";
//account name is required
if (_tempAccount.name.length == 0) {
_state = NOT_VALID
_siteNameErrorString = "Account name is required";
}
dispatchEvent( new Event("validationChange") );
dispatchEvent( new Event("stateChange") );
}
//--------------------------------------------------------------------
// Private Variables
//--------------------------------------------------------------------
private var accountImageRef:FileReference;
//--------------------------------------------------------------------
// Event Handlers
//--------------------------------------------------------------------
private function cancelAccountListener(e:CloseEvent):void
{
if (e.detail == Alert.YES) {
dispatcher.dispatchEvent( new AccountEvent(AccountEvent.CANCEL) );
}
}
private function selectAccountImageListener(e:Event):void
{
var event:AccountEvent = new AccountEvent(AccountEvent.UPLOAD_IMAGE);
event.image = e.target as FileReference;
dispatcher.dispatchEvent(event);
_state = UPLOADING;
//listen for progress events
event.image.addEventListener(ProgressEvent.PROGRESS, uploadProgressListener, false, 0, true);
dispatchEvent( new Event("stateChange") );
}
private function uploadProgressListener(e:ProgressEvent):void
{
bytesLoaded = e.bytesLoaded;
bytesTotal = e.bytesTotal;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment