Debian Codegraph @

dojox/mobile/FilteredListMixin.js

raw symbols SCIP
1 define([
2 "require",
3 "dojo/_base/array",
4 "dojo/_base/declare",
5 "dojo/_base/lang",
6 "dojo/dom",
7 "dojo/dom-class",
8 "dojo/dom-construct",
9 "dojo/aspect",
10 "dijit/registry",
11 "./SearchBox",
12 "./ScrollableView",
13 "./viewRegistry"
14 ], function(require, array, declare, lang, dom, domClass, domConstruct,
15 aspect, registry, SearchBox, ScrollableView, viewRegistry){
16
17 // module:
18 // dojox/mobile/FilteredListMixin
19
20 return declare("dojox.mobile.FilteredListMixin", null, {
21 // summary:
22 // Mixin for filtered lists.
23 // description:
24 // This mixin adds filtering capabilities to all dojox/mobile list widgets:
25 // dojox/mobile/RoundRectList and any of its subclasses (RoundRectStoreList,
26 // RoundRectDataList, EdgeToEdgeList, EdgeToEdgeStoreList, EdgeToEdgeDataList).
27 // When mixing this class into a list widget, the list items are dynamically
28 // filtered depending on the filtering string that the user enters in a
29 // dojox/mobile/SearchBox.
30 //
31 // This mixin supports the following use-cases:
32 // 1. For user's convenience, by simply mixing this class into a list widget
33 // the mixin creates a dojox/mobile/SearchBox and a dojox/mobile/ScrollableView.
34 // The list is placed inside the ScrollableView and the SearchBox, which allows
35 // filtering the list, is placed on top of the ScrollableView.
36 // 2. Alternatively, the user can create (and style) the instance of dojox/mobile/SearchBox,
37 // and specify its id using the property filterBoxRef of this mixin. This allows
38 // placing the SearchBox anywhere in the DOM, while the mixin takes care of
39 // the necessary glue to ensure the list is filtered according to the filter criteria
40 // entered in the SearchBox.
41 //
42 // The filtering works for lists backed by a store (dojo/store or dojo/data), as well
43 // as for lists not backed by a store. When filtering a list backed by a store
44 // containing hierarchical data (data items that are children of a parent data item),
45 // the store must support recursive search queries such that the filtering can match
46 // child items.
47 //
48 // For configuration purposes, the instance of dojox/mobile/SearchBox can be retrieved
49 // using the method getFilterBox(). If a dojox/mobile/ScrollableView is created by
50 // this mixin, it can be retrieved using getScrollableView().
51 //
52 // example:
53 // | <!-- Markup use-case: -->
54 // | <!-- SearchBox and ScrollableView created by the mixin. -->
55 // | <!-- Filtered EdgeToEdgeStoreList created in markup. -->
56 // | <div data-dojo-type="dojox/mobile/View">
57 // | <h1 data-dojo-type="dojox/mobile/Heading" data-dojo-props="fixed: 'top'">Some heading</h1>
58 // | <ul data-dojo-type="dojox/mobile/EdgeToEdgeStoreList"
59 // | data-dojo-mixins="dojox/mobile/FilteredListMixin"
60 // | data-dojo-props="placeHolder: 'Search', store: myStore"></ul>
61 // | </div>
62 // example:
63 // | <!-- Markup use-case: -->
64 // | <!-- SearchBox and ScrollableView created by the mixin. -->
65 // | <!-- Filtered RoundRectList created in markup. -->
66 // | <div data-dojo-type="dojox/mobile/View">
67 // | <h1 data-dojo-type="dojox/mobile/Heading" data-dojo-props="fixed: 'top'">Some heading</h1>
68 // | <ul id="list" data-dojo-type="dojox/mobile/RoundRectList"
69 // | data-dojo-mixins="dojox/mobile/FilteredListMixin"
70 // | data-dojo-props="placeHolder: 'Search'">
71 // | <li data-dojo-type="dojox/mobile/ListItem">Item 1</li>
72 // | <li data-dojo-type="dojox/mobile/ListItem">Item 2</li>
73 // | ...
74 // | </ul>
75 // | </div>
76 // example:
77 // | // Programmatic use-case:
78 // | // SearchBox and ScrollableView created by the mixin.
79 // | // Filtered EdgeToEdgeStoreList created programmatically.
80 // | require(["dojo/_base/declare", "dojo/ready", "dojox/mobile", "dojox/mobile/EdgeToEdgeStoreList",
81 // | "dojox/mobile/FilteredListMixin", ...], function(declare, ready, registry, ...){
82 // | ready(function(){
83 // | var listWidget =
84 // | new declare([EdgeToEdgeStoreList, FilteredListMixin])(
85 // | {placeHolder: 'Search', store: myStore, "filteredList"});
86 // | listWidget.startup();
87 // | });
88 // | });
89 // | ...
90 // | <div id="view" data-dojo-type="dojox/mobile/View">
91 // | <h1 data-dojo-type="dojox/mobile/Heading" data-dojo-props="fixed: 'top'">Some heading</h1>
92 // | <div id="filteredList">
93 // | </div>
94 // example:
95 // | <!-- Markup use-case: -->
96 // | <!-- SearchBox and ScrollableView provided by the user. -->
97 // | <!-- Filtered EdgeToEdgeDataList created in markup. -->
98 // | <div data-dojo-type="dojox/mobile/View">
99 // | <h1 data-dojo-type="dojox/mobile/Heading" data-dojo-props="fixed: 'top'">Some heading</h1>
100 // | <input id="filterBox" data-dojo-type="dojox/mobile/SearchBox" type="search"
101 // | class="mblFilteredEdgeToEdgeListSearchBox">
102 // | <div data-dojo-type="dojox/mobile/ScrollableView">
103 // | <ul data-dojo-type="dojox/mobile/EdgeToEdgeDataList"
104 // | data-dojo-mixins="dojox/mobile/FilteredListMixin"
105 // | data-dojo-props="filterBoxRef: 'filterBox', placeHolder: 'Search', store: myStore"></ul>
106 // | </div>
107 // | </div>
108 // example:
109 // | // Programmatic use-case:
110 // | // SearchBox and ScrollableView provided by the user.
111 // | // Filtered EdgeToEdgeStoreList created programmatically.
112 // | require(["dojo/_base/declare", "dojo/ready", "dijit/registry", "dojox/mobile",
113 // | "dojox/mobile/EdgeToEdgeStoreList", "dojox/mobile/FilteredListMixin",
114 // | "dojox/mobile/ScrollableView", ...], function(declare, ready, registry, ...){
115 // | ready(function(){
116 // | var view = registry.byId("scrollableView");
117 // | var listWidget =
118 // | new declare([EdgeToEdgeStoreList, FilteredListMixin])(
119 // | {id:"list", filterBoxRef: 'filterBox', placeHolder: 'Search', store: myStore});
120 // | listWidget.placeAt(view.containerNode);
121 // | listWidget.startup();
122 // | });
123 // | });
124 // | ...
125 // | <div data-dojo-type="dojox/mobile/View">
126 // | <h1 data-dojo-type="dojox/mobile/Heading" data-dojo-props="fixed: 'top'">Some heading</h1>
127 // | <input id="filterBox" data-dojo-type="dojox/mobile/SearchBox" type="search"
128 // | class="mblFilteredEdgeToEdgeListSearchBox">
129 // | <div id="scrollableView" data-dojo-type="dojox/mobile/ScrollableView">
130 // | </div>
131
132 // Implementation notes:
133 // - The mixin requires dojox/mobile/ScrollableView statically. It could be required
134 // dynamically, only when needed, that is in the use-case when the mixin creates the
135 // ScrollableView by itself. But this would create an usability trouble: if the user
136 // would want to get the instance of ScrollableView in a dojo/ready (say, for configuring
137 // it), he would need to require it upfront, to cope with the case of asynchronous
138 // loading. Thus, requiring it statically has been preferred, because it avoids this
139 // constraint on user's side and it is not a serious overhead, because in practice
140 // the filtering is used for long lists, for which a ScrollableView is anyway likely
141 // to be used.
142 // - Differently, the loading of the store/data modules is performed dynamically,
143 // in order to avoid their overhead when they are not actually needed.
144
145 // filterBoxRef: String
146 // The reference for the search box allowing to enter the filtering criteria.
147 // Only used at construction time:
148 // - If unspecified, the mixin creates a dojox/mobile/SearchBox and
149 // a dojox/mobile/ScrollableView. The list is placed inside the ScrollableView and the
150 // SearchBox, wrapped in a DIV, is placed on top of the ScrollableView.
151 // - If the string is the id of a widget which is an instance of dojox/mobile/SearchBox
152 // or a subclass, the mixin uses this SearchBox for filtering the list.
153 // - If the id is specified but does not reference a dojox/mobile/SearchBox or
154 // subclass, an error is thrown.
155 filterBoxRef: null,
156
157 // placeHolder: String
158 // Defines a hint to help users fill out the input field (as defined in HTML 5) of the
159 // dojox/mobile/SearchBox. This should only contain plain text (no HTML markup).
160 // When the SearchBox is provided by the user (not created by this mixin), its placeHolder
161 // property takes precedence.
162 placeHolder: "",
163
164 // filterBoxVisible: Boolean
165 // A flag which allows to show or hide the dojox/mobile/SearchBox associated with
166 // the list.
167 filterBoxVisible: true,
168
169 // _filterBox: [private] dojox/mobile/SearchBox
170 // The instance of dojox/mobile/SearchBox used by this mixin.
171 // Stored for getFilterBox().
172 _filterBox: null,
173
174 // _createdFilterBox: [private] dojox/mobile/SearchBox
175 // The instance of dojox/mobile/SearchBox created by this mixin, or null if none
176 // has been created. Stored for being able to destroy it together with the list widget.
177 _createdFilterBox: null,
178
179 // _createdScrollableView: [private] dojox/mobile/ScrollableView
180 // The instance of dojox/mobile/ScrollableView created by this mixin, if any.
181 // Stored for getScrollableView() and for being able to destroy it together
182 // with the list widget.
183 _createdScrollableView: null,
184
185 startup: function(){
186 if(this._started){ return; }
187
188 this.inherited(arguments);
189
190 if(this.filterBoxRef){
191 // Case #1: search box provided by the user
192 this._filterBox = registry.byId(this.filterBoxRef);
193
194 if (this._filterBox && this._filterBox.isInstanceOf(SearchBox)){
195 // If the list is backed by a dojox/mobile/_StoreListMixin, it
196 // has a labelProperty which is given precedence.
197 this._filterBox.set("searchAttr", this.labelProperty ? this.labelProperty : "label");
198 if(!this._filterBox.placeHolder){
199 // Give precedence to the placeHolder that may be specified on the provided SearchBox
200 this._filterBox.set("placeHolder", this.placeHolder);
201 }
202 this._filterBox.on("search", lang.hitch(this, "_onFilter"));
203 }else{
204 throw new Error("Cannot find a widget of type dojox/mobile/SearchBox or subclass " +
205 "at the specified filterBoxRef: " + this.filterBoxRef);
206 }
207 }else{
208 // Case #2: automatic mode. The mixin creates a SearchBox and a ScrollableView.
209 this._filterBox =
210 new SearchBox({
211 // If the list is backed by a dojox/mobile/_StoreListMixin, it
212 // has a labelProperty which is given precedence.
213 searchAttr: this.labelProperty ? this.labelProperty : "label",
214 ignoreCase: true,
215 incremental: true,
216 onSearch: lang.hitch(this, "_onFilter"),
217 selectOnClick: true,
218 placeHolder: this.placeHolder
219 });
220
221 // Store them to be able to destroy them together with the list (see destroy()).
222 this._createdFilterBox = this._filterBox;
223 this._createdScrollableView = new ScrollableView();
224
225 var currentDomNode = this.domNode,
226 listParentNode = this.domNode.parentNode;
227 listParentNode.replaceChild(this._createdScrollableView.domNode, this.domNode);
228 // Put the list inside the ScrollableView:
229 domConstruct.place(currentDomNode, this._createdScrollableView.containerNode);
230
231 var searchBoxParentDiv = domConstruct.create("div");
232 // Put the SearchBox as child of the DIV
233 domConstruct.place(this._createdFilterBox.domNode, searchBoxParentDiv);
234 // Put the DIV as sibling of the ScrollableView:
235 domConstruct.place(searchBoxParentDiv, this._createdScrollableView.domNode, "before");
236
237 if(this.filterBoxClass){
238 // Only adding the class when the mixin creates the SearchBox by itself.
239 domClass.add(searchBoxParentDiv, this.filterBoxClass);
240 }
241
242 this._createdFilterBox.startup();
243 this._createdScrollableView.startup();
244 this._createdScrollableView.resize();
245 }
246
247 // Do not use this.getScrollableView() here, because this doesn't cover the
248 // use-case when the scrollable is not created by this mixin.
249 var sv = viewRegistry.getEnclosingScrollable(this.domNode);
250 if(sv){
251 this.connect(sv, "onFlickAnimationEnd", lang.hitch(this, function(){
252 if(!this._filterBox.focusNode.value){ // if search criteria is empty
253 // store the scroll position such that we can reset the
254 // initial scroll when the user goes back to the unfiltered
255 // list (as done by some native mobile apps).
256 this._previousUnfilteredScrollPos = sv.getPos();
257 }
258 }));
259 }
260
261 if(!this.store){
262 this._createStore(this._initStore);
263 }else{
264 this._initStore();
265 }
266 },
267
268 _setFilterBoxVisibleAttr: function(/* Boolean */ visible){
269 // tags:
270 // private
271 this._set("filterBoxVisible", visible);
272 if (this._filterBox && this._filterBox.domNode){
273 this._filterBox.domNode.style.display = visible ? "" : "none";
274 }
275 },
276
277 _setPlaceHolderAttr: function(/* String */ placeHolder){
278 // tags:
279 // private
280 this._set("placeHolder", placeHolder);
281 if (this._filterBox){ // allow update after construction time
282 this._filterBox.set("placeHolder", placeHolder);
283 }
284 },
285
286 getFilterBox: function(){
287 // summary:
288 // Returns the dojox/mobile/SearchBox widget used for entering the filtering criteria.
289 // If an instance has been referenced at construction time using the property filterBoxRef,
290 // this instance is returned. Otherwise, returns the instance created by the mixin.
291 // This function allows the user to get the instance of SearchBox in order to customize
292 // its parameters.
293 return this._filterBox;
294 },
295
296 getScrollableView: function(){
297 // summary:
298 // Returns the instance of dojox/mobile/ScrollableView created by this mixin,
299 // or null if none has been created. The mixin creates a ScrollableView if and
300 // only if the property filterBoxRef is unspecified.
301 // This function allows the user to get the instance of ScrollableView in order to
302 // customize its parameters.
303 return this._createdScrollableView;
304 },
305
306 _initStore: function(){
307 // description:
308 // Initializes the store.
309 // tags:
310 // private
311 var store = this.store;
312 if(!store.get || !store.query){ // if old store (dojo/data)
313 // Detect the old dojo/data stores (since the stores don't actually extend a common
314 // base class, there is no direct way to do this check. Hence we rely on the presence
315 // or absence of these two properties of the new stores which are required for the
316 // list widgets).
317 // TODO: to be removed when removing the support for lists backed by the old dojo/data
318 // (EdgeToEdgeDataStore, RoundRectDataList).
319 require(["dojo/store/DataStore"], lang.hitch(this, function(DataStore){
320 // wrap the dojo/data store into a dojo/store
321 store = new DataStore({store: store});
322 this._filterBox.store = store;
323 }));
324 }else{
325 this._filterBox.store = store;
326 }
327 },
328
329 _createStore: function(initStoreFunction/* Function */){
330 // summary:
331 // Creates the store.
332 // description:
333 // This method is used when the list is not backed by a store. In this case,
334 // a store is created and filled with items containing the text of the list items.
335 // tags:
336 // private
337 require(["./_StoreListMixin", "dojo/store/Memory"], lang.hitch(this, function(module, Memory){
338 declare.safeMixin(this, new module());
339
340 this.append = true; // to avoid that _StoreListMixin.generateList destroys the items
341
342 // _StoreListMixin.createListItem creates a new item. Instead, we want to reuse the
343 // original item instance, hence:
344 this.createListItem = function(/*Object*/item){
345 return item.listItem;
346 };
347
348 aspect.before(this, "generateList", function(){
349 // remove all children
350 array.forEach(this.getChildren(), function(child){
351 child.domNode.parentNode.removeChild(child.domNode);
352 });
353 });
354
355 // Collect the text from the list items
356 var items = [];
357 var text = null;
358 array.forEach(this.getChildren(), function(child){
359 text = child.label ? child.label : (child.domNode.innerText || child.domNode.textContent);
360 items.push({label: text, listItem: child});
361 });
362 var listData = {items: items};
363 // store for the dojox/mobile/EdgeToEdgeStoreList
364 var store = new Memory({idProperty:"label", data: listData});
365 this.store = null;
366 this.query = {};
367 this.setStore(store, this.query, this.queryOptions);
368 lang.hitch(this, initStoreFunction)();
369 }));
370 },
371
372 _onFilter: function(results, query, options){
373 // summary:
374 // Internal handler for filtering events.
375 // tags:
376 // private
377 if(this.onFilter(results, query, options) === false){ return; } // user's filtering action
378 this.setQuery(query);
379
380 // Do not use this.getScrollableView() because this doesn't cover the
381 // use-case when the scrollable is not created by this mixin.
382 var sv = viewRegistry.getEnclosingScrollable(this.domNode);
383 if(sv){
384 // When the user goes back to the unfiltered list, restore the previous
385 // scroll position stored for unfiltered list (as done by some native mobile apps).
386 // Otherwise, reset the scroll position, to ensure that the new subset of
387 // items is visible.
388 sv.scrollTo(this._filterBox.focusNode.value ?
389 {x:0, y:0} :
390 this._previousUnfilteredScrollPos || {x:0, y:0});
391 }
392 },
393
394 onFilter: function(/*===== results, query, options =====*/){
395 // summary:
396 // User-defined function to handle filter actions. If the function returns false,
397 // the filtering is cancelled.
398 // tags:
399 // callback
400 },
401
402 destroy: function(/*Boolean?*/ preserveDom){
403 // summary:
404 // Destroys the widget. If the list has created dojox/mobile/SearchBox
405 // or dojox/mobile/ScrollableView widgets, these widgets are also destroyed.
406 // preserveDom: Boolean
407 // If true, this method will leave the original DOM structure alone.
408
409 this.inherited(arguments);
410
411 // Only destroy widgets created (thus, owned) by this mixin (if any).
412 if(this._createdFilterBox){
413 this._createdFilterBox.destroy(preserveDom);
414 this._createdFilterBox = null;
415 }
416 if(this._createdScrollableView){
417 this._createdScrollableView.destroy(preserveDom);
418 this._createdScrollableView = null;
419 }
420 }
421 });
422 });