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
/**
    @name Edo.containers.FormItem
    @class 
    @typeName formitem
    @description 带label左侧文本显示框的容器,提供表单布局方式
    @extend Edo.containers.Box
    @example 
 
 
*/ 
Edo.containers.FormItem = function(){    
    Edo.containers.FormItem.superclass.constructor.call(this);    
    
};
Edo.containers.FormItem.extend(Edo.containers.Box, {
    /**
        @name Edo.containers.FormItem#defaultHeight
        @property        
        @default 22
    */ 
    defaultHeight: 22, 
    /**
        @name Edo.containers.FormItem#minHeight
        @property        
        @default 22
    */ 
    minHeight: 22,
    /**
        @name Edo.containers.FormItem#border
        @property        
        @default [0,0,0,0]
    */ 
    border: [0,0,0,0],
    /**
        @name Edo.containers.FormItem#padding
        @property        
        @default [0,0,0,0]
    */ 
    padding: [0,0,0,0],
    
    /**
        @name Edo.containers.FormItem#labelPosition
        @property
        @type {String}
        @default left
        @description label方位.top/right/bottom/left
    */
    labelPosition: 'left',
    /**
        @name Edo.containers.FormItem#label
        @property
        @type {String}        
        @description 文本
    */
    label: '',
    /**
        @name Edo.containers.FormItem#labelWidth
        @property
        @type {String}        
        @description 文本宽度
    */
    labelWidth: 60,
    labelHeight: 22,
    /**
        @name Edo.containers.FormItem#labelAlign
        @property
        @type {String}        
        @description 文本偏移
    */
    labelAlign: 'left',
    /**
        @name Edo.containers.FormItem#labelCls
        @property
        @type {String}        
        @description 文本cls
    */
    labelCls: '',
    /**
        @name Edo.containers.FormItem#labelStyle
        @property
        @type {String}        
        @description 文本style
    */
    labelStyle: '',    
    
    elCls: 'e-formitem e-box e-div',
    
    forId: '',  //如果没有设置, 可以focus第一个child
    
    measure: function(){
        Edo.containers.FormItem.superclass.measure.call(this);
        if(this.labelPosition=='left' || this.labelPosition == 'right'){
            this.realWidth += this.labelWidth;
        }else{
            this.realHeight += this.labelHeight;
        }
        
        this.measureSize();
    },    
    getLayoutBox: function(){
        var box = Edo.containers.FormItem.superclass.getLayoutBox.call(this);        
        
        switch(this.labelPosition){
        case "top":
            box.y += this.labelHeight;
            box.height -= this.labelHeight;
        break;
        case "right":            
            box.width -= this.labelWidth;
        break;
        case "bottom":            
            box.height -= this.labelHeight;
        break;
        case "left":
            box.x += this.labelWidth;
            box.width -= this.labelWidth;
        break;
        }
        
        box.right = box.x + box.width;
        box.bottom = box.y + box.height;
        return box;
    },
    
    doLabel: function(){
        if(this.el){
            this.labelCt.innerHTML = '<div style="float:'+this.labelAlign+';">'+this.label+'</div>';
        }
    },
    _setLabel: function(value){       
        if(this.label != value){
            this.label = value;            
            this.doLabel();
        }
    },
    _setLabelAlign: function(value){       
        if(this.labelAlign != value){
            this.labelAlign = value;            
            this.doLabel();
        }
    },
    _setLabelPosition: function(value){       
        if(this.labelPosition != value){
            this.labelPosition = value;            
            this.relayout('labelPosition', value);
        }
    },
    _setLabelStyle: function(value){       
        if(this.labelStyle != value){
            this.labelStyle = value;
            if(this.labelCt) Edo.util.Dom.applyStyles(this.labelCt, value);
        }
    },
    _setLabelCls: function(value){       
        if(this.labelCls != value){
            this.labelCls = value;
            Edo.util.Dom.addClass(this.labelCt, this.labelCls);
        }
    },
    _setLabelWidth: function(value){
        value = parseInt(value);
        if(isNaN(value)) throw new Error('labelWidth must is Number type');        
        if(this.labelWidth != value){
            this.labelWidth = value;
            
            this.relayout('labelWidth', value);
        }
    },   
    _setForId: function(value){              
    
        if(this.forId != value){
            this.forId = value;
            
            var cmp = Edo.getCmp(value);
            if(cmp){
                //点击事件,激发cmp的focus方法.
                
                //获取cmp的focusEl,设置htmlFor
            }else{
                this.labelCt.dom.htmlFor = value;                
            }
        }
    },
    syncSize: function(){    //设置组件尺寸,并设置容器子元素的所有尺寸!
        Edo.containers.FormItem.superclass.syncSize.call(this);
        
        var box = this.getLayoutBox();
        var w = this.labelWidth, h = this.labelHeight;   
        var style = 'top:auto;right:auto;bottom:auto;left:auto;';     
        switch(this.labelPosition){
        case "top":
            w = box.width;
            style += 'left:'+this.padding[3]+'px;top:'+this.padding[0]+'px';
        break;        
        case "right":
            h = box.height;
            style += 'right:'+this.padding[1]+'px;top:'+this.padding[0]+'px';
        break;
        case "bottom":
            w = box.width;
            style += 'left:'+this.padding[3]+'px;bottom:'+this.padding[2]+'px';
        break;
        case "left":
            h = box.height;
            style += 'left:'+this.padding[3]+'px;top:'+this.padding[0]+'px';
        break;
        }        
        
        Edo.util.Dom.setSize(this.labelCt, w, h);
        Edo.util.Dom.applyStyles(this.labelCt, style);
    },
    getInnerHtml: function(sb){    
        sb[sb.length] = '<label for="'+this.forId+'" class="e-formitem-label '+this.labelCls+'" style="overflow:hidden;text-align:'+this.labelAlign+';'+this.labelStyle+';">'+this.label+'</label>';
        Edo.containers.Panel.superclass.getInnerHtml.call(this, sb);
    },        
    createChildren: function(el){        
        Edo.containers.FormItem.superclass.createChildren.call(this, el);
        this.scrollEl = this.el.lastChild;
        
        this.labelCt = this.el.firstChild;
    }
});
 
Edo.containers.FormItem.regType('formitem');