This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetterelement.js
241 lines (224 loc) · 6.84 KB
/
betterelement.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
/*
* BetterElement: Create custom HTML elements through client-side JS.
*/
/**
* Adds a <clock> element. If the type attribute is 'time', then show
* Date.toLocaleTimeString, and vice versa for 'date'.
*
* @global
*/
function doClock() {
var clockElement = new Element('clock');
clockElement.toExecuteOnRead = function (index, element) {
if (element.getAttribute('type') === 'time') {
element.innerHTML = new Date().toLocaleTimeString();
} else if (element.getAttribute('type') === 'date') {
element.innerHTML = new Date().toLocaleDateString();
}
};
clockElement.addAttribute('type', true, function (attribute) {
if (attribute === 'date' || attribute === 'time') {
return true;
// eslint-disable-next-line no-else-return
} else {
return false;
}
});
clockElement.readElements();
}
/**
* Adds a <random> tag. When min and max is valid, will generate a number
* between them.
*
* @global
*/
function doRandom() {
var randomElement = new Element('random');
randomElement.toExecuteOnRead = function (index, element) {
var min = Number(element.getAttribute('min'));
var max = Number(element.getAttribute('max'));
element.innerHTML = Math.floor((Math.random() * (max - min)) + min);
};
randomElement.addAttribute('min', true, Attribute.verifyPresets.number);
randomElement.addAttribute('max', true, Attribute.verifyPresets.number);
randomElement.readElements();
}
/**
* An object that semi-represents an HTML attribute.
*
* @constructor
* @global
* @param {String} nameParam The name of the attribute.
* @param {Boolean} requiredParam Weather or not the attribute is required, or if an error should be thrown if it is missing.
* @param {Function} verifyParam A callback to be called to verify if an attribute's value is valid.
* @param {String} valueParam The value of the attribute. Is undefined unless set.
* @returns {Attribute} The new attribute.
* @property {String} name The name of the attribute.
* @property {Boolean} required Weather or not the attribute is required, or if an error should be thrown if it is missing.
* @property {Function} verify A callback to be called to verify if an attribute's value is valid.
* @property {String} value The value of the attribute. Is undefined unless set.
*/
function Attribute(nameParam, requiredParam, verifyParam, valueParam) {
this.name = nameParam;
this.required = requiredParam || true;
this.value = valueParam || undefined;
this.verify = verifyParam || function () {
return true;
};
return this;
}
/**
* An object of preset verification methods.
* @type {Object}
* @property {Function} numbers The verification for if it is a number.
* @property {Function} regex The verification for if it is a valid regex.
* @memberof Attribute
*/
Attribute.verifyPresets = {};
/**
* Returns true if 'number' is a number.
*
* @param {Number|String} number The number to check
* @returns {Boolean} The result
*/
Attribute.verifyPresets.number = function (number) {
return !isNaN(Number(number));
};
/**
* Returns true if 'regex' is a valid regular expression.
*
* @param {String|Regex} regex The regex to check
* @returns {Boolean} The result
*/
Attribute.verifyPresets.regex = function (regex) {
try {
// The 'new RegExp()' throws a SyntaxError, so we wrap in try/catch.
return new RegExp(regex) !== undefined;
} catch (err) {
if (err instanceof SyntaxError) {
return false;
}
throw err;
}
};
/**
* A BetterElement element.
*
* @param {String} nameParam The name of the BetterElement (what will be inside of the <>s)
* @global
* @constructor
* @returns {Element}
*/
function Element(nameParam) {
/**
* The attributes of a BetterElement.
* @type {Attribute[]}
*/
this.attributes = [];
/**
* The name of the BetterElement. For example, in a <clock> tag
* would be 'clock'.
* @type {String}
* @constant
*/
this.name = nameParam;
/**
* A callback for when an element is read. Called once for each
* element.
* @type {Function}
*
* @param {Number} index The index of the element.
* @param {String} element The element's contents.
*/
this.toExecuteOnRead = undefined;
/**
* Adds an attribute to this.attributes.
*
* @param {Attribute|String} attribute The attribute itself, or a string for it's name.
* @param {Boolean} required Weather or not it is required.
* @param {Function} verify The verification function, called on each attribute's contents.
* @param {String} value The value of the attribute. Currently unused.
* @returns {Attribute} The finished attribute.
*/
this.addAttribute = function (attribute, required, verify, value) {
if (attribute instanceof Attribute) {
this.attributes.push(attribute);
} else {
this.attributes.push(new Attribute(attribute, required, verify, value));
}
return this.attributes[this.attributes.length - 1];
};
/**
* Deletes an attribute from this.attributes.
*
* @param {String} attributename The attribute name to find and delete.
* @returns {Attribute} The deleted attribute
*/
this.delAttribute = function (attributename) {
var i = 0;
for (; this.attributes[i].name !== attributename; i += 1) { /* empty */ }
var attribute = this.attributes[i];
this.attributes.splice(i, 1);
return attribute;
};
/**
* Reads all the current elements and parses them.
*
* <br/><b>Note</b>: Soon this may be split into seperate functions.
*
* @throws <b>Error</b> if readElements is run without a toExecuteOnRead
* @throws <b>Error</b> if an attribute is missing that is marked as required.
* @throws <b>Error</b> if attribute verification failed.
*/
this.readElements = function () {
this.elements = [].slice.call(document.getElementsByTagName(this.name));
if (this.toExecuteOnRead) {
var _this = this;
this.elements.forEach(function (element, index) {
_this.attributes.forEach(function (attribute) {
if (attribute.required && !element.getAttribute(attribute.name)) {
throw new Error('Missing attribute ' + attribute.name + ' in element ' + index);
}
if (attribute.verify) {
if (!attribute.verify(element.getAttribute(attribute.name))) {
throw new Error('Element verification failed (type ' +
attribute.name + ', value ' + element.getAttribute(attribute.name) + ')');
}
}
});
_this.toExecuteOnRead(index, element);
});
} else {
throw new Error(this.name + ' executed readElements() without a toExecuteOnRead!');
}
};
/**
* Fetches all appropiate elements from the DOM.
*
* @returns {HTMLCollection}
*/
this.getElements = function () {
return document.getElementsByTagName(this.name);
};
}
/**
* Injects BetterElement into module.exports if it exists.
*
* @global
* @private
*/
function bEinject() {
try {
if (module && module.exports) {
module.exports = {
Attribute,
Element,
builtins: {
doRandom,
doClock
}
};
}
} catch (err) {}
}
bEinject();