funky_tabs 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('funky_tabs', '0.1.1') do |p|
5
+ Echoe.new('funky_tabs', '0.1.2') do |p|
6
6
  p.description = "Create Ajaxified tabs with simple history through window location hashes for Rails."
7
7
  p.url = "http://github.com/thomaspouncy/funky_tabs"
8
8
  p.author = "Thomas Pouncy"
@@ -1,13 +1,6 @@
1
1
  class FunkyTabs::UpdateController < ApplicationController
2
2
  layout false
3
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
4
  def missing_funky_tab
12
5
  render FunkyTabs.missing_tab_action_path,:layout=>false and return
13
6
  end
@@ -11,6 +11,8 @@
11
11
  </div>
12
12
  <div style="clear:both;"></div>
13
13
  <script type="text/javascript">
14
+ <%= raw(@path_from_hash_arr) %>
15
+
14
16
  function selectFunkyTab(tab_index){
15
17
  jQuery('div#funky_tabs li.funky_tabs_list').removeClass('selected_tab');
16
18
  jQuery('ul.css-tabs li#funky_tabs_list-'+tab_index).addClass('selected_tab');
@@ -24,17 +26,35 @@ function selectFunkyTab(tab_index){
24
26
  <%= FunkyTabs.tab_select_callback.gsub(/:tab_number/,'tab_index') unless FunkyTabs.tab_select_callback.nil? %>
25
27
  }
26
28
  function funkyTabsAjaxCall(location_hash){
27
- call_hash_change = false;
28
29
  jQuery('div#funky_tabs div.funky_tab').html("<%= raw(FunkyTabs.loading_html) %>");
30
+
31
+ if((typeof(location_hash)=="string") && (location_hash.indexOf("#") != -1)){
32
+ location_hash = location_hash.replace("#","");
33
+ }
34
+
35
+ var tab_action_id = "";
36
+ location_hash = ''+location_hash;
37
+
38
+ var hash_components = location_hash.split("-");
39
+ var hash_lookup = hash_components[0];
40
+ if(hash_components.length > 1){
41
+ hash_lookup = hash_lookup + "-" + hash_components[1];
42
+ }
43
+ if(hash_components.length > 2){
44
+ tab_action_id = hash_components[2];
45
+ }
46
+
47
+
29
48
  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);
49
+
50
+ var path = path_from_hash_arr[hash_lookup];
51
+ if(tab_action_id != ""){
52
+ path = path.replace("?","?id="+tab_action_id+"&");
53
+ }
54
+ jQuery("div#funky_tabs div.selected_tab_div").load(path+'&location_hash='+location_hash);
31
55
  }
32
56
  function hashChangeEvent(){
33
- if(call_hash_change){
34
- funkyTabsAjaxCall(location.hash.replace("#",""));
35
- } else {
36
- call_hash_change = true;
37
- }
57
+ funkyTabsAjaxCall(location.hash);
38
58
  }
