source: Dev/branches/rest-dojo-ui/client/dijit/form/TextBox.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 6.0 KB
Line 
1define([
2        "dojo/_base/declare", // declare
3        "dojo/dom-construct", // domConstruct.create
4        "dojo/dom-style", // domStyle.getComputedStyle
5        "dojo/_base/kernel", // kernel.deprecated
6        "dojo/_base/lang", // lang.hitch
7        "dojo/_base/sniff", // has("ie") has("mozilla")
8        "dojo/_base/window", // win.doc.selection.createRange
9        "./_FormValueWidget",
10        "./_TextBoxMixin",
11        "dojo/text!./templates/TextBox.html",
12        ".."    // to export dijit._setSelectionRange, remove in 2.0
13], function(declare, domConstruct, domStyle, kernel, lang, has, win,
14                        _FormValueWidget, _TextBoxMixin, template, dijit){
15
16/*=====
17        var _FormValueWidget = dijit.form._FormValueWidget;
18        var _TextBoxMixin = dijit.form._TextBoxMixin;
19=====*/
20
21        // module:
22        //              dijit/form/TextBox
23        // summary:
24        //              A base class for textbox form inputs
25
26        var TextBox = declare(/*====="dijit.form.TextBox", =====*/ [_FormValueWidget, _TextBoxMixin], {
27                // summary:
28                //              A base class for textbox form inputs
29
30                templateString: template,
31                _singleNodeTemplate: '<input class="dijit dijitReset dijitLeft dijitInputField" data-dojo-attach-point="textbox,focusNode" autocomplete="off" type="${type}" ${!nameAttrSetting} />',
32
33                _buttonInputDisabled: has("ie") ? "disabled" : "", // allows IE to disallow focus, but Firefox cannot be disabled for mousedown events
34
35                baseClass: "dijitTextBox",
36
37                postMixInProperties: function(){
38                        var type = this.type.toLowerCase();
39                        if(this.templateString && this.templateString.toLowerCase() == "input" || ((type == "hidden" || type == "file") && this.templateString == this.constructor.prototype.templateString)){
40                                this.templateString = this._singleNodeTemplate;
41                        }
42                        this.inherited(arguments);
43                },
44
45                _onInput: function(e){
46                        this.inherited(arguments);
47                        if(this.intermediateChanges){ // _TextBoxMixin uses onInput
48                                var _this = this;
49                                // the setTimeout allows the key to post to the widget input box
50                                setTimeout(function(){ _this._handleOnChange(_this.get('value'), false); }, 0);
51                        }
52                },
53
54                _setPlaceHolderAttr: function(v){
55                        this._set("placeHolder", v);
56                        if(!this._phspan){
57                                this._attachPoints.push('_phspan');
58                                // dijitInputField class gives placeHolder same padding as the input field
59                                // parent node already has dijitInputField class but it doesn't affect this <span>
60                                // since it's position: absolute.
61                                this._phspan = domConstruct.create('span',{className:'dijitPlaceHolder dijitInputField'},this.textbox,'after');
62                        }
63                        this._phspan.innerHTML="";
64                        this._phspan.appendChild(document.createTextNode(v));
65                        this._updatePlaceHolder();
66                },
67
68                _updatePlaceHolder: function(){
69                        if(this._phspan){
70                                this._phspan.style.display=(this.placeHolder&&!this.focused&&!this.textbox.value)?"":"none";
71                        }
72                },
73
74                _setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
75                        this.inherited(arguments);
76                        this._updatePlaceHolder();
77                },
78
79                getDisplayedValue: function(){
80                        // summary:
81                        //              Deprecated.  Use get('displayedValue') instead.
82                        // tags:
83                        //              deprecated
84                        kernel.deprecated(this.declaredClass+"::getDisplayedValue() is deprecated. Use set('displayedValue') instead.", "", "2.0");
85                        return this.get('displayedValue');
86                },
87
88                setDisplayedValue: function(/*String*/ value){
89                        // summary:
90                        //              Deprecated.  Use set('displayedValue', ...) instead.
91                        // tags:
92                        //              deprecated
93                        kernel.deprecated(this.declaredClass+"::setDisplayedValue() is deprecated. Use set('displayedValue', ...) instead.", "", "2.0");
94                        this.set('displayedValue', value);
95                },
96
97                _onBlur: function(e){
98                        if(this.disabled){ return; }
99                        this.inherited(arguments);
100                        this._updatePlaceHolder();
101                },
102
103                _onFocus: function(/*String*/ by){
104                        if(this.disabled || this.readOnly){ return; }
105                        this.inherited(arguments);
106                        this._updatePlaceHolder();
107                }
108        });
109
110        if(has("ie")){
111                TextBox = declare(/*===== "dijit.form.TextBox.IEMixin", =====*/ TextBox, {
112                        declaredClass: "dijit.form.TextBox",    // for user code referencing declaredClass
113
114                        _isTextSelected: function(){
115                                var range = win.doc.selection.createRange();
116                                var parent = range.parentElement();
117                                return parent == this.textbox && range.text.length == 0;
118                        },
119
120                        postCreate: function(){
121                                this.inherited(arguments);
122                                // IE INPUT tag fontFamily has to be set directly using STYLE
123                                // the setTimeout gives IE a chance to render the TextBox and to deal with font inheritance
124                                setTimeout(lang.hitch(this, function(){
125                                        try{
126                                                var s = domStyle.getComputedStyle(this.domNode); // can throw an exception if widget is immediately destroyed
127                                                if(s){
128                                                        var ff = s.fontFamily;
129                                                        if(ff){
130                                                                var inputs = this.domNode.getElementsByTagName("INPUT");
131                                                                if(inputs){
132                                                                        for(var i=0; i < inputs.length; i++){
133                                                                                inputs[i].style.fontFamily = ff;
134                                                                        }
135                                                                }
136                                                        }
137                                                }
138                                        }catch(e){/*when used in a Dialog, and this is called before the dialog is
139                                                shown, s.fontFamily would trigger "Invalid Argument" error.*/}
140                                }), 0);
141                        }
142                });
143
144                // Overrides definition of _setSelectionRange from _TextBoxMixin (TODO: move to _TextBoxMixin.js?)
145                dijit._setSelectionRange = _TextBoxMixin._setSelectionRange = function(/*DomNode*/ element, /*Number?*/ start, /*Number?*/ stop){
146                        if(element.createTextRange){
147                                var r = element.createTextRange();
148                                r.collapse(true);
149                                r.moveStart("character", -99999); // move to 0
150                                r.moveStart("character", start); // delta from 0 is the correct position
151                                r.moveEnd("character", stop-start);
152                                r.select();
153                        }
154                }
155        }else if(has("mozilla")){
156                TextBox = declare(/*===== "dijit.form.TextBox.MozMixin", =====*/TextBox, {
157                        declaredClass: "dijit.form.TextBox",    // for user code referencing declaredClass
158
159                        _onBlur: function(e){
160                                this.inherited(arguments);
161                                if(this.selectOnClick){
162                                                // clear selection so that the next mouse click doesn't reselect
163                                        this.textbox.selectionStart = this.textbox.selectionEnd = undefined;
164                                }
165                        }
166                });
167        }else{
168                TextBox.prototype.declaredClass = "dijit.form.TextBox";
169        }
170        lang.setObject("dijit.form.TextBox", TextBox);  // don't do direct assignment, it confuses API doc parser
171
172        return TextBox;
173});
Note: See TracBrowser for help on using the repository browser.