Introduce proper player helper with proper template

openid
Marcin Kulik 12 years ago
parent 4fba36778e
commit 560ab8362a

@ -1,15 +1,10 @@
module AsciicastsHelper
def player_script_tag(asciicast, options = {})
speed = (options[:speed] || 1).to_f
benchmark = !!params[:bm]
auto_play = options.key?(:auto_play) ? !!options[:auto_play] : false
hud = options.key?(:hud) ? !!options[:hud] : true
def player(asciicast, options = {})
if params[:fallback]
klass = "AsciiIo.FallbackPlayer"
player_class = "AsciiIo.FallbackPlayer"
else
klass = "window.Worker && $.browser.webkit ? " \
player_class = "window.Worker && $.browser.webkit ? " \
"AsciiIo.Player : AsciiIo.FallbackPlayer"
end
@ -19,24 +14,17 @@ module AsciicastsHelper
renderer_class = "AsciiIo.Renderer.Pre"
end
return <<EOS.html_safe
<script>
$(function() {
var playerClass = #{klass};
window.player = new playerClass({
el: $('.player'),
cols: #{asciicast.terminal_columns},
lines: #{asciicast.terminal_lines},
speed: #{speed},
benchmark: #{benchmark},
model: new AsciiIo.Asciicast({ id: #{asciicast.id} }),
rendererClass: #{renderer_class},
autoPlay: #{auto_play},
hud: #{hud},
snapshot: "#{j asciicast.snapshot.to_s}"
});
});
</script>
EOS
render :partial => 'asciicasts/player', :locals => {
player_class: player_class,
cols: asciicast.terminal_columns,
lines: asciicast.terminal_lines,
speed: (options[:speed] || params[:speed] || 1).to_f,
benchmark: !!params[:bm],
asciicast_id: asciicast.id,
renderer_class: renderer_class,
auto_play: options.key?(:auto_play) ? !!options[:auto_play] : false,
hud: options.key?(:hud) ? !!options[:hud] : true,
snapshot: asciicast.snapshot
}
end
end

@ -1,5 +1,18 @@
<div class="player"></div>
<%= player_script_tag(asciicast,
:speed => local_assigns[:speed],
:hud => local_assigns[:hud]
) %>
<script>
$(function() {
var playerClass = <%= player_class.html_safe %>;
window.player = new playerClass({
el: $('.player'),
cols: <%= cols %>,
lines: <%= lines %>,
speed: <%= speed %>,
benchmark: <%= benchmark %>,
model: new AsciiIo.Asciicast({ id: <%= asciicast_id %> }),
rendererClass: <%= renderer_class.html_safe %>,
autoPlay: <%= auto_play %>,
hud: <%= hud %>,
snapshot: "<%= j snapshot %>"
});
});
</script>

@ -1,11 +1,6 @@
<section id="presentation" class="feature">
<div class="asciicast-wrapper">
<%= render :partial => 'asciicasts/player', :locals => {
:asciicast => @asciicast,
:speed => params[:speed],
:hud => true
} %>
<%= player @asciicast %>
</div>
</section>

@ -18,11 +18,7 @@
<div class="secondary_info">
<% if @asciicast -%>
<%= render :partial => 'asciicasts/player', :locals => {
:asciicast => @asciicast,
:speed => 2.0,
:hud => false
} %>
<%= player @asciicast, :speed => 2.0, :hud => false %>
<% end -%>
</div>
</div>

Loading…
Cancel
Save