This repository was archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathfilterable.js
236 lines (194 loc) · 5.94 KB
/
filterable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
//>>description: Renders the children of an element filterable via a callback and a textinput
//>>label: Filterable
//>>group: Widgets
//>>css.structure: ../css/structure/jquery.mobile.filterable.css
define( [
"jquery",
"../widget"
], function( jQuery ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, undefined ) {
// TODO rename filterCallback/deprecate and default to the item itself as the first argument
var defaultFilterCallback = function( index, searchValue ) {
return ( ( "" + ( $.mobile.getAttribute( this, "filtertext" ) || $( this ).text() ) )
.toLowerCase().indexOf( searchValue ) === -1 );
};
$.widget( "mobile.filterable", {
initSelector: ":jqmData(filter='true')",
options: {
filterReveal: false,
filterCallback: defaultFilterCallback,
enhanced: false,
input: null,
ignore: null,
children: "> li, > option, > optgroup option, > tbody tr, > .ui-controlgroup-controls > .ui-btn, > .ui-controlgroup-controls > .ui-checkbox, > .ui-controlgroup-controls > .ui-radio"
},
_create: function() {
var opts = this.options;
$.extend( this, {
_search: null,
_timer: 0
});
this._setInput( opts.input );
if ( !opts.enhanced ) {
this._filterItems( ( ( this._search && this._search.val() ) || "" ).toLowerCase() );
}
},
_onKeyUp: function() {
var val, lastval,
search = this._search;
if ( search ) {
val = search.val().toLowerCase(),
lastval = $.mobile.getAttribute( search[ 0 ], "lastval" ) + "";
if ( lastval && lastval === val ) {
// Execute the handler only once per value change
return;
}
if ( this._timer ) {
window.clearTimeout( this._timer );
this._timer = 0;
}
this._timer = this._delay( function() {
if ( this._trigger( "beforefilter", null, { input: search } ) === false ) {
return false;
}
// Change val as lastval for next execution
search[ 0 ].setAttribute( "data-" + $.mobile.ns + "lastval", val );
this._filterItems( val );
this._timer = 0;
}, 250 );
}
},
_getFilterableItems: function() {
var elem = this.element,
children = this.options.children,
items = !children ? { length: 0 }:
$.isFunction( children ) ? children():
children.nodeName ? $( children ):
children.jquery ? children:
this.element.find( children );
if ( items.length === 0 ) {
items = elem.children();
}
return items;
},
_filterItems: function( val ) {
var idx, callback, length, dst, noFilter
show = [],
hide = [],
opts = this.options,
filterItems = this._getFilterableItems();
if ( val != null ) {
callback = opts.filterCallback || defaultFilterCallback;
length = filterItems.length;
// Partition the items into those to be hidden and those to be shown
for ( idx = 0 ; idx < length ; idx++ ) {
noFilter = opts.ignore ? $( filterItems[ idx ] ).is( opts.ignore ) : false;
dst = ( !noFilter && callback.call( filterItems[ idx ], idx, val ) ) ? hide : show;
dst.push( filterItems[ idx ] );
}
}
// If nothing is hidden, then the decision whether to hide or show the items
// is based on the "filterReveal" option.
if ( hide.length === 0 ) {
filterItems[ ( opts.filterReveal && val.length === 0 ) ?
"addClass" : "removeClass" ]( "ui-screen-hidden" );
} else {
$( hide ).addClass( "ui-screen-hidden" );
$( show ).removeClass( "ui-screen-hidden" );
}
this._refreshChildWidget();
this._trigger( "filter", null, {
items: filterItems
});
},
// The Default implementation of _refreshChildWidget attempts to call
// refresh on collapsibleset, controlgroup, selectmenu, or listview
_refreshChildWidget: function() {
var widget, idx,
recognizedWidgets = [ "collapsibleset", "selectmenu", "controlgroup", "listview" ];
for ( idx = recognizedWidgets.length - 1 ; idx > -1 ; idx-- ) {
widget = recognizedWidgets[ idx ];
if ( $.mobile[ widget ] ) {
widget = this.element.data( "mobile-" + widget );
if ( widget && $.isFunction( widget.refresh ) ) {
widget.refresh();
}
}
}
},
// TODO: When the input is not internal, do not even store it in this._search
_setInput: function ( selector ) {
var search = this._search;
// Stop a pending filter operation
if ( this._timer ) {
window.clearTimeout( this._timer );
this._timer = 0;
}
if ( search ) {
this._off( search, "keyup keydown keypress change input" );
search = null;
}
if ( selector ) {
search = selector.jquery ? selector:
selector.nodeName ? $( selector ):
this.document.find( selector );
this._on( search, {
keydown: "_onKeyDown",
keypress: "_onKeyPress",
keyup: "_onKeyUp",
change: "_onKeyUp",
input: "_onKeyUp"
});
}
this._search = search;
},
// Prevent form submission
_onKeyDown: function( event ) {
this._preventKeyPress = false;
if ( event.keyCode === $.ui.keyCode.ENTER ) {
event.preventDefault();
this._preventKeyPress = true;
}
},
_onKeyPress: function( event ) {
if ( this._preventKeyPress ) {
event.preventDefault();
this._preventKeyPress = false;
}
},
_setOptions: function( options ) {
var refilter = !( ( options.filterReveal === undefined ) &&
( options.filterCallback === undefined ) &&
( options.children === undefined ) );
this._super( options );
if ( options.input !== undefined ) {
this._setInput( options.input );
refilter = true;
}
if ( refilter ) {
this.refresh();
}
},
_destroy: function() {
var opts = this.options,
items = this._getFilterableItems();
if ( opts.enhanced ) {
items.toggleClass( "ui-screen-hidden", opts.filterReveal );
} else {
items.removeClass( "ui-screen-hidden" );
}
},
refresh: function() {
if ( this._timer ) {
window.clearTimeout( this._timer );
this._timer = 0;
}
this._filterItems( ( ( this._search && this._search.val() ) || "" ).toLowerCase() );
}
});
})( jQuery );
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
});
//>>excludeEnd("jqmBuildExclude");