- Author:
- David Nickerson <nickerso@users.sourceforge.net>
- Date:
- 2009-07-16 02:00:03+12:00
- Desc:
- the starting point for the HH tutorial example
- Permanent Source URI:
- https://models.fieldml.org/workspace/a1/rawfile/f6a8f90307388eb4b040ee3566b84d88b59247f7/dojo-presentation/js/dojo/dijit/form/TextBox.js
dojo.provide("dijit.form.TextBox");
dojo.require("dijit.form._FormWidget");
dojo.declare(
"dijit.form.TextBox",
dijit.form._FormValueWidget,
{
// 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: "",
templatePath: dojo.moduleUrl("dijit.form", "templates/TextBox.html"),
baseClass: "dijitTextBox",
attributeMap: dojo.mixin(dojo.clone(dijit.form._FormValueWidget.prototype.attributeMap),
{maxLength:"focusNode"}),
getDisplayedValue: function(){
// summary:
// Returns the formatted value that the user sees in the textbox, which may be different
// from the serialized value that's actually sent to the server (see dijit.form.ValidationTextBox.serialize)
return this.filter(this.textbox.value);
},
getValue: function(){
return this.parse(this.getDisplayedValue(), this.constraints);
},
setValue: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
// summary:
// 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){
filteredValue = this.filter(value);
if(filteredValue !== null && ((typeof filteredValue != "number") || !isNaN(filteredValue))){
if(typeof formattedValue != "string"){
formattedValue = this.format(filteredValue, this.constraints);
}
}else{ formattedValue = ''; }
}
if(formattedValue != null && formattedValue != undefined){
this.textbox.value = formattedValue;
}
dijit.form.TextBox.superclass.setValue.call(this, filteredValue, priorityChange);
},
setDisplayedValue: function(/*String*/value, /*Boolean?*/ priorityChange){
// summary:
// 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.
//
// priorityChange:
// If true, an onChange event is fired immediately instead of
// waiting for the next blur event.
this.textbox.value = value;
this.setValue(this.getValue(), priorityChange);
},
format: function(/* String */ value, /* Object */ constraints){
// summary:
// Replacable function to convert a value to a properly formatted string
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
return value;
},
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.getDisplayedValue());
this.inherited(arguments);
/*#5297:if(this.srcNodeRef){
dojo.style(this.textbox, "cssText", this.style);
this.textbox.className += " " + this["class"];
}*/
this._layoutHack();
},
filter: function(val){
// summary:
// Apply specified filters to textbox value
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;
},
_setBlurValue: function(){
this.setValue(this.getValue(), (this.isValid ? this.isValid() : true));
},
_onBlur: function(){
this._setBlurValue();
this.inherited(arguments);
}
}
);
dijit.selectInputText = function(/*DomNode*/element, /*Number?*/ start, /*Number?*/ stop){
// summary:
// Select text in the input element argument, from start (default 0), to stop (default end).
// TODO: use functions in _editor/selection.js?
var _window = dojo.global;
var _document = dojo.doc;
element = dojo.byId(element);
if(isNaN(start)){ start = 0; }
if(isNaN(stop)){ stop = element.value ? element.value.length : 0; }
element.focus();
if(_document["selection"] && dojo.body()["createTextRange"]){ // IE
if(element.createTextRange){
var range = element.createTextRange();
with(range){
collapse(true);
moveStart("character", start);
moveEnd("character", stop);
select();
}
}
}else if(_window["getSelection"]){
var selection = _window.getSelection();
// FIXME: does this work on Safari?
if(element.setSelectionRange){
element.setSelectionRange(start, stop);
}
}
}