1 (function( $, undefined ) {
3 $.mobile.listview.prototype.options.filter = false;
4 $.mobile.listview.prototype.options.filterPlaceholder = "";
5 $.mobile.listview.prototype.options.filterTheme = "c";
6 // TODO rename callback/deprecate and default to the item itself as the first argument
7 var defaultFilterCallback = function( text, searchValue, item ) {
8 return text.toString().toLowerCase().indexOf( searchValue ) === -1;
11 $.mobile.listview.prototype.options.filterCallback = defaultFilterCallback;
13 $.mobile.$document.delegate( ":jqmData(role='listview')", "listviewcreate", function() {
16 listview = list.data( "listview" );
18 if ( !listview.options.filter ) {
22 var wrapper = $( "<form>", {
23 "class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
26 search = $( "<input>", {
27 placeholder: listview.options.filterPlaceholder
29 .attr( "data-" + $.mobile.ns + "type", "search" )
30 .jqmData( "lastval", "" )
31 .bind( "keyup change", function() {
33 var $this = $( this ),
34 val = this.value.toLowerCase(),
36 lastval = $this.jqmData( "lastval" ) + "",
40 // Check if a custom filter callback applies
41 isCustomFilterCallback = listview.options.filterCallback !== defaultFilterCallback;
43 listview._trigger( "beforefilter", "beforefilter", { input: this } );
45 // Change val as lastval for next execution
46 $this.jqmData( "lastval" , val );
47 if ( isCustomFilterCallback || val.length < lastval.length || val.indexOf( lastval ) !== 0 ) {
49 // Custom filter callback applies or removed chars or pasted something totally different, check all items
50 listItems = list.children();
53 // Only chars added, not removed, only use visible subset
54 listItems = list.children( ":not(.ui-screen-hidden)" );
59 // This handles hiding regular rows without the text we search for
60 // and any list dividers without regular rows shown under it
62 for ( var i = listItems.length - 1; i >= 0; i-- ) {
63 item = $( listItems[ i ] );
64 itemtext = $.mobile.getAttrFixed( item[0], "data-" + $.mobile.ns + "filtertext" ) || item.text();
66 if ( item.is( "li:jqmData(role=list-divider)" ) ) {
68 item.toggleClass( "ui-filter-hidequeue" , !childItems );
73 } else if ( listview.options.filterCallback( itemtext, val, item ) ) {
76 item.toggleClass( "ui-filter-hidequeue" , true );
79 // There's a shown item in the bucket
84 // Show items, not marked to be hidden
86 .filter( ":not(.ui-filter-hidequeue)" )
87 .toggleClass( "ui-screen-hidden", false );
89 // Hide items, marked to be hidden
91 .filter( ".ui-filter-hidequeue" )
92 .toggleClass( "ui-screen-hidden", true )
93 .toggleClass( "ui-filter-hidequeue", false );
97 //filtervalue is empty => show all
98 listItems.toggleClass( "ui-screen-hidden", false );
100 listview._refreshCorners();
105 if ( listview.options.inset ) {
106 wrapper.addClass( "ui-listview-filter-inset" );
109 wrapper.bind( "submit", function() {
112 .insertBefore( list );