]> git.wh0rd.org - tt-rss.git/blob - lib/dijit/form/_TextBoxMixin.js.uncompressed.js
update dojo to 1.7.3
[tt-rss.git] / lib / dijit / form / _TextBoxMixin.js.uncompressed.js
1 define("dijit/form/_TextBoxMixin", [
2 "dojo/_base/array", // array.forEach
3 "dojo/_base/declare", // declare
4 "dojo/dom", // dom.byId
5 "dojo/_base/event", // event.stop
6 "dojo/keys", // keys.ALT keys.CAPS_LOCK keys.CTRL keys.META keys.SHIFT
7 "dojo/_base/lang", // lang.mixin
8 ".." // for exporting dijit._setSelectionRange, dijit.selectInputText
9 ], function(array, declare, dom, event, keys, lang, dijit){
10
11 // module:
12 // dijit/form/_TextBoxMixin
13 // summary:
14 // A mixin for textbox form input widgets
15
16 var _TextBoxMixin = declare("dijit.form._TextBoxMixin", null, {
17 // summary:
18 // A mixin for textbox form input widgets
19
20 // trim: Boolean
21 // Removes leading and trailing whitespace if true. Default is false.
22 trim: false,
23
24 // uppercase: Boolean
25 // Converts all characters to uppercase if true. Default is false.
26 uppercase: false,
27
28 // lowercase: Boolean
29 // Converts all characters to lowercase if true. Default is false.
30 lowercase: false,
31
32 // propercase: Boolean
33 // Converts the first character of each word to uppercase if true.
34 propercase: false,
35
36 // maxLength: String
37 // HTML INPUT tag maxLength declaration.
38 maxLength: "",
39
40 // selectOnClick: [const] Boolean
41 // If true, all text will be selected when focused with mouse
42 selectOnClick: false,
43
44 // placeHolder: String
45 // Defines a hint to help users fill out the input field (as defined in HTML 5).
46 // This should only contain plain text (no html markup).
47 placeHolder: "",
48
49 _getValueAttr: function(){
50 // summary:
51 // Hook so get('value') works as we like.
52 // description:
53 // For `dijit.form.TextBox` this basically returns the value of the <input>.
54 //
55 // For `dijit.form.MappedTextBox` subclasses, which have both
56 // a "displayed value" and a separate "submit value",
57 // This treats the "displayed value" as the master value, computing the
58 // submit value from it via this.parse().
59 return this.parse(this.get('displayedValue'), this.constraints);
60 },
61
62 _setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
63 // summary:
64 // Hook so set('value', ...) works.
65 //
66 // description:
67 // Sets the value of the widget to "value" which can be of
68 // any type as determined by the widget.
69 //
70 // value:
71 // The visual element value is also set to a corresponding,
72 // but not necessarily the same, value.
73 //
74 // formattedValue:
75 // If specified, used to set the visual element value,
76 // otherwise a computed visual value is used.
77 //
78 // priorityChange:
79 // If true, an onChange event is fired immediately instead of
80 // waiting for the next blur event.
81
82 var filteredValue;
83 if(value !== undefined){
84 // TODO: this is calling filter() on both the display value and the actual value.
85 // I added a comment to the filter() definition about this, but it should be changed.
86 filteredValue = this.filter(value);
87 if(typeof formattedValue != "string"){
88 if(filteredValue !== null && ((typeof filteredValue != "number") || !isNaN(filteredValue))){
89 formattedValue = this.filter(this.format(filteredValue, this.constraints));
90 }else{ formattedValue = ''; }
91 }
92 }
93 if(formattedValue != null && formattedValue != undefined && ((typeof formattedValue) != "number" || !isNaN(formattedValue)) && this.textbox.value != formattedValue){
94 this.textbox.value = formattedValue;
95 this._set("displayedValue", this.get("displayedValue"));
96 }
97
98 if(this.textDir == "auto"){
99 this.applyTextDir(this.focusNode, formattedValue);
100 }
101
102 this.inherited(arguments, [filteredValue, priorityChange]);
103 },
104
105 // displayedValue: String
106 // For subclasses like ComboBox where the displayed value
107 // (ex: Kentucky) and the serialized value (ex: KY) are different,
108 // this represents the displayed value.
109 //
110 // Setting 'displayedValue' through set('displayedValue', ...)
111 // updates 'value', and vice-versa. Otherwise 'value' is updated
112 // from 'displayedValue' periodically, like onBlur etc.
113 //
114 // TODO: move declaration to MappedTextBox?
115 // Problem is that ComboBox references displayedValue,
116 // for benefit of FilteringSelect.
117 displayedValue: "",
118
119 _getDisplayedValueAttr: function(){
120 // summary:
121 // Hook so get('displayedValue') works.
122 // description:
123 // Returns the displayed value (what the user sees on the screen),
124 // after filtering (ie, trimming spaces etc.).
125 //
126 // For some subclasses of TextBox (like ComboBox), the displayed value
127 // is different from the serialized value that's actually
128 // sent to the server (see dijit.form.ValidationTextBox.serialize)
129
130 // TODO: maybe we should update this.displayedValue on every keystroke so that we don't need
131 // this method
132 // TODO: this isn't really the displayed value when the user is typing
133 return this.filter(this.textbox.value);
134 },
135
136 _setDisplayedValueAttr: function(/*String*/ value){
137 // summary:
138 // Hook so set('displayedValue', ...) works.
139 // description:
140 // Sets the value of the visual element to the string "value".
141 // The widget value is also set to a corresponding,
142 // but not necessarily the same, value.
143
144 if(value === null || value === undefined){ value = '' }
145 else if(typeof value != "string"){ value = String(value) }
146
147 this.textbox.value = value;
148
149 // sets the serialized value to something corresponding to specified displayedValue
150 // (if possible), and also updates the textbox.value, for example converting "123"
151 // to "123.00"
152 this._setValueAttr(this.get('value'), undefined);
153
154 this._set("displayedValue", this.get('displayedValue'));
155
156 // textDir support
157 if(this.textDir == "auto"){
158 this.applyTextDir(this.focusNode, value);
159 }
160 },
161
162 format: function(value /*=====, constraints =====*/){
163 // summary:
164 // Replaceable function to convert a value to a properly formatted string.
165 // value: String
166 // constraints: Object
167 // tags:
168 // protected extension
169 return ((value == null || value == undefined) ? "" : (value.toString ? value.toString() : value));
170 },
171
172 parse: function(value /*=====, constraints =====*/){
173 // summary:
174 // Replaceable function to convert a formatted string to a value
175 // value: String
176 // constraints: Object
177 // tags:
178 // protected extension
179
180 return value; // String
181 },
182
183 _refreshState: function(){
184 // summary:
185 // After the user types some characters, etc., this method is
186 // called to check the field for validity etc. The base method
187 // in `dijit.form.TextBox` does nothing, but subclasses override.
188 // tags:
189 // protected
190 },
191
192 /*=====
193 onInput: function(event){
194 // summary:
195 // Connect to this function to receive notifications of various user data-input events.
196 // Return false to cancel the event and prevent it from being processed.
197 // event:
198 // keydown | keypress | cut | paste | input
199 // tags:
200 // callback
201 },
202 =====*/
203 onInput: function(){},
204
205 __skipInputEvent: false,
206 _onInput: function(){
207 // summary:
208 // Called AFTER the input event has happened
209 // set text direction according to textDir that was defined in creation
210 if(this.textDir == "auto"){
211 this.applyTextDir(this.focusNode, this.focusNode.value);
212 }
213
214 this._refreshState();
215
216 // In case someone is watch()'ing for changes to displayedValue
217 this._set("displayedValue", this.get("displayedValue"));
218 },
219
220 postCreate: function(){
221 // setting the value here is needed since value="" in the template causes "undefined"
222 // and setting in the DOM (instead of the JS object) helps with form reset actions
223 this.textbox.setAttribute("value", this.textbox.value); // DOM and JS values should be the same
224
225 this.inherited(arguments);
226
227 // normalize input events to reduce spurious event processing
228 // onkeydown: do not forward modifier keys
229 // set charOrCode to numeric keycode
230 // onkeypress: do not forward numeric charOrCode keys (already sent through onkeydown)
231 // onpaste & oncut: set charOrCode to 229 (IME)
232 // oninput: if primary event not already processed, set charOrCode to 229 (IME), else do not forward
233 var handleEvent = function(e){
234 var charCode = e.charOrCode || e.keyCode || 229;
235 if(e.type == "keydown"){
236 switch(charCode){ // ignore "state" keys
237 case keys.SHIFT:
238 case keys.ALT:
239 case keys.CTRL:
240 case keys.META:
241 case keys.CAPS_LOCK:
242 return;
243 default:
244 if(charCode >= 65 && charCode <= 90){ return; } // keydown for A-Z can be processed with keypress
245 }
246 }
247 if(e.type == "keypress" && typeof charCode != "string"){ return; }
248 if(e.type == "input"){
249 if(this.__skipInputEvent){ // duplicate event
250 this.__skipInputEvent = false;
251 return;
252 }
253 }else{
254 this.__skipInputEvent = true;
255 }
256 // create fake event to set charOrCode and to know if preventDefault() was called
257 var faux = lang.mixin({}, e, {
258 charOrCode: charCode,
259 wasConsumed: false,
260 preventDefault: function(){
261 faux.wasConsumed = true;
262 e.preventDefault();
263 },
264 stopPropagation: function(){ e.stopPropagation(); }
265 });
266 // give web page author a chance to consume the event
267 if(this.onInput(faux) === false){
268 event.stop(faux); // return false means stop
269 }
270 if(faux.wasConsumed){ return; } // if preventDefault was called
271 setTimeout(lang.hitch(this, "_onInput", faux), 0); // widget notification after key has posted
272 };
273 array.forEach([ "onkeydown", "onkeypress", "onpaste", "oncut", "oninput", "oncompositionend" ], function(event){
274 this.connect(this.textbox, event, handleEvent);
275 }, this);
276 },
277
278 _blankValue: '', // if the textbox is blank, what value should be reported
279 filter: function(val){
280 // summary:
281 // Auto-corrections (such as trimming) that are applied to textbox
282 // value on blur or form submit.
283 // description:
284 // For MappedTextBox subclasses, this is called twice
285 // - once with the display value
286 // - once the value as set/returned by set('value', ...)
287 // and get('value'), ex: a Number for NumberTextBox.
288 //
289 // In the latter case it does corrections like converting null to NaN. In
290 // the former case the NumberTextBox.filter() method calls this.inherited()
291 // to execute standard trimming code in TextBox.filter().
292 //
293 // TODO: break this into two methods in 2.0
294 //
295 // tags:
296 // protected extension
297 if(val === null){ return this._blankValue; }
298 if(typeof val != "string"){ return val; }
299 if(this.trim){
300 val = lang.trim(val);
301 }
302 if(this.uppercase){
303 val = val.toUpperCase();
304 }
305 if(this.lowercase){
306 val = val.toLowerCase();
307 }
308 if(this.propercase){
309 val = val.replace(/[^\s]+/g, function(word){
310 return word.substring(0,1).toUpperCase() + word.substring(1);
311 });
312 }
313 return val;
314 },
315
316 _setBlurValue: function(){
317 this._setValueAttr(this.get('value'), true);
318 },
319
320 _onBlur: function(e){
321 if(this.disabled){ return; }
322 this._setBlurValue();
323 this.inherited(arguments);
324
325 if(this._selectOnClickHandle){
326 this.disconnect(this._selectOnClickHandle);
327 }
328 },
329
330 _isTextSelected: function(){
331 return this.textbox.selectionStart == this.textbox.selectionEnd;
332 },
333
334 _onFocus: function(/*String*/ by){
335 if(this.disabled || this.readOnly){ return; }
336
337 // Select all text on focus via click if nothing already selected.
338 // Since mouse-up will clear the selection need to defer selection until after mouse-up.
339 // Don't do anything on focus by tabbing into the widget since there's no associated mouse-up event.
340 if(this.selectOnClick && by == "mouse"){
341 this._selectOnClickHandle = this.connect(this.domNode, "onmouseup", function(){
342 // Only select all text on first click; otherwise users would have no way to clear
343 // the selection.
344 this.disconnect(this._selectOnClickHandle);
345
346 // Check if the user selected some text manually (mouse-down, mouse-move, mouse-up)
347 // and if not, then select all the text
348 if(this._isTextSelected()){
349 _TextBoxMixin.selectInputText(this.textbox);
350 }
351 });
352 }
353 // call this.inherited() before refreshState(), since this.inherited() will possibly scroll the viewport
354 // (to scroll the TextBox into view), which will affect how _refreshState() positions the tooltip
355 this.inherited(arguments);
356
357 this._refreshState();
358 },
359
360 reset: function(){
361 // Overrides dijit._FormWidget.reset().
362 // Additionally resets the displayed textbox value to ''
363 this.textbox.value = '';
364 this.inherited(arguments);
365 },
366 _setTextDirAttr: function(/*String*/ textDir){
367 // summary:
368 // Setter for textDir.
369 // description:
370 // Users shouldn't call this function; they should be calling
371 // set('textDir', value)
372 // tags:
373 // private
374
375 // only if new textDir is different from the old one
376 // and on widgets creation.
377 if(!this._created
378 || this.textDir != textDir){
379 this._set("textDir", textDir);
380 // so the change of the textDir will take place immediately.
381 this.applyTextDir(this.focusNode, this.focusNode.value);
382 }
383 }
384 });
385
386
387 _TextBoxMixin._setSelectionRange = dijit._setSelectionRange = function(/*DomNode*/ element, /*Number?*/ start, /*Number?*/ stop){
388 if(element.setSelectionRange){
389 element.setSelectionRange(start, stop);
390 }
391 };
392
393 _TextBoxMixin.selectInputText = dijit.selectInputText = function(/*DomNode*/ element, /*Number?*/ start, /*Number?*/ stop){
394 // summary:
395 // Select text in the input element argument, from start (default 0), to stop (default end).
396
397 // TODO: use functions in _editor/selection.js?
398 element = dom.byId(element);
399 if(isNaN(start)){ start = 0; }
400 if(isNaN(stop)){ stop = element.value ? element.value.length : 0; }
401 try{
402 element.focus();
403 _TextBoxMixin._setSelectionRange(element, start, stop);
404 }catch(e){ /* squelch random errors (esp. on IE) from unexpected focus changes or DOM nodes being hidden */ }
405 };
406
407 return _TextBoxMixin;
408 });