Created
October 29, 2008 17:30
-
-
Save code/20755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File: swf.rb | |
# Author: Luke Hubbard | |
# Gist: http://gist.github.com/20755 | |
# License: MIT | |
# NOTE THIS HAS NOW BEEN REPLACE BY | |
# http://github.com/code/swf_object_helper | |
# SWFObject Embed Helper - Easy swf embed using SWFObject and ruby magic | |
# | |
# ==== Installation (not currently a plugin, just copy and paste!): | |
# | |
# * Grab the latest code from http://code.google.com/p/swfobject/ (version 2.1.0) | |
# * Put the expressInstall.swf in /public/swfs/ and the swfobject.js in /public/javascripts/ | |
# * Add javascript_include_tag 'swfobject' in the head section of your layout | |
# * Put this code in your /app/helpers/application_helper.rb or another helper | |
# | |
# ==== Usage: | |
# | |
# swf <src>, <id>, <width>, <height>, <version>, <express_install>, <options>, <&block> | |
# | |
# ==== Notes: | |
# | |
# * You can use arguments or options or any combination of the two. Arguments take presidence. | |
# * If you pass a block it will be used to capture alternate content. | |
# * All embeds are added to swfobject.embeds js object. Allows for differed embeds and re-embeds | |
# * MAGIC WARNING! Has some magic to detect the width / height and flash version from the src name. | |
# * You don't need to give full path or even the extension it will prepend /swfs/ and append .swf if needed | |
# * Example embed using all the magic: swf 'gallery_600x400_f8' | |
# | |
# ==== Arguments: | |
# | |
# * <tt>:src:</tt> (required) the src of the swf file. e.g. 'intro' or '/flash/some.swf' | |
# * <tt>:id:</tt> (defaults to first part of src filename) id of the div to be created for the swf to be embedded into | |
# * <tt>:width:</tt> (magic if src match e.g. 200x100 ) width of the swf object | |
# * <tt>:height:</tt> (magic if src match e.g. 200x100 ) height of the swf object | |
# * <tt>:version:</tt> (magic if src match e.g. f8 if not there uses 9.0.0) flash player version | |
# * <tt>:express_install:</tt> (default /swfs/expressInstall.swf ) location of the express install swf file | |
# * <tt>:options:</tt> (optional) additional options (note: be used instead of arguments) | |
# * <tt>:block:</tt> (optional) if you pass a block captures alternative content | |
# | |
# ==== Options ( in addition to :src:, :id:, :width:, :height:, :version:, and :express_install: ): | |
# | |
# * <tt>:flashvars:</tt> (optional) hash of flashvars to be passed through to flash | |
# * <tt>:params:</tt> (optional) hash of params to be set on flash player, e.g. wmode | |
# * <tt>:attributes:</tt> (optional) hash of attributes to be set on the object / embed tags | |
# * <tt>:differed:</tt> (default false) differ the embed, you can embed later using swfobject.embeds['my_id'](); | |
# * <tt>:content:</tt> (optional) alternative content, if you are not using a block | |
# | |
# ==== Todo | |
# | |
# * Make some example gists showing erb and haml usage | |
# * Turn this into a plugin / gem for easy install | |
# * Make a merb version, think capture and content_tag are only rails specific bits. | |
# | |
def swf(*args, &block) | |
opts = (args.pop if args.last.is_a?(Hash)) || {} | |
src, id, width, height, version, express_install = args | |
src ||= opts[:src] | |
src = '/swfs/' + src unless src =~ /\// | |
src = src + '.swf' unless src =~ /\.swf/ | |
id ||= opts[:id] | |
id = $1.split(/\_/).first if id.nil? and src =~ /\/([a-z0-9\-\_]+)\.swf/i | |
width ||= opts[:width] | |
height ||= opts[:height] | |
width, height = $1, $2 if (width.nil? or height.nil?) and src =~ /[^a-z0-9]+([0-9]{1,4})x([0-9]{1,4})[^a-z0-9]+/i | |
version ||= opts[:version] | |
version = [$1, $3 || 0 , $5 || 0] * "." if version.nil? and src =~ /[^a-z0-9]+f[\-\_\.]?([0-9]+)([\-\_\.]([0-9]+))?([\-\_\.]([0-9]+))?[^a-z0-9]+/i | |
version ||= "9.0.0" | |
express_install ||= opts[:express_install] || '/srcs/expressInstall.swf' | |
flashvars = opts[:flashvars].nil? ? '{}' : opts[:flashvars].to_json | |
params = opts[:params].nil? ? '{}' : opts[:params].to_json | |
attributes = opts[:attributes].nil? ? '{}' : opts[:attributes].to_json | |
differed = opts[:differed].eql?(true) | |
content = (capture(&block) if block_given?) || opts[:content] || '' | |
embed = " (function(so, differed){ if(so.embeds == undefined){ so.embeds = {}; }; var embed = so.embeds['#{id}'] = function(){ so.embedSWF('#{src}', '#{id}', '#{width}', '#{height}', '#{version}', '#{express_install}', #{flashvars}, #{params}, #{attributes}); }; if(!differed){ embed(); }; })(swfobject, #{differed ? 'true' : 'false'}); " | |
"<div id=\"#{id}\">\n#{content}</div>\n#{content_tag(:script, embed)}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment