Merge pull request #1966 from rwldrn/jquery.mobile.listview.filter.js

jQuery core style guide conformance: jquery.mobile.listview.filter.js
This commit is contained in:
Ghislain Seguin 2011-06-29 06:19:35 -07:00
commit 233a57f3a4

View file

@ -4,13 +4,15 @@
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function($, undefined ) {
(function( $, undefined ) {
$.mobile.listview.prototype.options.filter = false;
$.mobile.listview.prototype.options.filterPlaceholder = "Filter items...";
$.mobile.listview.prototype.options.filterTheme = "c";
$( ":jqmData(role='listview')" ).live( "listviewcreate", function() {
var list = $( this ),
listview = list.data( "listview" );
@ -18,93 +20,95 @@ $( ":jqmData(role='listview')" ).live( "listviewcreate", function() {
return;
}
var wrapper = $( "<form>", { "class": "ui-listview-filter ui-bar-" + listview.options.filterTheme, "role": "search" } ),
var wrapper = $( "<form>", {
"class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
"role": "search"
}),
search = $( "<input>", {
placeholder: listview.options.filterPlaceholder
})
.attr( "data-" + $.mobile.ns + "type", "search" )
.jqmData( 'lastval', "" )
.bind( "keyup change", function() {
placeholder: listview.options.filterPlaceholder
})
.attr( "data-" + $.mobile.ns + "type", "search" )
.jqmData( "lastval", "" )
.bind( "keyup change", function() {
var val = this.value.toLowerCase(),
listItems=null,
lastval = $( this ).jqmData('lastval')+"";
var $this = $(this),
val = this.value.toLowerCase(),
listItems = null,
lastval = $this.jqmData( "lastval" ) + "",
childItems = false,
itemtext = "",
item;
//change val as lastval for next execution
$(this).jqmData( 'lastval' , val );
// Change val as lastval for next execution
$this.jqmData( "lastval" , val );
change = val.replace( new RegExp( "^" + lastval ) , "" );
change = val.replace( new RegExp( "^" + lastval ) , "" );
if( val.length < lastval.length || change.length != ( val.length - lastval.length ) ){
if ( val.length < lastval.length || change.length != ( val.length - lastval.length ) ) {
//removed chars or pasted something totaly different, check all items
listItems = list.children();
} else {
// Removed chars or pasted something totaly different, check all items
listItems = list.children();
} else {
//only chars added, not removed, only use visible subset
listItems = list.children( ':not(.ui-screen-hidden)' );
}
// Only chars added, not removed, only use visible subset
listItems = list.children( ":not(.ui-screen-hidden)" );
}
if ( val ) {
if ( val ) {
// This handles hiding regular rows without the text we search for
// and any list dividers without regular rows shown under it
var item,
childItems = false,
itemtext="";
// This handles hiding regular rows without the text we search for
// and any list dividers without regular rows shown under it
for ( var i = listItems.length - 1; i >= 0; i-- ) {
item = $( listItems[i] );
itemtext = item.jqmData( 'filtertext' ) || item.text();
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)" ) ) {
if ( item.is( "li:jqmData(role=list-divider)" ) ) {
item.toggleClass( 'ui-filter-hidequeue' , !childItems );
item.toggleClass( "ui-filter-hidequeue" , !childItems );
// New bucket!
childItems = false;
// New bucket!
childItems = false;
} else if ( itemtext.toLowerCase().indexOf( val ) === -1) {
} else if ( itemtext.toLowerCase().indexOf( val ) === -1 ) {
//mark to be hidden
item.toggleClass( 'ui-filter-hidequeue' , true );
} else {
//mark to be hidden
item.toggleClass( "ui-filter-hidequeue" , true );
} else {
// There's a shown item in the bucket
childItems = true;
}
// There"s a shown item in the bucket
childItems = true;
}
// show items, not marked to be hidden
listItems
.filter( ':not(.ui-filter-hidequeue)' )
.toggleClass('ui-screen-hidden',false);
// hide items, marked to be hidden
listItems
.filter( '.ui-filter-hidequeue' )
.toggleClass('ui-screen-hidden',true)
.toggleClass( 'ui-filter-hidequeue' , false );
}else{
//filtervalue is empty => show all
listItems.toggleClass('ui-screen-hidden',false);
}
})
.appendTo( wrapper )
.textinput();
if ($( this ).jqmData( "inset" ) ) {
// Show items, not marked to be hidden
listItems
.filter( ":not(.ui-filter-hidequeue)" )
.toggleClass("ui-screen-hidden",false);
// Hide items, marked to be hidden
listItems
.filter( ".ui-filter-hidequeue" )
.toggleClass("ui-screen-hidden",true)
.toggleClass( "ui-filter-hidequeue" , false );
} else {
//filtervalue is empty => show all
listItems.toggleClass("ui-screen-hidden",false);
}
})
.appendTo( wrapper )
.textinput();
if ( $( this ).jqmData( "inset" ) ) {
wrapper.addClass( "ui-listview-filter-inset" );
}
wrapper
.bind( "submit", function() {
return false;
})
.insertBefore( list );
wrapper.bind( "submit", function() {
return false;
})
.insertBefore( list );
});
})( jQuery );