accordion: added test for headers with descendants; replaced while-loop to find header-parent with call to parents

This commit is contained in:
Jörn Zaefferer 2008-06-21 16:16:59 +00:00
parent 9196c2022c
commit 2e3b8eac77
4 changed files with 212 additions and 272 deletions

View File

@ -33,9 +33,6 @@
<div id="main" style="position:absolute;top:-2000000px;"> <div id="main" style="position:absolute;top:-2000000px;">
<fieldset>
<legend>Standard, container is a div, header is h3 and content p</legend>
<div id="list1"> <div id="list1">
<a>There is one obvious advantage:</a> <a>There is one obvious advantage:</a>
<div> <div>
@ -66,14 +63,6 @@
</div> </div>
</div> </div>
</fieldset>
<fieldset>
<legend>Navigation - Unordered List with anchors and nested lists.</legend>
<p>The first item is cloned to allow the user to select it. The
active element is choosen based on location.hash: Open Drums and click
the first item ('Drums'), then reload the page.</p>
<ul id="navigation"> <ul id="navigation">
<li> <li>
<a class="head" href="?p=1.1.1">Guitar</a> <a class="head" href="?p=1.1.1">Guitar</a>
@ -86,7 +75,7 @@
</ul> </ul>
</li> </li>
<li> <li>
<a class="head" href="?p=1.1.2">Bass</a> <a class="head" href="?p=1.1.2"><span>Bass</span></a>
<ul> <ul>
<li><a href="?p=1.1.2.1">Electric</a></li> <li><a href="?p=1.1.2.1">Electric</a></li>
<li><a href="?p=1.1.2.2">Acoustic</a></li> <li><a href="?p=1.1.2.2">Acoustic</a></li>
@ -106,12 +95,6 @@
</ul> </ul>
</li> </li>
</ul> </ul>
</fieldset>
<fieldset>
<legend>With options, container is a definition list, header dt, content dd</legend>
<dl id="list2"> <dl id="list2">
<dt class="red">Red</dt> <dt class="red">Red</dt>
<dd> <dd>
@ -127,12 +110,6 @@
</dd> </dd>
</dl> </dl>
</fieldset>
<fieldset>
<legend>Divitus structure, div container, div header (class title), div
content, no active on startup and can be completely closed</legend>
<div id="list3"> <div id="list3">
<div> <div>
<div class="title">Tennis</div> <div class="title">Tennis</div>
@ -158,20 +135,6 @@
</div> </div>
</div> </div>
</fieldset>
<div id="switch">
<select>
<option>Switch to...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button id="close">Close all</button>
Activate via selector, eg. ':first' or 'a:first': <input id="switch2" />
</div>
<div id="log"><div><strong>Log of the 2nd accordion</strong></div></div>
</div> </div>
</body> </body>

View File

@ -10,10 +10,14 @@ jQuery.ui.accordion.defaults.animated = false;
function state(accordion) { function state(accordion) {
var args = $.makeArray(arguments).slice(1); var args = $.makeArray(arguments).slice(1);
$.each(args, function(i, n) { $.each(args, function(i, n) {
equals(n, accordion.find("div").eq(i).is(":visible")); equals(accordion.find("div").eq(i).is(":visible"), n);
}); });
} }
$.fn.triggerEvent = function(type, target) {
return this.triggerHandler(type, [jQuery.event.fix({ type: type, target: target })]);
}
test("basics", function() { test("basics", function() {
state($('#list1').accordion(), 1, 0, 0); state($('#list1').accordion(), 1, 0, 0);
}); });
@ -82,4 +86,17 @@ test("activate, jQuery or DOM element", function() {
state(ac, 0, 1, 0); state(ac, 0, 1, 0);
}); });
function state2(accordion) {
var args = $.makeArray(arguments).slice(1);
$.each(args, function(i, n) {
equals(accordion.find("ul").eq(i).is(":visible"), n);
});
}
test("handle click on header-descendant", function() {
var ac = $('#navigation').accordion({ header: '.head', autoHeight: false })
ac.triggerEvent("click", $('#navigation span:contains(Bass)')[0]);
state2(ac, 0, 1, 0);
});
})(jQuery); })(jQuery);

