Created
November 20, 2008 21:41
-
-
Save timcharper/27220 to your computer and use it in GitHub Desktop.
very messy! (need to take out inline javascript and refactor)
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
require 'tabset_helpers.rb' | |
ActionView::Base.send(:include, TabsetHelpers) |
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
module TabsetHelpers | |
def tabset(options={}, &block) | |
# set a default id | |
options[:id] ||= "tabset_div_" + ( $tabset_unique_id = ($tabset_unique_id||0) + 1 ).to_s | |
id = options[:id] | |
selected_tab = options.delete(:selected) || "" if options.has_key?(:selected) | |
@tabset_tabs=[] | |
tabs_content = capture(&block) | |
tabs_html = "" | |
# output the tabs | |
@tabset_tabs.each { | tab | | |
tabs_html << "<li id='#{tab[:id]}_tab_button'><a href=\"javascript:$('#{id}').tabset.gotab('#{tab[:id]}');\"><span>#{tab[:label]}</span></a></li>" | |
} | |
tabs_html = content_tag( | |
:div, | |
content_tag(:ul, tabs_html), | |
options | |
) | |
tab_names = @tabset_tabs.map{ |tab| tab[:id] } | |
# output the javascript | |
js_output = <<END_OF_STRING | |
Tabset = Class.create(); | |
Tabset.prototype = { | |
initialize: function(tabbar_container_id, tab_names) | |
{ | |
this.tab_elements=$H(); | |
this.tabbar_container = $(tabbar_container_id); | |
this.tabbar_container.tabset = this; | |
this.master_parent = null; | |
this.tab_content_container = this.tabbar_container.next("div"); | |
// put tabs somewhere so they and their content can still be found with selectors | |
this.tab_content_storage = new Element("div", {style: "display: none; position: absolute;"}); | |
this.tab_content_container.up().appendChild(this.tab_content_storage); | |
$A(tab_names).each(function(tab) { | |
tab_element = this.tab_content_container.down('#' + tab + '_tab'); | |
this.tab_elements.set(tab, tab_element); | |
this.master_parent = tab_element.parentNode; | |
}.bind(this)); | |
}, | |
gotab: function(tab_name) | |
{ | |
if (! this.tab_elements.get(tab_name)) return false; | |
$A(this.master_parent.childNodes).each(function(node) { | |
this.master_parent.removeChild(node); | |
if(node.tagName == "DIV") { | |
this.tab_content_storage.appendChild(node); | |
} | |
}.bind(this)); | |
(selected_tab = this.tabbar_container.down(".current")) && selected_tab.removeClassName("current"); | |
this.tabbar_container.down("#" + tab_name + '_tab_button').addClassName("current"); | |
node = this.tab_elements.get(tab_name); | |
node.remove(); | |
this.master_parent.appendChild(node); | |
} | |
} | |
new Tabset("#{id}", #{tab_names.to_json}); | |
END_OF_STRING | |
selected_tab||= @tabset_tabs.first[:id] | |
js_output << "$('#{id}').tabset.gotab('#{selected_tab}');" | |
concat(tabs_html, block.binding) | |
concat("<div>", block.binding) | |
concat(tabs_content, block.binding) | |
concat("</div>", block.binding) | |
# Output all of the necessary javascript | |
concat("<script language='javascript'>#{js_output}</script>", block.binding) | |
end | |
def define_tab(id, label, &block) | |
@tabset_tabs << { | |
:id => id, | |
:label => label | |
} | |
concat("<div id='#{id}_tab'>", block.binding) | |
concat(capture(&block), block.binding) | |
concat("</div>", block.binding) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment