This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
/
filterable.backcompat.js
214 lines (180 loc) · 6.27 KB
/
filterable.backcompat.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
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
//>>description: Links options present in the widget to be filtered to the input
//>>label: Filterable-widgetlink
//>>group: Widgets
//>>css.structure: ../css/structure/jquery.mobile.filterable.css
define( [
"jquery",
"./listview",
"./filterable" ], function( jQuery ) {
//>>excludeEnd("jqmBuildExclude");
(function( $, undefined ) {
// Create a function that will replace the _setOptions function of a widget,
// and will pass the options on to the input of the filterable.
var replaceSetOptions = function( self, orig ) {
return function( options ) {
orig.call( this, options );
self._syncTextInputOptions( options );
};
},
rDividerListItem = /(^|\s)ui-li-divider(\s|$)/,
origDefaultFilterCallback = $.mobile.filterable.prototype.options.filterCallback;
// Override the default filter callback with one that does not hide list dividers
$.mobile.filterable.prototype.options.filterCallback = function( index, searchValue ) {
return !this.className.match( rDividerListItem ) &&
origDefaultFilterCallback.call( this, index, searchValue );
};
$.widget( "mobile.filterable", $.mobile.filterable, {
options: {
filterPlaceholder: "Filter items...",
filterTheme: null
},
_create: function() {
var idx, widgetName,
elem = this.element,
recognizedWidgets = [ "collapsibleset", "selectmenu", "controlgroup", "listview" ],
createHandlers = {};
this._super();
$.extend( this, {
_widget: null
});
for ( idx = recognizedWidgets.length - 1 ; idx > -1 ; idx-- ) {
widgetName = recognizedWidgets[ idx ];
if ( $.mobile[ widgetName ] ) {
if ( this._setWidget( elem.data( "mobile-" + widgetName ) ) ) {
break;
} else {
createHandlers[ widgetName + "create" ] = "_handleCreate";
}
}
}
if ( !this._widget ) {
this._on( elem, createHandlers );
}
},
_handleCreate: function( evt ) {
this._setWidget( this.element.data( "mobile-" + evt.type.substring( 0, evt.type.length - 6 ) ) );
},
_trigger: function( type, event, data ) {
if ( this._widget && this._widget.widgetFullName === "mobile-listview" &&
type === "beforefilter" ) {
// Also trigger listviewbeforefilter if this widget is also a listview
this._widget._trigger( "beforefilter", event, data );
}
// Passing back the response enables calling preventDefault()
return this._super( type, event, data );
},
_setWidget: function( widget ) {
if ( !this._widget && widget ) {
this._widget = widget;
this._widget._setOptions = replaceSetOptions( this, this._widget._setOptions );
}
if ( !!this._widget ) {
this._syncTextInputOptions( this._widget.options );
if ( this._widget.widgetName === "listview" ) {
this._widget.options.hideDividers = true;
this._widget.element.listview( "refresh" );
}
}
return !!this._widget;
},
_isSearchInternal: function() {
return ( this._search && this._search.jqmData( "ui-filterable-" + this.uuid + "-internal" ) );
},
_setInput: function( selector ) {
var opts = this.options,
updatePlaceholder = true,
textinputOpts = {};
if ( !selector ) {
if ( this._isSearchInternal() ) {
// Ignore the call to set a new input if the selector goes to falsy and
// the current textinput is already of the internally generated variety.
return;
} else {
// Generating a new textinput widget. No need to set the placeholder
// further down the function.
updatePlaceholder = false;
selector = $( "<input " +
"data-" + $.mobile.ns + "type='search' " +
"placeholder='" + opts.filterPlaceholder + "'></input>" )
.jqmData( "ui-filterable-" + this.uuid + "-internal", true );
$( "<form class='ui-filterable'></form>" )
.append( selector )
.submit( function( evt ) {
evt.preventDefault();
selector.blur();
})
.insertBefore( this.element );
if ( $.mobile.textinput ) {
if ( this.options.filterTheme != null ) {
textinputOpts[ "theme" ] = opts.filterTheme;
}
selector.textinput( textinputOpts );
}
}
}
this._super( selector );
if ( this._isSearchInternal() && updatePlaceholder ) {
this._search.attr( "placeholder", this.options.filterPlaceholder );
}
},
_setOptions: function( options ) {
var ret = this._super( options );
// Need to set the filterPlaceholder after having established the search input
if ( options.filterPlaceholder !== undefined ) {
if ( this._isSearchInternal() ) {
this._search.attr( "placeholder", options.filterPlaceholder );
}
}
if ( options.filterTheme !== undefined && this._search && $.mobile.textinput ) {
this._search.textinput( "option", "theme", options.filterTheme );
}
return ret;
},
_destroy: function() {
if ( this._isSearchInternal() ) {
this._search.remove();
}
this._super();
},
_syncTextInputOptions: function( options ) {
var idx,
textinputOptions = {};
// We only sync options if the filterable's textinput is of the internally
// generated variety, rather than one specified by the user.
if ( this._isSearchInternal() && $.mobile.textinput ) {
// Apply only the options understood by textinput
for ( idx in $.mobile.textinput.prototype.options ) {
if ( options[ idx ] !== undefined ) {
if ( idx === "theme" && this.options.filterTheme != null ) {
textinputOptions[ idx ] = this.options.filterTheme;
} else {
textinputOptions[ idx ] = options[ idx ];
}
}
}
this._search.textinput( "option", textinputOptions );
}
}
});
// Instantiate a filterable on a listview that has the data-filter="true" attribute
// This is not necessary for static content, because the auto-enhance takes care of instantiating
// the filterable upon encountering data-filter="true". However, because of 1.3.x it is expected
// that a listview with data-filter="true" will be filterable even if you just instantiate a
// listview on it. The extension below ensures that this continues to happen in 1.4.x.
$.widget( "mobile.listview", $.mobile.listview, {
options: {
filter: false
},
_create: function() {
if ( this.options.filter === true &&
!this.element.data( "mobile-filterable" ) ) {
this.element.filterable();
}
return this._super();
}
});
})( jQuery );
//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
});
//>>excludeEnd("jqmBuildExclude");