Permalink
Browse files

glue update: actions and commands

  • Loading branch information...
seanhess committed May 18, 2009
1 parent e9e2efb commit 32383e99e0e0424b7f662af51e4c4231a24557f6
View
Binary file not shown.
View
Binary file not shown.
View
@@ -4,4 +4,6 @@
<component class="net.seanhess.glue.tags.Glue"/>
<component class="net.seanhess.glue.tags.GlueMap"/>
<component class="net.seanhess.glue.tags.Route"/>
+ <component class="net.seanhess.glue.tags.Command"/>
+ <component class="net.seanhess.glue.tags.Action"/>
</componentPackage>
@@ -0,0 +1,98 @@
+package net.seanhess.glue.tags
+{
+ import flash.events.Event;
+ import flash.events.EventDispatcher;
+ import flash.events.IEventDispatcher;
+ import flash.utils.Dictionary;
+ import flash.utils.Proxy;
+ import flash.utils.flash_proxy;
+
+ import net.seanhess.bifff.core.Scope;
+
+ [Event(name="call", type="flash.events.Event")]
+ dynamic public class Action extends Proxy implements IEventDispatcher
+ {
+ public var watch:IEventDispatcher;
+ protected var dispatcher:IEventDispatcher = new EventDispatcher();
+ protected var listeners:Dictionary = new Dictionary();
+
+ public function execute():void
+ {
+ addListeners();
+ dispatchEvent(new Event("call"));
+ }
+
+ protected function addListeners():void
+ {
+ for (var event:String in listeners)
+ listen(event, listeners[event]);
+ }
+
+ protected function removeListeners():void
+ {
+ if (watch == null) return;
+
+ for (var event:String in listeners)
+ watch.removeEventListener(event, handler);
+ }
+
+ override flash_proxy function setProperty(name:*, value:*):void
+ {
+ if (value is Action)
+ value = [value];
+
+ if (!(value is Array))
+ throw new Error("Use <eventName> to specify a list of actions to perform");
+
+ listeners[name] = value;
+ }
+
+ protected function listen(name:*, value:*):void
+ {
+ if (watch == null)
+ throw new Error("Set watch in order to listen to events");
+
+ watch.addEventListener(name, handler);
+ }
+
+ private function handler(event:Event):void
+ {
+ removeListeners();
+
+ var scope:Scope = new Scope();
+ scope.parent = Scope.current;
+ scope.event = event;
+ scope.target = watch; // wait, what's wrong with this?
+
+ Scope.current = scope;
+
+ for each (var action:Action in listeners[event.type])
+ action.execute();
+ }
+
+ public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
+ {
+ dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
+ }
+
+ public function dispatchEvent(event:Event):Boolean
+ {
+ return dispatcher.dispatchEvent(event);
+ }
+
+ public function hasEventListener(type:String):Boolean
+ {
+ return dispatcher.hasEventListener(type);
+ }
+
+ public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
+ {
+ dispatcher.removeEventListener(type, listener, useCapture);
+ }
+
+ public function willTrigger(type:String):Boolean
+ {
+ return dispatcher.willTrigger(type);
+ }
+ }
+}
@@ -0,0 +1,52 @@
+package net.seanhess.glue.tags
+{
+ import flash.utils.Proxy;
+ import flash.utils.flash_proxy;
+
+ import net.seanhess.bifff.core.Scope;
+
+ [DefaultProperty("actions")]
+ dynamic public class Command extends Proxy
+ {
+ private var defaults:Object = {};
+ private var _actions:Array;
+
+ [ArrayElementType("Object")]
+ public function set actions(value:Array):void
+ {
+ _actions = value;
+ }
+
+ public function execute(parameters:Object=null):void
+ {
+ var scope:Scope = Scope.current;
+
+ if (parameters)
+ {
+ for (var paramName:String in parameters)
+ {
+ if (defaults[paramName])
+ {
+ scope[paramName] = parameters[paramName];
+ }
+ else
+ {
+ throw new Error("Could not find parameter " + paramName);
+ }
+ }
+ }
+
+ for (var defaultName:String in defaults)
+ if (!scope.defined(defaultName))
+ scope[defaultName] = defaults[defaultName];
+
+ for each (var action:Action in _actions)
+ action.execute();
+ }
+
+ override flash_proxy function setProperty(name:*, value:*):void
+ {
+ defaults[name] = value;
+ }
+ }
+}

0 comments on commit 32383e9

Please sign in to comment.