-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformbot.js
162 lines (107 loc) · 3.35 KB
/
formbot.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
/**
* formbot.js - An easy-to-use agent to verify input values.
*
* @author duartecsoares
* @contact [email protected]
* @version 0.1
*
* @github https://github.com/duartecsoares/formbot.js
*
* @license
*
*
* @copyright 2015, duartecsoares
*
*/
(function(root) {
"use strict";
/**
* List of error messages
*
* @private
* @type Object
*/
var messages = {
"min" : "Field must have atleast %p1 characters",
"max" : "Field cant have more than %p1 characters",
"required" : "Field is required",
"default" : "Field is invalid"
},
dom = document,
forEach = Array.prototype.forEach,
getElement = function(id) {
return dom.getElementById(id);
},
on = function(eventName, action) {
this.addEventListener(eventName, action);
},
off = function(eventName, action) {
this.removeEventListener(eventName, action);
},
copy = function(object, properties) {
object = object || {};
properties.map(function(property) {
object[property.name] = property.action;
});
return object;
};
function Formbot(settings) {
var _constructor = function() {
console.log(settings);
};
_constructor();
}
Formbot.prototype = {
/**
* Describe what this method does
*
* @public
* @param {Object} rule Adds a new validation rule
* @returns {Object} Info about the rule
*/
add : function(rule) {},
process : function(form) {
var retrieveValue = function(input, iterator){
var value = input.value,
name = input.getAttribute("name");
return { name : name, value : value };
},
formData = [],
inputs = form.querySelectorAll("[data-formbot='input']");
forEach.call(inputs, function(input, iterator){
formData.push(retrieveValue(input, iterator));
});
console.log(formData);
return formData
},
listenTo : function(id) {
var _self = this,
binds = [{ name : "on", action: on }],
form = copy(getElement(id), binds),
submitEvent = function(e){
e.preventDefault();
var form = e.target;
_self.process(form);
};
form.on("submit", submitEvent);
},
stopListenTo : function(id) {
var _self = this,
binds = [{ name : "off", action: off }],
form = copy(getElement(id), binds);
form.off("submit", submitEvent);
}
};
/* todo
* put more descriptions on methods
* extract data from inputs within process function
* add more methods to formbot's api such as stopListen
*/
if (typeof define === 'function' && define.amd) {
define('Formbot', [], function () { return Formbot; });
} else if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.exports) {
module.exports = Formbot;
} else {
root.Formbot = Formbot;
}
}(this));