Skip to content

Instantly share code, notes, and snippets.

@fat
Created September 14, 2011 18:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fat/1217433 to your computer and use it in GitHub Desktop.
Save fat/1217433 to your computer and use it in GitHub Desktop.
tabs with history support
/* ========================================================
* bootstrap-tabs.js v1.3.0
* http://twitter.github.com/bootstrap/javascript.html#tabs
* ========================================================
* Copyright 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ======================================================== */
(function( $ ){
function activate ( element, container ) {
container.find('.active').removeClass('active')
element.addClass('active')
}
function tab( e ) {
var $this = $(this)
, href = $this.attr('href')
, $ul = $(e.liveFired)
, $controlled
if (/^#\w+/.test(href)) {
e.preventDefault()
location.href = location.href.replace(/#.*/,'') + href
if ($this.hasClass('active')) {
return
}
$href = $(href)
activate($this.parent(), $ul)
activate($href, $href.parent())
}
}
/* TABS/PILLS PLUGIN DEFINITION
* ============================ */
$.fn.tabs = $.fn.pills = function ( selector ) {
return this.each(function () {
$(this).delegate(selector || '.tabs li > a, .pills > li > a', 'click', tab)
})
}
$(function () {
$('body').tabs('ul[data-tabs] li > a, ul[data-pills] > li > a')
})
// Hashchange support for all modern browsers, and ie8+
// Integrate Ben Alman's hashchange plugin for ie6-7 support
// http://goo.gl/7ezFB
var url = location.href
$(window).bind('load hashchange', function () {
var $target = $(".tab-content :target, .pill-content :target")
, $tab
, href
if ($target.length) {
href = '#' + $target.attr('id')
$tab = $('.tabs a[href=' + href + '], .pills a[herf=' + href + ']')
activate($tab.parent(), $tab.parents('ul:first'))
activate($target, $target.parent())
} else if (url == location.href){
activate($('.tabs li:first, .pills li:first'), $('.tabs, .pills'))
activate($('.tab-content :first, .pill-content :first'), $('.tab-content, .pill-content'))
}
})
})( jQuery || ender )
@ded
Copy link

ded commented Sep 15, 2011

This will only work with jQuery since ender doesnt support the $.fn syntax, nor the $(fn) style of domReady

@fat
Copy link
Author

fat commented Sep 15, 2011

ender build jquery ;)

@fat
Copy link
Author

fat commented Sep 15, 2011

also, we do support $.fn - https://github.com/ender-js/ender-js/blob/master/ender.js#L63

edit -- not sure if that works actually ...

@ded
Copy link

ded commented Sep 15, 2011

i know we have $.fn -- i added it :p. the purpose was so that Ender modules could have jQuery compat without having to define the fn object. and usually at the end of your method you would say something like....

$.ender($.fn, true)

@fat
Copy link
Author

fat commented Sep 15, 2011

did you see my pull request? that seems easier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment