I created a new swfobject html helper class to manage embedded flash
media. I want to share it both for a review and to motivate contribute
great tips and content to this framework. I also have some questions
like: how to create static links right way? Im using my own Script
(and Style) helpers here, take it for a review too, if you like:



SWFOBJECT_JS_URL = '/own/static/swfobject.js'
SWFOBJECT_PLAYER_URL = '/own/static/mediaplayer.swf'
SWFOBJECT_DEFAULT_NO_CONTENT = SPAN(A("Get Flash -player",
_href="http://www.macromedia.com/go/getflashplayer";,
_target="_blank"), " to see the Flash content.")
SWFOBJECT_DEFAULT_WIDTH = 785
SWFOBJECT_DEFAULT_HEIGHT = 380
SWFOBJECT_REQUIRED_FLASH_VERSION = 8
SWFOBJECT_DEFAULT_ID = 'flash-player'
SWFOBJECT_DEFAULT_PLAYER_CLASS = 'flash-players'
SWFOBJECT_CONTAINER_CLASS = 'flash-content'

class SWFObject(object):
    """
       Flash media player

       Note: 1) copy swfobject.js, mediaplayer.swf, crossdomain.xml
and playlist.xml to /static/ directory
             2) see parameter and variable setters from:
http://blog.deconcept.com/swfobject/

       Example usage:

       so = SWFObject(player_id = "my-player")
       so.addParams(allowfullscreen = "true", allowscriptaccess =
"always", seamlesstabbing = "true")
       so.addVariables(file = '/own/static/playlist.xml', width = 785,
height = 380, displaywidth = 560, backcolor = '0xFFFFFF', frontcolor =
'0x6666FF', bufferlength = 5, overstretch = 'fit', autoscroll =
'true', thumbsinplaylist = 'false', autostart = 'false')
       print so
    """

    def __init__(self, sqlobject_url = None, no_flash_content = None,
player_id = None, player_url = None, width = None, height = None,
required_flash_version = None):
        self.sqlobject_url          = sqlobject_url or
SWFOBJECT_JS_URL
        self.player_id              = player_id or
SWFOBJECT_DEFAULT_ID
        self.player_url             = player_url or
SWFOBJECT_PLAYER_URL
        self.no_flash_content       = no_flash_content or
SWFOBJECT_DEFAULT_NO_CONTENT
        self.width                  = width or SWFOBJECT_DEFAULT_WIDTH
        self.height                 = height or
SWFOBJECT_DEFAULT_HEIGHT
        self.required_flash_version = required_flash_version or
SWFOBJECT_REQUIRED_FLASH_VERSION
        self.params                 = list()
        self.variables              = list()

    def addParams(self, **kwargs):
        for key, value in kwargs.items():
            self.params.append((key, value))

    def addVariables(self, **kwargs):
        for key, value in kwargs.items():
            self.variables.append((key, value))

    def _getParams(self):
        content = ''
        for key, value in self.params:
            content += "so.addParam('%s', '%s');%s" % (key, value,
NEWLINE)
        return content

    def _getVariables(self):
        content = ''
        for key, value in self.variables:
            content += "so.addVariable('%s', '%s');%s" % (key, value,
NEWLINE)
        return content

    def xml(self):
        contents = list()
        contents.append(Script(_src = self.sqlobject_url))
        if self.no_flash_content:
           contents.append(DIV(self.no_flash_content, _id =
self.player_id, _class = SWFOBJECT_DEFAULT_PLAYER_CLASS))

        script_content = """
var so = new SWFObject('%s', 'mpl', '%s', '%s', '8');
%s%sso.write('%s');
""" % (self.player_url,
       self.width,
       self.height,
       self._getParams(),
       self._getVariables(),
       self.player_id)

        contents.append(Script(script_content))
        return DIV(_class = SWFOBJECT_CONTAINER_CLASS, *contents).xml
()

    def __str__(self):
        return self.xml()



class Script(SCRIPT):
    """ ... """
    def xml(self):
        """
        Handle both external and internal script elements
        >>> print Script('function _alert() { alert("hi!"); }',
_language='javascript').xml()
        <script language="javascript" type="text/javascript">
        /* <![CDATA[ */
        function _alert() { alert("hi!"); }
        /* ]]> */
        </script>
        >>> print Script(_language='javascript', _src="/script.js").xml
()
        <script language="javascript" src="/script.js" type="text/
javascript"></script>
        """
        # if there is content, we presume, that this is an internal
script element
        if self.contents:
            # http://javascript.about.com/library/blxhtml.htm
            content = '%s/* <![CDATA[ */%s%s%s/* ]]> */%s' % (NEWLINE,
NEWLINE, self._getContent(), NEWLINE, NEWLINE)
            return '<%s%s>%s</%s>' % (self.tag_name,
self._getAttributes(), content, self.tag_name)
        # call parent xml method
        return SCRIPT.xml(self)


class Style(STYLE):
    """ ... """
    def xml(self):
        """
        Handle both external and internal style elements
        >>> print Style(_href="/style.css", _media="screen").xml()
        <link href="/style.css" media="screen" type="text/css" />
        >>> print Style('body {background: #333 url("/bg.gif") no-
repeat}').xml()
        <style type="text/css">
        <!--
        body {background: #333 url("/bg.gif") no-repeat}
        -->
        </style>

        """
        # if there is content, we presume, that this is an internal
style element
        if self.contents:
            # http://javascript.about.com/library/blxhtml.htm
            content = '%s<!--%s%s%s-->%s' % (NEWLINE, NEWLINE,
self._getContent(), NEWLINE, NEWLINE)
            return '<%s%s>%s</%s>' % (self.tag_name,
self._getAttributes(), content, self.tag_name)
        # call parent xml method
        return LINK(**self.attributes).xml()


SWFObject example creates html like:

<div class="flash-content"><script src="/own/static/swfobject.js"
type="text/javascript"></script><div class="flash-players" id="my-
player"><span><a href="http://www.macromedia.com/go/getflashplayer";
target="_blank">Get Flash -player</a> to see the Flash content.</
span></div><script type="text/javascript">
/* <![CDATA[ */

var so = new SWFObject('/own/static/mediaplayer.swf', 'mpl', '785',
'380', '8');
so.addParam('allowscriptaccess', 'always');
so.addParam('allowfullscreen', 'true');
so.addParam('seamlesstabbing', 'true');
so.addVariable('overstretch', 'fit');
so.addVariable('bufferlength', '5');
so.addVariable('frontcolor', '0x6666FF');
so.addVariable('height', '380');
so.addVariable('width', '785');
so.addVariable('displaywidth', '560');
so.addVariable('thumbsinplaylist', 'false');
so.addVariable('file', '/own/static/playlist.xml');
so.addVariable('backcolor', '0xFFFFFF');
so.addVariable('autoscroll', 'true');
so.addVariable('autostart', 'false');
so.write('my-player');

/* ]]> */
</script></div>

Please note 1) Style class creates both external and internal style 2)
embedded javascript start and end notes, that are fully explained and
argued here: http://javascript.about.com/library/blxhtml.htm

-Marko
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to