-
Notifications
You must be signed in to change notification settings - Fork 1
/
kino.Form.js
370 lines (322 loc) · 11.7 KB
/
kino.Form.js
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
/// <reference path="jquery.js" />
/// <reference path="kino.Template.js" />
(function () {
this.kino = this.kino || {};
var p = {
};
var f = function (s) {
p.init.call(this);
for (var i in s)
this[i] = s[i];
p.initItemMap.call(this, s);
}
var fi = {
_itemTypeHash: {},
_itemTemplateHash: {},
addType: function (s) {
///<summary>
///添加控件类型
///</summary>
///<param name="s" type="Json">控件属性</param>
if (typeof s.extend !== 'undefined') {
var extend = this._itemTypeHash[s.extend];
for (var i in extend)
if (typeof s[i] === 'undefined')
s[i] = extend[i];
}
//check if the new type implement getValue and getHtml method
if (typeof s.getValue === "undefined" || typeof s.getHtml === "undefined")
throw new Error("not yet emplement getValue or getHtml method");
this._itemTypeHash[s.type] = s;
return this;
},
removeType: function (typeName) {
delete this._itemTypeHash[typeName];
},
getType: function (typeName) {
return this._itemTypeHash[typeName];
},
setTempFunc: function (typeName, templateFunc) {
return this._itemTemplateHash[typeName] = templateFunc;
},
getTempFunc: function (typeName) {
return this._itemTemplateHash[typeName];
}
};
this.kino.Form = f;
this.kino.Item = fi;
p.initItemMap = function (s) {
if (s == null)
return;
if (s.items != null && s.items.length > 0) {
for (var i = 0; i < s.items.length; i++) {
this.addItem(s.items[i]);
}
}
}
p.init = function () {
///<summary>
///初始化
///</summary>
this.render = null;
this.items = new Array();
this.afterList = new Array();
this.groups = new Array();
this.initValues = {};
this.itemMap = {};
this.colnum = 1;
this.enableEmptyFix = true;
this.isView = false;
this.postHandle = $.post;
this.events = {};
}
f.prototype.renderTo = function (dom) {
///<summary>
///绑定容器
///</summary>
///<param name="dom" type="dom">容器DOM</param>
this.render = dom;
}
f.prototype.addItem = function (item) {
///<summary>
///增加表单元素
///</summary>
///<param name="item" type="json">表单元素</param>
var that = this;
if (typeof item.type !== 'undefined') {
var itemType = fi.getType(item.type);
//初始化item
if (typeof itemType.init === 'function')
itemType.init.call(item);
//item事件绑定监听
if (typeof item.events !== 'undefined')
itemType.eventHandle(item, this);
//添加到绑定后触发队列
if (typeof itemType.after === 'function')
this.afterList.push(function () {
var _item = this.get(item.name);
itemType.after.call(this, _item);
});
}
var len = this.groups.length;
if (len != 0 && this.groups[len - 1].name == "")
this.groups[len - 1].items.push(item);
else {
var newgroup = {
name: "",
items: [item]
}
this.groups.push(newgroup);
}
this.itemMap[item.name] = item;
}
f.prototype.setValues = function (s) {
for (var i in s)
this.itemMap[i].value = s[i];
};
f.prototype.enableViewMode = function () {
this.isView = true;
};
p.items2group = function () {
if (this.items == null || this.items.length == 0)
return;
var len = this.groups.length;
if (len != 0 && this.groups[len - 1].name == "")
this.groups[len - 1].items = this.groups[len - 1].items.concat(this.items);
else {
var newgroup = {
name: "",
items: this.items
}
this.groups.push(newgroup);
this.items = null;
}
};
f.templateStr = (function () {
var str = "<table>";
str = str + "@for(var i = 0; i < items.length; i++){"
str = str + "<tr>@if(items[i].obj.label!=null){<td>@items[i].obj.label</td>}<td @if(items[i].obj.label==null){colspan='2'}>@items[i].html</td></tr>"
str = str + "}</table>";
return str;
})();
f.prototype.bind = function (render) {
var that = this;
this.render = render || this.render;
var itemList = new Array();
for (var itemName in this.itemMap) {
var item = this.itemMap[itemName];
//判断类型来决定是否生成UI
if (typeof item.type !== 'undefined')
itemList[itemList.length] = {
obj: item,
html: p.getCellHtml.call(this, item)
}
//监听事件
if (typeof item.event !== 'undefined') {
for (var itemEvent in item.event) {
var currentEvent = item.event[itemEvent];
$(this.render).on(itemEvent, "[name='" + item.name + "']", function (e) {
currentEvent.call(this, e, that);
});
}
}
}
var html = kino.template(f.templateStr, { items: itemList }, { enableCleanMode: true, enableEscape: false });
this.render.innerHTML = html;
//处理after列表
for (var i = 0; i < this.afterList.length; i++)
this.afterList[i].call(this);
};
f.prototype.get = function (name) {
///<summary>
///根据name属性获取对应的item
///</summary>
///<param name="name" type="String">item的name属性</param>
///<returns type="Item"/>
var item = this.itemMap[name];
if (typeof item.$el === 'undefined')
item.$el = $(this.render).find("[name='" + name + "']");
if (typeof item.el === 'undefined')
item.el = item.$el[0];
return item;
};
p.getCellHtml = function (item) {
if (typeof this.initValues[item.name] !== 'undefined')
item.value = this.initValues[item.name];
//获取自定义属性字符串
var attrStr = "";
for (var i in item.attr) {
attrStr = attrStr + i + "=\"" + item.attr[i] + "\" ";
}
//使用替换掉attr的json配置
var _item = {};
for (var i in item) {
if (i != 'attr')
_item[i] = item[i];
};
_item.attr = attrStr;
return p.getConvertedHtml(_item);
};
p.getConvertedHtml = function (item) {
//获取模板文本
var templateText = fi.getType(item.type).getHtml();
//判断模板函数是否生成,是则获取并使用,否则就创建一个并缓存起来
var tempFunc = fi.getTempFunc(item.type);
if (tempFunc == null)
tempFunc = fi.setTempFunc(item.type, kino.getTemplateFunc(templateText, {
enableCleanMode: true,
enableEscape: false
}));
return kino.template(tempFunc, item);
}
p.isAddCell2Row = function (colnum, len, j, vcount) {
return colnum == 1 || vcount % colnum == 0 || j == len - 1;
};
f.prototype.getParams = function () {
///<summary>
///根据表单内容获取请求json
///</summary>
///<returns type="JSON" />
return this.getModelParam("");
};
f.prototype.getModelParam = function (modelName) {
///<summary>
///根据表单内容获取请求json
///</summary>
///<param name="nodelName" type="String">model名</param>
///<returns type="JSON" />
var json = {};
var mn = modelName;
if (mn == null)
mn = "model.";
else if (mn != '')
mn = mn + ".";
for (var i = 0; i < this.groups.length; i++) {
var items = this.groups[i].items;
for (var j = 0; j < items.length; j++) {
if (items[j].type == undefined) {
if (typeof (items[j].value) == "undefined") {
if (this.initValues[items[j].name] != null)
json[mn + items[j].name] = this.initValues[items[j].name];
}
else
json[mn + items[j].name] = items[j].value;
}
else {
var itemType = fi.getType(items[j].type);
json[mn + items[j].name] = itemType.getValue.call({ form: this, item: items[j] });
}
}
}
return json;
};
//增加类型
fi.addType({
type: 'base',
eventHandle: function (item, form) {
var eventList = ["click", "change", "dbclick", "mouseover", "mouseout", "keyup", "keydown"];
for (var i = 0; i < eventList.length; i++)
if (typeof item.events[eventList[i]] !== 'undefined' && typeof item.events[eventList[i]] === 'function') {
var eventName = eventList[i];
$(form.render).on(eventName, "[name=" + item.name + "]", function (e) {
item.events[eventName].call(this, e, item, form);
});
}
},
getHtml: function () {
return "";
},
getValue: function () {
return null;
}
}).addType({
type: 'txt',
extend: 'base',
getHtml: function () {
return "<input name='@name' type='text' value='@value' @attr />"
},
getValue: function () {
return this.form.get(this.item.name).$el.val();
}
}).addType({
type: 'msg',
getHtml: function () {
return "<span name='@name' @attr>@value</span>";
},
getValue: function () {
return this.form.get(this.item.name).$el.html();
}
}).addType({
type: 'list',
extend: 'base',
init: function () {
if (typeof this.dataField === 'undefined' && typeof this.textField === 'undefined') {
if (Object.prototype.toString.call(this.data[0]) == '[object Array]') {
this.dataField = 0;
this.textField = 1;
}
else if (typeof this.data[0].text !== 'undefined' && typeof this.data[0].value !== 'undefined') {
this.dataField = 'value';
this.textField = 'text';
}
}
},
getHtml: function () {
var str = "<select name='@name' @attr>@for(var i = 0; i < data.length; i++){<option value='@data[i][dataField]' ";
str = str + "@if(typeof selectedIndex!=='undefined' && selectedIndex==i){selected='selected'}";
str = str + "else if(typeof selectedValue!=='undefined' && selectedValue==data[i][dataField]){selected='selected'}"
str = str + ">@data[i][textField]</option>}</select>";
return str;
},
getValue: function () {
return this.form.get(this.item.name).$el.find("option:selected").val();
}
}).addType({
type: 'checkbox',
getHtml: function () {
var str = ""
},
getValue: function () {
}
});
})();