public
Description: a Ruby on Rails plugin for easier integration of the YUI library into a Ruby on Rails application. It will include helpers for generating widgets and including the YUI file dependencies.
Homepage: http://rubyforge.org/projects/yui4rails/
Clone URL: git://github.com/mghaught/yui4rails.git
yui4rails / lib / yui4rails / widgets / carousel.rb
100644 48 lines (45 sloc) 1.275 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Yui4Rails
module Widgets
class Carousel
def initialize(carousel_id, collection, options = {})
@carousel_id = carousel_id
@collection = collection
@options = defaults.merge(options)
@options[:size] = @collection.size
Yui4Rails::AssetManager.manager.add_components :carousel
render_head_script
end
 
def render_head_script
Yui4Rails::AssetManager.manager.add_script <<-PAGE
YAHOO.util.Event.addListener(window, "load", function()
{
new YAHOO.extension.Carousel("#{@carousel_id}",
{#{@options.keys.map{|key| optional_value(key)}.join(", ")}}
);
});
PAGE
end
 
private
# TODO - make a way to take a app-wide options to merge into these defaults
def defaults
{
:numVisible => 3,
:animationSpeed => 0.5,
:scrollInc => 3,
:navMargin => 60,
:prevElement => "prev_arrow",
:nextElement => "next_arrow",
:wrap => true
}
end
 
def optional_value(option, carousel_key = nil)
carousel_key ||= option
if @options.has_key?(option) && !@options[option].nil?
%\#{carousel_key}: #{@options[option].is_a?(String) ? "'#{@options[option]}'" : @options[option] }\
else
""
end
end
end
end
end