zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
    @name Edo.controls.TextInput
    @class
    @typeName textinput, text
    @description 文本输入框:单行/密码/多行
    @extend Edo.controls.Control
*/
Edo.controls.TextInput = function(){
    Edo.controls.TextInput.superclass.constructor.call(this);
    
    /**
        @name Edo.controls.TextInput#beforetextchange
        @event        
        @description 文本改变前事件
        @property {String} text 文本
    */
    /**
        @name Edo.controls.TextInput#textchange
        @event        
        @description 文本改变事件
        @property {String} text 文本
    */       
    /**
        @name Edo.controls.TextInput#textfocus
        @event
        @domevent
        @description 输入框获得焦点
    */   
    /**
        @name Edo.controls.TextInput#textblur
        @event
        @domevent
        @description 输入框失去焦点
    */
};
Edo.controls.TextInput.extend(Edo.controls.Control,{
    _htmltype: 'text',
    _mode: 'text',
    
    lineHeight: true,   //true(跟realHeight一样), false,不设置, 具体值30...
    
    /**
        @name Edo.controls.TextInput#defaultWidth
        @property
        @default 100
    */
    defaultWidth: 100,        
    /**
        @name Edo.controls.TextInput#minWidth
        @property
        @default 20
    */
    minWidth: 20,
    
    defaultHeight: 21,        
    minHeight: 20,
    /**
        @name Edo.controls.TextInput#text
        @property
        @type String
        @description 文本
    */ 
    text: '',    
    /**
        @name Edo.controls.TextInput#readOnly
        @property
        @type Boolean
        @default false
        @description 是否只读,默认false
    */
    readOnly: false,
    
    /**
        @name Edo.controls.TextInput#selectOnFocus
        @property
        @type Boolean
        @default false
        @description 是否在获得焦点的时候,选择文本.默认false
    */
    selectOnFocus: false,
    
    textStyle: '',
    textCls: '',
    
    elCls: 'e-text',
    
    /**
        @name Edo.controls.TextInput#emptyText
        @property
        @type String
        @description 文本为空时的显示文本
    */ 
    emptyText: '',
    /**
        @name Edo.controls.TextInput#emptyCls
        @property
        @type String
        @default 'e-text-empty'
        @description 文本为空时的显示样式
    */ 
    emptyCls: 'e-text-empty',
    
    //disabledClass : "",
    focusClass: 'e-text-focus',        
    
    //激发el.onchange
    /**
        @name Edo.controls.TextInput#changeAction
        @property
        @type String
        @default change
        @description 文本textchange事件由什么DOM事件激发,如change/blur/keyup/keydown等
    */ 
    changeAction: 'change',
    
    focus: function(){    
        //if(this.field) this.field.focus();
        var el = this.field;
        if(this.blurTimer) {
            clearTimeout(this.blurTimer);
            this.blurTimer = null;
        }
        setTimeout(function(){
            Edo.util.Dom.focus(el);
        },100);
    },
    blur: function(){        
        var sf = this;
        this.blurTimer = setTimeout(function(){
            Edo.util.Dom.blur(sf.field);
        }, 100);
        //if(this.field) this.field.blur();
    },
    getInnerHtml: function(sb){   
         
        var w = this.getFieldWidth() - 8;//8是 (1border + 3padding) * 2        
        var h = lh = this.realHeight - 4;//4是 (1border + 1padding) * 2
        
        if(this.lineHeight && this.lineHeight !== true){
            lh = this.lineHeight;
        }
        
        var text = this.text;
//        var text = Edo.isValue(this.text) ? this.text : this.emptyText;
//        this.elCls += ' ' + (text === this.emptyText ? this.emptyCls : '');
        
        if(this._mode == 'text'){
            sb[sb.length] = '<input type="'+this._htmltype+'" ';
        }else{
            sb[sb.length] = '<textarea ';
        }
        
        if(!this.isEnable()) sb[sb.length] = ' disabled="disabled" '; 
        sb[sb.length] = ' autocomplete="off" class="e-text-field" style="height:';
        sb[sb.length] = h;
        sb[sb.length] = 'px;line-height:';
        sb[sb.length] = lh;
        sb[sb.length] = 'px;width:';
        sb[sb.length] = w;
        sb[sb.length] = 'px;';
        sb[sb.length] = this.textStyle;
        sb[sb.length] = '" ';
        
        if(this.readOnly) {
            sb[sb.length] = ' readonly="true" ';
            this.elCls += ' e-text-readonly ';
        }            
        
        if(this._mode == 'text'){
            sb[sb.length] = ' value="';
            sb[sb.length] = text;
            sb[sb.length] = '" />';
        }else{
            sb[sb.length] = '>';
            sb[sb.length] = text;
            sb[sb.length] = '</textarea>';
        }        
    },
    createChildren: function(el){        
        Edo.controls.TextInput.superclass.createChildren.call(this, el);
        this.field = this.el.firstChild;        
    },
    initEvents: function(){        
        Edo.controls.TextInput.superclass.initEvents.c(this);
        var de = Edo.util.Dom;
        var f = this.field;
        de.on(f, 'blur', this._onBlur, this);
        de.on(f, 'focus', this._onFocus, this);
        
        
        de.on(f, this.changeAction, this._onTextChange, this);        
        //是否及时change?         
    },
    _onBlur: function(e){    
        if(this.within(e)){
            return;
        }
    
        var sf = this;
        this.blurTimer = setTimeout(function(){
            Edo.util.Dom.removeClass(sf.el, sf.focusClass);        
        }, 10);
        
        e.type = 'textblur';
        e.target = this;
        this.fireEvent('textblur', e);
        
        e.type = 'blur';
        this.fireEvent('blur', e);
        
//        if(this.field){
//            if(this.field.value === this.emptyText){
//                this.field.value = '';
//                Edo.util.Dom.addClass(this.el, this.emptyCls);
//            }else{
//                Edo.util.Dom.removeClass(this.el, this.emptyCls);
//            }
//            
//        }
    },
    _onFocus: function(e){
        e.stopDefault();
        
//        if(this.blurTimer) {
//            clearTimeout(this.blurTimer);
//            this.blurTimer = null;
//        }
        
        //Edo.util.Dom.addClass(this.el, this.focusClass);  
        
        if(!this.isEnable()){            
            this.blur();            
            return;
        }
        
        var sf = this;
        setTimeout(function(){
            Edo.util.Dom.addClass(sf.el, sf.focusClass);        
        }, 1);      
        function _(){
            if(this.field) this.field.select();
        }
        if(this.selectOnFocus){
            _.defer(20, this);
        }
        
        e.type = 'textfocus';
        e.target = this;
        this.fireEvent('textfocus', e);
        
        e.type = 'focus';
        this.fireEvent('focus', e);
        
//        if(this.field){
//            if(this.field.value === this.emptyText){
//                this.field.value = '';
//                Edo.util.Dom.addClass(this.el, this.emptyCls);
//            }else{
//                Edo.util.Dom.removeClass(this.el, this.emptyCls);
//            }
//            
//        }
    },
    _onTextChange: function(e){
        this._setText(this.field.value);
    },
    _setEnable: function(value){
        Edo.controls.TextInput.superclass._setEnable.call(this, value);  
        if(this.field){
            this.field.disabled = !this.isEnable();
        }
    },
    _setText: function(value){    
        if(!Edo.isValue(value)) value = '';   
        
        if(this.text !== value){
            if(this.fireEvent('beforetextchange', {
                type: 'beforetextchange',
                source: this,
                text: value
            }) === false) {
                if(this.el) this.field.value = this.text;
                return;
            }
        
            this.text = value;      //text就是传递进来的值
            if(this.el){
//                if(!Edo.isValue(value)) {   //如果为空,则使用emptyText显示
//                    value = this.emptyText;                                        
//                }
                this.field.value = value;
//                if(value === this.emptyText){
//                    Edo.util.Dom.addClass(this.el, this.emptyCls);
//                }else{
//                    Edo.util.Dom.removeClass(this.el, this.emptyCls);
//                }
            }
            
            this.changeProperty('text', value);
            
            this.fireEvent('textchange', {
                type: 'textchange',
                source: this,
                text: this.text
            });
        }
    }, 
    _setReadOnly: function(value){
        if(this.readOnly != value){
            this.readOnly = value;
            if(this.el){
                this.field.readOnly = value;                
                if(value){
                    Edo.util.Dom.addCls(this.el, 'e-text-readonly');
                }else{
                    Edo.util.Dom.removeCls(this.el, 'e-text-readonly');
                }
            }            
        }
    },
    //
    getFieldWidth: function(){
        var w = this.realWidth;
//        if(isSafari) {
//            w -= 8;
//        }
        return w;
    },
    syncSize: function(){    //设置组件尺寸,并设置容器子元素的所有尺寸!
        Edo.controls.TextInput.superclass.syncSize.call(this);
                
        var w = this.getFieldWidth(), h = this.realHeight, f = this.field;
        Edo.util.Dom.setSize(f, w, h-2);
        //line-height
        if(this.lineHeight){
            var h = this.lineHeight === true ? (h-4) : this.lineHeight;                       
            f.style.lineHeight = h+'px';
        }
    },     
    destroy : function(){
        Edo.util.Dom.clearEvent(this.field);            
        Edo.util.Dom.remove(this.field);
        this.field = null;
        Edo.controls.TextInput.superclass.destroy.call(this);       
    },
    //---------------验证
    clearInvalid: function(){
        Edo.controls.TextInput.superclass.clearInvalid.call(this);        
        if(this.el) Edo.util.Dom.removeClass(this.el, 'e-form-invalid');        
    },
    showInvalid: function(msg){
        Edo.controls.TextInput.superclass.showInvalid.call(this, msg);
        if(this.showValid){
            if(this.el) Edo.util.Dom.addClass(this.el, 'e-form-invalid');
            else this.cls += ' e-form-invalid';
        }
    }
//    ,   
//    //---------------编辑    
//    startEdit: function(data, x, y, w, h){
//        Edo.controls.TextInput.superclass.startEdit.apply(this, arguments);
//        //this.field.select();
//    }
});
 
Edo.controls.TextInput.regType('textinput', 'text');