-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwee-form.js
122 lines (94 loc) · 2.61 KB
/
jwee-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
//////////////////////////////////////////////////////////////////////////
// JWee JavaScript Framework & Toolkit
// Copyright (c) 2010 AUTHORS ([email protected])
// Licensed under the MIT and GPL (for now).
// http://www.jwee.org
//////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////
// Form
// ** UNDER CONSTRUCTION **
//////////////////////////////////////////////////////////////////////////
/***
@@@namespace wee
@@toolkit form
@desc All form related functions.
@@@namespace wee.form
@desc All form related functions.
@@function toQueryString
@arg the form
@ret form values as encoded url query string
@@function toObject
@arg the form
@ret {object} plain object with properties and values representating the form data
@@ajaxify
@arg the form
@advanced
***/
;(function(){
///////////////////////////////////////////////////
wee.form = {
toQueryString : function(form) {
form = wee(form);
var q = {}, e = Array.from(form.elements), len = e.length;
for( var i = 0; i < len; i++ ) {
q[e[i].name] = e[i].value;
}
return Object.toQueryString(q);
},
toObject : function(form) {
form = wee(form);
var q = {}, e = Array.from(form.elements), len = e.length;
for( var i = 0; i < len; i++ ) {
q[e[i].name] = e[i].value;
}
return q;
},
ajaxify : function(form) {
form = wee(form);
var
method = form.getAttribute('method') || 'get',
action = form.getAttribute('action'),
cb = typeof form.afterSubmit == 'function' ? form.afterSubmit : nully;
form.onsubmit = function() {
wee.ajax.post(action,cb,wee.form.toObject(form));
return false;
};
}
};
wee.event.onLoad( function() {
var len = document.forms.length;
while(len--) {
if( wee(document.forms[len]).hasClass('ajax') ) {
wee.form.ajaxify(document.froms[len]);
trace("Form", document.forms[len].name, 'ajaxified!');
}
}
});
///////////////////////////////////////////////
/***
@@class Form
@arg the form element or valid #id of form
@@namespace wee.form.Form
@@method elements
@ret the form elements
@@method submit
@desc submits the form
**/
///////////////////////////////////////////////
wee.Form = wee.form.Form = Class.create('Form', {
construct: function(form) {
this.form = wee(form);
},
elements: function() {
return Array.from(this.form.elements);
},
submit: function() {
this.form.submit();
},
toString: function() {
return wee.form.toQueryString(this.form);
}
});
///////////////////////////////////////////////////
})();
//////////////////////////////////////////////////////////////////////////