Skip to content

Commit

Permalink
Merge branch 'kasperp-tab-event'
Browse files Browse the repository at this point in the history
  • Loading branch information
fat committed Sep 30, 2011
2 parents 28c770b + a0bf8b6 commit d2de00f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
21 changes: 21 additions & 0 deletions docs/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,27 @@ <h4>$().tabs or $().pills</h4>
})
&lt;/script&gt;</pre>
</p>
<h3>Events</h3>
<table class="zebra-striped">
<thead>
<tr>
<th style="width: 150px;">Event</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>change</td>
<td>This event fires on tab change. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab respectively.</td>
</tr>
</tbody>
</table>

<pre class="prettyprint linenums">
$('#.tabs').bind('change', function (e) {
e.target // activated tab
e.relatedTarget // previous tab
})</pre>
<h3>Demo</h3>
<ul class="tabs" data-tabs="tabs" >
<li class="active"><a href="#home">Home</a></li>
Expand Down
14 changes: 10 additions & 4 deletions js/bootstrap-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@

function tab( e ) {
var $this = $(this)
, href = $this.attr('href')
, $ul = $this.closest('ul')
, $controlled
, href = $this.attr('href')
, previous

if (/^#\w+/.test(href)) {
e.preventDefault()

if ($this.hasClass('active')) {
if ($this.parent('li').hasClass('active')) {
return
}

previous = $ul.find('.active a')[0]
$href = $(href)

activate($this.parent('li'), $ul)
activate($href, $href.parent())

$this.trigger({
type: 'change'
, relatedTarget: previous
})
}
}

Expand All @@ -59,4 +65,4 @@
$('body').tabs('ul[data-tabs] li > a, ul[data-pills] > li > a')
})

}( window.jQuery || window.ender );
}( window.jQuery || window.ender );
44 changes: 36 additions & 8 deletions js/tests/unit/bootstrap-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,67 @@ $(function () {
})

test("should activate element by tab id", function () {
var tabsHTML = '<ul class="tabs">'
var $tabsHTML = $('<ul class="tabs">'
+ '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>'
+ '</ul>')


$('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff")

$(tabsHTML).tabs().find('a').last().click()
$tabsHTML.tabs().find('a').last().click()
equals($("#qunit-runoff").find('.active').attr('id'), "profile")

$(tabsHTML).tabs().find('a').first().click()
$tabsHTML.tabs().find('a').first().click()
equals($("#qunit-runoff").find('.active').attr('id'), "home")

$("#qunit-runoff").empty()
})

test("should activate element by pill id", function () {
var pillsHTML = '<ul class="pills">'
var $pillsHTML = $('<ul class="pills">'
+ '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>'
+ '</ul>')


$('<ul><li id="home"></li><li id="profile"></li></ul>').appendTo("#qunit-runoff")

$(pillsHTML).pills().find('a').last().click()
$pillsHTML.pills().find('a').last().click()
equals($("#qunit-runoff").find('.active').attr('id'), "profile")

$(pillsHTML).pills().find('a').first().click()
$pillsHTML.pills().find('a').first().click()
equals($("#qunit-runoff").find('.active').attr('id'), "home")

$("#qunit-runoff").empty()
})

test( "should trigger change event on activate", function () {
var $tabsHTML = $('<ul class="tabs">'
+ '<li class="active"><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>')
, $target
, count = 0
, relatedTarget
, target

$tabsHTML
.tabs()
.bind( "change", function (e) {
target = e.target
relatedTarget = e.relatedTarget
count++
})

$target = $tabsHTML
.find('a')
.last()
.click()

equals(relatedTarget, $tabsHTML.find('a').first()[0])
equals(target, $target[0])
equals(count, 1)
})

})

0 comments on commit d2de00f

Please sign in to comment.