funky_tabs 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +0 -0
- data/Manifest +19 -0
- data/README.rdoc +0 -0
- data/Rakefile +14 -0
- data/app/controllers/funky_tabs/update_controller.rb +14 -0
- data/app/views/funky_tabs/update/_ajax.js.erb +1 -0
- data/app/views/funky_tabs/update/_render_funky_tabs.html.erb +68 -0
- data/app/views/funky_tabs/update/missing_tab.html.erb +1 -0
- data/app/views/funky_tabs/update/update_tab.html.erb +10 -0
- data/config/routes.rb +5 -0
- data/funky_tabs.gemspec +30 -0
- data/init.rb +1 -0
- data/lib/funky_tabs/helpers.rb +32 -0
- data/lib/funky_tabs/rails.rb +7 -0
- data/lib/funky_tabs/renderers.rb +19 -0
- data/lib/funky_tabs/tab.rb +80 -0
- data/lib/funky_tabs/tab_action.rb +23 -0
- data/lib/funky_tabs.rb +159 -0
- data/test/funky_tabs_test.rb +23 -0
- metadata +86 -0
data/CHANGELOG
ADDED
File without changes
|
data/Manifest
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
Manifest
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
app/controllers/funky_tabs/update_controller.rb
|
6
|
+
app/views/funky_tabs/update/_ajax.js.erb
|
7
|
+
app/views/funky_tabs/update/_render_funky_tabs.html.erb
|
8
|
+
app/views/funky_tabs/update/missing_tab.html.erb
|
9
|
+
app/views/funky_tabs/update/update_tab.html.erb
|
10
|
+
config/routes.rb
|
11
|
+
funky_tabs.gemspec
|
12
|
+
init.rb
|
13
|
+
lib/funky_tabs.rb
|
14
|
+
lib/funky_tabs/helpers.rb
|
15
|
+
lib/funky_tabs/rails.rb
|
16
|
+
lib/funky_tabs/renderers.rb
|
17
|
+
lib/funky_tabs/tab.rb
|
18
|
+
lib/funky_tabs/tab_action.rb
|
19
|
+
test/funky_tabs_test.rb
|
data/README.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('funky_tabs', '0.1.1') do |p|
|
6
|
+
p.description = "Create Ajaxified tabs with simple history through window location hashes for Rails."
|
7
|
+
p.url = "http://github.com/thomaspouncy/funky_tabs"
|
8
|
+
p.author = "Thomas Pouncy"
|
9
|
+
p.email = "thomas.pouncy@gmail.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class FunkyTabs::UpdateController < ApplicationController
|
2
|
+
layout false
|
3
|
+
|
4
|
+
def funky_tab_action
|
5
|
+
tab,tab_action,tab_action_id = FunkyTabs.tab_and_tab_action_from_location_hash(params[:location_hash])
|
6
|
+
action_to_call = FunkyTabs.content_path_for_tab_and_tab_action(tab,tab_action,tab_action_id)
|
7
|
+
redirect_to action_to_call+"?funky_tabs=true&location_hash=#{params[:location_hash]}" and return unless action_to_call.include?("?")
|
8
|
+
redirect_to action_to_call+"&funky_tabs=true&location_hash=#{params[:location_hash]}" and return
|
9
|
+
end
|
10
|
+
|
11
|
+
def missing_funky_tab
|
12
|
+
render FunkyTabs.missing_tab_action_path,:layout=>false and return
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
funkyTabsAjaxCall('<%= @location_hash_for_ajax %>');
|
@@ -0,0 +1,68 @@
|
|
1
|
+
<!-- BEGIN Funky Tabs -->
|
2
|
+
<div id="funky_tabs">
|
3
|
+
<ul class="css-tabs">
|
4
|
+
<% FunkyTabs.tabs.each_index do |tab_index| %>
|
5
|
+
<li id="funky_tabs_list-<%= tab_index %>" class="funky_tabs_list"><a href="#funky_tab-<%= tab_index %>"><%= FunkyTabs.tabs[tab_index].title %></a></li>
|
6
|
+
<% end %>
|
7
|
+
</ul>
|
8
|
+
<% FunkyTabs.tabs.each_index do |tab_index| %>
|
9
|
+
<div id="funky_tab-<%= tab_index %>" class="funky_tab"></div>
|
10
|
+
<% end %>
|
11
|
+
</div>
|
12
|
+
<div style="clear:both;"></div>
|
13
|
+
<script type="text/javascript">
|
14
|
+
function selectFunkyTab(tab_index){
|
15
|
+
jQuery('div#funky_tabs li.funky_tabs_list').removeClass('selected_tab');
|
16
|
+
jQuery('ul.css-tabs li#funky_tabs_list-'+tab_index).addClass('selected_tab');
|
17
|
+
jQuery('div#funky_tabs div.funky_tab').removeClass('selected_tab_div');
|
18
|
+
jQuery('div#funky_tabs div#funky_tab-'+tab_index).addClass('selected_tab_div');
|
19
|
+
if(load_on_select){
|
20
|
+
funkyTabsAjaxCall(tab_index);
|
21
|
+
} else {
|
22
|
+
load_on_select = true;
|
23
|
+
}
|
24
|
+
<%= FunkyTabs.tab_select_callback.gsub(/:tab_number/,'tab_index') unless FunkyTabs.tab_select_callback.nil? %>
|
25
|
+
}
|
26
|
+
function funkyTabsAjaxCall(location_hash){
|
27
|
+
call_hash_change = false;
|
28
|
+
jQuery('div#funky_tabs div.funky_tab').html("<%= raw(FunkyTabs.loading_html) %>");
|
29
|
+
window.location.hash = location_hash;
|
30
|
+
jQuery("div#funky_tabs div.selected_tab_div").load("/funky_tabs/update/funky_tab_action?location_hash="+location_hash);
|
31
|
+
}
|
32
|
+
function hashChangeEvent(){
|
33
|
+
if(call_hash_change){
|
34
|
+
funkyTabsAjaxCall(location.hash.replace("#",""));
|
35
|
+
} else {
|
36
|
+
call_hash_change = true;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
if(typeof(jQuery(window).hashchange) == "undefined"){
|
40
|
+
// dont have special plugin, so use onhashchange event. NOTE: this will not work in ie 6 or 7
|
41
|
+
window.onhashchange = hashChangeEvent;
|
42
|
+
} else {
|
43
|
+
// if we can find the hashchange jquery plugin (http://benalman.com/projects/jquery-hashchange-plugin/), then use it
|
44
|
+
jQuery(window).hashchange(hashChangeEvent);
|
45
|
+
}
|
46
|
+
var load_on_select = true;
|
47
|
+
var call_hash_change = true;
|
48
|
+
jQuery(function() {
|
49
|
+
var initial_tab = <%= FunkyTabs.default_tab_index %>;
|
50
|
+
if(window.location.hash != ""){
|
51
|
+
initial_tab = parseInt(window.location.hash.replace("#","").split("-")[0]);
|
52
|
+
}
|
53
|
+
var tab = jQuery("#funky_tabs").tabs({
|
54
|
+
show: function(event,ui){
|
55
|
+
},
|
56
|
+
select: function(event,ui){
|
57
|
+
selectFunkyTab(ui.index);
|
58
|
+
},
|
59
|
+
effect: 'ajax'
|
60
|
+
});
|
61
|
+
if(initial_tab == <%= FunkyTabs.default_tab_index %>){
|
62
|
+
selectFunkyTab(initial_tab);
|
63
|
+
} else {
|
64
|
+
jQuery("div#funky_tabs").tabs("select",initial_tab);
|
65
|
+
}
|
66
|
+
});
|
67
|
+
</script>
|
68
|
+
<!-- END Funky Tabs -->
|
@@ -0,0 +1 @@
|
|
1
|
+
That tab action appears to be missing. We apologize for the inconvenience.
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<script type="text/javascript">
|
2
|
+
window.location.hash = '<%= @location_hash %>';
|
3
|
+
jQuery("div#funky_tabs div#funky_tab-<%= @tab_index %>").html("<%= escape_javascript(@render_content) %>");
|
4
|
+
if(!jQuery("div#funky_tabs div#funky_tab-<%= @tab_index %>").hasClass('selected_tab_div')){
|
5
|
+
load_on_select = false;
|
6
|
+
jQuery("#funky_tabs").tabs("select", <%= @tab_index %>);
|
7
|
+
}
|
8
|
+
<%= FunkyTabs.tab_load_callback.gsub(/:tab_number/,@tab_index) unless FunkyTabs.tab_load_callback.nil? %>
|
9
|
+
</script>
|
10
|
+
|
data/config/routes.rb
ADDED
data/funky_tabs.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{funky_tabs}
|
5
|
+
s.version = "0.1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Thomas Pouncy"]
|
9
|
+
s.date = %q{2011-05-20}
|
10
|
+
s.description = %q{Create Ajaxified tabs with simple history through window location hashes for Rails.}
|
11
|
+
s.email = %q{thomas.pouncy@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/funky_tabs.rb", "lib/funky_tabs/helpers.rb", "lib/funky_tabs/rails.rb", "lib/funky_tabs/renderers.rb", "lib/funky_tabs/tab.rb", "lib/funky_tabs/tab_action.rb"]
|
13
|
+
s.files = ["CHANGELOG", "Manifest", "README.rdoc", "Rakefile", "app/controllers/funky_tabs/update_controller.rb", "app/views/funky_tabs/update/_ajax.js.erb", "app/views/funky_tabs/update/_render_funky_tabs.html.erb", "app/views/funky_tabs/update/missing_tab.html.erb", "app/views/funky_tabs/update/update_tab.html.erb", "config/routes.rb", "funky_tabs.gemspec", "init.rb", "lib/funky_tabs.rb", "lib/funky_tabs/helpers.rb", "lib/funky_tabs/rails.rb", "lib/funky_tabs/renderers.rb", "lib/funky_tabs/tab.rb", "lib/funky_tabs/tab_action.rb", "test/funky_tabs_test.rb"]
|
14
|
+
s.homepage = %q{http://github.com/thomaspouncy/funky_tabs}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Funky_tabs", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{funky_tabs}
|
18
|
+
s.rubygems_version = %q{1.6.2}
|
19
|
+
s.summary = %q{Create Ajaxified tabs with simple history through window location hashes for Rails.}
|
20
|
+
s.test_files = ["test/funky_tabs_test.rb"]
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'funky_tabs'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'funky_tabs'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module FunkyTabs
|
5
|
+
module Helpers
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
def render_funky_tabs
|
9
|
+
#@tab_data = FunkyTabs
|
10
|
+
return render_to_string :partial=>"funky_tabs/update/render_funky_tabs"
|
11
|
+
end
|
12
|
+
|
13
|
+
def location_hash_for_funky_tab(tab,tab_action=nil,tab_action_id=nil)
|
14
|
+
return FunkyTabs.location_hash_for_tab_action(tab,tab_action,tab_action_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def redirect_to_funky_tab(tab,tab_action=nil,tab_action_id=nil)
|
18
|
+
params[:location_hash] = FunkyTabs.location_hash_for_tab_action(tab,tab_action,tab_action_id)
|
19
|
+
action_to_call = FunkyTabs.content_path_for_tab_and_tab_action(tab,tab_action)
|
20
|
+
redirect_to action_to_call+"?funky_tabs=true&location_hash=#{params[:location_hash]}" and return unless action_to_call.include?("?")
|
21
|
+
redirect_to action_to_call+"&funky_tabs=true&location_hash=#{params[:location_hash]}" and return
|
22
|
+
end
|
23
|
+
|
24
|
+
def js_to_funky_tab(tab,tab_action=nil,tab_action_id=nil)
|
25
|
+
@location_hash_for_ajax = FunkyTabs.location_hash_for_tab_action(tab,tab_action,tab_action_id)
|
26
|
+
return render_to_string :partial=>"funky_tabs/update/ajax.js"
|
27
|
+
end
|
28
|
+
helper_method :render_funky_tabs, :redirect_to_funky_tab, :js_to_funky_tab
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'funky_tabs'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module FunkyTabs
|
5
|
+
module Renderers
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module InstanceMethods
|
9
|
+
def render(*args, &block)
|
10
|
+
super(*args, &block) and return if params[:funky_tabs].nil?
|
11
|
+
@tab_index = FunkyTabs.tab_index_from_location_hash(params[:location_hash])
|
12
|
+
super(*args, &block) and return if @tab_index.nil?
|
13
|
+
@location_hash = params[:location_hash]
|
14
|
+
@render_content = render_to_string(*args, &block)
|
15
|
+
super(:template=>"funky_tabs/update/update_tab",:layout=>false)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'funky_tabs'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module FunkyTabs
|
5
|
+
class Tab
|
6
|
+
attr_accessor :name
|
7
|
+
attr_accessor :title
|
8
|
+
attr_accessor :default_tab_action
|
9
|
+
attr_accessor :tab_actions
|
10
|
+
|
11
|
+
#default values for attrs
|
12
|
+
def title
|
13
|
+
@title ||= name
|
14
|
+
end
|
15
|
+
def tab_actions
|
16
|
+
@tab_actions ||= []
|
17
|
+
end
|
18
|
+
def default_tab_action
|
19
|
+
@default_tab_action ||= add_tab_action("index")
|
20
|
+
end
|
21
|
+
|
22
|
+
#tab methods
|
23
|
+
def initialize(hash_or_string)
|
24
|
+
# initialize default tab action unless the user has provided one of their own
|
25
|
+
default_tab_action unless hash_or_string.is_a?(Hash) && hash_or_string.has_key?(:default_tab_action)
|
26
|
+
|
27
|
+
return set_attrs({:name=>hash_or_string}) if hash_or_string.is_a?(String)
|
28
|
+
return set_attrs(hash_or_string) if hash_or_string.is_a?(Hash)
|
29
|
+
raise FunkyTabsException.new("invalid tab parameters #{hash_or_string.inspect}")
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_attrs(attrs_hash)
|
33
|
+
raise FunkyTabsException.new("invalid tab attrs format #{attrs_hash.inspect}") unless attrs_hash.is_a?(Hash)
|
34
|
+
# in order to make sure that default action gets called after actions have been instantiated, sort hash keys
|
35
|
+
attrs_hash.keys.sort.each do |key|
|
36
|
+
self.send("#{key.to_s}=",attrs_hash[key])
|
37
|
+
end
|
38
|
+
return self
|
39
|
+
end
|
40
|
+
|
41
|
+
# when setting a default action, create and link to action if doesnt exist
|
42
|
+
def default_tab_action=(sym_or_string)
|
43
|
+
@default_tab_action = add_tab_action(sym_or_string)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Right now this method just appends actions, it doesn't delete them.
|
47
|
+
def tab_actions=(array_or_string_or_hash)
|
48
|
+
return add_tab_action(array_or_string_or_hash) if array_or_string_or_hash.is_a?(String)||array_or_string_or_hash.is_a?(Hash)
|
49
|
+
return add_tab_actions(array_or_string_or_hash) if array_or_string_or_hash.is_a?(Array)
|
50
|
+
raise FunkyTabsException.new("invalid actions format #{array_or_string_or_hash.inspect}")
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_tab_action(tab_action_or_string_or_hash)
|
54
|
+
return nil if tab_action_or_string_or_hash.nil? || tab_actions.blank?
|
55
|
+
tab_action_or_string_or_hash = tab_action_or_string_or_hash.to_s if tab_action_or_string_or_hash.is_a?(Symbol)
|
56
|
+
return tab_actions.index {|action| action.name.downcase == tab_action_or_string_or_hash.downcase} if tab_action_or_string_or_hash.is_a?(String)
|
57
|
+
return tab_actions.index {|action| action.name == tab_action_or_string_or_hash[:name] } if tab_action_or_string_or_hash.is_a?(Hash)
|
58
|
+
return tab_actions.index(tab_action_or_string_or_hash) if tab_action_or_string_or_hash.is_a?(FunkyTabs::TabAction)
|
59
|
+
raise FunkyTabsException.new("Dont know how to find an action with #{tab_action_or_string_or_hash.inspect}")
|
60
|
+
end
|
61
|
+
|
62
|
+
def add_tab_action(string_or_hash)
|
63
|
+
tab_action_index = find_tab_action(string_or_hash)
|
64
|
+
if tab_action_index.nil?
|
65
|
+
new_tab_action = FunkyTabs::TabAction.new(string_or_hash)
|
66
|
+
tab_actions << new_tab_action
|
67
|
+
return new_tab_action
|
68
|
+
else
|
69
|
+
return tab_actions[tab_action_index]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def add_tab_actions(array_of_properties)
|
74
|
+
array_of_properties.each do |action_properties|
|
75
|
+
add_tab_action(action_properties)
|
76
|
+
end
|
77
|
+
return tab_actions
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'funky_tabs'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module FunkyTabs
|
5
|
+
class TabAction
|
6
|
+
attr_accessor :name
|
7
|
+
attr_accessor :content_path
|
8
|
+
|
9
|
+
def initialize(hash_or_string)
|
10
|
+
return set_attrs({:name=>hash_or_string}) if hash_or_string.is_a?(String)
|
11
|
+
return set_attrs(hash_or_string) if hash_or_string.is_a?(Hash)
|
12
|
+
raise FunkyTabsException.new("invalid action parameters #{hash_or_string.inspect}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def set_attrs(attrs_hash)
|
16
|
+
raise FunkyTabsException.new("invalid action attrs format #{attrs_hash.inspect}") unless attrs_hash.is_a?(Hash)
|
17
|
+
attrs_hash.each do |key,val|
|
18
|
+
self.send("#{key}=",val)
|
19
|
+
end
|
20
|
+
return self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/funky_tabs.rb
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'funky_tabs'
|
2
|
+
require 'rails'
|
3
|
+
require 'action_controller'
|
4
|
+
|
5
|
+
module FunkyTabs
|
6
|
+
require 'funky_tabs/helpers'
|
7
|
+
require 'funky_tabs/renderers'
|
8
|
+
require 'funky_tabs/rails'
|
9
|
+
require 'funky_tabs/tab'
|
10
|
+
require 'funky_tabs/tab_action'
|
11
|
+
|
12
|
+
class FunkyTabsException < Exception
|
13
|
+
end
|
14
|
+
|
15
|
+
ActionController::Base.send :include, FunkyTabs::Helpers
|
16
|
+
ActionController::Base.send :include, FunkyTabs::Renderers
|
17
|
+
|
18
|
+
mattr_accessor :tabs
|
19
|
+
def self.tabs
|
20
|
+
@@tabs ||= []
|
21
|
+
end
|
22
|
+
|
23
|
+
# make sure to include / at end of tab controller
|
24
|
+
mattr_accessor :default_tab_index
|
25
|
+
def self.default_tab_index
|
26
|
+
@@default_tab_index ||= 0
|
27
|
+
end
|
28
|
+
mattr_accessor :default_tabs_controller
|
29
|
+
def self.default_tabs_controller
|
30
|
+
@@default_tabs_controller ||= "funky_tabs/update"
|
31
|
+
end
|
32
|
+
mattr_accessor :missing_tab_action_path
|
33
|
+
def self.missing_tab_action_path
|
34
|
+
@@missing_tab_action_path ||= "/funky_tabs/update/missing_tab"
|
35
|
+
end
|
36
|
+
mattr_accessor :tab_load_callback
|
37
|
+
mattr_accessor :tab_select_callback
|
38
|
+
mattr_accessor :loading_html
|
39
|
+
def self.loading_html
|
40
|
+
@@loading_html ||= "Loading..."
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.setup
|
44
|
+
yield self
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.add_tab(string_or_hash)
|
48
|
+
tab_index = find_tab(string_or_hash)
|
49
|
+
if tab_index.nil?
|
50
|
+
# if the tab doesnt exist, create it
|
51
|
+
new_tab = FunkyTabs::Tab.new(string_or_hash)
|
52
|
+
tabs << new_tab
|
53
|
+
return new_tab
|
54
|
+
else
|
55
|
+
# if the tab exists, return it
|
56
|
+
return tabs[tab_index]
|
57
|
+
end
|
58
|
+
raise FunkyTabsException.new("Dont know how to add this tab with #{string_or_hash.inspect}")
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.add_tabs(array_of_properties)
|
62
|
+
raise FunkyTabsException.new("dont know how to create tabs from #{array_of_properties.inspect}") unless array_of_properties.is_a?(Array)
|
63
|
+
array_of_properties.each do |tab_properties|
|
64
|
+
add_tab(tab_properties)
|
65
|
+
end
|
66
|
+
return tabs
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.tab(sym_or_string)
|
70
|
+
tab_index = find_tab(sym_or_string.to_s)
|
71
|
+
return tabs[tab_index] unless tab_index.nil?
|
72
|
+
return nil
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.find_tab(tab_or_string_or_hash)
|
76
|
+
return nil if tab_or_string_or_hash.blank? || tabs.blank?
|
77
|
+
tab_or_string_or_hash = tab_or_string_or_hash.to_s if tab_or_string_or_hash.is_a?(Symbol)
|
78
|
+
return tabs.index {|tab| tab.name.downcase == tab_or_string_or_hash.downcase} if tab_or_string_or_hash.is_a?(String)
|
79
|
+
return tabs.index {|action| action.name == tab_or_string_or_hash[:name] } if tab_or_string_or_hash.is_a?(Hash)
|
80
|
+
return tabs.index(tab_or_string_or_hash) if tab_or_string_or_hash.is_a?(FunkyTabs::Tab)
|
81
|
+
raise FunkyTabsException.new("dont know how to find tab with #{tab_or_string_or_hash.inspect}")
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.location_hash_for_tab_action(tab,tab_action=nil,tab_action_id=nil)
|
85
|
+
return nil if tab.nil?
|
86
|
+
|
87
|
+
tab_index = find_tab(tab)
|
88
|
+
return nil if tab_index.nil?
|
89
|
+
|
90
|
+
tab_action_index = tabs[tab_index].find_tab_action(tab_action)
|
91
|
+
|
92
|
+
return tab_index.to_s if tab_action_index.nil?
|
93
|
+
return "#{tab_index}-#{tab_action_index}" if tab_action_id.nil?
|
94
|
+
return "#{tab_index}-#{tab_action_index}-#{tab_action_id}"
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.location_hash_for_indices(tab_index,tab_action_index=nil)
|
98
|
+
tab_index = tab_index.to_i
|
99
|
+
tab_action_index = tab_action_index.to_i unless tab_action_index.nil?
|
100
|
+
tab = tab_index.nil? ? nil : tabs[tab_index]
|
101
|
+
tab_action = nil
|
102
|
+
tab_action = tab.tab_actions[tab_action_index] unless tab.nil? || tab_action_index.nil?
|
103
|
+
return location_hash_for_tab_action(tab,tab_action)
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.content_path_for_tab_and_tab_action(tab,tab_action=nil,tab_action_id=nil)
|
107
|
+
tab_index = find_tab(tab)
|
108
|
+
return missing_tab_action_path if tab_index.nil?
|
109
|
+
tab = tabs[tab_index]
|
110
|
+
tab_action_index = tab.find_tab_action(tab_action||tab.default_tab_action)
|
111
|
+
return missing_tab_action_path if tab_action_index.nil?
|
112
|
+
tab_action = tab.tab_actions[tab_action_index]
|
113
|
+
unless tab_action.content_path.blank?
|
114
|
+
return tab_action.content_path if tab_action_id.nil?
|
115
|
+
return tab_action.content_path+"&id=#{tab_action_id}" if tab_action.content_path.include?("?")
|
116
|
+
return tab_action.content_path+"?id=#{tab_action_id}"
|
117
|
+
end
|
118
|
+
if tab_action.name.downcase == "index"
|
119
|
+
content_path = "/#{default_tabs_controller}/#{tab.name.gsub(/\s/,"_").downcase}_index"
|
120
|
+
else
|
121
|
+
content_path = "/#{default_tabs_controller}/#{tab_action.name.gsub(/\s/,"_").downcase}_#{tab.name.gsub(/\s/,"_").downcase}"
|
122
|
+
end
|
123
|
+
return content_path if tab_action_id.nil?
|
124
|
+
return "#{content_path}/#{tab_action_id}"
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.tab_and_tab_action_from_location_hash(location_hash)
|
128
|
+
return nil,nil,nil if tabs.blank?
|
129
|
+
return tabs[default_tab_index],nil,nil if location_hash.blank?
|
130
|
+
|
131
|
+
indices = location_hash.to_s.split("-")
|
132
|
+
return nil,nil,nil if indices.first.nil?
|
133
|
+
tab_index = indices.first.to_i
|
134
|
+
tab = tabs[tab_index]
|
135
|
+
return nil,nil,nil if tab.nil?
|
136
|
+
tab_action_index = indices[1].to_i
|
137
|
+
return tab,nil,nil if tab_action_index.nil?
|
138
|
+
return tab,nil,nil if tab_action_index.to_i >= tab.tab_actions.length
|
139
|
+
tab_action = tab.tab_actions[tab_action_index]
|
140
|
+
|
141
|
+
tab_action_id = indices[2]
|
142
|
+
return tab,tab_action,tab_action_id
|
143
|
+
end
|
144
|
+
|
145
|
+
def self.tab_index_from_location_hash(location_hash)
|
146
|
+
return default_tab_index if tabs.blank? || location_hash.blank?
|
147
|
+
indices = location_hash.to_s.split("-");
|
148
|
+
return default_tab_index if indices.first.nil?
|
149
|
+
tab = tabs[indices.first.to_i]
|
150
|
+
return default_tab_index if tab.nil?
|
151
|
+
return indices.first
|
152
|
+
end
|
153
|
+
|
154
|
+
# TODO: Figure out way to reset all mattr_accessors
|
155
|
+
def self.reset
|
156
|
+
tabs.clear
|
157
|
+
tab_root = ""
|
158
|
+
end
|
159
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FunkyTabsTest < ActiveSupport::TestCase
|
4
|
+
def setup
|
5
|
+
FunkyTabs.reset
|
6
|
+
FunkyTabs.add_tab("first tab")
|
7
|
+
FunkyTabs.add_tab(:name=>"second tab",:default_tab_action=>"edit")
|
8
|
+
FunkyTabs.add_tab(:name=>"third tab",:tab_actions=>"edit")
|
9
|
+
FunkyTabs.add_tab(:name=>"fourth tab",:tab_actions=>["edit",{:name=>"update",:content_path=>"test_path",:use_regular_content_paths=>false}])
|
10
|
+
end
|
11
|
+
|
12
|
+
test "content_path_for_location_hash returns default action path given no action" do
|
13
|
+
assert_equal "funky_tabs/first_tab/index",FunkyTabs.content_path_for_location_hash("0")
|
14
|
+
end
|
15
|
+
|
16
|
+
test "content_path_for_location_hash returns action path given action" do
|
17
|
+
assert_equal "funky_tabs/third_tab/edit",FunkyTabs.content_path_for_location_hash("2-1")
|
18
|
+
end
|
19
|
+
|
20
|
+
test "content_path_for_location_hash returns nil given invalid object" do
|
21
|
+
assert_nil FunkyTabs.content_path_for_location_hash("bogus hash")
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: funky_tabs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Pouncy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-20 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Create Ajaxified tabs with simple history through window location hashes for Rails.
|
18
|
+
email: thomas.pouncy@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- CHANGELOG
|
25
|
+
- README.rdoc
|
26
|
+
- lib/funky_tabs.rb
|
27
|
+
- lib/funky_tabs/helpers.rb
|
28
|
+
- lib/funky_tabs/rails.rb
|
29
|
+
- lib/funky_tabs/renderers.rb
|
30
|
+
- lib/funky_tabs/tab.rb
|
31
|
+
- lib/funky_tabs/tab_action.rb
|
32
|
+
files:
|
33
|
+
- CHANGELOG
|
34
|
+
- Manifest
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- app/controllers/funky_tabs/update_controller.rb
|
38
|
+
- app/views/funky_tabs/update/_ajax.js.erb
|
39
|
+
- app/views/funky_tabs/update/_render_funky_tabs.html.erb
|
40
|
+
- app/views/funky_tabs/update/missing_tab.html.erb
|
41
|
+
- app/views/funky_tabs/update/update_tab.html.erb
|
42
|
+
- config/routes.rb
|
43
|
+
- funky_tabs.gemspec
|
44
|
+
- init.rb
|
45
|
+
- lib/funky_tabs.rb
|
46
|
+
- lib/funky_tabs/helpers.rb
|
47
|
+
- lib/funky_tabs/rails.rb
|
48
|
+
- lib/funky_tabs/renderers.rb
|
49
|
+
- lib/funky_tabs/tab.rb
|
50
|
+
- lib/funky_tabs/tab_action.rb
|
51
|
+
- test/funky_tabs_test.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/thomaspouncy/funky_tabs
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --line-numbers
|
59
|
+
- --inline-source
|
60
|
+
- --title
|
61
|
+
- Funky_tabs
|
62
|
+
- --main
|
63
|
+
- README.rdoc
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "1.2"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: funky_tabs
|
81
|
+
rubygems_version: 1.6.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Create Ajaxified tabs with simple history through window location hashes for Rails.
|
85
|
+
test_files:
|
86
|
+
- test/funky_tabs_test.rb
|