-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfieldcontainer.js
More file actions
370 lines (347 loc) · 9.02 KB
/
Copy pathfieldcontainer.js
File metadata and controls
370 lines (347 loc) · 9.02 KB
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
367
368
369
370
/**
* @fileOverview 表单字段的容器扩展
* @ignore
*/
var $ = require('jquery'),
BUI = require('bui-common'),
Field = require('./field'),
GroupValid = require('./groupvalid'),
PREFIX = BUI.prefix;
var FIELD_XCLASS = 'form-field',
CLS_FIELD = PREFIX + FIELD_XCLASS,
CLS_GROUP = PREFIX + 'form-group',
FIELD_TAGS = 'input,select,textarea';
function isField(node){
return node.is(FIELD_TAGS);
}
/**
* 获取节点需要封装的子节点
* @ignore
*/
function getDecorateChilds(node,srcNode){
if(node != srcNode){
if(isField(node)){
return [node];
}
var cls = node.attr('class');
if(cls && (cls.indexOf(CLS_GROUP) !== -1 || cls.indexOf(CLS_FIELD) !== -1)){
return [node];
}
}
var rst = [],
children = node.children();
BUI.each(children,function(subNode){
rst = rst.concat(getDecorateChilds($(subNode),srcNode));
});
return rst;
}
var containerView = BUI.Component.View.extend([GroupValid.View]);
/**
* 表单字段容器的扩展类
* @class BUI.Form.FieldContainer
* @extends BUI.Component.Controller
* @mixins BUI.Form.GroupValid
*/
var container = BUI.Component.Controller.extend([GroupValid],
{
//同步数据
syncUI : function(){
var _self = this,
fields = _self.getFields(),
validators = _self.get('validators');
BUI.each(fields,function(field){
var name = field.get('name');
if(validators[name]){
field.set('validator',validators[name]);
}
});
BUI.each(validators,function(item,key){
//按照ID查找
if(key.indexOf('#') == 0){
var id = key.replace('#',''),
child = _self.getChild(id,true);
if(child){
child.set('validator',item);
}
}
});
},
/**
* 获取封装的子控件节点
* @protected
* @override
*/
getDecorateElments : function(){
var _self = this,
el = _self.get('el');
var items = getDecorateChilds(el,el);
return items;
},
/**
* 根据子节点获取对应的子控件 xclass
* @protected
* @override
*/
findXClassByNode : function(childNode, ignoreError){
if(childNode.attr('type') === 'checkbox'){
return FIELD_XCLASS + '-checkbox';
}
if(childNode.attr('type') === 'radio'){
return FIELD_XCLASS + '-radio';
}
if(childNode.attr('type') === 'number'){
return FIELD_XCLASS + '-number';
}
if(childNode.hasClass('calendar')){
return FIELD_XCLASS + '-date';
}
if(childNode[0].tagName == "SELECT"){
return FIELD_XCLASS + '-select';
}
if(isField(childNode)){
return FIELD_XCLASS;
}
return BUI.Component.Controller.prototype.findXClassByNode.call(this,childNode, ignoreError);
},
/**
* 获取表单编辑的对象
* @return {Object} 编辑的对象
*/
getRecord : function(){
var _self = this,
rst = {},
fields = _self.getFields();
BUI.each(fields,function(field){
var name = field.get('name'),
value = _self._getFieldValue(field);
if(!rst[name]){//没有值,直接赋值
rst[name] = value;
}else if(BUI.isArray(rst[name]) && value != null){//已经存在值,并且是数组,加入数组
rst[name].push(value);
}else if(value != null){ //否则封装成数组,并加入数组
var arr = [rst[name]]
arr.push(value);
rst[name] = arr;
}
});
return rst;
},
/**
* 获取表单字段
* @return {Array} 表单字段
*/
getFields : function(name){
var _self = this,
rst = [],
children = _self.get('children');
BUI.each(children,function(item){
if(item instanceof Field){
if(!name || item.get('name') == name){
rst.push(item);
}
}else if(item.getFields){
rst = rst.concat(item.getFields(name));
}
});
return rst;
},
/**
* 根据name 获取表单字段
* @param {String} name 字段名
* @return {BUI.Form.Field} 表单字段或者 null
*/
getField : function(name){
var _self = this,
fields = _self.getFields(),
rst = null;
BUI.each(fields,function(field){
if(field.get('name') === name){
rst = field;
return false;
}
});
return rst;
},
/**
* 根据索引获取字段的name
* @param {Number} index 字段的索引
* @return {String} 字段名称
*/
getFieldAt : function (index) {
return this.getFields()[index];
},
/**
* 根据字段名
* @param {String} name 字段名
* @param {*} value 字段值
*/
setFieldValue : function(name,value){
var _self = this,
fields = _self.getFields(name);
BUI.each(fields,function(field){
_self._setFieldValue(field,value);
});
},
//设置字段域的值
_setFieldValue : function(field,value){
//如果字段不可用,则不能设置值
if(field.get('disabled')){
return;
}
//如果是可勾选的
if(field instanceof Field.Check){
var fieldValue = field.get('value');
if(value !== undefined && (fieldValue == value || (BUI.isArray(value) && BUI.Array.contains(fieldValue,value)))){
field.set('checked',true);
}else{
field.set('checked',false);
}
}else{
if(value == null){
value = '';
}
field.clearErrors(true);//清理错误
field.set('value',value);
}
},
/**
* 获取字段值,不存在字段时返回null,多个同名字段时,checkbox返回一个数组
* @param {String} name 字段名
* @return {*} 字段值
*/
getFieldValue : function(name){
var _self = this,
fields = _self.getFields(name),
rst = [];
BUI.each(fields,function(field){
var value = _self._getFieldValue(field);
if(value){
rst.push(value);
}
});
if(rst.length === 0){
return null;
}
if(rst.length === 1){
return rst[0]
}
return rst;
},
//获取字段域的值
_getFieldValue : function(field){
if(!(field instanceof Field.Check) || field.get('checked')){
return field.get('value');
}
return null;
},
/**
* 清除所有表单域的值
*/
clearFields : function(){
this.clearErrors(true);
this.setRecord({})
},
/**
* 设置表单编辑的对象
* @param {Object} record 编辑的对象
*/
setRecord : function(record){
var _self = this,
fields = _self.getFields();
BUI.each(fields,function(field){
var name = field.get('name');
_self._setFieldValue(field,record[name]);
});
},
/**
* 更新表单编辑的对象
* @param {Object} record 编辑的对象
*/
updateRecord : function(record){
var _self = this,
fields = _self.getFields();
BUI.each(fields,function(field){
var name = field.get('name');
if(record.hasOwnProperty(name)){
_self._setFieldValue(field,record[name]);
}
});
},
/**
* 设置控件获取焦点,设置第一个子控件获取焦点
*/
focus : function(){
var _self = this,
fields = _self.getFields(),
firstField = fields[0];
if(firstField){
firstField.focus();
}
},
//禁用控件
_uiSetDisabled : function(v){
var _self = this,
children = _self.get('children');
BUI.each(children,function(item){
item.set('disabled',v);
});
}
},
{
ATTRS : {
/**
* 表单的数据记录,以键值对的形式存在
* @type {Object}
*/
record : {
setter : function(v){
this.setRecord(v);
},
getter : function(){
return this.getRecord();
}
},
/**
* 内部元素的验证函数,可以使用2中选择器
* <ol>
* <li>id: 使用以'#'为前缀的选择器,可以查找字段或者分组,添加联合校验</li>
* <li>name: 不使用任何前缀,没查找表单字段</li>
* </ol>
* @type {Object}
*/
validators : {
value : {
}
},
/**
* 默认的加载控件内容的配置,默认值:
* <pre>
* {
* property : 'children',
* dataType : 'json'
* }
* </pre>
* @type {Object}
*/
defaultLoaderCfg : {
value : {
property : 'children',
dataType : 'json'
}
},
disabled : {
sync : false
},
isDecorateChild : {
value : true
},
xview : {
value : containerView
}
}
},{
xclass : 'form-field-container'
}
);
container.View = containerView;
module.exports = container;