ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovichpackage com.yahoo.util
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich import flash.display.Stage;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich import flash.external.ExternalInterface;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich import flash.utils.getDefinitionByName;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich import flash.system.Security;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public class YUIBridge extends Object
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public var flashvars:Object;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich private var _jsHandler:String;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich private var _swfID:String;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich private var _yId:String;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich private var _stage:Stage;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich private var _instances:Object = {};
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function YUIBridge(stage:Stage)
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich _stage = stage;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich flashvars = _stage.loaderInfo.parameters;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if (flashvars["yId"] && flashvars["YUIBridgeCallback"] && flashvars["YUISwfId"] && ExternalInterface.available) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich _jsHandler = flashvars["YUIBridgeCallback"];
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich _swfID = flashvars["YUISwfId"];
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich _yId = flashvars["yId"];
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if(flashvars.hasOwnProperty("allowedDomain"))
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich Security.allowDomain(flashvars.allowedDomain);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.addCallback("createInstance", createInstance);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.addCallback("exposeMethod", exposeMethod);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.addCallback("getProperty", getProperty);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.addCallback("setProperty", setProperty);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function createInstance(instanceId:String, className:String, constructorArguments:Array = null) : void {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich constructorArguments == null ? constructorArguments = [] : constructorArguments = constructorArguments;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich var cA:Array = constructorArguments;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich var classReference:Class = getDefinitionByName(className) as Class;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich var instance:Object;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich switch (cA.length) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich default: instance = new classReference();
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 1: instance = new classReference(cA[0]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 2: instance = new classReference(cA[0], cA[1]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 3: instance = new classReference(cA[0], cA[1], cA[2]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 4: instance = new classReference(cA[0], cA[1], cA[2], cA[3]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 5: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 6: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 7: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 8: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6], cA[7]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 9: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6], cA[7], cA[8]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 10: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6], cA[7], cA[8], cA[9]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 11: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6], cA[7], cA[8], cA[9], cA[10]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 12: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6], cA[7], cA[8], cA[9], cA[10], cA[11]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich case 13: instance = new classReference(cA[0], cA[1], cA[2], cA[3], cA[4], cA[5], cA[6], cA[7], cA[8], cA[9], cA[10], cA[11], cA[12]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich _instances[instanceId] = instance;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function exposeMethod(instanceId:String, methodName:String, exposedName:String = "") : void {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich exposedName == "" ? exposedName = methodName : exposedName = exposedName;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if (_instances[instanceId] && ExternalInterface.available) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.addCallback(exposedName, _instances[instanceId][methodName]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function getProperty (instanceId:String, propertyName:String) : Object {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if (_instances[instanceId] && _instances[instanceId].hasOwnProperty(propertyName)) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich return _instances[instanceId][propertyName];
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich else {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich return null;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function setProperty (instanceId:String, propertyName:String, propertyValue:Object) : void {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if (_instances[instanceId] && _instances[instanceId].hasOwnProperty(propertyName)) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich _instances[instanceId][propertyName] = propertyValue;
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function addCallbacks (callbacks:Object) : void {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if (ExternalInterface.available) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich for (var callback:String in callbacks) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich trace("Added callback for " + callback + ", function " + callbacks[callback]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.addCallback(callback, callbacks[callback]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich sendEvent({type:"swfReady"});
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich public function sendEvent (evt:Object) : void {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich if (ExternalInterface.available) {
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich ExternalInterface.call("YUI.applyTo", _yId, _jsHandler, [_swfID, evt]);
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }
ec3da7f85c70d991f53c50587791088b994256f7Allen Rabinovich }