Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 0b9186c

Browse files
author
Gabriel Schulhof
committed
Collapsible: Handle icon-related option setting uniformly
This adds tests for fbbb29e Closes gh-7081
1 parent fbbb29e commit 0b9186c

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

js/widgets/collapsible.js

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ $.widget( "mobile.collapsible", {
174174
},
175175

176176
_applyOptions: function( options ) {
177-
var isCollapsed, newTheme, oldTheme, hasCorners,
177+
var isCollapsed, newTheme, oldTheme, hasCorners, hasIcon,
178178
elem = this.element,
179179
currentOpts = this._renderedOptions,
180180
ui = this._ui,
@@ -191,41 +191,61 @@ $.widget( "mobile.collapsible", {
191191

192192
isCollapsed = elem.hasClass( "ui-collapsible-collapsed" );
193193

194-
// Only options referring to the current state need to be applied right away
195-
// It is enough to store options covering the alternate in this.options.
194+
// We only need to apply the cue text for the current state right away.
195+
// The cue text for the alternate state will be stored in the options
196+
// and applied the next time the collapsible's state is toggled
196197
if ( isCollapsed ) {
197198
if ( opts.expandCueText !== undefined ) {
198199
status.text( opts.expandCueText );
199200
}
200-
if ( opts.collapsedIcon !== undefined ) {
201-
if ( currentOpts.collapsedIcon ) {
202-
anchor.removeClass( "ui-icon-" + currentOpts.collapsedIcon );
203-
}
204-
if ( /false/i.test( opts.collapsedIcon ) ) {
205-
anchor.removeClass( "ui-btn-icon-" + ( currentOpts.iconPos === "right" ? "right" : "left" ) );
206-
} else {
207-
anchor.addClass( "ui-icon-" + opts.collapsedIcon )
208-
.toggleClass( "ui-btn-icon-" + ( currentOpts.iconPos === "right" ? "right" : "left" ), true );
209-
}
210-
}
211201
} else {
212202
if ( opts.collapseCueText !== undefined ) {
213203
status.text( opts.collapseCueText );
214204
}
215-
if ( opts.expandedIcon !== undefined ) {
216-
if ( currentOpts.expandedIcon ) {
217-
anchor.removeClass( "ui-icon-" + currentOpts.expandedIcon );
218-
}
219-
if ( opts.expandedIcon ) {
220-
anchor.addClass( "ui-icon-" + opts.expandedIcon );
221-
}
222-
}
223205
}
224206

225-
if ( opts.iconpos !== undefined ) {
226-
anchor
227-
.removeClass( iconposClass( currentOpts.iconpos ) )
228-
.addClass( iconposClass( opts.iconpos ) );
207+
// Update icon
208+
209+
// Is it supposed to have an icon?
210+
hasIcon =
211+
212+
// If the collapsedIcon is being set, consult that
213+
( opts.collapsedIcon !== undefined ? opts.collapsedIcon !== false :
214+
215+
// Otherwise consult the existing option value
216+
currentOpts.collapsedIcon !== false );
217+
218+
219+
// If any icon-related options have changed, make sure the new icon
220+
// state is reflected by first removing all icon-related classes
221+
// reflecting the current state and then adding all icon-related
222+
// classes for the new state
223+
if ( !( opts.iconpos === undefined &&
224+
opts.collapsedIcon === undefined &&
225+
opts.expandedIcon === undefined ) ) {
226+
227+
// Remove all current icon-related classes
228+
anchor.removeClass( [ iconposClass( currentOpts.iconpos ) ]
229+
.concat( ( currentOpts.expandedIcon ?
230+
[ "ui-icon-" + currentOpts.expandedIcon ] : [] ) )
231+
.concat( ( currentOpts.collapsedIcon ?
232+
[ "ui-icon-" + currentOpts.collapsedIcon ] : [] ) )
233+
.join( " " ) );
234+
235+
// Add new classes if an icon is supposed to be present
236+
if ( hasIcon ) {
237+
anchor.addClass(
238+
[ iconposClass( opts.iconpos !== undefined ?
239+
opts.iconpos : currentOpts.iconpos ) ]
240+
.concat( isCollapsed ?
241+
[ "ui-icon-" + ( opts.collapsedIcon !== undefined ?
242+
opts.collapsedIcon :
243+
currentOpts.collapsedIcon ) ] :
244+
[ "ui-icon-" + ( opts.expandedIcon !== undefined ?
245+
opts.expandedIcon :
246+
currentOpts.expandedIcon ) ] )
247+
.join( " " ) );
248+
}
229249
}
230250

231251
if ( opts.theme !== undefined ) {

tests/integration/collapsible/collapsible_core.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,29 @@
210210

211211
module( "Icons", {});
212212

213+
test( "expandedIcon behavior when collapsedIcon set to false", function() {
214+
var collapsible = $( "#collapsible-collapsed-icon-false-expanded-icon" );
215+
collapsible.collapsible( "expand" );
216+
collapsible.collapsible( "option", "collapsedIcon", false );
217+
deepEqual( collapsible.find( "a" ).hasClass( "ui-icon-minus" ),
218+
false, "Turning off collapsedIcon makes expanded icon disappear" );
219+
deepEqual( collapsible.find( "a" ).hasClass( "ui-btn-icon-left" ),
220+
false, "Turning off collapsedIcon makes iconpos class disappear" );
221+
collapsible.collapsible( "option", "collapsedIcon", "plus" );
222+
deepEqual( collapsible.find( "a" ).hasClass( "ui-icon-minus" ),
223+
true, "Turning on collapsedIcon makes expanded icon reappear" );
224+
deepEqual( collapsible.find( "a" ).hasClass( "ui-btn-icon-left" ),
225+
true, "Turning on collapsedIcon makes iconpos class reappear" );
226+
});
227+
228+
test( "iconpos behavior when collapsedIcon set to false", function() {
229+
var collapsible = $( "#collapsible-collapsed-icon-false-iconpos" );
230+
collapsible.collapsible( "option", "collapsedIcon", false );
231+
collapsible.collapsible( "option", "iconpos", "top" );
232+
deepEqual( collapsible.find( "a" ).hasClass( "ui-btn-icon-top" ),
233+
false, "Setting iconpos while collapsedIcon is off has no effect" );
234+
});
235+
213236
test( "Collapsible with custom icons", function(){
214237
var collapsibles = $( "#collapsible-with-custom-icons" ).find( ".ui-collapsible" );
215238

tests/integration/collapsible/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636

3737
<div data-nstest-role="page">
3838
<div data-nstest-role="content">
39+
<div id="collapsible-collapsed-icon-false-expanded-icon" data-nstest-role="collapsible">
40+
<h4>Header</h4>
41+
<p>Contents</p>
42+
</div>
43+
<div id="collapsible-collapsed-icon-false-iconpos" data-nstest-role="collapsible">
44+
<h4>Header</h4>
45+
<p>Contents</p>
46+
</div>
3947
<div id="collapsible-iconpos-test" data-nstest-role="collapsible" data-nstest-iconpos="xyzzy">
4048
<h4>Header</h4>
4149
<p>Contents</p>

0 commit comments

Comments
 (0)