Initializing Tabs
Tabs are usually used in concert with an element that activates the tab. Tabs are initialized on the activating item instead of the tab.
If there are no menus that activate tab elements on the page, tabs can be initialized globally by using $.tab()
and activated programmatically using $.tab('change tab', path);
Tabs are connected to their activators with a metadata attribute data-tab
. This should be added to both the activating element and the tab itself.
Managing State
...with Hash Tags
Hash tags use in page links and onhashstatechange
to create history events for each tab. This is sometimes easier to use than the more advanced push state, because it does not require you to route those URLs on your server. All in page links will route to the same url.
...with HTML5 State
Tabs can can use html5 push state to manage page state without using #/foo
links. When a user refreshes a page using push state the server will be queried for the new url. This means you must set up appropriate routes in your backend to match each link.
Tabs use Asual's Address library to provide cross-browser push state support. This makes sure in browser that don't support push state, hashchange
is used with in page anchors to provide history functionality.
Setting Paths
Make sure when you use history to specify the path. This is required for determining the stem url from the part of the url maintaining in page history. The path is the base URL without any internal state. This cannot be set automatically to window.location
because refreshing the page will change future page refreshes the URL may include an internal link.
AJAX Content
...with automatic routing
Specifying the setting auto: true
, will automatically retrieve content at a remote url matching the url set in the browser.
So for example the tab inbox
will retrieve content from the URL base-url/inbox/
The URL will receive an addition HTTP Header 'X-Remote': true
. You can use this on your server's back-end to determine whether a request is an AJAX request from a tab, or a full page request.
Queries with 'X-Remote': true
should refresh only the tabbed content, while queries without are normal resources and should refresh the entire page contents
This uses a similar technique to pJax or Rail's turbolinks.
...with an API Behavior
Tabs provide an optional coupling with API which allow you to specify a templated API endpoint that a tab can query
Tabs will automatically pass the url variable {$tab}
which can be replaced for RESTful API links.
Behavior
All the following behaviors can be called using the syntax$('.foo').tab('behavior name', argumentOne, argumentTwo)
attach events(selector, event) | Attaches tab action to given selector. Default event if none specified is toggle |
show | Shows tab |
hide | Hides tab |
toggle | Toggles visibility of tab |
is open | Returns whether tab is open |
is closed | Returns whether tab is closed |
Multi-level Tabs with Defaults
This example shows how default tabs are opened recursively for multiple tiers of tabs
Example with hash page history
Retreiving Dynamic Tab Content
Transition Settings
Form settings modify the transition behavior
Setting | Default | Description |
---|---|---|
overlay | false | Whether tab should overlay page instead of pushing page to the side |
useCSS | true | Whether to use css animations or fallback javascript animations |
duration | 300 | Duration of side bar animation |
DOM Settings
DOM settings specify how this module should interface with the DOM
Setting | Default | Description |
---|---|---|
namespace | tab | Event namespace. Makes sure module teardown does not effect other events attached to an element. |
className |
className: {
active : 'active',
pushed : 'pushed',
top : 'top',
left : 'left',
right : 'right',
bottom : 'bottom'
}
|
Class names used to attach style to state |
Debug Settings
Debug settings controls debug output to the console
Setting | Default | Description |
---|---|---|
name | Tab | Name used in debug logs |
debug | True | Provides standard debug output to console |
performance | True | Provides standard debug output to console |
verbose | True | Provides ancillary debug output to console |
errors |
error : {
method : 'The method you called is not defined.',
notFound : 'There were no elements that matched the specified selector'
}
|