Debian Codegraph @

dijit/form/TextBox.js

raw symbols SCIP
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 this._phspan = domConstruct.create('span', {
69 // dijitInputField class gives placeHolder same padding as the input field
70 // parent node already has dijitInputField class but it doesn't affect this <span>
71 // since it's position: absolute.
72 className: 'dijitPlaceHolder dijitInputField'
73 }, this.textbox, 'after');
74 this.own(
75 on(this._phspan, "mousedown", function(evt){ evt.preventDefault(); }),
76 on(this._phspan, "touchend, pointerup, MSPointerUp", lang.hitch(this, function(){
77 // If the user clicks placeholder rather than the <input>, need programmatic focus. Normally this
78 // is done in _FormWidgetMixin._onFocus() but after [30663] it's done on a delay, which is ineffective.
79 this.focus();
80 }))
81 );
82 }
83 this._phspan.innerHTML="";
84 this._phspan.appendChild(this._phspan.ownerDocument.createTextNode(v));
85 this._updatePlaceHolder();
86 },
87
88 _onInput: function(/*Event*/ evt){
89 // summary:
90 // Called AFTER the input event has happened
91 // See if the placeHolder text should be removed or added while editing.
92 this.inherited(arguments);
93 this._updatePlaceHolder();
94 },
95
96 _updatePlaceHolder: function(){
97 if(this._phspan){
98 this._phspan.style.display = (this.placeHolder && !this.textbox.value) ? "" : "none";
99 }
100 },
101
102 _setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
103 this.inherited(arguments);
104 this._updatePlaceHolder();
105 },
106
107 getDisplayedValue: function(){
108 // summary:
109 // Deprecated. Use get('displayedValue') instead.
110 // tags:
111 // deprecated
112 kernel.deprecated(this.declaredClass+"::getDisplayedValue() is deprecated. Use get('displayedValue') instead.", "", "2.0");
113 return this.get('displayedValue');
114 },
115
116 setDisplayedValue: function(/*String*/ value){
117 // summary:
118 // Deprecated. Use set('displayedValue', ...) instead.
119 // tags:
120 // deprecated
121 kernel.deprecated(this.declaredClass+"::setDisplayedValue() is deprecated. Use set('displayedValue', ...) instead.", "", "2.0");
122 this.set('displayedValue', value);
123 },
124
125 _onBlur: function(e){
126 if(this.disabled){ return; }
127 this.inherited(arguments);
128 this._updatePlaceHolder();
129
130 if(has("mozilla")){
131 if(this.selectOnClick){
132 // clear selection so that the next mouse click doesn't reselect
133 this.textbox.selectionStart = this.textbox.selectionEnd = undefined;
134 }
135 }
136 },
137
138 _onFocus: function(/*String*/ by){
139 if(this.disabled || this.readOnly){ return; }
140 this.inherited(arguments);
141 this._updatePlaceHolder();
142 }
143 });
144
145 if(has("ie") < 9){
146 TextBox.prototype._isTextSelected = function(){
147 var range = this.ownerDocument.selection.createRange();
148 var parent = range.parentElement();
149 return parent == this.textbox && range.text.length > 0;
150 };
151
152 // Overrides definition of _setSelectionRange from _TextBoxMixin (TODO: move to _TextBoxMixin.js?)
153 dijit._setSelectionRange = _TextBoxMixin._setSelectionRange = function(/*DomNode*/ element, /*Number?*/ start, /*Number?*/ stop){
154 if(element.createTextRange){
155 var r = element.createTextRange();
156 r.collapse(true);
157 r.moveStart("character", -99999); // move to 0
158 r.moveStart("character", start); // delta from 0 is the correct position
159 r.moveEnd("character", stop-start);
160 r.select();
161 }
162 }
163 }
164
165 if(has("dojo-bidi")){
166 TextBox = declare("dijit.form.TextBox", TextBox, {
167 _setPlaceHolderAttr: function(v){
168 this.inherited(arguments);
169 this.applyTextDir(this._phspan);
170 }
171 });
172 }
173
174 return TextBox;
175 });