Permalink
Browse files

Cached filter text for static lists

  • Loading branch information...
1 parent 10b905a commit 4d67232f53afbe352653130a03fa2ba2364c8604 @jakeboone02 committed Mar 5, 2012
Showing with 73 additions and 19 deletions.
  1. +73 −19 js/jquery.mobile.listview.filter.js
@@ -16,12 +16,32 @@ $.mobile.listview.prototype.options.filterCallback = function( text, searchValue
$( document ).delegate( ":jqmData(role='listview')", "listviewcreate", function() {
var list = $( this ),
- listview = list.data( "listview" );
+ listview = list.data( "listview" ),
+ filterIndex = [],
+ dividerIndex = {},
+ listIsDynamic = list.jqmData( "dynamic" ) == true;
if ( !listview.options.filter ) {
return;
}
+ // Cache the filter text if the list is not dynamic
+ if ( !listIsDynamic ) {
+
+ list.children().each(function(i, v) {
+
+ var item = $( this );
+
+ if ( item.is( "li:jqmData(role=list-divider)" ) ) {
+
+ dividerIndex[ i ] = true;
+ } else {
+
+ filterIndex[ i ] = item.jqmData( "filtertext" ) || item.text();
+ }
+ });
+ }
+
var wrapper = $( "<form>", {
"class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
"role": "search"
@@ -39,44 +59,76 @@ $( document ).delegate( ":jqmData(role='listview')", "listviewcreate", function(
lastval = $this.jqmData( "lastval" ) + "",
childItems = false,
itemtext = "",
- item;
+ item,
+ hideQueue = [];
// Change val as lastval for next execution
$this.jqmData( "lastval" , val );
- if ( val.length < lastval.length || val.indexOf(lastval) !== 0 ) {
+ if ( !listIsDynamic || val.length < lastval.length || val.indexOf(lastval) !== 0 ) {
- // Removed chars or pasted something totally different, check all items
+ // Check all items if the list is not dynamic or if the user removed
+ // characters or pasted something totally different
listItems = list.children();
} else {
-
+
// Only chars added, not removed, only use visible subset
listItems = list.children( ":not(.ui-screen-hidden)" );
}
if ( val ) {
// This handles hiding regular rows without the text we search for
- // and any list dividers without regular rows shown under it
+ // and any list dividers without regular rows shown under it. If
+ // the list is not dynamic, we can search the cached filter values
+ // instead of accessing the DOM elements.
+
+ if ( !listIsDynamic ) {
- for ( var i = listItems.length - 1; i >= 0; i-- ) {
- item = $( listItems[ i ] );
- itemtext = item.jqmData( "filtertext" ) || item.text();
+ for ( var i = filterIndex.length - 1; i >= 0; i-- ) {
- if ( item.is( "li:jqmData(role=list-divider)" ) ) {
+ if ( dividerIndex[ i ] ) {
- item.toggleClass( "ui-filter-hidequeue" , !childItems );
+ if ( !childItems ) {
- // New bucket!
- childItems = false;
+ hideQueue.push( i );
+ }
- } else if ( listview.options.filterCallback( itemtext, val ) ) {
+ childItems = false;
+ } else if ( listview.options.filterCallback( filterIndex[ i ], val ) ) {
- //mark to be hidden
- item.toggleClass( "ui-filter-hidequeue" , true );
- } else {
+ hideQueue.push( i );
+ } else {
- // There's a shown item in the bucket
- childItems = true;
+ childItems = true;
+ }
+ }
+
+ for ( var h = 0, hl = hideQueue.length; h < hl; h++ ) {
+
+ listItems.eq( hideQueue[ h ] ).toggleClass( "ui-filter-hidequeue", true );
+ }
+ } else {
+
+ for ( var i = listItems.length - 1; i >= 0; i-- ) {
+ item = $( listItems[ i ] );
+ itemtext = item.jqmData( "filtertext" ) || item.text();
+
+ if ( item.is( "li:jqmData(role=list-divider)" ) ) {
+
+ item.toggleClass( "ui-filter-hidequeue" , !childItems );
+
+ // New bucket!
+ childItems = false;
+
+ } else if ( listview.options.filterCallback( itemtext, val ) ) {
+
+ //mark to be hidden
+ item.toggleClass( "ui-filter-hidequeue" , true );
+ } else {
+
+ // There's a shown item in the bucket
+ childItems = true;
+ }
}
}
@@ -91,6 +143,8 @@ $( document ).delegate( ":jqmData(role='listview')", "listviewcreate", function(
.toggleClass( "ui-screen-hidden", true )
.toggleClass( "ui-filter-hidequeue", false );
+ hideQueue = [];
+
} else {
//filtervalue is empty => show all

0 comments on commit 4d67232

Please sign in to comment.