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