-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooth.js
307 lines (292 loc) · 10.3 KB
/
tooth.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
(function () {
'use strict';
var $ = {
about : {
'builtby' : 'Exceptions',
'version' : 'version 1.3'
},
get : function (params) {
return new DOM(params);
},
set: function (tagName, attrs) {
var element = document.createElement(tagName);
var el = new DOM(element);
if (attrs) {
if (attrs.className) {
el.addClass(attrs.className);
delete attrs.className;
}
if (attrs.text) {
el.text(attrs.text);
delete attrs.text;
}
if (attrs.html) {
el.html(attrs.html);
delete attrs.html;
}
for (var key in attrs) {
if (attrs.hasOwnProperty(key)) {
el.attr(key, attrs[key]);
}
}
}
return el;
},
Forms: function (formId) {
return new Form(formId);
},
docready: function (fn) {
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', fn, false);
//window.addEventListener('load', fn, false);
}
else if (document.attachEvent) {
document.attachEvent('onreadystatechange', fn);
//window.attachEvent('onload', fn);
}
}
}
var DOM = function (params) {
/* ----------------------------------------------------------------
* selector gets the parameter as an array and returns this as an
* object
* ----------------------------------------------------------------
*/
var selector;
var i;
if (typeof params === 'string'){
selector = document.querySelectorAll(params);
} else if (params.length) {
selector = params;
} else {
selector = [params];
}
this.length = selector.length;
for (i=0; i<this.length; i++) {
this[i] = selector[i];
}
return this;
}
/* =====================================================================
* DOM PROTOTYPE FUNCTIONS
* =====================================================================
*/
DOM.prototype = {
/* -----------------------------------------------------------------
* Helper functions. only used inside the DOM prototype
* -----------------------------------------------------------------
*/
map: function (callbackfunc) {
var results = [];
var len;
for (len = 0; len < this.length; len++) {
results.push(callbackfunc.call(this, this[len], len));
}
return results;
},
retOp: function (callbackfunc) {
this.map(callbackfunc);
return this;
},
retVal: function (callbackfunc) {
var m;
m = this.map(callbackfunc);
return m.length > 1 ? m : m[0];
},
/* -----------------------------------------------------------------
* Actual DOM functions
* -----------------------------------------------------------------
*/
hide: function () {
this.retOp(function (el) {
el.style.display = 'none';
});
return this;
},
show: function () {
this.retOp(function (el) {
el.style.display = 'block';
});
return this;
},
html: function (htm) {
if (typeof htm !== 'undefined') {
this.retOp(function(el){
el.innerHTML = htm;
});
return this;
} else {
return this.retVal(function (el) {
return el.innerHTML;
});
}
},
text: function (txt) {
if (typeof txt !== 'undefined') {
this.retOp(function(el){
el.innerText = txt;
});
return this;
} else {
return this.retVal(function (el) {
return el.innerText;
});
}
},
val: function (setvalue) {
// set the value attribute of the html element or returns it
if (typeof setvalue !== 'undefined') {
this.retOp(function (el) {
el.value = setvalue;
});
return this;
} else {
return this.retVal(function (el) {
return el.value;
});
}
},
addClass: function (classes) {
var addedClass = '';
var n;
if(typeof classes !== 'string'){
for(; n < classes.length; n++) {
addedClass += ' ' + classes[n];
}
} else {
addedClass += ' ' + classes;
}
return this.retOp(function (el) {
el.className += addedClass;
});
},
removeClass: function (klass) {
/* the 'if' function fixes IE8 bug of non-support for indexOf */
if (typeof Array.prototype.indexOf !== 'function'){
Array.prototype.indexOf = function(item){
for(var i =0; i < this.length; i++){
if (this[i] === item){
return i;
}
}
return -1;
}
}
return this.retOp( function (el) {
var cs = el.className.split(' ');
var i;
while ((i = cs.indexOf(klass)) > -1) {
cs = cs.slice(0, i).concat(cs.slice(++i));
}
el.className = cs.join(' ');
});
},
attr: function (attr, val) {
/* this is used to set an attr of a func to a particular value
it works in hand with the set function of creation */
if (typeof val !== 'undefined') {
return this.retOp(function (el) {
el.setAttribute(attr, val);
});
} else {
return this.retVal(function (el) {
return el.getAttribute(attr);
});
}
},
/* appending and removing child nodes */
append: function(el){
return this.retOp(function(parEl, i){
el.retOp(function(childEl){
if(i>0){
childEl = childEl.cloneNode(true);
}
parEl.appendChild(childEl);
});
});
},
remove: function () {
return this.retOp(function (el) {
return el.parentNode.removeChild(el)
});
},
error: function( msg ) {
throw new Error( msg );
},
on: function (evnt, fn) {
/* test which of the event features exist before returning
the right function */
if (document.addEventListener) {
return this.retOp(function(el){
el.addEventListener(evnt, fn, false);
});
} else if (document.attachEvent) {
return this.retOp(function(el){
el.attachEvent('on' + evnt, fn);
});
} else {
return this.retOp(function(el){
el['on' + evnt] = fn;
});
}
},
off: function (evnt, fn) {
/* test which of the event features exist before returning
the right function */
if (document.removeEventListener) {
return this.retOp(function(el){
el.removeEventListener(evnt, fn, false);
});
} else if (document.detachEvent) {
return this.retOp(function(el){
el.detachEvent('on' + evnt, fn);
});
} else {
return this.retOp(function(el){
el['on' + evnt] = null;
});
}
}
}
/* =====================================================================
* END OF DOM FUNCTIONS
* =====================================================================
* BEGINNING OF FORM FUNCTIONS
* =====================================================================
*/
var Form = function (formId) {
this.form = document.forms[formId];
this.fieldval = ''; //represents the value gotten from the field
return this;
}
Form.prototype = {
mapforms: function (formfunc) {
var formresults = formfunc.call(this, this.form);
return formresults;
},
validate_as_emailfield: function (fieldId) {
var val;
var fieldval;
var tester;
val = this.mapforms(function(el){
return el.elements[fieldId];
});
fieldval = val.value;
var tester = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;
return tester.test(fieldval);
},
error_on_emailfield: function (fieldId) {
var val;
val = this.mapforms(function(el){
return el.elements[fieldId];
});
val.value = 'please input your email correctly';
document.getElementById(fieldId).style.color = 'red';
}
}
/* =================== INITIALIZER ==================================== */
if (!window.$) {
window.$ = $;
}
/* =============== * END INITIALIZER * ================================ */
})();