]>
git.wh0rd.org - tt-rss.git/blob - 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
){
12 // dijit/form/_TextBoxMixin
14 // A mixin for textbox form input widgets
16 var _TextBoxMixin
= declare("dijit.form._TextBoxMixin", null, {
18 // A mixin for textbox form input widgets
21 // Removes leading and trailing whitespace if true. Default is false.
25 // Converts all characters to uppercase if true. Default is false.
29 // Converts all characters to lowercase if true. Default is false.
32 // propercase: Boolean
33 // Converts the first character of each word to uppercase if true.
37 // HTML INPUT tag maxLength declaration.
40 // selectOnClick: [const] Boolean
41 // If true, all text will be selected when focused with mouse
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).
49 _getValueAttr: function(){
51 // Hook so get('value') works as we like.
53 // For `dijit.form.TextBox` this basically returns the value of the <input>.
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
);
62 _setValueAttr: function(value
, /*Boolean?*/ priorityChange
, /*String?*/ formattedValue
){
64 // Hook so set('value', ...) works.
67 // Sets the value of the widget to "value" which can be of
68 // any type as determined by the widget.
71 // The visual element value is also set to a corresponding,
72 // but not necessarily the same, value.
75 // If specified, used to set the visual element value,
76 // otherwise a computed visual value is used.
79 // If true, an onChange event is fired immediately instead of
80 // waiting for the next blur event.
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
= ''; }
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"));
98 if(this.textDir
== "auto"){
99 this.applyTextDir(this.focusNode
, formattedValue
);
102 this.inherited(arguments
, [filteredValue
, priorityChange
]);
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.
110 // Setting 'displayedValue' through set('displayedValue', ...)
111 // updates 'value', and vice-versa. Otherwise 'value' is updated
112 // from 'displayedValue' periodically, like onBlur etc.
114 // TODO: move declaration to MappedTextBox?
115 // Problem is that ComboBox references displayedValue,
116 // for benefit of FilteringSelect.
119 _getDisplayedValueAttr: function(){
121 // Hook so get('displayedValue') works.
123 // Returns the displayed value (what the user sees on the screen),
124 // after filtering (ie, trimming spaces etc.).
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)
130 // TODO: maybe we should update this.displayedValue on every keystroke so that we don't need
132 // TODO: this isn't really the displayed value when the user is typing
133 return this.filter(this.textbox
.value
);
136 _setDisplayedValueAttr: function(/*String*/ value
){
138 // Hook so set('displayedValue', ...) works.
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.
144 if(value
=== null || value
=== undefined){ value
= '' }
145 else if(typeof value
!= "string"){ value
= String(value
) }
147 this.textbox
.value
= value
;
149 // sets the serialized value to something corresponding to specified displayedValue
150 // (if possible), and also updates the textbox.value, for example converting "123"
152 this._setValueAttr(this.get('value'), undefined);
154 this._set("displayedValue", this.get('displayedValue'));
157 if(this.textDir
== "auto"){
158 this.applyTextDir(this.focusNode
, value
);
162 format: function(value
/*=====, constraints =====*/){
164 // Replaceable function to convert a value to a properly formatted string.
166 // constraints: Object
168 // protected extension
169 return ((value
== null || value
== undefined) ? "" : (value
.toString
? value
.toString() : value
));
172 parse: function(value
/*=====, constraints =====*/){
174 // Replaceable function to convert a formatted string to a value
176 // constraints: Object
178 // protected extension
180 return value
; // String
183 _refreshState: function(){
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.
193 onInput: function(event){
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.
198 // keydown | keypress | cut | paste | input
203 onInput: function(){},
205 __skipInputEvent
: false,
206 _onInput: function(){
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
);
214 this._refreshState();
216 // In case someone is watch()'ing for changes to displayedValue
217 this._set("displayedValue", this.get("displayedValue"));
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
225 this.inherited(arguments
);
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
244 if(charCode
>= 65 && charCode
<= 90){ return; } // keydown for A-Z can be processed with keypress
247 if(e
.type
== "keypress" && typeof charCode
!= "string"){ return; }
248 if(e
.type
== "input"){
249 if(this.__skipInputEvent
){ // duplicate event
250 this.__skipInputEvent
= false;
254 this.__skipInputEvent
= true;
256 // create fake event to set charOrCode and to know if preventDefault() was called
257 var faux
= lang
.mixin({}, e
, {
258 charOrCode
: charCode
,
260 preventDefault: function(){
261 faux
.wasConsumed
= true;
264 stopPropagation: function(){ e
.stopPropagation(); }
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
270 if(faux
.wasConsumed
){ return; } // if preventDefault was called
271 setTimeout(lang
.hitch(this, "_onInput", faux
), 0); // widget notification after key has posted
273 array
.forEach([ "onkeydown", "onkeypress", "onpaste", "oncut", "oninput", "oncompositionend" ], function(event
){
274 this.connect(this.textbox
, event
, handleEvent
);
278 _blankValue
: '', // if the textbox is blank, what value should be reported
279 filter: function(val
){
281 // Auto-corrections (such as trimming) that are applied to textbox
282 // value on blur or form submit.
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.
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().
293 // TODO: break this into two methods in 2.0
296 // protected extension
297 if(val
=== null){ return this._blankValue
; }
298 if(typeof val
!= "string"){ return val
; }
300 val
= lang
.trim(val
);
303 val
= val
.toUpperCase();
306 val
= val
.toLowerCase();
309 val
= val
.replace(/[^\s]+/g, function(word
){
310 return word
.substring(0,1).toUpperCase() + word
.substring(1);
316 _setBlurValue: function(){
317 this._setValueAttr(this.get('value'), true);
320 _onBlur: function(e
){
321 if(this.disabled
){ return; }
322 this._setBlurValue();
323 this.inherited(arguments
);
325 if(this._selectOnClickHandle
){
326 this.disconnect(this._selectOnClickHandle
);
330 _isTextSelected: function(){
331 return this.textbox
.selectionStart
== this.textbox
.selectionEnd
;
334 _onFocus: function(/*String*/ by
){
335 if(this.disabled
|| this.readOnly
){ return; }
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
344 this.disconnect(this._selectOnClickHandle
);
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
);
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
);
357 this._refreshState();
361 // Overrides dijit._FormWidget.reset().
362 // Additionally resets the displayed textbox value to ''
363 this.textbox
.value
= '';
364 this.inherited(arguments
);
366 _setTextDirAttr: function(/*String*/ textDir
){
368 // Setter for textDir.
370 // Users shouldn't call this function; they should be calling
371 // set('textDir', value)
375 // only if new textDir is different from the old one
376 // and on widgets creation.
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
);
387 _TextBoxMixin
._setSelectionRange
= dijit
._setSelectionRange = function(/*DomNode*/ element
, /*Number?*/ start
, /*Number?*/ stop
){
388 if(element
.setSelectionRange
){
389 element
.setSelectionRange(start
, stop
);
393 _TextBoxMixin
.selectInputText
= dijit
.selectInputText = function(/*DomNode*/ element
, /*Number?*/ start
, /*Number?*/ stop
){
395 // Select text in the input element argument, from start (default 0), to stop (default end).
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; }
403 _TextBoxMixin
._setSelectionRange(element
, start
, stop
);
404 }catch(e
){ /* squelch random errors (esp. on IE) from unexpected focus changes or DOM nodes being hidden */ }
407 return _TextBoxMixin
;