[TemporaryStorage] add files required for SDK build
[samples/web/TemporaryStorage.git] / tizen-web-ui-fw / latest / js / src / widgets / listview.filter.js
1 (function( $, undefined ) {
2
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;
9         };
10
11 $.mobile.listview.prototype.options.filterCallback = defaultFilterCallback;
12
13 $.mobile.$document.delegate( ":jqmData(role='listview')", "listviewcreate", function() {
14
15         var list = $( this ),
16                 listview = list.data( "listview" );
17
18         if ( !listview.options.filter ) {
19                 return;
20         }
21
22         var wrapper = $( "<form>", {
23                         "class": "ui-listview-filter ui-bar-" + listview.options.filterTheme,
24                         "role": "search"
25                 }),
26                 search = $( "<input>", {
27                         placeholder: listview.options.filterPlaceholder
28                 })
29                 .attr( "data-" + $.mobile.ns + "type", "search" )
30                 .jqmData( "lastval", "" )
31                 .bind( "keyup change", function() {
32
33                         var $this = $( this ),
34                                 val = this.value.toLowerCase(),
35                                 listItems = null,
36                                 lastval = $this.jqmData( "lastval" ) + "",
37                                 childItems = false,
38                                 itemtext = "",
39                                 item,
40                                 // Check if a custom filter callback applies
41                                 isCustomFilterCallback = listview.options.filterCallback !== defaultFilterCallback;
42
43                         listview._trigger( "beforefilter", "beforefilter", { input: this } );
44
45                         // Change val as lastval for next execution
46                         $this.jqmData( "lastval" , val );
47                         if ( isCustomFilterCallback || val.length < lastval.length || val.indexOf( lastval ) !== 0 ) {
48
49                                 // Custom filter callback applies or removed chars or pasted something totally different, check all items
50                                 listItems = list.children();
51                         } else {
52
53                                 // Only chars added, not removed, only use visible subset
54                                 listItems = list.children( ":not(.ui-screen-hidden)" );
55                         }
56
57                         if ( val ) {
58
59                                 // This handles hiding regular rows without the text we search for
60                                 // and any list dividers without regular rows shown under it
61
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();
65
66                                         if ( item.is( "li:jqmData(role=list-divider)" ) ) {
67
68                                                 item.toggleClass( "ui-filter-hidequeue" , !childItems );
69
70                                                 // New bucket!
71                                                 childItems = false;
72
73                                         } else if ( listview.options.filterCallback( itemtext, val, item ) ) {
74
75                                                 //mark to be hidden
76                                                 item.toggleClass( "ui-filter-hidequeue" , true );
77                                         } else {
78
79                                                 // There's a shown item in the bucket
80                                                 childItems = true;
81                                         }
82                                 }
83
84                                 // Show items, not marked to be hidden
85                                 listItems
86                                         .filter( ":not(.ui-filter-hidequeue)" )
87                                         .toggleClass( "ui-screen-hidden", false );
88
89                                 // Hide items, marked to be hidden
90                                 listItems
91                                         .filter( ".ui-filter-hidequeue" )
92                                         .toggleClass( "ui-screen-hidden", true )
93                                         .toggleClass( "ui-filter-hidequeue", false );
94
95                         } else {
96
97                                 //filtervalue is empty => show all
98                                 listItems.toggleClass( "ui-screen-hidden", false );
99                         }
100                         listview._refreshCorners();
101                 })
102                 .appendTo( wrapper )
103                 .textinput();
104
105         if ( listview.options.inset ) {
106                 wrapper.addClass( "ui-listview-filter-inset" );
107         }
108
109         wrapper.bind( "submit", function() {
110                 return false;
111         })
112         .insertBefore( list );
113 });
114
115 })( jQuery );