Skip to content

Instantly share code, notes, and snippets.

@darscan
Created April 28, 2010 22:32
Show Gist options
  • Save darscan/382830 to your computer and use it in GitHub Desktop.
Save darscan/382830 to your computer and use it in GitHub Desktop.
A rough (and bad) AS3 Rich Routing utility
/*
* Copyright (c) 2010 the original author or authors
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/
package org.robotlegs.utilities.routing
{
public class PathSpec
{
protected var _spec:String;
protected var _path:String;
protected var _match:Boolean;
protected var _params:Object;
public function PathSpec(spec:String, path:String)
{
_spec = spec;
_path = path;
_match = false;
_params = {};
parse();
}
protected function parse():void
{
var segs:Array = _spec.split('/');
var test:Array = _path.split('/');
if (test.length < segs.length)
return;
var len:int = segs.length;
var seg:String;
for (var i:int = 0; i < len; i++)
{
seg = segs[i];
if (seg.indexOf(':') == 0)
{
_params[seg.substr(1)] = test[i];
continue;
}
else if (seg == '*')
{
_params['splat'] ||= [];
_params['splat'].push(test[i]);
continue;
}
else if (seg != test[i])
return;
}
_match = true;
}
public function get match():Boolean
{
return _match;
}
public function get params():Object
{
return _params;
}
}
}
/*
* Copyright (c) 2010 the original author or authors
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/
package org.robotlegs.utilities.routing
{
import org.osflash.signals.Signal;
public class Route
{
protected var _open:Boolean;
protected var _opened:Signal;
protected var _changed:Signal;
protected var _closed:Signal;
protected var _path:String;
protected var _silent:Boolean;
[Inject]
public var router:Router;
public function Route()
{
_opened = new Signal(String);
_changed = new Signal(String);
_closed = new Signal(String);
}
[PostConstruct]
public function init():void
{
router.pathChanged.add(onPathChanged);
}
public function get opened():Signal
{
return _opened;
}
public function get changed():Signal
{
return _changed;
}
public function get closed():Signal
{
return _closed;
}
public function get open():Boolean
{
return _open;
}
public function get path():String
{
return _path;
}
protected function onPathChanged(path:String):void
{
_path = path;
if (match(path))
{
if (!_silent)
router.reportMatch(true);
if (open)
{
onChange(path);
_changed.dispatch(path);
}
else
{
_open = true;
onOpen(path);
_opened.dispatch(path);
}
}
else if (open)
{
_open = false;
onClose(path);
_closed.dispatch(path);
}
}
protected function match(path:String):Boolean
{
return false;
}
protected function onOpen(path:String):void
{
// HOOK: override
}
protected function onChange(path:String):void
{
// HOOK: override
}
protected function onClose(path:String):void
{
// HOOK: override
}
}
}
/*
* Copyright (c) 2010 the original author or authors
*
* Permission is hereby granted to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
*/
package org.robotlegs.utilities.routing
{
import com.asual.swfaddress.SWFAddress;
import com.asual.swfaddress.SWFAddressEvent;
import org.osflash.signals.Signal;
public class Router
{
protected var _started:Boolean;
protected var _defaultPath:String;
protected var _path:String;
protected var _pathChanged:Signal;
protected var _routeFound:Signal;
protected var _routeNotFound:Signal;
protected var _routeFoundFlag:Boolean;
public function Router()
{
_pathChanged = new Signal(String);
_routeFound = new Signal(String);
_routeNotFound = new Signal(String);
}
public function get pathChanged():Signal
{
return _pathChanged;
}
public function get routeFound():Signal
{
return _routeFound;
}
public function get routeNotFound():Signal
{
return _routeNotFound;
}
public function set path(path:String):void
{
_path = path;
SWFAddress.setValue(path);
}
public function get path():String
{
return _path;
}
public function set defaultPath(value:String):void
{
_defaultPath = value;
}
public function start():void
{
if (!_started)
{
_started = true;
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, onChange);
}
}
public function stop():void
{
if (_started)
{
_started = false;
SWFAddress.removeEventListener(SWFAddressEvent.CHANGE, onChange);
}
}
public function reportMatch(matched:Boolean):void
{
_routeFoundFlag ||= matched;
}
protected function onChange(e:SWFAddressEvent):void
{
_path = (e.path == '/' && _defaultPath) ? _defaultPath : e.path;
_routeFoundFlag = false;
_pathChanged.dispatch(_path);
if (_routeFoundFlag)
{
_routeFound.dispatch(_path);
}
else
{
_routeNotFound.dispatch(_path);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment