For `dijit.form.TextBox` this basically returns the value of the <input>.
For `dijit.form.MappedTextBox` subclasses, which have both
a "displayed value" and a separate "submit value",
This treats the "displayed value" as the master value, computing the
submit value from it via this.parse().
dojox.mobile.app.TextBox._setValueAttr
type
Function
parameters:
value: (typeof The)
visual element value is also set to a corresponding,
but not necessarily the same, value.
priorityChange: (typeof Boolean)
If true, an onChange event is fired immediately instead of
waiting for the next blur event.
formattedValue: (typeof String)
If specified, used to set the visual element value,
otherwise a computed visual value is used.
var filteredValue; if(value !== undefined){ // TODO: this is calling filter() on both the display value and the actual value. // I added a comment to the filter() definition about this, but it should be changed. filteredValue = this.filter(value); if(typeof formattedValue != "string"){ if(filteredValue !== null && ((typeof filteredValue != "number") || !isNaN(filteredValue))){ formattedValue = this.filter(this.format(filteredValue, this.constraints)); }else{ formattedValue = ''; } } } if(formattedValue != null && formattedValue != undefined && ((typeof formattedValue) != "number" || !isNaN(formattedValue)) && this.textbox.value != formattedValue){ this.textbox.value = formattedValue; }
Sets the value of the widget to "value" which can be of
any type as determined by the widget.
dojox.mobile.app.TextBox.displayedValue
type
String
summary
For subclasses like ComboBox where the displayed value
(ex: Kentucky) and the serialized value (ex: KY) are different,
this represents the displayed value.
Setting 'displayedValue' through attr('displayedValue', ...)
updates 'value', and vice-versa. Otherwise 'value' is updated
from 'displayedValue' periodically, like onBlur etc.
TODO: move declaration to MappedTextBox?
Problem is that ComboBox references displayedValue,
for benefit of FilteringSelect.
Returns the displayed value (what the user sees on the screen),
after filtering (ie, trimming spaces etc.).
For some subclasses of TextBox (like ComboBox), the displayed value
is different from the serialized value that's actually
sent to the server (see dijit.form.ValidationTextBox.serialize)
// summary: // After the user types some characters, etc., this method is // called to check the field for validity etc. The base method // in `dijit.form.TextBox` does nothing, but subclasses override. // tags: // protected
summary
After the user types some characters, etc., this method is
called to check the field for validity etc. The base method
in `dijit.form.TextBox` does nothing, but subclasses override.
if(e && e.type && /key/i.test(e.type) && e.keyCode){ switch(e.keyCode){ case dojo.keys.SHIFT: case dojo.keys.ALT: case dojo.keys.CTRL: case dojo.keys.TAB: return; } } if(this.intermediateChanges){ var _this = this; // the setTimeout allows the key to post to the widget input box setTimeout(function(){ _this._handleOnChange(_this.get('value'), false); }, 0); } this._refreshState();
// summary: // A base class for textbox form inputs
// trim: Boolean // Removes leading and trailing whitespace if true. Default is false. trim: false,
// uppercase: Boolean // Converts all characters to uppercase if true. Default is false. uppercase: false,
// lowercase: Boolean // Converts all characters to lowercase if true. Default is false. lowercase: false,
// propercase: Boolean // Converts the first character of each word to uppercase if true. propercase: false,
// maxLength: String // HTML INPUT tag maxLength declaration. maxLength: "",
// selectOnClick: [const] Boolean // If true, all text will be selected when focused with mouse selectOnClick: false,
// placeHolder: String // Defines a hint to help users fill out the input field (as defined in HTML 5). // This should only contain plain text (no html markup). placeHolder: "",
_getValueAttr: function(){ // summary: // Hook so attr('value') works as we like. // description: // For `dijit.form.TextBox` this basically returns the value of the . // // For `dijit.form.MappedTextBox` subclasses, which have both // a "displayed value" and a separate "submit value", // This treats the "displayed value" as the master value, computing the // submit value from it via this.parse(). return this.parse(this.get('displayedValue'), this.constraints); },
_setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){ // summary: // Hook so attr('value', ...) works. // // description: // Sets the value of the widget to "value" which can be of // any type as determined by the widget. // // value: // The visual element value is also set to a corresponding, // but not necessarily the same, value. // // formattedValue: // If specified, used to set the visual element value, // otherwise a computed visual value is used. // // priorityChange: // If true, an onChange event is fired immediately instead of // waiting for the next blur event.
var filteredValue; if(value !== undefined){ // TODO: this is calling filter() on both the display value and the actual value. // I added a comment to the filter() definition about this, but it should be changed. filteredValue = this.filter(value); if(typeof formattedValue != "string"){ if(filteredValue !== null && ((typeof filteredValue != "number") || !isNaN(filteredValue))){ formattedValue = this.filter(this.format(filteredValue, this.constraints)); }else{ formattedValue = ''; } } } if(formattedValue != null && formattedValue != undefined && ((typeof formattedValue) != "number" || !isNaN(formattedValue)) && this.textbox.value != formattedValue){ this.textbox.value = formattedValue; }
// displayedValue: String // For subclasses like ComboBox where the displayed value // (ex: Kentucky) and the serialized value (ex: KY) are different, // this represents the displayed value. // // Setting 'displayedValue' through attr('displayedValue', ...) // updates 'value', and vice-versa. Otherwise 'value' is updated // from 'displayedValue' periodically, like onBlur etc. // // TODO: move declaration to MappedTextBox? // Problem is that ComboBox references displayedValue, // for benefit of FilteringSelect. displayedValue: "",
_getDisplayedValueAttr: function(){ // summary: // Hook so attr('displayedValue') works. // description: // Returns the displayed value (what the user sees on the screen), // after filtering (ie, trimming spaces etc.). // // For some subclasses of TextBox (like ComboBox), the displayed value // is different from the serialized value that's actually // sent to the server (see dijit.form.ValidationTextBox.serialize)
return this.filter(this.textbox.value); },
_setDisplayedValueAttr: function(/*String*/value){ // summary: // Hook so attr('displayedValue', ...) works. // description: // Sets the value of the visual element to the string "value". // The widget value is also set to a corresponding, // but not necessarily the same, value.
if(value === null || value === undefined){ value = '' } else if(typeof value != "string"){ value = String(value) } this.textbox.value = value; this._setValueAttr(this.get('value'), undefined, value); },
format: function(/* String */ value, /* Object */ constraints){ // summary: // Replacable function to convert a value to a properly formatted string. // tags: // protected extension return ((value == null || value == undefined) ? "" : (value.toString ? value.toString() : value)); },
parse: function(/* String */ value, /* Object */ constraints){ // summary: // Replacable function to convert a formatted string to a value // tags: // protected extension
return value; // String },
_refreshState: function(){ // summary: // After the user types some characters, etc., this method is // called to check the field for validity etc. The base method // in `dijit.form.TextBox` does nothing, but subclasses override. // tags: // protected },
_onInput: function(e){ if(e && e.type && /key/i.test(e.type) && e.keyCode){ switch(e.keyCode){ case dojo.keys.SHIFT: case dojo.keys.ALT: case dojo.keys.CTRL: case dojo.keys.TAB: return; } } if(this.intermediateChanges){ var _this = this; // the setTimeout allows the key to post to the widget input box setTimeout(function(){ _this._handleOnChange(_this.get('value'), false); }, 0); } this._refreshState(); },
postCreate: function(){ // setting the value here is needed since value="" in the template causes "undefined" // and setting in the DOM (instead of the JS object) helps with form reset actions
this.textbox.setAttribute("value", this.textbox.value); // DOM and JS values shuld be the same this.inherited(arguments); if(dojo.isMoz || dojo.isOpera){ this.connect(this.textbox, "oninput", this._onInput); }else{ this.connect(this.textbox, "onkeydown", this._onInput); this.connect(this.textbox, "onkeyup", this._onInput); this.connect(this.textbox, "onpaste", this._onInput); this.connect(this.textbox, "oncut", this._onInput); }
if(val === null){ return this._blankValue; } if(typeof val != "string"){ return val; } if(this.trim){ val = dojo.trim(val); } if(this.uppercase){ val = val.toUpperCase(); } if(this.lowercase){ val = val.toLowerCase(); } if(this.propercase){ val = val.replace(/[^\s]+/g, function(word){ return word.substring(0,1).toUpperCase() + word.substring(1); }); } return val;
summary
Auto-corrections (such as trimming) that are applied to textbox
value on blur or form submit.
description
For MappedTextBox subclasses, this is called twice
- once with the display value
- once the value as set/returned by attr('value', ...)
and attr('value'), ex: a Number for NumberTextBox.
In the latter case it does corrections like converting null to NaN. In
the former case the NumberTextBox.filter() method calls this.inherited()
to execute standard trimming code in TextBox.filter().
TODO: break this into two methods in 2.0
// Select all text on focus via click if nothing already selected. // Since mouse-up will clear the selection need to defer selection until after mouse-up. // Don't do anything on focus by tabbing into the widget since there's no associated mouse-up event. if(this.selectOnClick && by == "mouse"){ this._selectOnClickHandle = this.connect(this.domNode, "onmouseup", function(){ // Only select all text on first click; otherwise users would have no way to clear // the selection. this.disconnect(this._selectOnClickHandle);
// Check if the user selected some text manually (mouse-down, mouse-move, mouse-up) // and if not, then select all the text var textIsNotSelected; textIsNotSelected = this.textbox.selectionStart == this.textbox.selectionEnd; if(textIsNotSelected){ this.selectInputText(this.textbox); } }); }
// summary: // A base class for textbox form inputs
// trim: Boolean // Removes leading and trailing whitespace if true. Default is false. trim: false,
// uppercase: Boolean // Converts all characters to uppercase if true. Default is false. uppercase: false,
// lowercase: Boolean // Converts all characters to lowercase if true. Default is false. lowercase: false,
// propercase: Boolean // Converts the first character of each word to uppercase if true. propercase: false,
// maxLength: String // HTML INPUT tag maxLength declaration. maxLength: "",
// selectOnClick: [const] Boolean // If true, all text will be selected when focused with mouse selectOnClick: false,
// placeHolder: String // Defines a hint to help users fill out the input field (as defined in HTML 5). // This should only contain plain text (no html markup). placeHolder: "",
_getValueAttr: function(){ // summary: // Hook so attr('value') works as we like. // description: // For `dijit.form.TextBox` this basically returns the value of the . // // For `dijit.form.MappedTextBox` subclasses, which have both // a "displayed value" and a separate "submit value", // This treats the "displayed value" as the master value, computing the // submit value from it via this.parse(). return this.parse(this.get('displayedValue'), this.constraints); },
_setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){ // summary: // Hook so attr('value', ...) works. // // description: // Sets the value of the widget to "value" which can be of // any type as determined by the widget. // // value: // The visual element value is also set to a corresponding, // but not necessarily the same, value. // // formattedValue: // If specified, used to set the visual element value, // otherwise a computed visual value is used. // // priorityChange: // If true, an onChange event is fired immediately instead of // waiting for the next blur event.
var filteredValue; if(value !== undefined){ // TODO: this is calling filter() on both the display value and the actual value. // I added a comment to the filter() definition about this, but it should be changed. filteredValue = this.filter(value); if(typeof formattedValue != "string"){ if(filteredValue !== null && ((typeof filteredValue != "number") || !isNaN(filteredValue))){ formattedValue = this.filter(this.format(filteredValue, this.constraints)); }else{ formattedValue = ''; } } } if(formattedValue != null && formattedValue != undefined && ((typeof formattedValue) != "number" || !isNaN(formattedValue)) && this.textbox.value != formattedValue){ this.textbox.value = formattedValue; }
// displayedValue: String // For subclasses like ComboBox where the displayed value // (ex: Kentucky) and the serialized value (ex: KY) are different, // this represents the displayed value. // // Setting 'displayedValue' through attr('displayedValue', ...) // updates 'value', and vice-versa. Otherwise 'value' is updated // from 'displayedValue' periodically, like onBlur etc. // // TODO: move declaration to MappedTextBox? // Problem is that ComboBox references displayedValue, // for benefit of FilteringSelect. displayedValue: "",
_getDisplayedValueAttr: function(){ // summary: // Hook so attr('displayedValue') works. // description: // Returns the displayed value (what the user sees on the screen), // after filtering (ie, trimming spaces etc.). // // For some subclasses of TextBox (like ComboBox), the displayed value // is different from the serialized value that's actually // sent to the server (see dijit.form.ValidationTextBox.serialize)
return this.filter(this.textbox.value); },
_setDisplayedValueAttr: function(/*String*/value){ // summary: // Hook so attr('displayedValue', ...) works. // description: // Sets the value of the visual element to the string "value". // The widget value is also set to a corresponding, // but not necessarily the same, value.
if(value === null || value === undefined){ value = '' } else if(typeof value != "string"){ value = String(value) } this.textbox.value = value; this._setValueAttr(this.get('value'), undefined, value); },
format: function(/* String */ value, /* Object */ constraints){ // summary: // Replacable function to convert a value to a properly formatted string. // tags: // protected extension return ((value == null || value == undefined) ? "" : (value.toString ? value.toString() : value)); },
parse: function(/* String */ value, /* Object */ constraints){ // summary: // Replacable function to convert a formatted string to a value // tags: // protected extension
return value; // String },
_refreshState: function(){ // summary: // After the user types some characters, etc., this method is // called to check the field for validity etc. The base method // in `dijit.form.TextBox` does nothing, but subclasses override. // tags: // protected },
_onInput: function(e){ if(e && e.type && /key/i.test(e.type) && e.keyCode){ switch(e.keyCode){ case dojo.keys.SHIFT: case dojo.keys.ALT: case dojo.keys.CTRL: case dojo.keys.TAB: return; } } if(this.intermediateChanges){ var _this = this; // the setTimeout allows the key to post to the widget input box setTimeout(function(){ _this._handleOnChange(_this.get('value'), false); }, 0); } this._refreshState(); },
postCreate: function(){ // setting the value here is needed since value="" in the template causes "undefined" // and setting in the DOM (instead of the JS object) helps with form reset actions
this.textbox.setAttribute("value", this.textbox.value); // DOM and JS values shuld be the same this.inherited(arguments); if(dojo.isMoz || dojo.isOpera){ this.connect(this.textbox, "oninput", this._onInput); }else{ this.connect(this.textbox, "onkeydown", this._onInput); this.connect(this.textbox, "onkeyup", this._onInput); this.connect(this.textbox, "onpaste", this._onInput); this.connect(this.textbox, "oncut", this._onInput); } },
_blankValue: '', // if the textbox is blank, what value should be reported filter: function(val){ // summary: // Auto-corrections (such as trimming) that are applied to textbox // value on blur or form submit. // description: // For MappedTextBox subclasses, this is called twice // - once with the display value // - once the value as set/returned by attr('value', ...) // and attr('value'), ex: a Number for NumberTextBox. // // In the latter case it does corrections like converting null to NaN. In // the former case the NumberTextBox.filter() method calls this.inherited() // to execute standard trimming code in TextBox.filter(). // // TODO: break this into two methods in 2.0 // // tags: // protected extension if(val === null){ return this._blankValue; } if(typeof val != "string"){ return val; } if(this.trim){ val = dojo.trim(val); } if(this.uppercase){ val = val.toUpperCase(); } if(this.lowercase){ val = val.toLowerCase(); } if(this.propercase){ val = val.replace(/[^\s]+/g, function(word){ return word.substring(0,1).toUpperCase() + word.substring(1); }); } return val; },
// Select all text on focus via click if nothing already selected. // Since mouse-up will clear the selection need to defer selection until after mouse-up. // Don't do anything on focus by tabbing into the widget since there's no associated mouse-up event. if(this.selectOnClick && by == "mouse"){ this._selectOnClickHandle = this.connect(this.domNode, "onmouseup", function(){ // Only select all text on first click; otherwise users would have no way to clear // the selection. this.disconnect(this._selectOnClickHandle);
// Check if the user selected some text manually (mouse-down, mouse-move, mouse-up) // and if not, then select all the text var textIsNotSelected; textIsNotSelected = this.textbox.selectionStart == this.textbox.selectionEnd; if(textIsNotSelected){ this.selectInputText(this.textbox); } }); }