]> git.wh0rd.org - tt-rss.git/blob - lib/dijit/form/TextBox.js
upgrade Dojo to 1.6.1
[tt-rss.git] / lib / dijit / form / TextBox.js
1 /*
2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5 */
6
7
8 if(!dojo._hasResource["dijit.form.TextBox"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dijit.form.TextBox"] = true;
10 dojo.provide("dijit.form.TextBox");
11 dojo.require("dijit.form._FormWidget");
12
13
14 dojo.declare(
15 "dijit.form.TextBox",
16 dijit.form._FormValueWidget,
17 {
18 // summary:
19 // A base class for textbox form inputs
20
21 // trim: Boolean
22 // Removes leading and trailing whitespace if true. Default is false.
23 trim: false,
24
25 // uppercase: Boolean
26 // Converts all characters to uppercase if true. Default is false.
27 uppercase: false,
28
29 // lowercase: Boolean
30 // Converts all characters to lowercase if true. Default is false.
31 lowercase: false,
32
33 // propercase: Boolean
34 // Converts the first character of each word to uppercase if true.
35 propercase: false,
36
37 // maxLength: String
38 // HTML INPUT tag maxLength declaration.
39 maxLength: "",
40
41 // selectOnClick: [const] Boolean
42 // If true, all text will be selected when focused with mouse
43 selectOnClick: false,
44
45 // placeHolder: String
46 // Defines a hint to help users fill out the input field (as defined in HTML 5).
47 // This should only contain plain text (no html markup).
48 placeHolder: "",
49
50 templateString: dojo.cache("dijit.form", "templates/TextBox.html", "<div class=\"dijit dijitReset dijitInline dijitLeft\" id=\"widget_${id}\" role=\"presentation\"\n\t><div class=\"dijitReset dijitInputField dijitInputContainer\"\n\t\t><input class=\"dijitReset dijitInputInner\" dojoAttachPoint='textbox,focusNode' autocomplete=\"off\"\n\t\t\t${!nameAttrSetting} type='${type}'\n\t/></div\n></div>\n"),
51 _singleNodeTemplate: '<input class="dijit dijitReset dijitLeft dijitInputField" dojoAttachPoint="textbox,focusNode" autocomplete="off" type="${type}" ${!nameAttrSetting} />',
52
53 _buttonInputDisabled: dojo.isIE ? "disabled" : "", // allows IE to disallow focus, but Firefox cannot be disabled for mousedown events
54
55 baseClass: "dijitTextBox",
56
57 attributeMap: dojo.delegate(dijit.form._FormValueWidget.prototype.attributeMap, {
58 maxLength: "focusNode"
59 }),
60
61 postMixInProperties: function(){
62 var type = this.type.toLowerCase();
63 if(this.templateString && this.templateString.toLowerCase() == "input" || ((type == "hidden" || type == "file") && this.templateString == dijit.form.TextBox.prototype.templateString)){
64 this.templateString = this._singleNodeTemplate;
65 }
66 this.inherited(arguments);
67 },
68
69 _setPlaceHolderAttr: function(v){
70 this._set("placeHolder", v);
71 if(!this._phspan){
72 this._attachPoints.push('_phspan');
73 /* dijitInputField class gives placeHolder same padding as the input field
74 * parent node already has dijitInputField class but it doesn't affect this <span>
75 * since it's position: absolute.
76 */
77 this._phspan = dojo.create('span',{className:'dijitPlaceHolder dijitInputField'},this.textbox,'after');
78 }
79 this._phspan.innerHTML="";
80 this._phspan.appendChild(document.createTextNode(v));
81
82 this._updatePlaceHolder();
83 },
84
85 _updatePlaceHolder: function(){
86 if(this._phspan){
87 this._phspan.style.display=(this.placeHolder&&!this._focused&&!this.textbox.value)?"":"none";
88 }
89 },
90
91 _getValueAttr: function(){
92 // summary:
93 // Hook so get('value') works as we like.
94 // description:
95 // For `dijit.form.TextBox` this basically returns the value of the <input>.
96 //
97 // For `dijit.form.MappedTextBox` subclasses, which have both
98 // a "displayed value" and a separate "submit value",
99 // This treats the "displayed value" as the master value, computing the
100 // submit value from it via this.parse().
101 return this.parse(this.get('displayedValue'), this.constraints);
102 },
103
104 _setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
105 // summary:
106 // Hook so set('value', ...) works.
107 //
108 // description:
109 // Sets the value of the widget to "value" which can be of
110 // any type as determined by the widget.
111 //
112 // value:
113 // The visual element value is also set to a corresponding,
114 // but not necessarily the same, value.
115 //
116 // formattedValue:
117 // If specified, used to set the visual element value,
118 // otherwise a computed visual value is used.
119 //
120 // priorityChange:
121 // If true, an onChange event is fired immediately instead of
122 // waiting for the next blur event.
123
124 var filteredValue;
125 if(value !== undefined){
126 // TODO: this is calling filter() on both the display value and the actual value.
127 // I added a comment to the filter() definition about this, but it should be changed.
128 filteredValue = this.filter(value);
129 if(typeof formattedValue != "string"){
130 if(filteredValue !== null && ((typeof filteredValue != "number") || !isNaN(filteredValue))){
131 formattedValue = this.filter(this.format(filteredValue, this.constraints));
132 }else{ formattedValue = ''; }
133 }
134 }
135 if(formattedValue != null && formattedValue != undefined && ((typeof formattedValue) != "number" || !isNaN(formattedValue)) && this.textbox.value != formattedValue){
136 this.textbox.value = formattedValue;
137 this._set("displayedValue", this.get("displayedValue"));
138 }
139
140 this._updatePlaceHolder();
141
142 this.inherited(arguments, [filteredValue, priorityChange]);
143 },
144
145 // displayedValue: String
146 // For subclasses like ComboBox where the displayed value
147 // (ex: Kentucky) and the serialized value (ex: KY) are different,
148 // this represents the displayed value.
149 //
150 // Setting 'displayedValue' through set('displayedValue', ...)
151 // updates 'value', and vice-versa. Otherwise 'value' is updated
152 // from 'displayedValue' periodically, like onBlur etc.
153 //
154 // TODO: move declaration to MappedTextBox?
155 // Problem is that ComboBox references displayedValue,
156 // for benefit of FilteringSelect.
157 displayedValue: "",
158
159 getDisplayedValue: function(){
160 // summary:
161 // Deprecated. Use get('displayedValue') instead.
162 // tags:
163 // deprecated
164 dojo.deprecated(this.declaredClass+"::getDisplayedValue() is deprecated. Use set('displayedValue') instead.", "", "2.0");
165 return this.get('displayedValue');
166 },
167
168 _getDisplayedValueAttr: function(){
169 // summary:
170 // Hook so get('displayedValue') works.
171 // description:
172 // Returns the displayed value (what the user sees on the screen),
173 // after filtering (ie, trimming spaces etc.).
174 //
175 // For some subclasses of TextBox (like ComboBox), the displayed value
176 // is different from the serialized value that's actually
177 // sent to the server (see dijit.form.ValidationTextBox.serialize)
178
179 // TODO: maybe we should update this.displayedValue on every keystroke so that we don't need
180 // this method
181 // TODO: this isn't really the displayed value when the user is typing
182 return this.filter(this.textbox.value);
183 },
184
185 setDisplayedValue: function(/*String*/ value){
186 // summary:
187 // Deprecated. Use set('displayedValue', ...) instead.
188 // tags:
189 // deprecated
190 dojo.deprecated(this.declaredClass+"::setDisplayedValue() is deprecated. Use set('displayedValue', ...) instead.", "", "2.0");
191 this.set('displayedValue', value);
192 },
193
194 _setDisplayedValueAttr: function(/*String*/ value){
195 // summary:
196 // Hook so set('displayedValue', ...) works.
197 // description:
198 // Sets the value of the visual element to the string "value".
199 // The widget value is also set to a corresponding,
200 // but not necessarily the same, value.
201
202 if(value === null || value === undefined){ value = '' }
203 else if(typeof value != "string"){ value = String(value) }
204
205 this.textbox.value = value;
206
207 // sets the serialized value to something corresponding to specified displayedValue
208 // (if possible), and also updates the textbox.value, for example converting "123"
209 // to "123.00"
210 this._setValueAttr(this.get('value'), undefined);
211
212 this._set("displayedValue", this.get('displayedValue'));
213 },
214
215 format: function(/*String*/ value, /*Object*/ constraints){
216 // summary:
217 // Replacable function to convert a value to a properly formatted string.
218 // tags:
219 // protected extension
220 return ((value == null || value == undefined) ? "" : (value.toString ? value.toString() : value));
221 },
222
223 parse: function(/*String*/ value, /*Object*/ constraints){
224 // summary:
225 // Replacable function to convert a formatted string to a value
226 // tags:
227 // protected extension
228
229 return value; // String
230 },
231
232 _refreshState: function(){
233 // summary:
234 // After the user types some characters, etc., this method is
235 // called to check the field for validity etc. The base method
236 // in `dijit.form.TextBox` does nothing, but subclasses override.
237 // tags:
238 // protected
239 },
240
241 _onInput: function(e){
242 if(e && e.type && /key/i.test(e.type) && e.keyCode){
243 switch(e.keyCode){
244 case dojo.keys.SHIFT:
245 case dojo.keys.ALT:
246 case dojo.keys.CTRL:
247 case dojo.keys.TAB:
248 return;
249 }
250 }
251 if(this.intermediateChanges){
252 var _this = this;
253 // the setTimeout allows the key to post to the widget input box
254 setTimeout(function(){ _this._handleOnChange(_this.get('value'), false); }, 0);
255 }
256 this._refreshState();
257
258 // In case someone is watch()'ing for changes to displayedValue
259 this._set("displayedValue", this.get("displayedValue"));
260 },
261
262 postCreate: function(){
263 if(dojo.isIE){ // IE INPUT tag fontFamily has to be set directly using STYLE
264 // the setTimeout gives IE a chance to render the TextBox and to deal with font inheritance
265 setTimeout(dojo.hitch(this, function(){
266 var s = dojo.getComputedStyle(this.domNode);
267 if(s){
268 var ff = s.fontFamily;
269 if(ff){
270 var inputs = this.domNode.getElementsByTagName("INPUT");
271 if(inputs){
272 for(var i=0; i < inputs.length; i++){
273 inputs[i].style.fontFamily = ff;
274 }
275 }
276 }
277 }
278 }), 0);
279 }
280
281 // setting the value here is needed since value="" in the template causes "undefined"
282 // and setting in the DOM (instead of the JS object) helps with form reset actions
283 this.textbox.setAttribute("value", this.textbox.value); // DOM and JS values should be the same
284
285 this.inherited(arguments);
286
287 if(dojo.isMoz || dojo.isOpera){
288 this.connect(this.textbox, "oninput", "_onInput");
289 }else{
290 this.connect(this.textbox, "onkeydown", "_onInput");
291 this.connect(this.textbox, "onkeyup", "_onInput");
292 this.connect(this.textbox, "onpaste", "_onInput");
293 this.connect(this.textbox, "oncut", "_onInput");
294 }
295 },
296
297 _blankValue: '', // if the textbox is blank, what value should be reported
298 filter: function(val){
299 // summary:
300 // Auto-corrections (such as trimming) that are applied to textbox
301 // value on blur or form submit.
302 // description:
303 // For MappedTextBox subclasses, this is called twice
304 // - once with the display value
305 // - once the value as set/returned by set('value', ...)
306 // and get('value'), ex: a Number for NumberTextBox.
307 //
308 // In the latter case it does corrections like converting null to NaN. In
309 // the former case the NumberTextBox.filter() method calls this.inherited()
310 // to execute standard trimming code in TextBox.filter().
311 //
312 // TODO: break this into two methods in 2.0
313 //
314 // tags:
315 // protected extension
316 if(val === null){ return this._blankValue; }
317 if(typeof val != "string"){ return val; }
318 if(this.trim){
319 val = dojo.trim(val);
320 }
321 if(this.uppercase){
322 val = val.toUpperCase();
323 }
324 if(this.lowercase){
325 val = val.toLowerCase();
326 }
327 if(this.propercase){
328 val = val.replace(/[^\s]+/g, function(word){
329 return word.substring(0,1).toUpperCase() + word.substring(1);
330 });
331 }
332 return val;
333 },
334
335 _setBlurValue: function(){
336 this._setValueAttr(this.get('value'), true);
337 },
338
339 _onBlur: function(e){
340 if(this.disabled){ return; }
341 this._setBlurValue();
342 this.inherited(arguments);
343
344 if(this._selectOnClickHandle){
345 this.disconnect(this._selectOnClickHandle);
346 }
347 if(this.selectOnClick && dojo.isMoz){
348 this.textbox.selectionStart = this.textbox.selectionEnd = undefined; // clear selection so that the next mouse click doesn't reselect
349 }
350
351 this._updatePlaceHolder();
352 },
353
354 _onFocus: function(/*String*/ by){
355 if(this.disabled || this.readOnly){ return; }
356
357 // Select all text on focus via click if nothing already selected.
358 // Since mouse-up will clear the selection need to defer selection until after mouse-up.
359 // Don't do anything on focus by tabbing into the widget since there's no associated mouse-up event.
360 if(this.selectOnClick && by == "mouse"){
361 this._selectOnClickHandle = this.connect(this.domNode, "onmouseup", function(){
362 // Only select all text on first click; otherwise users would have no way to clear
363 // the selection.
364 this.disconnect(this._selectOnClickHandle);
365
366 // Check if the user selected some text manually (mouse-down, mouse-move, mouse-up)
367 // and if not, then select all the text
368 var textIsNotSelected;
369 if(dojo.isIE){
370 var range = dojo.doc.selection.createRange();
371 var parent = range.parentElement();
372 textIsNotSelected = parent == this.textbox && range.text.length == 0;
373 }else{
374 textIsNotSelected = this.textbox.selectionStart == this.textbox.selectionEnd;
375 }
376 if(textIsNotSelected){
377 dijit.selectInputText(this.textbox);
378 }
379 });
380 }
381
382 this._updatePlaceHolder();
383
384 // call this.inherited() before refreshState(), since this.inherited() will possibly scroll the viewport
385 // (to scroll the TextBox into view), which will affect how _refreshState() positions the tooltip
386 this.inherited(arguments);
387
388 this._refreshState();
389 },
390
391 reset: function(){
392 // Overrides dijit._FormWidget.reset().
393 // Additionally resets the displayed textbox value to ''
394 this.textbox.value = '';
395 this.inherited(arguments);
396 }
397 }
398 );
399
400 dijit.selectInputText = function(/*DomNode*/ element, /*Number?*/ start, /*Number?*/ stop){
401 // summary:
402 // Select text in the input element argument, from start (default 0), to stop (default end).
403
404 // TODO: use functions in _editor/selection.js?
405 var _window = dojo.global;
406 var _document = dojo.doc;
407 element = dojo.byId(element);
408 if(isNaN(start)){ start = 0; }
409 if(isNaN(stop)){ stop = element.value ? element.value.length : 0; }
410 dijit.focus(element);
411 if(_document["selection"] && dojo.body()["createTextRange"]){ // IE
412 if(element.createTextRange){
413 var r = element.createTextRange();
414 r.collapse(true);
415 r.moveStart("character", -99999); // move to 0
416 r.moveStart("character", start); // delta from 0 is the correct position
417 r.moveEnd("character", stop-start);
418 r.select();
419 }
420 }else if(_window["getSelection"]){
421 if(element.setSelectionRange){
422 element.setSelectionRange(start, stop);
423 }
424 }
425 };
426
427 }