39
59
  if(typeof(jQuery(window).hashchange) == "undefined"){
40
60
  // dont have special plugin, so use onhashchange event. NOTE: this will not work in ie 6 or 7
@@ -44,7 +64,6 @@ if(typeof(jQuery(window).hashchange) == "undefined"){
44
64
  jQuery(window).hashchange(hashChangeEvent);
45
65
  }
46
66
  var load_on_select = true;
47
- var call_hash_change = true;
48
67
  jQuery(function() {
49
68
  var initial_tab = <%= FunkyTabs.default_tab_index %>;
50
69
  if(window.location.hash != ""){
data/funky_tabs.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{funky_tabs}
5
- s.version = "0.1.1"
5
+ s.version = "0.1.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Thomas Pouncy"]
9
- s.date = %q{2011-05-20}
9
+ s.date = %q{2011-05-21}
10
10
  s.description = %q{Create Ajaxified tabs with simple history through window location hashes for Rails.}
11
11
  s.email = %q{thomas.pouncy@gmail.com}
12
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"]
@@ -6,7 +6,7 @@ module FunkyTabs
6
6
  def self.included(base)
7
7
  base.class_eval do
8
8
  def render_funky_tabs
9
- #@tab_data = FunkyTabs
9
+ @path_from_hash_arr = FunkyTabs.javascript_key_array
10
10
  return render_to_string :partial=>"funky_tabs/update/render_funky_tabs"
11
11
  end
12
12
 
@@ -14,18 +14,11 @@ module FunkyTabs
14
14
  return FunkyTabs.location_hash_for_tab_action(tab,tab_action,tab_action_id)
15
15
  end
16
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
17
  def js_to_funky_tab(tab,tab_action=nil,tab_action_id=nil)
25
18
  @location_hash_for_ajax = FunkyTabs.location_hash_for_tab_action(tab,tab_action,tab_action_id)
26
19
  return render_to_string :partial=>"funky_tabs/update/ajax.js"
27
20
  end
28
- helper_method :render_funky_tabs, :redirect_to_funky_tab, :js_to_funky_tab
21
+ helper_method :render_funky_tabs, :js_to_funky_tab, :location_hash_for_funky_tab
29
22
  end
30
23
  end
31
24
  end
@@ -10,7 +10,13 @@ module FunkyTabs
10
10
  super(*args, &block) and return if params[:funky_tabs].nil?
11
11
  @tab_index = FunkyTabs.tab_index_from_location_hash(params[:location_hash])
12
12
  super(*args, &block) and return if @tab_index.nil?
13
- @location_hash = params[:location_hash]
13
+ logger.debug "full path is #{request.fullpath}"
14
+ if FunkyTabs.correct_path_for_location_hash?(request.fullpath,params[:location_hash])
15
+ @location_hash = params[:location_hash]
16
+ else
17
+ @location_hash = FunkyTabs.location_hash_for_content_path(request.fullpath)
18
+ end
19
+ super(*args, &block) and return if @location_hash.nil?
14
20
  @render_content = render_to_string(*args, &block)
15
21
  super(:template=>"funky_tabs/update/update_tab",:layout=>false)
16
22
  end
data/lib/funky_tabs.rb CHANGED
@@ -20,7 +20,22 @@ module FunkyTabs
20
20
  @@tabs ||= []
21
21
  end
22
22
 
23
- # make sure to include / at end of tab controller
23
+ mattr_accessor :location_hashes_for_content_paths
24
+ def self.location_hashes_for_content_paths
25
+ return @@location_hashes_for_content_paths unless @@location_hashes_for_content_paths.blank?
26
+
27
+ path_hash = {}
28
+ tabs.each do |tab|
29
+ tab.tab_actions.each do |tab_action|
30
+ location_hash = location_hash_for_tab_action(tab,tab_action)
31
+ path = content_path_for_tab_and_tab_action(tab,tab_action)
32
+ path_hash[path] = location_hash
33
+ end
34
+ end
35
+
36
+ @@location_hashes_for_content_paths = path_hash
37
+ end
38
+
24
39
  mattr_accessor :default_tab_index
25
40
  def self.default_tab_index
26
41
  @@default_tab_index ||= 0
@@ -88,8 +103,9 @@ module FunkyTabs
88
103
  return nil if tab_index.nil?
89
104
 
90
105
  tab_action_index = tabs[tab_index].find_tab_action(tab_action)
106
+ default_index = tabs[tab_index].find_tab_action(tabs[tab_index].default_tab_action)
91
107
 
92
- return tab_index.to_s if tab_action_index.nil?
108
+ return tab_index.to_s if tab_action_index.nil? || tab_action_index == default_index
93
109
  return "#{tab_index}-#{tab_action_index}" if tab_action_id.nil?
94
110
  return "#{tab_index}-#{tab_action_index}-#{tab_action_id}"
95
111
  end
@@ -104,12 +120,16 @@ module FunkyTabs
104
120
  end
105
121
 
106
122
  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]
123
+ unless tab.is_a?(FunkyTabs::Tab)
124
+ tab_index = find_tab(tab)
125
+ return missing_tab_action_path if tab_index.nil?
126
+ tab = tabs[tab_index]
127
+ end
128
+ unless tab_action.is_a?(FunkyTabs::TabAction)
129
+ tab_action_index = tab.find_tab_action(tab_action||tab.default_tab_action)
130
+ return missing_tab_action_path if tab_action_index.nil?
131
+ tab_action = tab.tab_actions[tab_action_index]
132
+ end
113
133
  unless tab_action.content_path.blank?
114
134
  return tab_action.content_path if tab_action_id.nil?
115
135
  return tab_action.content_path+"&id=#{tab_action_id}" if tab_action.content_path.include?("?")
@@ -151,6 +171,32 @@ module FunkyTabs
151
171
  return indices.first
152
172
  end
153
173
 
174
+ def self.correct_path_for_location_hash?(path,location_hash)
175
+ return false if path.blank? || location_hash.blank?
176
+ path = path.split("?").first
177
+
178
+ tab,tab_action,tab_action_id = tab_and_tab_action_from_location_hash(location_hash)
179
+ return path == content_path_for_tab_and_tab_action(tab,tab_action)
180
+ end
181
+
182
+ def self.location_hash_for_content_path(path)
183
+ path = path.split("?").first
184
+ return location_hashes_for_content_paths[path] rescue nil
185
+ end
186
+
187
+ def self.javascript_key_array
188
+ js_array = "var path_from_hash_arr = {"
189
+ location_hashes_for_content_paths.each do |path,location_hash|
190
+ if path.include?("?")
191
+ path = path + "&funky_tabs=true"
192
+ else
193
+ path = path + "?funky_tabs=true"
194
+ end
195
+ js_array = js_array + "'#{location_hash}':'#{path}',"
196
+ end
197
+ js_array = js_array.chomp(",")+"};"
198
+ end
199
+
154
200
  # TODO: Figure out way to reset all mattr_accessors
155
201
  def self.reset
156
202
  tabs.clear
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: funky_tabs
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Thomas Pouncy
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-20 00:00:00 -07:00
13
+ date: 2011-05-21 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16