Module: QUI::Tabs::MainHelper

Defined in:
lib/qui-tabs-main-helper.rb

Instance Method Summary collapse

Instance Method Details

#tabs(options) ⇒ Object

Raises:

  • (RuntimeError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/qui-tabs-main-helper.rb', line 51

def tabs(options)
  @tabs_count ||= 0
  raise RuntimeError, "@tabs_count must be integer" unless @tabs_count.is_a? Fixnum

  default_id = "tabs_#{@tabs_count}"
  options[:id] ||= default_id
  
  headers = ""
  bodies = ""
  
  options[:tabs].each_with_index do |tab, i|
    if tab[:header][:text]
      if tab[:header][:text].is_a? Symbol
        header = t(tab[:header][:text])
      else
        header = tab[:header][:text].to_s
      end
    elsif tab[:header][:inline]
      header = ERB.new(tab[:header][:inline]).result
    elsif tab[:header][:partial]
      header = capture { render :partial => tab[:header][:partial], :locals => tab[:header][:locals] }
    else
      header = ""
    end
    
    headers << (:a, header, :id => "#{options[:id]}_tab_#{i}_header", :class => (i == 0 ? "current" : "" ), :href => "#", :onclick => "Tab.find(#{@tabs_count}).setCurrent(#{escape_javascript(i.to_s)}); return false;")

    if tab[:body][:text]
      if tab[:body][:text].is_a? Symbol
        body = t(tab[:body][:text])
      else
        body = tab[:body][:text].to_s
      end
    elsif tab[:body][:inline]
      body = ERB.new(tab[:body][:inline]).result
    elsif tab[:body][:partial]
      body = capture { render :partial => tab[:body][:partial], :locals => tab[:body][:locals] }
    else
      body = ""
    end
    
    bodies << (:div, body, :id => "#{options[:id]}_tab_#{i}_body", :class => (i == 0 ? "current" : "" ))
  end

  result = "<div class=\"tabs\" id=\"#{options[:id]}\">"
  result << "<div class=\"header\">"
  result << headers
  result << "</div>"

  result << "<div class=\"body\">"
  result << bodies
  result << "</div>"
  result << "</div>"

  js_options = { :id => @tabs_count, :count => options[:tabs].size }
  js_options.merge!({ :custom_html_id => options[:id] }) unless options[:id] == default_id
  result << javascript_tag("Tab.create(#{js_options.to_json});")

  @tabs_count += 1

  result
end

#tabs_javascriptObject



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
49
# File 'lib/qui-tabs-main-helper.rb', line 4

def tabs_javascript
<<-END
var Tab = {
    _all : {},
    
    create : function(options) {
this._all[options["id"]] = options;
if(options["custom_html_id"])
  this._all[options["id"]]["html_id"] = options["custom_html_id"]
else
  this._all[options["id"]]["html_id"] = "tabs_" + options["id"];
  
    },
    
    find : function(id) {
var obj = {
  id : id,
  setCurrent : function(index) {
    for(var i = 0; i < Tab._all[id]["count"]; i++) {
      var e = document.getElementById(Tab._all[id]["html_id"] + "_tab_" + i + "_header");
      if(e.className == "current") {
        e.className = "";
        break;
      }
    }
    var e = document.getElementById(Tab._all[id]["html_id"] + "_tab_" + index + "_header");
    e.className = "current";

    for(var i = 0; i < Tab._all[id]["count"]; i++) {
      var e = document.getElementById(Tab._all[id]["html_id"] + "_tab_" + i + "_body");
      if(e.className == "current") {
        e.className = "";
        break;
      }
    }
    var e = document.getElementById(Tab._all[id]["html_id"] + "_tab_" + index + "_body");
    e.className = "current";

  }
}

return obj;
    }
  }
END
end