Debian Codegraph @

dijit/form/FilteringSelect.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare", // declare
3 "dojo/_base/lang", // lang.mixin
4 "dojo/when",
5 "./MappedTextBox",
6 "./ComboBoxMixin"
7 ], function(declare, lang, when, MappedTextBox, ComboBoxMixin){
8
9 // module:
10 // dijit/form/FilteringSelect
11
12 return declare("dijit.form.FilteringSelect", [MappedTextBox, ComboBoxMixin], {
13 // summary:
14 // An enhanced version of the HTML SELECT tag, populated dynamically
15 //
16 // description:
17 // An enhanced version of the HTML SELECT tag, populated dynamically. It works
18 // very nicely with very large data sets because it can load and page data as needed.
19 // It also resembles ComboBox, but does not allow values outside of the provided ones.
20 // If OPTION tags are used as the data provider via markup, then the
21 // OPTION tag's child text node is used as the displayed value when selected
22 // while the OPTION tag's value attribute is used as the widget value on form submit.
23 // To set the default value when using OPTION tags, specify the selected
24 // attribute on 1 of the child OPTION tags.
25 //
26 // Similar features:
27 //
28 // - There is a drop down list of possible values.
29 // - You can only enter a value from the drop down list. (You can't
30 // enter an arbitrary value.)
31 // - The value submitted with the form is the hidden value (ex: CA),
32 // not the displayed value a.k.a. label (ex: California)
33 //
34 // Enhancements over plain HTML version:
35 //
36 // - If you type in some text then it will filter down the list of
37 // possible values in the drop down list.
38 // - List can be specified either as a static list or via a javascript
39 // function (that can get the list from a server)
40
41 // required: Boolean
42 // True (default) if user is required to enter a value into this field.
43 required: true,
44
45 _lastDisplayedValue: "",
46
47 _isValidSubset: function(){
48 return this._opened;
49 },
50
51 isValid: function(){
52 // Overrides ValidationTextBox.isValid()
53 return !!this.item || (!this.required && this.get('displayedValue') == ""); // #5974
54 },
55
56 _refreshState: function(){
57 if(!this.searchTimer){ // state will be refreshed after results are returned
58 this.inherited(arguments);
59 }
60 },
61
62 _callbackSetLabel: function(
63 /*Array*/ result,
64 /*Object*/ query,
65 /*Object*/ options,
66 /*Boolean?*/ priorityChange){
67 // summary:
68 // Callback from dojo.store after lookup of user entered value finishes
69
70 // setValue does a synchronous lookup,
71 // so it calls _callbackSetLabel directly,
72 // and so does not pass dataObject
73 // still need to test against _lastQuery in case it came too late
74 if((query && query[this.searchAttr] !== this._lastQuery) || (!query && result.length && this.store.getIdentity(result[0]) != this._lastQuery)){
75 return;
76 }
77 if(!result.length){
78 //#3268: don't modify display value on bad input
79 //#3285: change CSS to indicate error
80 this.set("value", '', priorityChange || (priorityChange === undefined && !this.focused), this.textbox.value, null);
81 }else{
82 this.set('item', result[0], priorityChange);
83 }
84 },
85
86 _openResultList: function(/*Object*/ results, /*Object*/ query, /*Object*/ options){
87 // Callback when a data store query completes.
88 // Overrides ComboBox._openResultList()
89
90 // #3285: tap into search callback to see if user's query resembles a match
91 if(query[this.searchAttr] !== this._lastQuery){
92 return;
93 }
94 this.inherited(arguments);
95
96 if(this.item === undefined){ // item == undefined for keyboard search
97 // If the search returned no items that means that the user typed
98 // in something invalid (and they can't make it valid by typing more characters),
99 // so flag the FilteringSelect as being in an invalid state
100 this.validate(true);
101 }
102 },
103
104 _getValueAttr: function(){
105 // summary:
106 // Hook for get('value') to work.
107
108 // don't get the textbox value but rather the previously set hidden value.
109 // Use this.valueNode.value which isn't always set for other MappedTextBox widgets until blur
110 return this.valueNode.value;
111 },
112
113 _getValueField: function(){
114 // Overrides ComboBox._getValueField()
115 return "value";
116 },
117
118 _setValueAttr: function(/*String*/ value, /*Boolean?*/ priorityChange, /*String?*/ displayedValue, /*item?*/ item){
119 // summary:
120 // Hook so set('value', value) works.
121 // description:
122 // Sets the value of the select.
123 // Also sets the label to the corresponding value by reverse lookup.
124 if(!this._onChangeActive){ priorityChange = null; }
125
126 if(item === undefined){
127 if(value === null || value === ''){
128 value = '';
129 if(!lang.isString(displayedValue)){
130 this._setDisplayedValueAttr(displayedValue||'', priorityChange);
131 return;
132 }
133 }
134
135 var self = this;
136 this._lastQuery = value;
137 when(this.store.get(value), function(item){
138 self._callbackSetLabel(item? [item] : [], undefined, undefined, priorityChange);
139 });
140 }else{
141 this.valueNode.value = value;
142 this.inherited(arguments, [value, priorityChange, displayedValue, item]);
143 }
144 },
145
146 _setItemAttr: function(/*item*/ item, /*Boolean?*/ priorityChange, /*String?*/ displayedValue){
147 // summary:
148 // Set the displayed valued in the input box, and the hidden value
149 // that gets submitted, based on a dojo.data store item.
150 // description:
151 // Users shouldn't call this function; they should be calling
152 // set('item', value)
153 // tags:
154 // private
155 this.inherited(arguments);
156 this._lastDisplayedValue = this.textbox.value;
157 },
158
159 _getDisplayQueryString: function(/*String*/ text){
160 return text.replace(/([\\\*\?])/g, "\\$1");
161 },
162
163 _setDisplayedValueAttr: function(/*String*/ label, /*Boolean?*/ priorityChange){
164 // summary:
165 // Hook so set('displayedValue', label) works.
166 // description:
167 // Sets textbox to display label. Also performs reverse lookup
168 // to set the hidden value. label should corresponding to item.searchAttr.
169
170 if(label == null){ label = ''; }
171
172 // This is called at initialization along with every custom setter.
173 // Usually (or always?) the call can be ignored. If it needs to be
174 // processed then at least make sure that the XHR request doesn't trigger an onChange()
175 // event, even if it returns after creation has finished
176 if(!this._created){
177 if(!("displayedValue" in this.params)){
178 return;
179 }
180 priorityChange = false;
181 }
182
183 // Do a reverse lookup to map the specified displayedValue to the hidden value.
184 // Note that if there's a custom labelFunc() this code
185 if(this.store){
186 this.closeDropDown();
187 var query = lang.clone(this.query); // #6196: populate query with user-specifics
188
189 // Generate query
190 var qs = this._getDisplayQueryString(label), q;
191 if(this.store._oldAPI){
192 // remove this branch for 2.0
193 q = qs;
194 }else{
195 // Query on searchAttr is a regex for benefit of dojo/store/Memory,
196 // but with a toString() method to help dojo/store/JsonRest.
197 // Search string like "Co*" converted to regex like /^Co.*$/i.
198 q = this._patternToRegExp(qs);
199 q.toString = function(){ return qs; };
200 }
201 this._lastQuery = query[this.searchAttr] = q;
202
203 // If the label is not valid, the callback will never set it,
204 // so the last valid value will get the warning textbox. Set the
205 // textbox value now so that the impending warning will make
206 // sense to the user
207 this.textbox.value = label;
208 this._lastDisplayedValue = label;
209 this._set("displayedValue", label); // for watch("displayedValue") notification
210 var _this = this;
211 var options = {
212 queryOptions: {
213 ignoreCase: this.ignoreCase,
214 deep: true
215 }
216 };
217 lang.mixin(options, this.fetchProperties);
218 this._fetchHandle = this.store.query(query, options);
219 when(this._fetchHandle, function(result){
220 _this._fetchHandle = null;
221 _this._callbackSetLabel(result || [], query, options, priorityChange);
222 }, function(err){
223 _this._fetchHandle = null;
224 if(!_this._cancelingQuery){ // don't treat canceled query as an error
225 console.error('dijit.form.FilteringSelect: ' + err.toString());
226 }
227 });
228 }
229 },
230
231 undo: function(){
232 this.set('displayedValue', this._lastDisplayedValue);
233 }
234 });
235 });