View File

@ -64,9 +64,6 @@
<div class="ui-slider-handle" style="position: absolute; height: 21px; left: 0px; bottom: 0px; width: 17px; background-image: url(http://developer.yahoo.com/yui/examples/slider/assets/thumb-n.gif);"></div> <div class="ui-slider-handle" style="position: absolute; height: 21px; left: 0px; bottom: 0px; width: 17px; background-image: url(http://developer.yahoo.com/yui/examples/slider/assets/thumb-n.gif);"></div>
</div> </div>
<fieldset>
<legend>Standard, container is a div, header is h3 and content p</legend>
<div id="list1"> <div id="list1">
<a>There is one obvious advantage:</a> <a>There is one obvious advantage:</a>
<div> <div>
@ -97,14 +94,6 @@
</div> </div>
</div> </div>
</fieldset>
<fieldset>
<legend>Navigation - Unordered List with anchors and nested lists.</legend>
<p>The first item is cloned to allow the user to select it. The
active element is choosen based on location.hash: Open Drums and click
the first item ('Drums'), then reload the page.</p>
<ul id="navigation"> <ul id="navigation">
<li> <li>
<a class="head" href="?p=1.1.1">Guitar</a> <a class="head" href="?p=1.1.1">Guitar</a>
@ -117,7 +106,7 @@
</ul> </ul>
</li> </li>
<li> <li>
<a class="head" href="?p=1.1.2">Bass</a> <a class="head" href="?p=1.1.2"><span>Bass</span></a>
<ul> <ul>
<li><a href="?p=1.1.2.1">Electric</a></li> <li><a href="?p=1.1.2.1">Electric</a></li>
<li><a href="?p=1.1.2.2">Acoustic</a></li> <li><a href="?p=1.1.2.2">Acoustic</a></li>
@ -137,12 +126,6 @@
</ul> </ul>
</li> </li>
</ul> </ul>
</fieldset>
<fieldset>
<legend>With options, container is a definition list, header dt, content dd</legend>
<dl id="list2"> <dl id="list2">
<dt class="red">Red</dt> <dt class="red">Red</dt>
<dd> <dd>
@ -158,12 +141,6 @@
</dd> </dd>
</dl> </dl>
</fieldset>
<fieldset>
<legend>Divitus structure, div container, div header (class title), div
content, no active on startup and can be completely closed</legend>
<div id="list3"> <div id="list3">
<div> <div>
<div class="title">Tennis</div> <div class="title">Tennis</div>
@ -189,21 +166,6 @@
</div> </div>
</div> </div>
</fieldset>
<div id="switch">
<select>
<option>Switch to...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button id="close">Close all</button>
Activate via selector, eg. ':first' or 'a:first': <input id="switch2" />
</div>
<div id="log"><div><strong>Log of the 2nd accordion</strong></div></div>
<div id="resizable1" style="background: green; width: 100px; height: 100px;">I'm a resizable.</div> <div id="resizable1" style="background: green; width: 100px; height: 100px;">I'm a resizable.</div>
<img src="images/test.jpg" id="resizable2" style="width: 100px; height: 100px;"/> <img src="images/test.jpg" id="resizable2" style="width: 100px; height: 100px;"/>

View File

@ -180,9 +180,7 @@ function clickHandler(event) {
// due to the event delegation model, we have to check if one // due to the event delegation model, we have to check if one
// of the parent elements is our actual header, and find that // of the parent elements is our actual header, and find that
if ( clicked.parents(options.header).length ) { if ( clicked.parents(options.header).length ) {
while ( !clicked.is(options.header) ) { clicked = clicked.parents(options.header + ":first");
clicked = clicked.parent();
}
} }
var clickedActive = clicked[0] == options.active[0]; var clickedActive = clicked[0] == options.active[0];