]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/form/TextBox.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dijit / form / TextBox.js
1 if(!dojo._hasResource["dijit.form.TextBox"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit.form.TextBox"] = true;
3 dojo.provide("dijit.form.TextBox");
4
5 dojo.require("dijit.form._FormWidget");
6
7 dojo.declare(
8         "dijit.form.TextBox",
9         dijit.form._FormValueWidget,
10         {
11                 //      summary:
12                 //              A base class for textbox form inputs
13                 //
14                 //      trim: Boolean
15                 //              Removes leading and trailing whitespace if true.  Default is false.
16                 trim: false,
17
18                 //      uppercase: Boolean
19                 //              Converts all characters to uppercase if true.  Default is false.
20                 uppercase: false,
21
22                 //      lowercase: Boolean
23                 //              Converts all characters to lowercase if true.  Default is false.
24                 lowercase: false,
25
26                 //      propercase: Boolean
27                 //              Converts the first character of each word to uppercase if true.
28                 propercase: false,
29
30                 //      maxLength: String
31                 //              HTML INPUT tag maxLength declaration.
32                 maxLength: "",
33
34                 templateString:"<input class=\"dijit dijitReset dijitLeft\" dojoAttachPoint='textbox,focusNode' name=\"${name}\"\n\tdojoAttachEvent='onmouseenter:_onMouse,onmouseleave:_onMouse,onfocus:_onMouse,onblur:_onMouse,onkeypress:_onKeyPress,onkeyup'\n\tautocomplete=\"off\" type=\"${type}\"\n\t/>\n",
35                 baseClass: "dijitTextBox",
36
37                 attributeMap: dojo.mixin(dojo.clone(dijit.form._FormValueWidget.prototype.attributeMap),
38                         {maxLength:"focusNode"}),
39
40                 getDisplayedValue: function(){
41                         //      summary:
42                         //              Returns the formatted value that the user sees in the textbox, which may be different
43                         //              from the serialized value that's actually sent to the server (see dijit.form.ValidationTextBox.serialize)
44                         return this.filter(this.textbox.value);
45                 },
46
47                 getValue: function(){
48                         return this.parse(this.getDisplayedValue(), this.constraints);
49                 },
50
51                 setValue: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
52                         //      summary: 
53                         //              Sets the value of the widget to "value" which can be of
54                         //              any type as determined by the widget.
55                         //
56                         //      value:
57                         //              The visual element value is also set to a corresponding,
58                         //              but not necessarily the same, value.
59                         //
60                         //      formattedValue:
61                         //              If specified, used to set the visual element value,
62                         //              otherwise a computed visual value is used.
63                         //
64                         //      priorityChange:
65                         //              If true, an onChange event is fired immediately instead of 
66                         //              waiting for the next blur event.
67
68                         var filteredValue = this.filter(value);
69                         if((((typeof filteredValue == typeof value) && (value !== undefined/*#5317*/)) || (value === null/*#5329*/)) && (formattedValue == null || formattedValue == undefined)){
70                                 formattedValue = this.format(filteredValue, this.constraints);
71                         }
72                         if(formattedValue != null && formattedValue != undefined){
73                                 this.textbox.value = formattedValue;
74                         }
75                         dijit.form.TextBox.superclass.setValue.call(this, filteredValue, priorityChange);
76                 },
77
78                 setDisplayedValue: function(/*String*/value, /*Boolean?*/ priorityChange){
79                         //      summary: 
80                         //              Sets the value of the visual element to the string "value".
81                         //              The widget value is also set to a corresponding,
82                         //              but not necessarily the same, value.
83                         //
84                         //      priorityChange:
85                         //              If true, an onChange event is fired immediately instead of 
86                         //              waiting for the next blur event.
87
88                         this.textbox.value = value;
89                         this.setValue(this.getValue(), priorityChange);
90                 },
91
92                 format: function(/* String */ value, /* Object */ constraints){
93                         //      summary:
94                         //              Replacable function to convert a value to a properly formatted string
95                         return ((value == null || value == undefined) ? "" : (value.toString ? value.toString() : value));
96                 },
97
98                 parse: function(/* String */ value, /* Object */ constraints){
99                         //      summary:
100                         //              Replacable function to convert a formatted string to a value
101                         return value;
102                 },
103
104                 postCreate: function(){
105                         // setting the value here is needed since value="" in the template causes "undefined"
106                         // and setting in the DOM (instead of the JS object) helps with form reset actions
107                         this.textbox.setAttribute("value", this.getDisplayedValue());
108                         this.inherited(arguments);
109
110                         /*#5297:if(this.srcNodeRef){
111                                 dojo.style(this.textbox, "cssText", this.style);
112                                 this.textbox.className += " " + this["class"];
113                         }*/
114                         this._layoutHack();
115                 },
116
117                 filter: function(val){
118                         //      summary:
119                         //              Apply specified filters to textbox value
120                         if(val === null || val === undefined){ return ""; }
121                         else if(typeof val != "string"){ return val; }
122                         if(this.trim){
123                                 val = dojo.trim(val);
124                         }
125                         if(this.uppercase){
126                                 val = val.toUpperCase();
127                         }
128                         if(this.lowercase){
129                                 val = val.toLowerCase();
130                         }
131                         if(this.propercase){
132                                 val = val.replace(/[^\s]+/g, function(word){
133                                         return word.substring(0,1).toUpperCase() + word.substring(1);
134                                 });
135                         }
136                         return val;
137                 },
138
139                 _setBlurValue: function(){
140                         this.setValue(this.getValue(), (this.isValid ? this.isValid() : true));
141                 },
142
143                 _onBlur: function(){
144                         this._setBlurValue();
145                         this.inherited(arguments);
146                 },
147
148                 onkeyup: function(){
149                         //      summary:
150                         //              User replaceable keyup event handler
151                 }
152         }
153 );
154
155 dijit.selectInputText = function(/*DomNode*/element, /*Number?*/ start, /*Number?*/ stop){
156         //      summary:
157         //              Select text in the input element argument, from start (default 0), to stop (default end).
158
159         // TODO: use functions in _editor/selection.js?
160         var _window = dojo.global;
161         var _document = dojo.doc;
162         element = dojo.byId(element);
163         if(isNaN(start)){ start = 0; }
164         if(isNaN(stop)){ stop = element.value ? element.value.length : 0; }
165         element.focus();
166         if(_document["selection"] && dojo.body()["createTextRange"]){ // IE
167                 if(element.createTextRange){
168                         var range = element.createTextRange();
169                         with(range){
170                                 collapse(true);
171                                 moveStart("character", start);
172                                 moveEnd("character", stop);
173                                 select();
174                         }
175                 }
176         }else if(_window["getSelection"]){
177                 var selection = _window.getSelection();
178                 // FIXME: does this work on Safari?
179                 if(element.setSelectionRange){
180                         element.setSelectionRange(start, stop);
181                 }
182         }
183 }
184
185 }