funky_tabs 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,12 +1,13 @@
1
1
  == Funky Tabs
2
2
 
3
- Funky Tabs is a ruby gem designed to work with Rails 3 and jQuery UI. It allows you to define a tabbed navigation system as a ruby object, and renders out all the necessary javascript to produce these tabs. At the moment it provides the following functionality:
3
+ Funky Tabs is a ruby gem designed to work with Rails 3 and jQuery UI. It allows you to define a tabbed, ajax-ified navigation system as a ruby object, and renders out all the necessary javascript to produce these tabs. At the moment it provides the following functionality:
4
4
  * Ajax tabs mean that each tab is pulled via an Ajax request when it is selected to avoid reloading entire pages.
5
5
  * A simple tab and tab action naming scheme, so that all you have to do is provide the necessary views.
6
6
  * A unique identifier for each page and action, so that the user can easily bookmark whatever page they are on and return to it later.
7
7
  * Ajaxified history through either the HashChange jQuery plugin(http://benalman.com/projects/jquery-hashchange-plugin/) if it is available in your project, or through the window.onhashchange event. This means that although everything is done through Ajax, the forward and back mechanisms should still work correctly.
8
8
  * Helper methods to produce the necessary javascript to link to whichever tabs and actions you would like.
9
- * Automatic wrapping of tab and tab action views in Ajax response code.t
9
+ * Automatic wrapping of tab and tab action views in Ajax response code.
10
+ * Ajax failure handling.
10
11
 
11
12
  == Installation
12
13
 
@@ -21,43 +22,46 @@ Then, create a "funky_tabs.rb" file in "config/initializers". This file is where
21
22
  FunkyTabs.setup do |ft|
22
23
  # Optional parameters
23
24
  ft.loading_html = "<p style='text-align: center;'><img src='/images/loading.gif' /></p>"
24
- ft.tab_load_callback = "updateHeight();"
25
- ft.tab_select_callback = "expandTutorialStep(:tab_number);"
25
+ ft.ajax_fail_message = "Something went wrong"
26
+ ft.tab_load_callback = "afterLoad(:tab_number);"
27
+ ft.tab_select_callback = "afterSelect(:tab_number);"
26
28
 
27
29
  # REQUIRED
28
- ft.default_tabs_controller = 'admin/pro'
30
+ ft.default_tabs_controller = 'some/controller'
29
31
 
30
32
  # Define your tabs and tab actions here:
31
33
  ft.add_tab("Welcome")
32
- ft.add_tab(:name=>"profile",:title=>"Your Profile",:default_tab_action=>{:name=>"profile",:content_path=>"/admin/admin_users/profile"})
33
- ft.add_tab(:name=>"products",:title=>"Your Products/Sites",:default_tab_action=>"list",:tab_actions=>["list","add","edit"])
34
- ft.add_tab(:name=>"giveaways",:title=>"Your Giveaways",:default_tab_action=>"list",:tab_actions=>["list","add","edit"])
35
- ft.add_tab(:name=>"widgets",:title=>"Your Widgets")
36
- ft.add_tab(:name=>"dashboard",:title=>"Your Dashboard")
34
+ ft.add_tab(:name=>"lions",:title=>"Simba",:default_tab_action=>{:name=>"show",:content_path=>"/path/to/the_lion_king"})
35
+ ft.add_tab(:name=>"tigers",:title=>"Tony",:default_tab_action=>"free",:tab_actions=>["free","round_up"])
36
+ ft.add_tab(:name=>"bears",:title=>"Baloo",:default_tab_action=>"round_up",:tab_actions=>["free","round_up"])
37
+ ft.add_tab(:name=>"exclamations",:title=>"Oh My")
37
38
  end
38
39
 
39
40
  == Configuration Options
40
41
 
41
42
  Here's a brief description of the various configuration options:
42
43
  * loading_html - This is the placeholder html that Funky Tabs will put in a tab while its loading. If you don't assign this value, Funky Tabs will just use the text "Loading..."
44
+ * ajax_fail_message - This is the placeholder message that Funky Tabs will display in a tab if the ajax call for a tab action fails. If you don't assign this value, Funky Tabs will just use the text "It appears something has gone wrong. We apologize for the inconvenience."
43
45
  * tab_load_callback - Here you can put any javascript you want Funky Tabs to call after a tab loads. Any instance of ":tab_number" will be replaced with the current tab number.
44
46
  * tab_select_callback - Here you can put any javascript you want Funky Tabs to call after a tab is selected, but before the Ajax call is returned. Similarly to the load callback, instances of ":tab_number" will be replaces with the current tab number.
47
+
45
48
  * default_tabs_controller(REQUIRED) - Funky Tabs works on the assumption that all of the views for your tabs are stored in the same controller, although you can override this behavior for individual tab actions. This setting tells Funky Tabs where to look for tab views.
46
49
 
47
- Finally, we have the add_tab method. The FunkyTabs object represents an entire navigation system, and contains one or more tabs. Each tab has one or more tab actions associated with it, so, for example, a welcome tab may only have an "index" action, while a management tab may have "list","add", and "edit" actions.
50
+ Finally, we have the add_tab method. The FunkyTabs object represents an entire navigation system, and contains one or more tabs. Each tab has one or more tab actions associated with it, so, for example, a welcome tab may only have an "index" action, while a tigers tab may have "free" and "round_up" actions.
48
51
 
49
- NOTE: This method was originally intended only to be called during initialization. If you need to call it later, to dynamically add a tab, you will need to call render_funky_tabs again, so that Rails will add the new tabs to the necessary javascript. I haven't tested adding tabs after initialization, so I can't guarantee it will work.
52
+ If you only pass a string to add_tab, it will create a tab with just a default tab action of "index".(For example, the above code results in a "welcome" tab with a single tab action "index" that looks for "/some/controller/welcome_index").
50
53
 
51
- If you only pass a string to add_tab, it will create a tab with just a default tab action of "index". Other parameters you may pass to add_tab are:
52
- * name - this is the name of the view Rails will look to render. The name is the only required parameter for defining a tab. The name is combined with the name of the action to find the view, as follows:
53
- If the tab action is "index" then FunkyTabs will look for the view "/default_tabs_controller/name_index", so in the above example, the index action for the Welcome tab comes from "/admin/pro/welcome_index"
54
- For any other tab action, Funky Tabs will look for the view "/default_tabs_controller/action_name" Therefore, in the above example, the list action for the products tab coems from "/admin/pro/list_products"
54
+ Other parameters you may pass to add_tab are:
55
+ * name - this is the name of the view Rails will look to render. The name is the only required parameter for defining a tab. The name is combined with the name of each tab action to find the view for that tab action, as follows:
56
+ - If the tab action is "index" then FunkyTabs will look for the view "/default_tabs_controller/name_index", so in the above example, the "index" action for the "welcome" tab comes from "/some/controller/welcome_index"
57
+ - For any other tab action, Funky Tabs will look for the view "/default_tabs_controller/action_name" Therefore, in the above example, the "free" action for the "tigers" tab comes from "/some/controller/free_tigers"
55
58
  * title - Title is the phrase that the javascript will display for the tab in the navigation system. This defaults to be the same as the name.
56
59
  * default_tab_action - This parameter takes either a hash or string defining the default tab action for Funky Tabs to take when the user first selects a tab. This value defaults to an "index" action.
57
60
  * tab_actions - This is where you tell Funky Tabs all the possible actions that can happen within a tab. Each tab action is defined by either a string or a hash of parameters, so tab_actions can either take a string or hash if there is only one tab action available, or an array of strings or hashes if there are many tab actions available. Each tab action can take the following parameters:
58
- name - Funky Tabs uses the tab action name to look for the view to render when this action is called. This is required for each tab action.
59
- content_path - Funky Tabs assumes by default that the view for every tab action is related to the name of the tab and the tab action, but if you want to override this functionality, or link to a method in another controller, you can do this on a tab action by tab action basis. Simply pass a fullpath to the desired controller/action and Funky Tabs will look there instead. NOTE: if you define the content_path parameter, you must define it with a fullpath. Funky Tabs doesn't know if you're just defining an action or a controller and an action, so even if the file for this tab action is in the same controller as all the other tab actions, you still need to explicitly include that controller
60
- For example: Your default_tabs_controller is "tabs" and you've added a new "save" action for your "products" tab, but you want to render the file "tabs/awesome_file.html" instead of "tabs/save_products.html", so you call add_tab(:name=>'products',:tab_actions=>[{:name=>'save',:content_path=>'/tabs/awesome_file.html'}]) Even though "tabs" is already defined as your default_tabs_controller, Funky Tabs will not know what to do if you just define the content path as "awesome_file.html".
61
+ - name - Funky Tabs uses the tab action name to look for the view to render when this action is called. This is required for each tab action.
62
+ - content_path - Funky Tabs assumes by default that the view for every tab action is related to the name of the tab and the tab action, but if you want to override this functionality, or link to a method in another controller, you can do this on a tab action by tab action basis. Simply pass a fullpath to the desired controller/action and Funky Tabs will look there instead. (NOTE: if you define the content_path parameter, you must define it with a fullpath. Funky Tabs doesn't know if you're just defining an action or a controller and an action, so even if the file for this tab action is in the same controller as all the other tab actions, you still need to explicitly include that controller. For example, your default_tabs_controller is "tabs" and you've added a new "save" action for your "products" tab, but you want to render the file "tabs/awesome_file.html" instead of "tabs/save_products.html", so you call add_tab(:name=>'products',:tab_actions=>[{:name=>'save',:content_path=>'/tabs/awesome_file.html'}]) Even though "tabs" is already defined as your default_tabs_controller, Funky Tabs will not know what to do if you just define the content path as "awesome_file.html".)
63
+
64
+ NOTE: The add_tab method was originally intended only to be called during initialization. If you need to call it after initialization to dynamically add a tab, you will need to call render_funky_tabs again so that Rails will add the new tabs to the necessary javascript. I haven't tested adding tabs after initialization, so I can't guarantee it will work.
61
65
 
62
66
  == Funky Tabs Naming Scheme
63
67
 
@@ -72,10 +76,9 @@ Funky Tabs adds a few helper methods to all your controllers and views automatic
72
76
  <%= render_funky_tabs %>
73
77
 
74
78
  * js_to_funky_tab(tab,tab_action,tab_action_id) - This method writes out the javascript necessary to change the current tab and tab action to a new tab or tab action. The id parameter allows you to pass an object id to a tab action method, and you may pass javascript as the id.
75
-
76
- For example, the following code uses javascript to get the product id from the "current_product" variable. Note the use of quotation marks.
79
+ - For example, the following code uses javascript to get the product id from the "current_product" variable. Note the use of quotation marks.
77
80
  <div onclick="<%= js_to_funky_tab(:products,:edit,"'+current_product+'") %>">Edit this Product</div>
78
- The tab_action and tab_action_id parameters are optional. If you call this method without a tab_action, it will call the default tab action.
81
+ - The tab_action and tab_action_id parameters are optional. If you call this method without a tab_action, it will call the default tab action.
79
82
 
80
83
  * location_hash_for_funky_tab(tab,tab_action,tab_action_id) - if, for whatever reason, you just need to know what the unique location hash would be for a particular tab, tab action and object id, this method will tell you.
81
84
 
@@ -83,4 +86,11 @@ Funky Tabs adds a few helper methods to all your controllers and views automatic
83
86
 
84
87
  Since every request with a Funky Tabs navigation system is an Ajax request, you might expect that you would have to format your tab views somehow to get everything to work. However, Funky Tabs does this for you by automatically wrapping any GET request sent with parameters "funky_tabs=true&location_hash=whatever" in the necessary code to update and or switch to the appropriate tab.
85
88
 
86
- For example, if you have a Welcome tab with an "index" action, all you have to do is put whatever HTML you want in the welcome_index.html view and Funky Tabs will handle the javascript necessary to load the HTML into the appropriate tab when the user selects the "Welcome" tab.
89
+ For example, if you have a Welcome tab with an "index" action, all you have to do is put whatever HTML you want in the welcome_index.html view and Funky Tabs will handle the javascript necessary to load the HTML into the appropriate tab when the user selects the "Welcome" tab. Funky Tabs will also select the appropriate tab if you need to link from a tab_action in one tab to a tab_action in a separate tab.
90
+
91
+ == Location Hashes
92
+
93
+ The goal of Funky Tabs is to provide a complete tabbed, Ajax navigation system that provides a unique url for each action a user takes. This allows users to bookmark pages on your site, and use the forward and back buttons as they would with a non-ajax site, but gives you the speed of a totally ajax-ified system. To do this, Funky Tabs generates a unique location hash for each tab and tab action, and uses javascript to update the location hash of the page as it goes along.
94
+
95
+ Ben Alman has developed a great jQuery plugin to extend forward and back button functionality to window location hash changes (http://benalman.com/projects/jquery-hashchange-plugin/), which Funky Tabs uses if the plugin has been included in your project. If you do not have this plugin, Funky Tabs defaults to using the window.onhashchange event, which will work in most modern browsers, but fails in IE 6 and 7. The majority of my testing has been with this plugin included, so I recommend it highly.
96
+
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.3') do |p|
5
+ Echoe.new('funky_tabs', '0.1.4') 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"
@@ -60,7 +60,12 @@ function funkyTabsAjaxCall(location_hash){
60
60
  path = path.replace("?","?id="+tab_action_id+"&");
61
61
  }
62
62
 
63
- jQuery("div#funky_tabs div.selected_tab_div").load(path+'&location_hash='+location_hash);
63
+ jQuery("div#funky_tabs div.selected_tab_div").load(path+'&location_hash='+location_hash,function(response, status, xhr) {
64
+ if (status == "error") {
65
+ var msg = "<%= FunkyTabs.ajax_fail_message %>";
66
+ $("div#funky_tabs div.selected_tab_div").html(msg);
67
+ }
68
+ });
64
69
 
65
70
  }
66
71
  function hashChangeEvent(){
@@ -77,11 +82,10 @@ if(typeof(jQuery(window).hashchange) == "undefined"){
77
82
  // if we can find the hashchange jquery plugin (http://benalman.com/projects/jquery-hashchange-plugin/), then use it
78
83
  jQuery(window).hashchange(hashChangeEvent);
79
84
  }
80
-
81
85
  jQuery(function() {
82
- var initial_tab = <%= FunkyTabs.default_tab_index %>;
86
+ var initial_hash = "";
83
87
  if(window.location.hash != ""){
84
- initial_tab = parseInt(window.location.hash.replace("#","").split("-")[0]);
88
+ initial_hash = window.location.hash.replace("#","");
85
89
  }
86
90
  var tab = jQuery("#funky_tabs").tabs({
87
91
  show: function(event,ui){
@@ -89,12 +93,21 @@ jQuery(function() {
89
93
  select: function(event,ui){
90
94
  selectFunkyTab(ui.index);
91
95
  },
92
- effect: 'ajax'
96
+ effect: 'ajax',
97
+ selected: <%= FunkyTabs.default_tab_index %>
93
98
  });
94
- if(initial_tab == <%= FunkyTabs.default_tab_index %>){
95
- selectFunkyTab(initial_tab);
99
+ if(initial_hash == ""){
100
+ selectFunkyTab(<%= FunkyTabs.default_tab_index %>);
96
101
  } else {
97
- jQuery("div#funky_tabs").tabs("select",initial_tab);
102
+ initial_tab = parseInt(initial_hash.split("-")[0]);
103
+ if(initial_tab == <%= FunkyTabs.default_tab_index %>){
104
+ selectFunkyTab(<%= FunkyTabs.default_tab_index %>);
105
+ } else {
106
+ load_on_select = false;
107
+ var initial_tab = parseInt(initial_hash.split("-")[0]);
108
+ jQuery("div#funky_tabs").tabs('select',initial_tab);
109
+ funkyTabsAjaxCall(initial_hash);
110
+ }
98
111
  }
99
112
  });
100
113
  </script>
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.3"
5
+ s.version = "0.1.4"
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-21}
9
+ s.date = %q{2011-05-24}
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"]
@@ -8,7 +8,6 @@ module FunkyTabs
8
8
  module InstanceMethods
9
9
  def render(*args, &block)
10
10
  super(*args, &block) and return if params[:funky_tabs].nil?
11
- logger.debug "full path is #{request.fullpath}"
12
11
  if FunkyTabs.correct_path_for_location_hash?(request.fullpath,params[:location_hash])
13
12
  @location_hash = params[:location_hash]
14
13
  else
data/lib/funky_tabs.rb CHANGED
@@ -54,6 +54,10 @@ module FunkyTabs
54
54
  def self.loading_html
55
55
  @@loading_html ||= "Loading..."
56
56
  end
57
+ mattr_accessor :ajax_fail_message
58
+ def self.ajax_fail_message
59
+ @@ajax_fail_message ||= "It appears something has gone wrong. We apologize for the inconvenience."
60
+ end
57
61
 
58
62
  def self.setup
59
63
  yield self
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.3
5
+ version: 0.1.4
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-21 00:00:00 -07:00
13
+ date: 2011-05-24 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16