flowplayer-gem/lib/flowplayer/player.rb

97 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

2011-01-31 22:22:47 +00:00
require 'json'
module Flowplayer
class Player
2011-02-01 21:03:28 +00:00
attr_accessor :options, :functions, :dom_id, :swf
2011-02-01 22:58:56 +00:00
def initialize(dom_id, swf, lib='jquery', &block)
@dom_id, @swf, @lib = dom_id, swf, lib
2011-01-31 22:22:47 +00:00
@options = {}
@functions = {}
block.call(self)
self
end
def to_js
json = options_to_javascript
json += functions_to_javascript
"{#{json.join(', ')}}"
end
def script_tags
2011-02-18 04:18:52 +00:00
final = library("flowplayer(\"#{dom_id}\", #{swf.to_json}, #{to_js});")
2011-01-31 22:22:47 +00:00
<<-EOS
<script type='text/javascript'>
//<![CDATA[
2011-02-01 22:58:56 +00:00
#{final}
2011-01-31 22:22:47 +00:00
//]]>
</script>
EOS
end
2011-02-01 22:58:56 +00:00
def library(func)
case @lib
when 'jquery'
jquery(func)
when 'prototype'
prototype(func)
end
end
def jquery(func)
<<-EOS
$(document).ready(function() {
#{func}
});
EOS
end
def prototype(func)
<<-EOS
document.observe("dom:loaded", function() {
#{func}
});
EOS
end
2011-02-16 19:11:17 +00:00
def only_play_button!(opts = {})
options[:plugins] ||= {}
options[:plugins][:controls] ||= {}
hash = {
:mute => false,
:slowForward => false,
:time => false,
:slowBackwards => false,
:volume => false,
:scrubber => false,
:stop => false,
:fullscreen => false,
:play => true
}
hash.merge!(opts)
options[:plugins][:controls].merge!(hash)
end
2011-01-31 22:22:47 +00:00
private
def functions_to_javascript
functions.map {|option, function| "\"#{option}\":#{function}"}
end
def options_to_javascript
options.map do |option, value|
2011-02-01 22:58:56 +00:00
"\"#{option}\":#{value.to_json}"
2011-01-31 22:22:47 +00:00
end
end
def method_missing(method, *args, &block)
raise "Setters are not supported use method('whatever') to set configs" if /\=$/.match(method.to_s)
if block.nil?
options[method] = args.first
else
functions[method] = "function() { #{block.call.gsub(/\;$/, '')}; }"
end
return method
end
end
end