Quantcast
Channel: Jozef Chúťka's blog » object
Viewing all articles
Browse latest Browse all 2

EmbedCode class to generate html code

$
0
0

embedcode

EmbedCode class generates html code for your .swf files. You can define url, size, parameters, flashVars and the result is valid standard (<object>, <params>, <embed>) html code, that you can just copy paste into your htmls, blogs, gigya widgets etc.

package sk.yoz.html.EmbedCode.as:

package sk.yoz.html
{
    import flash.display.LoaderInfo;
    import flash.net.URLVariables;
    
    public class EmbedCode extends Object
    {
        private var _params:Object = {};
        private var _flashVars:URLVariables;
        private var _width:uint;
        private var _height:uint;
        private var _url:String;
        
        private var loaderInfo:LoaderInfo;
        private var parameters:Object;
        
        public var encodeAmpersands:Boolean = false;
        
        public function EmbedCode(loaderInfo:LoaderInfo = null, parameters:Object = null)
        {
            super();
            this.loaderInfo = loaderInfo;
            this.parameters = parameters;
            
            useActualURL();
            useActualFlashVars();
        }
        
        public static function suggest(loaderInfo:LoaderInfo = null, parameters:Object = null, 
            encodeAmpersands:Boolean = false):EmbedCode
        {
            var embedCode:EmbedCode = new EmbedCode(loaderInfo, parameters);
            embedCode.encodeAmpersands = encodeAmpersands;
            embedCode.useActualURL();
            embedCode.useActualSize();
            embedCode.useActualFlashVars();
            return embedCode;
        }
        
        public static function containsProtocol(path:String):Boolean
        {
            return path.indexOf("http://") == 0 || path.indexOf("https://") == 0;
        }
        
        public static function urlToDomain(url:String):String
        {
            if(!containsProtocol(url))
                return null;
            var index:int = url.indexOf("/", 9);
            if(index == -1)
                return null;
            return url.substr(0, index);
        }
        
        public function get domain():String
        {
            return urlToDomain(url);
        }
        
        public function addParam(param:String, value:String):void
        {
            _params[param] = value;
        }
        
        public function set params(value:Object):void
        {
            _params = value;
        }
        
        public function get params():Object
        {
            return _params;
        }
        
        public function addFlashVar(variable:String, value:String):void
        {
            if(!flashVars)
                flashVars = new URLVariables();
            _flashVars[variable] = value;
        }
        
        public function set flashVars(value:URLVariables):void
        {
            _flashVars = value;
        }
        
        public function get flashVars():URLVariables
        {
            return _flashVars;
        }
        
        public function get flashVarsQueryString():String
        {
            var queryString:String = flashVars.toString();
            if(encodeAmpersands)
                queryString = queryString.replace(/\&/g, "&amp;");
            return queryString;
        }
        
        public function set width(value:uint):void
        {
            _width = value;
        }
        
        public function get width():uint
        {
            return _width;
        }
        
        public function set height(value:uint):void
        {
            _height = value;
        }
        
        public function get height():uint
        {
            return _height;
        }
        
        public function set url(value:String):void
        {
            _url = value;
        }
        
        public function get url():String
        {
            return _url;
        }
        
        public function toString():String
        {
            var key:String;
            var r:String = '';
            r += '<object width="' + width + '" height="' + height + '">';
            
            r += '<param name="movie" value="' + url + '"></param>';
            for(key in params)
                r += '<param name="' + key + '" value="' + params[key] + '"></param>';
            if(flashVars)
                r += '<param name="FlashVars" value="' + flashVarsQueryString + '"></param>';
                
            r += '<embed src="' + url + '" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash"';
            for(key in params)
                r += ' ' + key + '="' + params[key] + '" ';
            if(flashVars)
                r += ' FlashVars="' + flashVarsQueryString + '" ';
            r += '></embed>';
            
            r += '</object>';
            return r;
        }
        
        public function useActualURL():void
        {
            if(!loaderInfo)
                return;
            url = loaderInfo.url;
        }
        
        public function useActualSize():void
        {
            if(!loaderInfo)
                return;
            width = loaderInfo.width;
            height = loaderInfo.height;
        }
        
        public function useActualFlashVars():void
        {
            flashVars = new URLVariables();
            if(!parameters)
                return;
            for(var key:String in parameters)
                addFlashVar(key, parameters[key]);
        }
    }
}

Application

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init()" xmlns:html="sk.yoz.html.*">
<mx:Script>
<![CDATA[
    import sk.yoz.html.EmbedCode;
    
    private function init():void
    {
        code1.text = EmbedCode.suggest(loaderInfo, parameters, true).toString();
        
        var embedCode2:EmbedCode = new EmbedCode();
        embedCode2.encodeAmpersands = true;
        embedCode2.width = 100;
        embedCode2.height = 100;
        embedCode2.url = "http://domain/my.swf";
        embedCode2.addFlashVar("myFlashVar", "123");
        embedCode2.addFlashVar("mySuperFlashVar", "okey");
        embedCode2.addParam("wmode", "transparent");
        embedCode2.addParam("allowfullscreen", "true");
        
        code2.text = embedCode2.toString();
    }
]]>
</mx:Script>
<mx:TextArea id="code1" width="100%" height="100%" />
<mx:TextArea id="code2" width="100%" height="100%" />
</mx:Application>

code1.text:

<object width="500" height="375"><param name="movie" value="file:///D:/work/flex/...swf"></param><param name="FlashVars" value="isAdmin=true&amp;isFacebook=true"></param><embed src="file:///D:/work/flex/...swf" width="500" height="375" type="application/x-shockwave-flash" FlashVars="isAdmin=true&amp;isFacebook=true" ></embed></object>

code2.text:

<object width="100" height="100"><param name="movie" value="http://domain/my.swf"></param><param name="wmode" value="transparent"></param><param name="allowfullscreen" value="true"></param><param name="FlashVars" value="mySuperFlashVar=okey&amp;myFlashVar=123"></param><embed src="http://domain/my.swf" width="100" height="100" type="application/x-shockwave-flash" wmode="transparent"  allowfullscreen="true"  FlashVars="mySuperFlashVar=okey&amp;myFlashVar=123" ></embed></object>

Viewing all articles
Browse latest Browse all 2

Trending Articles