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

Commit 2f726f1

Browse files
author
Gabriel Schulhof
committed
Filterable: Instantiate on data-filter="true" listview for backcompat
(cherry picked from commit f2c2128) Closes gh-7311 Fixes gh-6911
1 parent ab9ab7e commit 2f726f1

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

js/widgets/filterable.backcompat.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
define( [
88
"jquery",
9+
"./listview",
910
"./filterable" ], function( jQuery ) {
1011
//>>excludeEnd("jqmBuildExclude");
1112
(function( $, undefined ) {
@@ -189,6 +190,24 @@ $.widget( "mobile.filterable", $.mobile.filterable, {
189190
}
190191
});
191192

193+
// Instantiate a filterable on a listview that has the data-filter="true" attribute
194+
// This is not necessary for static content, because the auto-enhance takes care of instantiating
195+
// the filterable upon encountering data-filter="true". However, because of 1.3.x it is expected
196+
// that a listview with data-filter="true" will be filterable even if you just instantiate a
197+
// listview on it. The extension below ensures that this continues to happen in 1.4.x.
198+
$.widget( "mobile.listview", $.mobile.listview, {
199+
options: {
200+
filter: false
201+
},
202+
_create: function() {
203+
if ( this.options.filter === true &&
204+
!this.element.data( "mobile-filterable" ) ) {
205+
this.element.filterable();
206+
}
207+
return this._super();
208+
}
209+
});
210+
192211
})( jQuery );
193212
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
194213
});

tests/unit/filterable/filterable_core.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
( function( $ ){
66

7+
module( "Filterable tests" );
8+
9+
asyncTest( "filterReveal filterable shows all items when all items match filter text", function() {
10+
var input = $( "#test-filter-reveal-show-all-input" ),
11+
list = $( "#test-filter-reveal-show-all-list" );
12+
13+
expect( 1 );
14+
15+
input.val( "Test" ).trigger( "change" );
16+
17+
setTimeout( function() {
18+
deepEqual( list.children( ".ui-screen-hidden" ).length, 0,
19+
"All items visible when search value matches them all" );
20+
start();
21+
}, 500 );
22+
});
23+
724
module( "Backwards compatibility tests" );
825

926
test( "Listview with filter has hideDividers option set to true", function() {
@@ -27,17 +44,19 @@ test( "Filterable input prevents default on ENTER", function() {
2744
deepEqual( event.isDefaultPrevented(), true, "Subsequent keypress default is also prevented" );
2845
});
2946

30-
asyncTest( "filterReveal filterable shows all items when all items match filter text", function() {
31-
var input = $( "#test-filter-reveal-show-all-input" ),
32-
list = $( "#test-filter-reveal-show-all-list" );
33-
34-
expect( 1 );
47+
asyncTest( "Working filterable is instantiated on dynamic listview when data-filter='true'", function() {
48+
var list = $( "<ul data-nstest-filter='true'><li>Chicago</li><li>Berlin</li><li>Windsor</li></ul>" )
49+
.appendTo( "#content" )
50+
.listview(),
51+
input = list.prev().find( "input" );
3552

36-
input.val( "Test" ).trigger( "change" );
53+
expect( 3 );
3754

55+
deepEqual( !!list.data( "mobile-filterable" ), true, "Filterable widget is present on listview" );
56+
input.val( "o" ).trigger( "change" );
3857
setTimeout( function() {
39-
deepEqual( list.children( ".ui-screen-hidden" ).length, 0,
40-
"All items visible when search value matches them all" );
58+
deepEqual( list.children( ".ui-screen-hidden" ).length, 1, "One child was hidden" );
59+
deepEqual( list.children( ".ui-screen-hidden" ).text(), "Berlin", "'Berlin' was hidden" );
4160
start();
4261
}, 500 );
4362
});

tests/unit/filterable/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<div id="qunit"></div>
4747

4848
<div data-nstest-role="page">
49-
<div class="ui-content">
49+
<div id="content" class="ui-content">
5050
<div id="hidedividers-option-test" data-nstest-role="listview" data-nstest-filter="true">
5151
</div>
5252
<form id="test-input-preventDefault-form">

0 commit comments

Comments
 (0)