jiapeng 8 years ago
parent
commit
a1cf46f098
7 changed files with 1224 additions and 802 deletions
  1. 369 368
      js/codehtml.js
  2. 95 0
      js/dict.js
  3. 76 0
      js/dropdown.js
  4. 132 156
      js/form.js
  5. 186 0
      js/select.js
  6. 53 29
      js/text.js
  7. 313 249
      js/util.js

+ 369 - 368
js/codehtml.js

@ -1,103 +1,166 @@
1
!(function($) {
2
	$.define(["jQuery", "doc", "body"], "code", function($, doc, $body) {
3
		/**
4
		 * env={ ce:current Element; cd:current Data; ci:current index in
5
		 * ListElement array[ci=index] or object[ci=key] es:Element stack; Array
6
		 * ds:Data stack; Array is:index stack;Array sh:value handler
7
		 * function(env,key){return [String]} dir:directive handler
8
		 * Aarray(function(env){}.call(elementObj={},env) }
9
		 */
10
		var ch_val_buider = {
11
				"c": function( /* env */ ) {
12
					return this.k;
13
				},
14
				"s": function(env) {
15
					var v = env.cd;
16
					if(v) v = this.k ? v[this.k] : v;
17
					if(v) return v;
18
					return "";
19
				},
20
				"date": function(env) {}
21
			},
22
			simpleAttrHandler = function(env) {
23
				env.ce.setAttribute(this.n, this.v);
24
			},
25
			AttrHandler = function(env) {
26
				var ret = [],
27
					va = this.v;
28
				for(var i = 0; i < va.length; ++i) {
29
					var item = va[i];
30
					ret.push[env.sh[item.h].call(this, env)];
31
				}
32
				env.ce.setAttribute(this.n, ret.join(""));
33
			},
34
			/**
35
			 * r:array s:text value f:lslast value in array Is * Text
36
			 */
37
			strSplit_s = function(r, s, f) {
38
				if(f) {
39
					var tmp = r[r.length - 1];
40
					tmp.k = tmp.k + s;
41
				} else {
42
					r.push({ k: s, h: "c" });
43
				}
1
$.define(["jQuery", "doc", "body"], "code", function($, doc, $body) {
2
	/**
3
	 * env={ ce:current Element; cd:current Data; ci:current index in
4
	 * ListElement array[ci=index] or object[ci=key] es:Element stack; Array
5
	 * ds:Data stack; Array is:index stack;Array sh:value handler
6
	 * function(env,key){return [String]} dir:directive handler
7
	 * Aarray(function(env){}.call(elementObj={},env) }
8
	 */
9
	var ch_val_buider = {
10
			"c": function( /* env */ ) {
11
				return this.k;
44 12
			},
45
			strSplit_o = function(r /* array */ , shell /* trim value like {{shell}} */ ) {
46
				var tmp = shell.split("-");
47
				var obj = { k: tmp.shift(), h: "s" };
48
				if(tmp.length) { obj.h = tmp.shift(); }
49
				if(tmp.length) { obj.p = tmp; }
50
				r.push(obj);
13
			"s": function(env) {
14
				var v = env.cd;
15
				if(v) v = this.k ? v[this.k] : v;
16
				if(v) return v;
17
				return "";
51 18
			},
52
			strSplit = function(s) { // s.length>4
53
				var r = [],
54
					len = s.length,
55
					si = 0,
56
					/* parse start index */
57
					ei, /* end shell ei = s.indexOf("}}",bi+2) */
58
					shell, /* {{shell}} */
59
					tmp, /* last shell */
60
					f /* prev is not shell */ ,
61
					bi = nv.indexOf("{{"); /* begin shell bi = s.indexOf("{{",si) */
62
				if(bi >= 0) {
63
					ei = nv.indexOf("}}", bi);
64
					while(si < len) {
65
						if(bi > si) { r.push({ k: s.substring(si, bi), h: "c" });
66
							f = true; }
67
						shell = s.substring(bi + 2, ei).trim();
68
						si = ei + 2;
69
						if(shell.length) {
70
							f = false;
71
							strSplit_o(r, shell);
72
						} else { strSplit_s(r, s.substring(bi, si), f);
73
							f = true; }
74
						if(si >= len) return r;
75
						bi = s.indexOf("{{", si);
76
						if(bi < 0) { strSplit_s(r, s.substring(si), f); return r; }
77
						ei = s.indexOf("}}", bi);
78
						if(ei < 0) { strSplit_s(r, s.substring(si), f); return r; }
19
			"date": function(env) {}
20
		},
21
		simpleAttrHandler = function(env) {
22
			env.ce.setAttribute(this.n, this.v);
23
		},
24
		AttrHandler = function(env) {
25
			var ret = [],
26
				va = this.v;
27
			for(var i = 0; i < va.length; ++i) {
28
				var item = va[i];
29
				ret.push[env.sh[item.h].call(this, env)];
30
			}
31
			env.ce.setAttribute(this.n, ret.join(""));
32
		},
33
		/**
34
		 * r:array s:text value f:lslast value in array Is * Text
35
		 */
36
		strSplit_s = function(r, s, f) {
37
			if(f) {
38
				var tmp = r[r.length - 1];
39
				tmp.k = tmp.k + s;
40
			} else {
41
				r.push({ k: s, h: "c" });
42
			}
43
		},
44
		strSplit_o = function(r /* array */ , shell /* trim value like {{shell}} */ ) {
45
			var tmp = shell.split("-");
46
			var obj = { k: tmp.shift(), h: "s" };
47
			if(tmp.length) { obj.h = tmp.shift(); }
48
			if(tmp.length) { obj.p = tmp; }
49
			r.push(obj);
50
		},
51
		strSplit = function(s) { // s.length>4
52
			var r = [],
53
				len = s.length,
54
				si = 0,
55
				/* parse start index */
56
				ei, /* end shell ei = s.indexOf("}}",bi+2) */
57
				shell, /* {{shell}} */
58
				tmp, /* last shell */
59
				f /* prev is not shell */ ,
60
				bi = nv.indexOf("{{"); /* begin shell bi = s.indexOf("{{",si) */
61
			if(bi >= 0) {
62
				ei = nv.indexOf("}}", bi);
63
				while(si < len) {
64
					if(bi > si) {
65
						r.push({ k: s.substring(si, bi), h: "c" });
66
						f = true;
67
					}
68
					shell = s.substring(bi + 2, ei).trim();
69
					si = ei + 2;
70
					if(shell.length) {
71
						f = false;
72
						strSplit_o(r, shell);
73
					} else {
74
						strSplit_s(r, s.substring(bi, si), f);
75
						f = true;
79 76
					}
77
					if(si >= len) return r;
78
					bi = s.indexOf("{{", si);
79
					if(bi < 0) { strSplit_s(r, s.substring(si), f); return r; }
80
					ei = s.indexOf("}}", bi);
81
					if(ei < 0) { strSplit_s(r, s.substring(si), f); return r; }
80 82
				}
81
				return s;
82
			},
83
			strCompile = function(s) {
84
				var r = strSplit(s);
83
			}
84
			return s;
85
		},
86
		strCompile = function(s) {
87
			var r = strSplit(s);
88
			if(typeof s != "string") {
89
				return r.length > 1 ? r : r[0];
90
				/*
91
				 * [{k:"",h:"",p:[]},...] :
92
				 * {k:"",h:"",p:[]}
93
				 */
94
			}
95
			return s; // String;
96
		},
97
		simpleAttrHand = function(env) {
98
			env.ce.setAttribute(this.n, this.v);
99
		},
100
		singleAttrHand = function(env) {
101
			env.ce.setAttribute(this.n, env.sh[this.h].call(this.v, env));
102
		},
103
		arrayAttrHand = function(env) {
104
			var item, ret = [],
105
				vs = this.v,
106
				len = vs.length;
107
			for(var i = 0; i < len; ++i) {
108
				item = vs[i];
109
				ret.push(env.sh[item.h].call(item, env));
110
			}
111
			env.ce.setAttribute(this.n, ret.join(""));
112
		},
113
		/* ret.h(env); attribute compiler */
114
		attrCompile = function(attr) {
115
			var s = attr.value || "",
116
				ret = { n: attr.name, v: s, h: simpleAttrHand };
117
			if(s.length > 4) {
118
				s = strCompile(s);
85 119
				if(typeof s != "string") {
86
					return r.length > 1 ? r : r[0];
87
					/*
88
					 * [{k:"",h:"",p:[]},...] :
89
					 * {k:"",h:"",p:[]}
90
					 */
120
					ret.v = s;
121
					// is Array
122
					ret.h = s.length ? arrayAttrHand : singleAttrHand;
91 123
				}
92
				return s; // String;
93
			},
94
			simpleAttrHand = function(env) {
95
				env.ce.setAttribute(this.n, this.v);
96
			},
97
			singleAttrHand = function(env) {
98
				env.ce.setAttribute(this.n, env.sh[this.h].call(this.v, env));
99
			},
100
			arrayAttrHand = function(env) {
124
			}
125
			return ret;
126
		},
127
		simpleTextHand = function(env) {
128
			env.ce.appendChild(doc.createTextNode(this.v));
129
		},
130
		singleTextHand = function(env) {
131
			env.ce.appendChild(doc.createTextNode(env.sh[this.h].call(this.v, env)));
132
		},
133
		arrayTextHand = function(env) {
134
			var item, ret = [],
135
				vs = this.v,
136
				len = vs.length;
137
			for(var i = 0; i < len; ++i) {
138
				item = vs[i];
139
				ret.push(env.sh[item.h].call(item, env));
140
			}
141
			env.ce.appendChild(doc.createTextNode(ret.join("")));
142
		},
143
		/* ret.h(env); textNode compiler */
144
		textCompile = function(s /* s = textNode.nodeValue */ ) {
145
			var len = s.length,
146
				ret = { v: s, h: "_t" };
147
			if(len > 4) {
148
				s = textCompile(s);
149
				if(typeof s != "string") {
150
					ret.v = s;
151
					// is Array
152
					ret.h = s.length ? "_at" : "_st";
153
				}
154
			}
155
			return ret;
156
		},
157
		ch_dir_container = {
158
			/* simpleTextHand= */
159
			"_t": function(env) { env.ce.appendChild(doc.createTextNode(this.v)); },
160
			/* singleTextHand= */
161
			"_st": function(env) { env.ce.appendChild(doc.createTextNode(env.sh[this.h].call(this.v, env))); },
162
			/* arrayTextHand= */
163
			"_at": function(env) {
101 164
				var item, ret = [],
102 165
					vs = this.v,
103 166
					len = vs.length;
@ -105,300 +168,238 @@
105 168
					item = vs[i];
106 169
					ret.push(env.sh[item.h].call(item, env));
107 170
				}
108
				env.ce.setAttribute(this.n, ret.join(""));
171
				env.ce.appendChild(doc.createTextNode(ret.join("")));
109 172
			},
110
			/* ret.h(env); attribute compiler */
111
			attrCompile = function(attr) {
112
				var s = attr.value || "",
113
					ret = { n: attr.name, v: s, h: simpleAttrHand };
114
				if(s.length > 4) {
115
					s = strCompile(s);
116
					if(typeof s != "string") {
117
						ret.v = s;
118
						// is Array
119
						ret.h = s.length ? arrayAttrHand : singleAttrHand;
120
					}
173
			"_": function(env) {
174
				var ele = doc.createElement(this.n),
175
					as = this.as,
176
					es = this.es,
177
					item;
178
				env.ce.appendChild(ele);
179
				env.es.push(env.ce);
180
				env.ce = ele;
181
				for(var i = 0; i < as.length; ++i) {
182
					as[i].h(env);
121 183
				}
122
				return ret;
123
			},
124
			simpleTextHand = function(env) {
125
				env.ce.appendChild(doc.createTextNode(this.v));
126
			},
127
			singleTextHand = function(env) {
128
				env.ce.appendChild(doc.createTextNode(env.sh[this.h].call(this.v, env)));
129
			},
130
			arrayTextHand = function(env) {
131
				var item, ret = [],
132
					vs = this.v,
133
					len = vs.length;
134
				for(var i = 0; i < len; ++i) {
135
					item = vs[i];
136
					ret.push(env.sh[item.h].call(item, env));
184
				for(var i = 0; i < es.length; ++i) {
185
					item = es[i];
186
					env.dir(item.h).call(item, env);
137 187
				}
138
				env.ce.appendChild(doc.createTextNode(ret.join("")));
188
				env.ce = env.es.pop();
139 189
			},
140
			/* ret.h(env); textNode compiler */
141
			textCompile = function(s /* s = textNode.nodeValue */ ) {
142
				var len = s.length,
143
					ret = { v: s, h: "_t" };
144
				if(len > 4) {
145
					s = textCompile(s);
146
					if(typeof s != "string") {
147
						ret.v = s;
148
						// is Array
149
						ret.h = s.length ? "_at" : "_st";
190
			"list": function(env) {
191
				var hand = env.dir["_"],
192
					as = this.as,
193
					es = this.es,
194
					item, data = env.cd,
195
					p = this.p;
196
				if(data && p && p.length && p[0]) {
197
					env.ds.push(data);
198
					data = env.cd = data[p[0]] || [];
199
					env.is.push(env.ci);
200
					for(var i = 0; i < data.length; ++i) {
201
						env.ci = i;
202
						env.cd = data[i];
203
						hand.call(this, env);
204
					}
205
					env.ci = env.is.pop();
206
					env.cd = env.ds.pop();
207
				} else if(data && data.length) {
208
					env.is.push(env.ci);
209
					for(var i = 0; i < data.length; ++i) {
210
						env.ci = i;
211
						hand.call(this, env);
150 212
					}
213
					env.ci = env.is.pop();
151 214
				}
152
				return ret;
153 215
			},
154
			ch_dir_container = {
155
				/* simpleTextHand= */
156
				"_t": function(env) { env.ce.appendChild(doc.createTextNode(this.v)); },
157
				/* singleTextHand= */
158
				"_st": function(env) { env.ce.appendChild(doc.createTextNode(env.sh[this.h].call(this.v, env))); },
159
				/* arrayTextHand= */
160
				"_at": function(env) {
161
					var item, ret = [],
162
						vs = this.v,
163
						len = vs.length;
164
					for(var i = 0; i < len; ++i) {
165
						item = vs[i];
166
						ret.push(env.sh[item.h].call(item, env));
216
			"each": function(env) {
217
				var hand = env.dir["_"],
218
					as = this.as,
219
					es = this.es,
220
					item, data = env.cd,
221
					p = this.p;
222
				if(data && p && p.length && p[0]) {
223
					env.ds.push(data);
224
					env.is.push(env.ci);
225
					for(var i = 0; i < p.length; ++i) {
226
						env.ci = p[i];
227
						env.cd = data[env.ci];
228
						hand.call(this, env);
167 229
					}
168
					env.ce.appendChild(doc.createTextNode(ret.join("")));
169
				},
170
				"_": function(env) {
171
					var ele = doc.createElement(this.n),
172
						as = this.as,
173
						es = this.es,
174
						item;
175
					env.ce.appendChild(ele);
176
					env.es.push(env.ce);
177
					env.ce = ele;
178
					for(var i = 0; i < as.length; ++i) {
179
						as[i].h(env);
180
					}
181
					for(var i = 0; i < es.length; ++i) {
182
						item = es[i];
183
						env.dir(item.h).call(item, env);
230
					env.ci = env.is.pop();
231
					env.cd = env.ds.pop();
232
				} else if(data) {
233
					env.is.push(env.ci);
234
					for(var key in data) {
235
						env.ci = k;
236
						hand.call(this, env);
184 237
					}
185
					env.ce = env.es.pop();
186
				},
187
				"list": function(env) {
188
					var hand = env.dir["_"],
189
						as = this.as,
190
						es = this.es,
191
						item, data = env.cd,
192
						p = this.p;
193
					if(data && p && p.length && p[0]) {
194
						env.ds.push(data);
195
						data = env.cd = data[p[0]] || [];
196
						env.is.push(env.ci);
197
						for(var i = 0; i < data.length; ++i) {
198
							env.ci = i;
199
							env.cd = data[i];
200
							hand.call(this, env);
201
						}
202
						env.ci = env.is.pop();
203
						env.cd = env.ds.pop();
204
					} else if(data && data.length) {
205
						env.is.push(env.ci);
206
						for(var i = 0; i < data.length; ++i) {
207
							env.ci = i;
208
							hand.call(this, env);
209
						}
210
						env.ci = env.is.pop();
238
					env.ci = env.is.pop();
239
				}
240
			},
241
			"val": function(env) {
242
				var hand = env.dir["_"],
243
					as = this.as,
244
					es = this.es,
245
					items, item, data = env.cd,
246
					p = this.p;
247
				if(p && p.length && p[0]) {
248
					items = p[0].split("\\.");
249
					env.ds.push(data);
250
					while(items.length && data) {
251
						data = data[items.shift()];
211 252
					}
212
				},
213
				"each": function(env) {
214
					var hand = env.dir["_"],
215
						as = this.as,
216
						es = this.es,
217
						item, data = env.cd,
218
						p = this.p;
219
					if(data && p && p.length && p[0]) {
220
						env.ds.push(data);
221
						env.is.push(env.ci);
222
						for(var i = 0; i < p.length; ++i) {
223
							env.ci = p[i];
224
							env.cd = data[env.ci];
225
							hand.call(this, env);
226
						}
227
						env.ci = env.is.pop();
228
						env.cd = env.ds.pop();
229
					} else if(data) {
230
						env.is.push(env.ci);
231
						for(var key in data) {
232
							env.ci = k;
233
							hand.call(this, env);
234
						}
235
						env.ci = env.is.pop();
253
					if(items.length === 0 && data) {
254
						env.cd = data;
255
						hand.call(this, env);
236 256
					}
237
				},
238
				"val": function(env) {
239
					var hand = env.dir["_"],
240
						as = this.as,
241
						es = this.es,
242
						items, item, data = env.cd,
243
						p = this.p;
244
					if(p && p.length && p[0]) {
245
						items = p[0].split("\\.");
246
						env.ds.push(data);
247
						while(items.length && data) {
248
							data = data[items.shift()];
249
						}
250
						if(items.length === 0 && data) {
251
							env.cd = data;
252
							hand.call(this, env);
253
						}
254
						env.cd = env.ds.pop();
257
					env.cd = env.ds.pop();
258
				}
259
			},
260
			"valTag": function(env) {
261
				var hand = env.dir["_"],
262
					as = this.as,
263
					es = this.es,
264
					items, item, data = env.cd,
265
					p = this.p;
266
				if(p && p.length && p[0]) {
267
					items = p[0].split("\\.");
268
					env.ds.push(data);
269
					while(items.length && data) {
270
						data = data[items.shift()];
255 271
					}
256
				},
257
				"valTag": function(env) {
258
					var hand = env.dir["_"],
259
						as = this.as,
260
						es = this.es,
261
						items, item, data = env.cd,
262
						p = this.p;
263
					if(p && p.length && p[0]) {
264
						items = p[0].split("\\.");
265
						env.ds.push(data);
266
						while(items.length && data) {
267
							data = data[items.shift()];
272
					if(items.length === 0 && data) {
273
						env.cd = data;
274
						var ele = doc.createElement(this.n);
275
						env.ce.appendChild(ele);
276
						env.es.push(env.ce);
277
						env.ce = ele;
278
						for(var i = 0; i < as.length; ++i) {
279
							as[i].h(env);
268 280
						}
269
						if(items.length === 0 && data) {
270
							env.cd = data;
271
							var ele = doc.createElement(this.n);
272
							env.ce.appendChild(ele);
273
							env.es.push(env.ce);
274
							env.ce = ele;
275
							for(var i = 0; i < as.length; ++i) {
276
								as[i].h(env);
277
							}
278
							for(var i = 0; i < es.length; ++i) {
279
								item = es[i];
280
								env.dir(item.h).call(item, env);
281
							}
282
							env.ce = env.es.pop();
281
						for(var i = 0; i < es.length; ++i) {
282
							item = es[i];
283
							env.dir(item.h).call(item, env);
283 284
						}
284
						env.cd = env.ds.pop();
285
						env.ce = env.es.pop();
285 286
					}
287
					env.cd = env.ds.pop();
286 288
				}
287
			},
288
			childCompile = function(chs, ret) {
289
				var lisT = false;
290
				for(var i = 0; i < chs.length; ++i) {
291
					var ch = chs[i];
292
					if(ch.nodeType == 1) {
293
						if(lisT) {
294
							ret.push(textCompile(s));
295
						}
296
						ret.push(eleCompile(ch));
297
						lisT = false;
298
					} else if(ch.nodeType == 3) {
299
						s = listT ? (s + ch.nodeValue) : ch.nodeValue;
300
						listT = true;
289
			}
290
		},
291
		childCompile = function(chs, ret) {
292
			var lisT = false;
293
			for(var i = 0; i < chs.length; ++i) {
294
				var ch = chs[i];
295
				if(ch.nodeType == 1) {
296
					if(lisT) {
297
						ret.push(textCompile(s));
301 298
					}
299
					ret.push(eleCompile(ch));
300
					lisT = false;
301
				} else if(ch.nodeType == 3) {
302
					s = listT ? (s + ch.nodeValue) : ch.nodeValue;
303
					listT = true;
302 304
				}
303
				if(listT) {
304
					ret.push(textCompile(s));
305
				}
306
			},
307
			/* ret.h(env) Element compiler */
308
			eleCompile = function(ele) {
309
				var attrs = ele.attributes,
310
					cdir = ele.getAttribute("ch-dir") || "_",
311
					ret = { n: ele.node },
312
					as = ret.as = [],
313
					es = ret.es = [],
314
					lisT, s, ch;
315
				ch = cdir.split("-");
316
				ret.h = ch.shift();
317
				if(ch.length) ret.p = ch;
318
				for(var i = 0; i < attrs.length; ++i) {
319
					as.push(attrCompile(attrs[i]));
320
				}
321
				childCompile(ele.children, ret.es);
322
				return tet;
323
			},
324
			parseElement = function(ele) {
325
				var env = { sh: {}, dir: {} },
326
					chs = [];
327
				childCompile(ele.children, chs);
328
				$.extend(env.sh, ch_val_buider);
329
				$.extend(env.dir, ch_dir_container);
330
				return {
331
					"shell": function(name, hand) {
332
						if(hand) {
333
							env.sh[name] = hand;
334
							return this;
335
						} else return env.sh[name];
336
					},
337
					"dir": function(name, hand) {
338
						if(hand) {
339
							env.dir[name] = hand;
340
							return this;
341
						} else return env.dir[name];
342
					},
343
					"fill": function(pe, data) {
344
						env.es = [];
345
						env.ds = [];
346
						env.is = [];
347
						env.ce = pe, env.cd = data;
348
						for(var i = 0; i < chs.length; ++i) {
349
							var ch = chs[i];
350
							env.dir[ch.h].call(ch, env);
351
						}
305
			}
306
			if(listT) {
307
				ret.push(textCompile(s));
308
			}
309
		},
310
		/* ret.h(env) Element compiler */
311
		eleCompile = function(ele) {
312
			var attrs = ele.attributes,
313
				cdir = ele.getAttribute("ch-dir") || "_",
314
				ret = { n: ele.node },
315
				as = ret.as = [],
316
				es = ret.es = [],
317
				lisT, s, ch;
318
			ch = cdir.split("-");
319
			ret.h = ch.shift();
320
			if(ch.length) ret.p = ch;
321
			for(var i = 0; i < attrs.length; ++i) {
322
				as.push(attrCompile(attrs[i]));
323
			}
324
			childCompile(ele.children, ret.es);
325
			return tet;
326
		},
327
		parseElement = function(ele) {
328
			var env = { sh: {}, dir: {} },
329
				chs = [];
330
			childCompile(ele.children, chs);
331
			$.extend(env.sh, ch_val_buider);
332
			$.extend(env.dir, ch_dir_container);
333
			return {
334
				"shell": function(name, hand) {
335
					if(hand) {
336
						env.sh[name] = hand;
352 337
						return this;
353
					}
354
				};
355
			},
356
			defBh = function(data) { return data; },
357
			parseCode(ele) = function(ele) {
358
				var hand = parseElement(ele),
359
					$ele = $(ele),
360
					bh = defBh,
361
					lses = [];
362
				return $.extend(hand, {
363
					"val": function(data) {
364
						$ele.empty();
365
						data = bh(data);
366
						var docf = doc.createDocumentFragment();
367
						this.fill(docf, data);
368
						ele.appendChild(docf);
369
						for(var i = 0; i < lses.length; ++i) {
370
							lses[i]();
371
						}
372
						return this;
373
					},
374
					"empty": function() {
375
						$(ele).empty();
376
						return this;
377
					},
378
					"listen": function(h) {
379
						if(h) lses.push(h);
338
					} else return env.sh[name];
339
				},
340
				"dir": function(name, hand) {
341
					if(hand) {
342
						env.dir[name] = hand;
380 343
						return this;
344
					} else return env.dir[name];
345
				},
346
				"fill": function(pe, data) {
347
					env.es = [];
348
					env.ds = [];
349
					env.is = [];
350
					env.ce = pe, env.cd = data;
351
					for(var i = 0; i < chs.length; ++i) {
352
						var ch = chs[i];
353
						env.dir[ch.h].call(ch, env);
381 354
					}
382
				});
383
			},
384
			parseHtmlTemplate = function(c) {
385
				var $div = $("<div style='display:none;'></div>");
386
				$div.appendTo($body).html(c);
387
				var h = parseCode($div[0]);
388
				$div.remove();
389
				return $.extend(h, {
390
					"appendTo": function(pe, data) {
391
						var docf = doc.createDocumentFragment();
392
						this.fill(docf, data);
393
						pe.appendChild(docf);
394
					}
395
				});
355
					return this;
356
				}
396 357
			};
397
		return {
398
			"parse": parseElement,
399
			"element": parseCode,
400
			"template": parseHtmlTemplate
358
		},
359
		defBh = function(data) { return data; },
360
		parseCode(ele) = function(ele) {
361
			var hand = parseElement(ele),
362
				$ele = $(ele),
363
				bh = defBh,
364
				lses = [];
365
			return $.extend(hand, {
366
				"val": function(data) {
367
					$ele.empty();
368
					data = bh(data);
369
					var docf = doc.createDocumentFragment();
370
					this.fill(docf, data);
371
					ele.appendChild(docf);
372
					for(var i = 0; i < lses.length; ++i) {
373
						lses[i]();
374
					}
375
					return this;
376
				},
377
				"empty": function() {
378
					$(ele).empty();
379
					return this;
380
				},
381
				"listen": function(h) {
382
					if(h) lses.push(h);
383
					return this;
384
				}
385
			});
386
		},
387
		parseHtmlTemplate = function(c) {
388
			var $div = $("<div style='display:none;'></div>");
389
			$div.appendTo($body).html(c);
390
			var h = parseCode($div[0]);
391
			$div.remove();
392
			return $.extend(h, {
393
				"appendTo": function(pe, data) {
394
					var docf = doc.createDocumentFragment();
395
					this.fill(docf, data);
396
					pe.appendChild(docf);
397
				}
398
			});
401 399
		};
402
	});
403
404
})(jQuery);
400
	return {
401
		"parse": parseElement,
402
		"element": parseCode,
403
		"template": parseHtmlTemplate
404
	};
405
});

+ 95 - 0
js/dict.js

@ -0,0 +1,95 @@
1
$.define(["jQuery", "util"], "dict", function($, util) {
2
	var cache = {},
3
		handCache = {},
4
		uri = "/ajax/sys/dict/item/",
5
		eh = {},
6
		config = { mask: false };
7
		load: function(code) {
8
			util.get(uri + code, null, function(data) {
9
				cache[code] = data;
10
				var hs = handCache[code];
11
				if(hs && hs.length) {
12
					for(var i = 0; i < hs.length; ++i) {
13
						hs[i](data);
14
					}
15
					delete handCache[code];
16
				}
17
			}, eh, config);
18
		};
19
	return {
20
		apply: function(code, hand) {
21
			var dict = cache[code];
22
			if(!dict) {
23
				var hs = handCache[dictCode];
24
				if(!hs) {
25
					handCache[code] = hs = [];
26
					load(dictCode);
27
				}
28
				hs.push(hand);
29
			} else {
30
				hand(dict);
31
			}
32
		},
33
		refresh: function(code, hand) {
34
			if(hand) {
35
				var hs = handCache[code];
36
				if(!hs) {
37
					handCache[code] = hs = [];
38
				}
39
				hs.push(hand);
40
			}
41
			var dict = cache[code];
42
			if(dict) {
43
				delete cache[code];
44
				load(code);
45
			}
46
		},
47
		getCap: function(items, code) {
48
			for(var i = 0; i < items.length; ++i) {
49
				var item = items[i];
50
				if(code === item.code) {
51
					return item.caption;
52
				} else if(item.children && item.children.length) {
53
					var ret = get(item.children, code);
54
					if(ret) return ret;
55
				}
56
			}
57
			return false;
58
		},
59
		get:function(code){
60
			return cache[code];
61
		},
62
		transfer: function($e, dict, code) {
63
			dict = dict || $e.attr("dict");
64
			code = code || $e.attr("code");
65
			if(dict && code) {
66
				this.apply(dict, function(items) {
67
					var cp = get(items, code);
68
					if(cp) {
69
						$e.removeClass("invalid-dict").text(cp);
70
					} else {
71
						$e.addClass("invalid-dict").text("不可翻译的");
72
					}
73
				});
74
			}
75
		},
76
		doTransfer: function() {
77
			$(".hand_dict").each(function() {
78
				var $this = $this;
79
				var dict = $this.attr("dictCode") || $this.attr("dict"),
80
					code = $this.attr("itemCode") || $this.attr("code");
81
				if(dict && code) {
82
					this.apply(dict, function(items) {
83
						var cp = this.getCap(items, code);
84
						if(cp) {
85
							$e.removeClass("invalid-dict").text(cp);
86
						} else {
87
							$e.addClass("invalid-dict").text("不可翻译的");
88
						}
89
					});
90
				}
91
				$this.removeClass("hand_dict");
92
			});
93
		}
94
	};
95
});

+ 76 - 0
js/dropdown.js

@ -0,0 +1,76 @@
1
$.define(["jQuery", "util", "doc"], "dropdown", function($, util, doc) {
2

3
	var dd_bg = "dd_bg",
4
		q_dd_bg = ".dd_bg",
5
		dd_ctn = ".dd-ctn",
6
		q_dd_ctn = ".dd-ctn",
7
		dd_hand = "dd-hand",
8
		q_dd_hand = ".dd-hand",
9
		open = "open",
10
		dd_clean = "dd-clean",
11
		dd_drop = "dd-drop",
12
		q_dd_drop = ".dd-drop",
13
		dd_hold = "dd-hold",
14
		dd_hold_once = "dd-hold-once",readOnly="readOnly",showOnly="showOnly",
15
		clearMenus = function(e) {
16
			if(e && e.which === 3) return
17
			$(q_dd_bg).remove();
18
			$(q_dd_ctn).each(function() {
19
				var $this = $(this),
20
					cls = util.classCheck(this, [open, dd_hold, dd_hold_once, dd_clean]);
21
				relatedTarget = {
22
					relatedTarget: this
23
				};
24
				if(!cls[open]) return; //hasClass('open')
25
				if(cls[dd_hold]) return; //hasClass('dd-hold')
26
				if(cls[dd_hold_once]) { //hasClass('dd-hold-once')
27
					$this.removeClass(dd_hold_once);
28
					return;
29
				}
30
				$this.trigger(evt = $.Event('hide.dropdown', relatedTarget));
31
				if(evt.isDefaultPrevented()) return;
32
				$this.removeClass(open).trigger('hidden.dropdown', relatedTarget);
33
				if(cls[dd_clean]) {
34
					$this.find(q_dd_drop).remove();
35
				}
36
			});
37
		},
38
		toggle = function(e) {
39
			var $this = $(this),
40
				$ddc = $this.parents(q_dd_ctn);
41
			if(!$ddc.length) return;
42
			var cls=util.classCheck($ddc[0],[open,q_readOnly,q_showOnly,dd_hold,dd_hold_once])
43
			clearMenus();
44
			if(!cls[open]) {
45
				if(cls[readOnly] || cls[showOnly]) return;
46
				if(cls(dd_hold)) return false;
47
				if[cls[dd_hold_once]) {
48
					$ddc.removeClass(dd_hold_once);
49
					return false;
50
				}
51
				var relatedTarget = {
52
					relatedTarget: this
53
				}
54
				$ddc.trigger(e = $.Event('show.dropdown', relatedTarget));
55
				if(e.isDefaultPrevented()) return
56
				$ddc.addClass(open).trigger('shown.dropdown', relatedTarget);
57
			}
58
			return false;
59
		};
60
	$(doc).on("click.dropdown", clearMenus).on("click.dropdown", q_dd_hand, toggle);
61

62
	return {
63
		hold: function($e) {
64
			var $p = $e.parents(q_dd_ctn);
65
			if($p.length) {
66
				$($p[0]).addClass(dd_hold);
67
			}
68
		},
69
		holdOnce: function($e) {
70
			var $p = $e.parents(q_dd_ctn);
71
			if($p.length) {
72
				$($p[0]).addClass(dd_hold_once);
73
			}
74
		}
75
	};
76
});

+ 132 - 156
js/form.js

@ -1,176 +1,152 @@
1
!(function($) {
1
$.define(["jQuery", "util"], "form", function($, util) {
2 2
3
	$.define(["jQuery", "util"], "form", function($, util) {
3
	var impls = [],
4 4
5
		var impls = [],
6
7
			/* default impl */
8
			di = function(val) {
9
				var v = val,
10
					dv = null;
11
				return {
12
					val: function(data) {
13
						if(arguments.length) {
14
							v = data;
15
							return this;
16
						}
17
						return v;
18
					},
19
					reset: function(data) {
20
						if(arguments.length) {
21
							dv = data;
22
							return this;
5
		/* default impl */
6
		di = function(val) {
7
			var v = val,
8
				rules = [],
9
				dv = null;
10
			return {
11
				get: function() {
12
					return v;
13
				},
14
				set: function(vd) {
15
					v = vd;
16
				},
17
				reset: function() {
18
					dv = data;
19
				},
20
				validate: function() {
21
					return util.validate(rules, this);
22
				},
23
				addRules: function(rule) {
24
					util.addRules(rules, rule);
25
				},
26
				valid: function() {},
27
				invalid: function(reson) {}
28
			};
29
		},
30
		vd = function(items) {
31
			for(key in items) {
32
				if(!items[key].validate())
33
					return false;
34
			}
35
			return true;
36
		},
37
		/* create form instance by jQuery obj */
38
		bf = function($e) {
39
			if($e.length === 1) {
40
				var items = {},
41
					rules = [];
42
				$e.find(".form-item").each(function() {
43
					var $this = $(this);
44
					for(var i = 0; i < impls.length; ++i) {
45
						var item = impls[i]($this);
46
						if(item && item.name) {
47
							items[item.name] = item;
23 48
						}
24
						dv = data;
25
						validate
26
						return this;
49
					}
50
				});
51
				return {
52
					item: function(name) {
53
						return items[name];
27 54
					},
28 55
					validate: function(vds) {
29
						if(vds) {
30
							// add valid obj
31
							return this;
32
						} else {
33
							return true;
56
						if(vd(items)){
57
							return util.validate(rules,this);							
34 58
						}
35
					}
36
				};
37
			},
38
			svd = function(items, vds) {
39
				for(key in vds) {
40
					var im = items[key],
41
						vd = vds[key];
42
					if(im && vd)
43
						im.validate(vd);
44
				}
45
				return this;
46
			},
47
			vd = function(items) {
48
				for(key in items) {
49
					if(!items[key].validate())
50
						return false;
51
				}
52
				return true;
53
			},
54
			vda = function(valids, form) {
55
				for(var i = 0; i < valids.length; ++i) {
56
					if(!valids[i](form))
57 59
						return false;
58
				}
59
				return true;
60
			},
61
62
			/* create form instance by jQuery obj */
63
			bf = function($e) {
64
				if($e.length === 1) {
65
					var items = {},
66
						valids = [];
67
					$e.find(".form-item").each(function() {
68
						var $this = $(this);
69
						for(var i = 0; i < impls.length; ++i) {
70
							var item = impls[i]($this);
71
							if(item && item.name) {
72
								items[item.name] = item;
60
					},
61
					addRules: function(rule) {
62
						var te = $.type(rule);
63
						if("function" === te) {
64
							rules.push(rule);
65
						} else if("array" === te) {
66
							rules.concat(rule);
67
						} else if("object" === te) {
68
							for(key in rule) {
69
								var im = items[key];
70
								if(im) im.addRules(rule[key]);
73 71
							}
74 72
						}
75
					});
76
					return {
77
						item: function(name) {
78
							return items[name];
79
						},
80
						validate: function(vds) {
81
							if(arguments.length) {
82
								$.isArray(data) ? valids.concat(data) : svd(items, vds);
83
								return this;
84
							} else {
85
								return vd(items) ? vda(valids, this) : false;
86
							}
87
						},
88
						val: function(data) {
89
							if(arguments.length) {
90
								if(data) {
91
									for(key in data) {
92
										var ch = items[key];
93
										if(!ch) {
94
											ch = items[key] = di();
95
										}
96
										ch.val(data[key]);
73
					},
74
					val: function(data) {
75
						if(arguments.length) {
76
							if(data) {
77
								for(key in data) {
78
									var ch = items[key];
79
									if(!ch) {
80
										ch = items[key] = di();
97 81
									}
82
									ch.set(data[key]);
98 83
								}
99
								return this;
100
							}
101
							var ret = {};
102
							for(key in items) {
103
								ret[key] = items[key].val();
104 84
							}
105
							return ret;
106
						},
107
						reset: function(data) {
108
							if(arguments.leng) {
109
								for(key in data) {
110
									var item = itmes[key];
111
									if(item)
112
										item.reset(data[key]);
113
								}
114
							} else {
115
								for(key in items) {
116
									items[key].reset();
85
							return this;
86
						}
87
						var ret = {};
88
						for(key in items) {
89
							ret[key] = items[key].get();
90
						}
91
						return ret;
92
					},
93
					reset: function() {
94
						for(key in items) {
95
							items[key].reset();
96
						}
97
					},
98
					get: function(url, data, eh, config) {
99
						util.get(url, data, function(rd) {
100
							if(config && config.check) {
101
								rd = config.ckeck(rd);
102
								if(rd) {
103
									this.reset();
104
									this.val(rd);
117 105
								}
118 106
							}
119
						},
120
						get: function(url, data, eh, config) {
121
							util.get(url, data, function(rd) {
122
								if(config && config.check) {
123
									rd = config.ckeck(rd);
124
									if(rd) {
125
										this.reset();
126
										this.val(rd);
127
									}
128
								}
129
							}, eh, config);
130
						},
131
						post: function(url, data, eh, config) {
132
							util.post(url, data, function(rd) {
133
								if(config && config.check) {
134
									rd = config.ckeck(rd);
135
									if(rd) {
136
										this.reset();
137
										this.val(rd);
138
									}
107
						}, eh, config);
108
					},
109
					post: function(url, data, eh, config) {
110
						util.post(url, data, function(rd) {
111
							if(config && config.check) {
112
								rd = config.ckeck(rd);
113
								if(rd) {
114
									this.reset();
115
									this.val(rd);
139 116
								}
140
							}, eh, config);
141
						},
142
						doGet: function(url, sh, eh, config) {
143
							if(this.validate()) {
144
								util.get(url, this.val(), sh, eh, config);
145
							}
146
						},
147
						doPost: function(url, sh, eh, config) {
148
							if(this.validate()) {
149
								util.post(url, this.val(), sh, eh, config);
150
							}
151
						},
152
						doPut: function(url, sh, eh, config) {
153
							if(this.validate()) {
154
								util.put(url, this.val(), sh, eh, config);
155
							}
156
						},
157
						doDel: function(url, sh, eh, config) {
158
							if(this.validate()) {
159
								util.del(url, this.val(), sh, eh, config);
160 117
							}
118
						}, eh, config);
119
					},
120
					doGet: function(url, sh, eh, config) {
121
						if(this.validate()) {
122
							util.get(url, this.val(), sh, eh, config);
161 123
						}
162
					};
163
				}
164
				return null;
165
			}
166
167
		return {
168
			build: bf,
169
			register: function(impl) {
170
				impls.push(impl)
124
					},
125
					doPost: function(url, sh, eh, config) {
126
						if(this.validate()) {
127
							util.post(url, this.val(), sh, eh, config);
128
						}
129
					},
130
					doPut: function(url, sh, eh, config) {
131
						if(this.validate()) {
132
							util.put(url, this.val(), sh, eh, config);
133
						}
134
					},
135
					doDel: function(url, sh, eh, config) {
136
						if(this.validate()) {
137
							util.del(url, this.val(), sh, eh, config);
138
						}
139
					}
140
				};
171 141
			}
172
		};
142
			return null;
143
		}
173 144
174
	});
145
	return {
146
		build: bf,
147
		register: function(impl) {
148
			impls.push(impl)
149
		}
150
	};
175 151
176
})(jQuery);
152
});

+ 186 - 0
js/select.js

@ -0,0 +1,186 @@
1
$.use(["jQuery", "form", "doc", "util", "dropdown", "dict"], function($, form, doc, util, dd, dict) {
2
	var readOnly = "readOnly",
3
		showOnly = "showOnly",
4
		modelName = 'select',
5
		tInt = "int",
6
		tBool = "bool",
7
		tString = "string",
8
		def = "defVal",
9
		placeholder = "placeholder",
10
		required = "required",
11
		tree = "tree",
12
		queried = "queried",
13
		handCode = { an: "code", av: "" },
14
		spanCode = { an: "code", av: "" },
15
		spanDictCode = { an: "dictCode", av: "" },
16
		pa = [""],
17
		jhref = { an: "href", av: "javascript:;" },
18
		hand = {
19
			tn: "a",
20
			attrs: [
21
				{ an: "class", av: "dd-hand" },
22
				jhref,
23
				handCode,
24
			],
25
			chs: [
26
				{ tn: "i", attrs: [{ an: "class", av: "icon icon-drop" }] },
27
				{ tn: "span", attrs: [spanCode, spanDictCode] },
28
				{ tn: "i", attrs: [{ an: "class", av: "icon icon-close" }] },
29
				{ tn: "a", attrs: [jhref, { an: "class", av: placeholder }], chs: pa }
30
			]
31
		},
32
		seim = { an: "class", av: "select-item" },
33
		sebi = { an: "class", av: "branch-item" },
34
		buildTreeDrop = function(isQueried, items, pa) {
35
			items.forEach(function(item) {
36
				if(isQueried || item.enabled) {
37
					var li = { tn: "li", attrs: [], chs: [] };
38
					if(item.children && item.children.length) {
39
						var bhand = { tn: "a", attrs: [], chs: [] };
40
						bhand.attrs.push({ an: "code", av: item.code });
41
						bhand.chs.push({ tn: "span", chs: [item.caption] });
42
						li.attrs.push(sebi);
43
						li.chs.push(bhand);
44
						buildTreeDrop(isQueried, item.children, li.chs);
45
					} else {
46
						li.attrs.push(seim);
47
						li.attrs.push({ an: "code", av: item.code });
48
						li.chs.push(item.caption);
49
					}
50
					pa.push(li);
51
				}
52
			});
53
		},
54
		//			}
55
		//			$ul.find(QF_SELECT_ITEM).on("click.jr_select_api", select_selectItem);
56
		//			$ul.find(QF_BRANCH_ITEM).on("click.jr_select_api", select_branchItem);
57
		//			return $ul;
58
		//	}
59

60
		singleShown = function($e, dictCode, clss, $h, $span) {
61
			return clss[tree] ? function(evt) {
62
				var items = dict.get(dictCode);
63
				var drop = { tn: "div", attrs[{ an: "class", av: "dd-drop" }], chs = [] };
64
				if(items) {
65
					var ul = { tn: "ul", chs: [] };
66
					buildTreeDrop(clss[queried], items, ul.chs);
67
					drop.chs.push(ul);
68
				} else {
69
					drop.chs.push("loading.........");
70
				}
71
				util.appendChild($e[0], drop);
72
				if(items) {
73
					$e.find(".select-item").on("click", function() {
74
						var cc = this.getAttribute("code");
75
						$h.attr("code", cc);
76
						$span.attr("code", cc);
77
						$span[0].textContent = this.textContent;
78

79
					});
80
					$e.find(".branch-item").on("click", function(evt) {
81
						$(this).toggleClass("open");
82
						$e.addClass("dd-hold-once");
83
					});
84
				}
85
			} : function(evt) {
86
				var items = dict.get(dictCode);
87
				var drop = { tn: "div", attrs[{ an: "class", av: "dd-drop" }], chs = [] };
88
				if(items) {
89
					var ul = { tn: "ul", chs: [] };
90
					items.forEach(function(item) {
91
						if(clss[queried] || item.enabled) {
92
							this.push({ tn: "li", attr[seim, { an: "code": av: item.code }], chs: [item.caption] });
93
						}
94
					}, ul.chs);
95
					drop.chs.push(ul);
96
				} else {
97
					drop.chs.push("loading.........");
98
				}
99
				util.appendChild($e[0], drop);
100
				if(items) $e.find(".select-item").on("click", function(e) {
101
					var cc = this.getAttribute("code");
102
					$h.attr("code", cc);
103
					$span.attr("code", cc);
104
					$span[0].textContent = this.textContent;
105
				});
106
			};
107
		};
108

109
	from.register(function($e) {
110
		var cls = util.classCheck($e[0], [readOnly, showOnly, modelName, tInt, tBool, required, tree, queried]);
111
		if(cls[modelName]) {
112
			var n = attr("name") || $e.attr("id"),
113
				ve, rules = [];
114
			if(!n) {
115
				throw "Attribute[name] is invalid";
116
			}
117
			var dv = $e.attr(def) || "",
118
				dictCode = $e.attr("dictCode") || "";
119
			if(!dictCode) { throw "Attribute[dictCode] is invalid"; }
120
			$e.empty().addClass("dd-ctn").addClass("dd-clean");
121
			handCode.av = spanCode.av = dv;
122
			spanDictCode.av = dictCode;
123
			pa[0] = $e.attr(placeholder) || "";
124
			util.appendChild($e[0], hand);
125
			var $h = $e.children("a");
126
			var $span = $h.children("span");
127
			if(!(cls[readOnly] || cls[showOnly])) {
128
				$e.on("shown", singleShown($e[0], dictCode, cls, $h, $span));
129
				$e.find(".icon-close").on("click", function() {
130
					$e.addClass("dd-hold-once");
131
					$h.attr("code", "");
132
					$span.attr("code", "").text("");
133
				});
134
			}
135
			if(dv) {
136
				dict.tranfer($span, dictCode, dv);
137
			} else {
138
				dict.apply(dictCode, util.noop);
139
			}
140
			return {
141
				name: n,
142
				get: function() {
143
					var vd = $h.attr("code");
144
					if(vd) {
145
						return css[tBool]?'1'===vd:cls[tInt]?parseInt(vd):vd;
146
					}
147
				},
148
				set: function(data) {
149
					if(data){
150
						var cc = data===true?"1":(""+data);
151
						$h.attr("code",cc);
152
						$span.attr("code",cc);
153
						dict.transfer($span,dictCode,cc);
154
					}else if(data===false || data==0){
155
						$h.attr("code",'0');
156
						$span.attr("code",'0');
157
						dict.transfer($span,dictCode,'0');
158
					}else{
159
						$h.attr("code",'');
160
						$span.attr("code",'').text("");
161
					}
162
				},
163
				validate: function() {
164
					if(cls[required]){
165
						if(!$h.attr("code")){
166
							return "不可为空";
167
						}
168
					}
169
					return util.validate(rules, this);
170
				},
171
				addRules: function(rule) {
172
					util.addRules(rules, rule);
173
				}
174
				reset: function(data) {
175
					this.set(dv);
176
				},
177
				valid: function() { util.valid($e); },
178
				invalid: function(reson) {
179
					util.invalid($e);
180
					util.error(reson);
181
				}
182
			};
183
		}
184

185
	});
186
});

+ 53 - 29
js/text.js

@ -1,31 +1,55 @@
1
(function($,form){	
2
	from.register(function($e){
3
		var n=$e.attr("name")||$e.attr("name"), $,dv,t="string";
4
		if($e.hasClass("int")) t="int";
5
		if($e.hasClass("float")) t="float";		
6
		return {
7
			name:n,
8
			val:function(data){},
9
			validate:function(h){
10
				
11
			},
12
			reset:function(data){
13
				if(arguments.leng){
14
					dv=data;
15
				}else{
16
					this.val(dv);
17
				}
18
			},
19
			type:function(){
20
				return t;
1
$.define(["jQuery", "form", "util"],"text",function($, form, util) {
2
	var trim="trim",readOnly="readOnly",showOnly="showOnly",modelName='text',tInt="int",tBool="bool",tFloat="float",tString="string",pw="password",def="defVal",placeholder="placeholder",required="required";	
3
	from.register(function($e) {
4
		var cls = util.classCheck($e[0],[modelName,trim,readOnly,showOnly,tInt,tBool,tFloat,pw,]);
5
		if(cls[modelName]) {
6
			var n = $e.attr("name") || $e.attr("id"),ve, rules = [];
7
			if(!n) {
8
				throw "Attribute[name] is invalid";
21 9
			}
22
			
23
		};
24
	});	
25
})(jQuery,form);
26
10
			dv = $e.attr(def) || "";
11
			$e.empty();
12
			if(cls[showOnly]) {
13
				$e.text(dv);
14
			} else {
15
				ve = $("<input type='" + (cls[pw] ? pw :modelName) + "' />");
16
				var tmp = this.ele.attr(placeholder);
17
				if(tmp) ve.attr(placeholder, tmp);
18
				if(cls[readOnly]) ve.prop('readonly', "readonly");
19
				ve.appendTo($e);
20
			}
21
			return {
22
				name: n,
23
				get: function() {
24
					var vd = ve.val();
25
					if(vd) {
26
						return   cls[tInt] ? parseInt(vd):cls[tFloat]? parseFloat(vd):vd;
27
					}
28
				},
29
				set: function(data) {
30
					if(cls[showOnly) $e.text(data) else ve.val(data);
31
				},
32
				validate: function() {
33
					if(cls[required]){
34
						if(!ve.val()){
35
							return "不可为空或空字符串";
36
						}
37
					}
38
					return util.validate(rules, this);
39
				},
40
				addRules: function(rule) {
41
					util.addRules(rules, rule);
42
				}
43
				reset: function(data) {
44
					this.set(dv);
45
				},
46
				valid: function() { util.valid($e); },
47
				invalid: function(reson) {
48
					util.invalid($e);
49
					util.error(reson);
50
				}
51
			};
52
		}
27 53
28
!(function($){
29
	
30
	
31
})(jQuery);
54
	});
55
});

+ 313 - 249
js/util.js

@ -1,262 +1,326 @@
1
!(function($) {
2
	$.define(["jQuery", "body", "win"], "util", function($, $body, win) {
3
		var noop = function() {},
4
			infoDiv = $("#g_info"),
5
			errDiv = $("#g_err"),
6
			warnDiv = $("#g_warn"),
7
			msgDiv = $("#g_msg"),
8
			err_msg = {},
9
			layer_curr = {
10
				index: 5000000,
11
				remove: noop,
12
				css: noop
13
			},
14
			layer_remove = function() {
15
				this.shade.remove();
16
				this.ctn.empty();
17
				this.ctn.remove();
18
				this.prev.css("display", "block");
19
				_g_layer_curr = this.prev;
20
			},
21
			fh = function(e) {
22
				$(this).parent().hide();
23
			},
24
			bh = function($p) {
25
				$p.find(".click-hide-parent").on("click", fh);
26
			},
27
			fillContent = function($c /* child node [String|Function|jQObj] */ , $p) {
28
				if($c) {
29
					if(typeof $c === "string") {
30
						$p.html(p);
31
					} else if(typeof p == "function") {
32
						$c.call($p);
33
					} else if($c.jquery) {
34
						$p.append($c);
35
					}
1
$.define(["jQuery", "body", "win"], "util", function($, $body, win) {
2
	var rclass = /[\t\r\n\f]/g,
3
		noop = function() {},
4
		infoDiv = $("#g_info"),
5
		errDiv = $("#g_err"),
6
		warnDiv = $("#g_warn"),
7
		msgDiv = $("#g_msg"),
8
		err_msg = {},
9
		layer_curr = {
10
			index: 5000000,
11
			remove: noop,
12
			css: noop
13
		},
14
		layer_remove = function() {
15
			this.shade.remove();
16
			this.ctn.empty();
17
			this.ctn.remove();
18
			this.prev.css("display", "block");
19
			_g_layer_curr = this.prev;
20
		},
21
		fh = function(e) {
22
			$(this).parent().hide();
23
		},
24
		bh = function($p) {
25
			$p.find(".click-hide-parent").on("click", fh);
26
		},
27
		fillContent = function($c /* child node [String|Function|jQObj] */ , $p) {
28
			if($c) {
29
				if(typeof $c === "string") {
30
					$p.html(p);
31
				} else if(typeof p == "function") {
32
					$c.call($p);
33
				} else if($c.jquery) {
34
					$p.append($c);
36 35
				}
37
			},
38
			modal = function(ctn) {
39
				var inx = _g_layer_curr.index + 2,
40
					ly = {
41
						index: inx,
42
						remove: layer_remove,
43
						prev: _g_layer_curr
44
					};
45
				ly.shade = $(
46
					"<div class='layer-shade layer-" + inx + "' style='z-index:" +
47
					inx + ";'></div>").appendTo(body);
48
				++inx;
49
				ly.ctn = $(
50
					"<div class='layer-ctn layer-" + inx + "' style='z-index:" +
51
					inx + ";'></div>").appendTo(body);
52
				ly.prev = layer_curr;
53
				layer_curr = ly;
54
				fillContent(ctn, ly.ctn);
55
				return ly;
56
			},
57
			rmModal = function() {
58
				layer_curr.remove();
59
			},
36
			}
37
		},
38
		modal = function(ctn) {
39
			var inx = _g_layer_curr.index + 2,
40
				ly = {
41
					index: inx,
42
					remove: layer_remove,
43
					prev: _g_layer_curr
44
				};
45
			ly.shade = $(
46
				"<div class='layer-shade layer-" + inx + "' style='z-index:" +
47
				inx + ";'></div>").appendTo(body);
48
			++inx;
49
			ly.ctn = $(
50
				"<div class='layer-ctn layer-" + inx + "' style='z-index:" +
51
				inx + ";'></div>").appendTo(body);
52
			ly.prev = layer_curr;
53
			layer_curr = ly;
54
			fillContent(ctn, ly.ctn);
55
			return ly;
56
		},
57
		rmModal = function() {
58
			layer_curr.remove();
59
		},
60 60
61
			// function error message
62
			fem = function($c, $t) {
63
				var $e = $(
64
						"<div class='err-ctn'><i class='icon click-hide-parent'></i></div>")
65
					.appendTo(errDiv);
66
				$("<span></span>").appendTo($e).html($c);
67
				bh($e);
68
				setTimeout(function() {
69
					$e.remove()
70
				}, $t ? $t : 10000);
71
			},
72
			// function warn message
73
			fwm = function($c, $t) {
74
				var $w = $(
75
						"<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
76
					.appendTo(warnDiv);
77
				$("<span></span>").appendTo($w).html($c);
78
				bh($w);
79
				setTimeout(function() {
80
					$w.remove()
81
				}, $t ? $t : 3000);
82
			},
83
			fm = function($c, $t) {
84
				var $m = $(
85
						"<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
86
					.appendTo(warnDiv);
87
				$("<span></span>").appendTo($w).html($c);
88
				bh($m);
89
				setTimeout(function() {
90
					$w.remove()
91
				}, $t ? $t : 3000);
92
			},
93
			loadref = 0,
94
			loadingDiv = $("#g_loading"),
95
			loading = function() {
96
				++loadref;
97
				if(loadref === 1) {
98
					loadingDiv.show()
99
				}
100
			},
101
			unLoading = function() {
102
				--loadref;
103
				if(loadref === 0) {
104
					loadingDiv.hide()
105
				}
106
			},
107
			am = function(title, content, hand) {
108
				if(content) {
109
					if($.isFunction(content)) {
110
						hand = content;
111
						content = title;
112
						title = "提示";
113
					}
114
				} else {
61
		// function error message
62
		fem = function($c, $t) {
63
			var $e = $(
64
					"<div class='err-ctn'><i class='icon click-hide-parent'></i></div>")
65
				.appendTo(errDiv);
66
			$("<span></span>").appendTo($e).html($c);
67
			bh($e);
68
			setTimeout(function() {
69
				$e.remove()
70
			}, $t ? $t : 10000);
71
		},
72
		// function warn message
73
		fwm = function($c, $t) {
74
			var $w = $(
75
					"<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
76
				.appendTo(warnDiv);
77
			$("<span></span>").appendTo($w).html($c);
78
			bh($w);
79
			setTimeout(function() {
80
				$w.remove()
81
			}, $t ? $t : 3000);
82
		},
83
		fm = function($c, $t) {
84
			var $m = $(
85
					"<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
86
				.appendTo(warnDiv);
87
			$("<span></span>").appendTo($w).html($c);
88
			bh($m);
89
			setTimeout(function() {
90
				$w.remove()
91
			}, $t ? $t : 3000);
92
		},
93
		loadref = 0,
94
		loadingDiv = $("#g_loading"),
95
		loading = function() {
96
			++loadref;
97
			if(loadref === 1) {
98
				loadingDiv.show()
99
			}
100
		},
101
		unLoading = function() {
102
			--loadref;
103
			if(loadref === 0) {
104
				loadingDiv.hide()
105
			}
106
		},
107
		am = function(title, content, hand) {
108
			if(content) {
109
				if($.isFunction(content)) {
110
					hand = content;
115 111
					content = title;
116 112
					title = "提示";
117 113
				}
118
				hand = hand || noop;
119
				var ly = modal('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title"></h4></div><div class="modal-body"><p></p></div><div class="modal-footer"><button type="button" class="btn">关闭</button></div></div></div>');
120
				ly.ctn.find(".modal-title").text(title);
121
				$c = ly.ctn.find("p").text(content);
122
				ly.ctn.find("button").on("click", function() {
123
					rmModal();
124
					hand();
125
				});
126
			},
127
			bm_addBtn = function($p, $cp, $h) {
128
				var $b = $('<button type="button" class="btn"></button>').appendTo($p);
129
				b.text($cp).on("click", function() {
130
					rmModal(), $h()
131
				});
132
			},
133
			bm = function(obj) {
134
				var ly = modal('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title"></h4></div><div class="modal-body"><p></p></div><div class="modal-footer"></div></div></div>');
135
				ly.ctn.find(".modal-title").text(obj.title || "确认");
136
				ly.ctn.find("p").text(obj.content);
137
				var m_footer = ly.ctn.find(".modal-footer"),
138
					btnH = function(btn) {
139
						var $btn = $('<button type="button" class="btn"></button>')
140
							.appendTo(m_footer);
141
						var caption = btn.caption || "未命名的",
142
							handler = btn.hand || noop;
143
						$btn.text(caption);
144
						$btn.on("click", function() {
145
							util.closeModalLayer();
146
							handler();
147
						});
148
					};
149
				for(var i = 0; i < obj.btns.length; ++i) {
150
					bm_addBtn(m_footer, btns[i].caption || "no name", btns[i].hand ||
151
						noop);
152
				}
153
			},
154
			g_def_err_hand = function(ep) {
155
				fem("http access error:\r\n" + JSON.stirngify(ep));
156
			},
157
			g_err = {
158
				"defErrHand": function(ep) {
114
			} else {
115
				content = title;
116
				title = "提示";
117
			}
118
			hand = hand || noop;
119
			var ly = modal('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title"></h4></div><div class="modal-body"><p></p></div><div class="modal-footer"><button type="button" class="btn">关闭</button></div></div></div>');
120
			ly.ctn.find(".modal-title").text(title);
121
			$c = ly.ctn.find("p").text(content);
122
			ly.ctn.find("button").on("click", function() {
123
				rmModal();
124
				hand();
125
			});
126
		},
127
		bm_addBtn = function($p, $cp, $h) {
128
			var $b = $('<button type="button" class="btn"></button>').appendTo($p);
129
			b.text($cp).on("click", function() {
130
				rmModal(), $h()
131
			});
132
		},
133
		bm = function(obj) {
134
			var ly = modal('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h4 class="modal-title"></h4></div><div class="modal-body"><p></p></div><div class="modal-footer"></div></div></div>');
135
			ly.ctn.find(".modal-title").text(obj.title || "确认");
136
			ly.ctn.find("p").text(obj.content);
137
			var m_footer = ly.ctn.find(".modal-footer"),
138
				btnH = function(btn) {
139
					var $btn = $('<button type="button" class="btn"></button>')
140
						.appendTo(m_footer);
141
					var caption = btn.caption || "未命名的",
142
						handler = btn.hand || noop;
143
					$btn.text(caption);
144
					$btn.on("click", function() {
145
						util.closeModalLayer();
146
						handler();
147
					});
148
				};
149
			for(var i = 0; i < obj.btns.length; ++i) {
150
				bm_addBtn(m_footer, btns[i].caption || "no name", btns[i].hand ||
151
					noop);
152
			}
153
		},
154
		g_def_err_hand = function(ep) {
155
			fem("http access error:\r\n" + JSON.stirngify(ep));
156
		},
157
		g_err = {
158
			"defErrHand": function(ep) {
159 159
160
				},
161
				"0": "未定义的错误",
162 160
			},
163
			/**
164
			 * ep={code:"",msg:"",detailMsg:"",url:""} pa=function(code,msg,detailMsg)
165
			 * pa=boolean
166
			 */
167
			ajaxErrHand = function(ep, eh) {
168
				if(eh) {
169
					var pt = $.type(eh);
170
					if(pt == "booean") {
171
						g_def_err_hand(ep);
172
					} else if(pt == "function") {
173
						eh(ep);
161
			"0": "未定义的错误",
162
		},
163
		/**
164
		 * ep={code:"",msg:"",detailMsg:"",url:""} pa=function(code,msg,detailMsg)
165
		 * pa=boolean
166
		 */
167
		ajaxErrHand = function(ep, eh) {
168
			if(eh) {
169
				var pt = $.type(eh);
170
				if(pt == "booean") {
171
					g_def_err_hand(ep);
172
				} else if(pt == "function") {
173
					eh(ep);
174
				} else {
175
					var ph = eh[ep.code] || g_err[ep.code] || eh["defErrHand"] ||
176
						g_err["defErrHand"];
177
					if(typeof ph === "string") {
178
						fem(ph);
174 179
					} else {
175
						var ph = eh[ep.code] || g_err[ep.code] || eh["defErrHand"] ||
176
							g_err["defErrHand"];
177
						if(typeof ph === "string") {
178
							fem(ph);
179
						} else {
180
							ph(ep);
181
						}
180
						ph(ep);
182 181
					}
183 182
				}
184
			},
185
			ajaxAccess = function(method, pUrl, pData, sh, eh, config) {
186
				config = config || {};
187
				if(false !== config.mask)
188
					loading();
189
				config.traditional = true;
190
				config.type = method;
191
				config.url = pUrl, config.data = pData;
192
				config.contentType = method === "put" ? "application/json" : "application/x-www-form-urlencoded";
193
				config.success = function(rd) {
194
					if(rd.success) {
195
						sh(rd.data);
196
					} else {
197
						rd.code = "" + rd.code;
198
						rd.url = pUrl;
199
						ajaxErrHand(ep, eh);
200
					}
201
				};
202
				config.error = function(jqXHR, textStatus, errorThrown) {
203
					ajaxErrHand({
204
						code: textStatus,
205
						msg: textStatus,
206
						detailMsg: textStatus,
207
						xhr: jqXHR,
208
						eObj: errorThrown,
209
						url: pUrl
210
					}, eh);
211
				};
212
				$.ajax(config).always(false !== config.mask ? unLoading : noop);
183
			}
184
		},
185
		ajaxAccess = function(method, pUrl, pData, sh, eh, config) {
186
			config = config || {};
187
			if(false !== config.mask)
188
				loading();
189
			config.traditional = true;
190
			config.type = method;
191
			config.url = pUrl, config.data = pData;
192
			config.contentType = method === "put" ? "application/json" : "application/x-www-form-urlencoded";
193
			config.success = function(rd) {
194
				if(rd.success) {
195
					sh(rd.data);
196
				} else {
197
					rd.code = "" + rd.code;
198
					rd.url = pUrl;
199
					ajaxErrHand(ep, eh);
200
				}
213 201
			};
214
215
		return {
216
			showModal: modal,
217
			closeModal: rmModal,
218
			showLoading: loading,
219
			hideLoading: unLoading,
220
			listModalIndex: function() {
221
				return lay_curr.index + 1;
222
			},
223
			error: fem,
224
			warn: fwm,
225
			msg: fm,
226
			alert: am,
227
			boxMsg: bm,
228
			confirm: function(msg, yes, no) {
229
				bm({
230
					content: msg,
231
					btns: [{
232
						caption: "取消",
233
						hand: no || noop
234
					}, {
235
						caption: "确认",
236
						hand: yes
237
					}]
238
				});
239
			},
240
			get: function(url, data, sh, eh, config) {
241
				ajaxAccess("get", url, data, sh, eh, config);
242
			},
243
			post: function(url, data, sh, eh, config) {
244
				ajaxAccess("post", url, data, sh, eh, config);
245
			},
246
			put: function(url, data, sh, eh, config) {
247
				ajaxAccess("post", url, data ? JSON.stringify(data) : "", sh, eh,
248
					config);
249
			},
250
			del: function(url, sh, eh, pObj) {
251
				ajaxAccess("post", url, null, sh, eh, pOjb);
252
			},
253
			noop: noop,
254
			returnTrue: function() {
255
				return true;
256
			},
257
			returnFalse: function() {
258
				return false;
202
			config.error = function(jqXHR, textStatus, errorThrown) {
203
				ajaxErrHand({
204
					code: textStatus,
205
					msg: textStatus,
206
					detailMsg: textStatus,
207
					xhr: jqXHR,
208
					eObj: errorThrown,
209
					url: pUrl
210
				}, eh);
211
			};
212
			$.ajax(config).always(false !== config.mask ? unLoading : noop);
213
		},
214
		buildElement = function(pe, obj) {
215
			var e;
216
			if(typeof obj === 'string') {
217
				e = doc.createTextNode(obj);
218
				pe.appendChild(e);
219
			} else {
220
				e = doc.createElement(obj.tn);
221
				pe.appendChild(e);
222
				if(obj.attrs && obj.attrs.length) {
223
					obj.attrs.forEach(function(attr) {
224
						e.setAttribute(attr.an, attr.av);
225
					});
226
				}
227
				if(obj.chs && obj.chs.length) {
228
					obj.chs.forEach(function(ch) {
229
						buildElement(e,ch);
230
					});
231
				}
259 232
			}
260 233
		};
261
	});
262
})(jQuery);
234
235
	return {
236
		classCheck: function(ele, clses) {
237
			var ret = {},
238
				cs = " " + (ele.getAttribute("class") || "") + " ",
239
				cls;
240
			for(var i = 0; i < clses.length; ++i) {
241
				var cls = clses[i];
242
				ret[cls] = cs.indexOf(" " + cls + " ") > -1;
243
			}
244
			return ret;
245
		},
246
		showModal: modal,
247
		closeModal: rmModal,
248
		showLoading: loading,
249
		hideLoading: unLoading,
250
		listModalIndex: function() {
251
			return lay_curr.index + 1;
252
		},
253
		error: fem,
254
		warn: fwm,
255
		msg: fm,
256
		alert: am,
257
		boxMsg: bm,
258
		confirm: function(msg, yes, no) {
259
			bm({
260
				content: msg,
261
				btns: [{
262
					caption: "取消",
263
					hand: no || noop
264
				}, {
265
					caption: "确认",
266
					hand: yes
267
				}]
268
			});
269
		},
270
		get: function(url, data, sh, eh, config) {
271
			ajaxAccess("get", url, data, sh, eh, config);
272
		},
273
		post: function(url, data, sh, eh, config) {
274
			ajaxAccess("post", url, data, sh, eh, config);
275
		},
276
		put: function(url, data, sh, eh, config) {
277
			ajaxAccess("post", url, data ? JSON.stringify(data) : "", sh, eh,
278
				config);
279
		},
280
		del: function(url, sh, eh, pObj) {
281
			ajaxAccess("post", url, null, sh, eh, pOjb);
282
		},
283
		noop: noop,
284
		returnTrue: function() {
285
			return true;
286
		},
287
		returnFalse: function() {
288
			return false;
289
		},
290
		addRules: function(target, rule) {
291
			var te = $.type(rule);
292
			if("function" === te) {
293
				target.push(rule);
294
			} else if("array" === te) {
295
				target.concat(rule);
296
			}
297
		},
298
		validate: function(rules, obj) {
299
			for(var i = 0; i < rules.length; ++i) {
300
				var ret = rules[i](obj);
301
				if(ret) {
302
					if(obj.invalid) {
303
						obj.invalid(ret);
304
					}
305
					return false;
306
				}
307
			}
308
			if(obj.valid) {
309
				obj.valid();
310
			}
311
			return true;
312
		},
313
		valid: function($e) {
314
			$e.removeClass("invald");
315
		},
316
		invalid: function($e) {
317
			$e.addClass("invald");
318
		},
319
		appendChild: function(e,obj) {
320
			var docf = doc.createDocumentFragment();
321
			buildElement(docf,obj);
322
			e.appendChild(docf);
323
		},
324
		
325
	};
326
});