-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_to_dom.js
339 lines (291 loc) · 9.85 KB
/
json_to_dom.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'use strict';
/**
* @typedef {object} JTD
* @property {JTD_Element} JTD_Element
* @property {JTD_Element_Group} JTD_Element_Group
* @property {JTD_Document} JTD_Document
*/
/**
* {JTD_Element, JTD_Element_Group, JTD_Document} = json_to_dom
* @namespace json_to_dom
* @returns {JTD}
*/
const json_to_dom = (() => {
const defaultJTD_Element = {
name: "",
classes: [],
id: false,
domType: "div"
};
// JTD_Element protected methods
const checkOptions = Symbol("checkOptions");
const getType = Symbol("getType");
const getRandomID = Symbol("getRandomID");
const combineClasses = Symbol("combineClasses");
const makeDOMElement = Symbol("makeDOMElement");
// JTD_Element protected properties
const validOptionsTypes = Symbol("validOptionsTypes");
const props = Symbol("props");
const DOMElement = Symbol("DOMElement");
const ID = Symbol("ID");
/**
* Creates a JTD Element
* @class
* @memberof json_to_dom
* @returns {JTD_Element}
*/
class JTD_Element {
/**
* @param {object} [options]
* @param {string} [options.name=""] Prepends the random generated IDs with this value.
* @param {string[]} [options.classes=[]] CSS classes to be added to the element.
* @param {boolean|string} [options.id=false] If set to `true`: will generate a random ID for the element with `options.name` as a prefix. If value is a `string`: will generate a random ID with set value as a postfix and `options.name` as a prefix.
* @param {string} [options.domType=div] Sets a DOM element type to be created.
*/
constructor(options) {
this[validOptionsTypes] = {
"name": "string",
"classes": "array",
"id": ["boolean", "string"],
"domType": "string",
"text": "string"
};
this[props] = Object.assign({}, defaultJTD_Element, this[checkOptions](options));
this[ID] = this[getRandomID]();
this[DOMElement] = this[makeDOMElement]();
}
/**
* Gets the created DOM element
* @readonly
* @returns {HTMLElement} DOM element generated.
*/
get domElement() {
return this[DOMElement];
}
/**
* Gets the random generated ID
* @readonly
* @returns {string} Random ID string.
*/
get id() {
return this[ID];
}
// Protected methods:
/**
* @private
* @param {object} obj
* @returns {string} A string representation of the Object type.
*/
[getType] (obj) {
return Object.prototype.toString.call(obj).split(' ')[1].slice(0,-1).toLowerCase();
}
/**
* @private
* @param {*} options
* @throws Will throw an error if value passed is not an Object.
* @returns {object} Returns an unchanged options Object.
*/
[checkOptions] (options) {
if (this[getType](options) !== "object") {
throw new Error(`Options is not an Object: options= ${options}`);
}
for (let option in options) {
if (Object.prototype.hasOwnProperty.call(options, option) && (option in this[validOptionsTypes])) {
if (this[getType](this[validOptionsTypes][option]) === "array") {
const validOption = this[validOptionsTypes][option].filter(optionType => {
return this[getType](options[option]) === optionType;
}).join('');
if (validOption === '') throw new Error(`Option ${option} is of invalid type`);
}
else if (this[getType](this[validOptionsTypes][option]) === "string") {
if ((this[getType](options[option]) !== this[validOptionsTypes][option])) {
throw new Error(`Option ${option} is of invalid type`);
}
}
}
}
return options;
}
/**
* Gets a random(ish) string.
* @private
* @returns {string} A random string;
*/
[getRandomID]() {
//from https://codewithmark.com/easily-generate-random-alphanumeric-string-in-javascript
return Math.random().toString(36).replace('0.', '');
}
/**
* Combines classes arrays
* @private
* @param {sting[]} classes An array of classes to be added to the element.
* @returns {string[]} An array of classes.
*/
[combineClasses](...classes) {
return classes.join(' ').replace(/[,]/g," ").trim();
}
/**
* Create a DOM element
* @private
* @returns {HTMLElement} A generated DOM element.
*/
[makeDOMElement]() {
// create a DOM element
const element = document.createElement(this[props].domType);
element.textContent = this[props].text;
// set id property
switch(this[getType](this[props].id)) {
case "boolean":
if (this[props].id) {
element.id = `${this[props].name !== "" ? this[props].name + "-" : ""}${this[ID]}`;
}
break;
case "string":
element.id = `${this[props].name !== "" ? this[props].name + "-" : ""}${this[ID]}-${this[props].id}`;
break;
default:
throw new Error("options.id is not type of 'string' nor 'boolean'");
}
// set classes
if (this[props].classes.length > 0) element.className = this[combineClasses](this[props].classes);
return element;
}
}
// JTD_Element_Group protected properties
const groupElements = Symbol("groupElements");
// JTD_Element_Group protected methods
const validateElements = Symbol("validateElements");
/**
* Creates a JTD Element Group
* @class
* @extends JTD_Element
* @returns {JTD_Element_Group}
*/
class JTD_Element_Group extends JTD_Element {
/**
* @param {Object} options
* @param {JTD_Element[]} [elements]
*/
constructor(options, elements) {
super(options);
this[groupElements] = [];
this.children = this[validateElements](elements);
}
/**
* @type {JTD_Element[]} Array of group children
* @returns {JTD_Element[]} Array of group children
*/
set children(elements) {
// remove duplicates
elements = [...new Set(elements)];
// filter out elements already included
if (this[groupElements].length > 0) {
elements = elements.filter(element => !this[groupElements].includes(element))
}
this[groupElements] = this[groupElements].concat(this[validateElements](elements));
elements.forEach(element => this[DOMElement].appendChild(element.domElement));
}
get children() {
return this[groupElements];
}
/**
* Get child of the group by ID.
* @param {string} childID An ID of the child.
* @returns {JTD_Element|null} An instance of the JTD_Element if found OR `null` otherwise.
*/
getChildById(childID) {
return this[groupElements].filter(element => element.id === childID)[0] || null;
}
/**
* Removes a child from the group
* @param {(JTD_Element|JTD_Element[])} children An instance or an `Array` of instances of `JTD_Element` to remove.
* @returns {JTD_Element_Group} An instance of the `JTD_Element_Group` for chaining.
*/
removeChildren(children) {
if (this[groupElements].length > 0) {
this[validateElements](children).forEach(child => {
child.domElement.remove();
this[groupElements] = this[groupElements].filter(element => element.id !== child.id);
});
}
return this;
}
/**
* @private
* @param {JTD_Element|JTD_Element[]} elements
* @returns {JTD_Element[]}
*/
[validateElements] (elements) {
if (this[getType](elements) !== "array") elements = [elements];
elements.forEach(element => {
if (!(element instanceof JTD_Element)) throw new Error(`${element} is not instance of JTD_Element`);
});
return elements;
}
}
// JTD_Document protected methods
const validateData = Symbol("validateData");
const makeDocument = Symbol("makeDocument");
// JTD_Document protected properties
const DATA = Symbol("DATA");
const documentData = Symbol("documentData");
/**
* Creates a JTD Document
* @class
* @returns {JTD_Document}
*/
class JTD_Document {
/**
* @param {object} JSON_DATA JSON data to be converted to HTML
*/
constructor(JSON_DATA) {
this[DATA] = this[validateData](JSON_DATA);
this[documentData] = this[makeDocument](this[DATA])[0];
}
/**
* Gets the generated DOM Element
* @type {HTMLElement}
* @readonly
*/
get DOMElement () {
return this[documentData].domElement;
}
[makeDocument](JSON_DATA) {
const document = JSON_DATA.reduce((acc, cur) => {
switch (cur.type) {
case "group":
acc.push(new JTD_Element_Group({
"name": cur.name || "",
"classes": cur.classes || [],
"id": cur.id || false,
"domType": cur.domType || "div",
}, this[makeDocument](cur.children)));
return acc;
case "child":
acc.push(new JTD_Element({
"name": cur.name || "",
"classes": cur.classes || [],
"id": cur.id || false,
"domType": cur.domType || "div",
"text": cur.text || ""
}));
return acc;
default:
throw new Error (`Unrecognized type: ${cur.type}`);
}
}, []);
return document;
}
[getType] (obj) {
return Object.prototype.toString.call(obj).split(' ')[1].slice(0,-1).toLowerCase();
}
[validateData](JSON_DATA) {
if (this[getType](JSON_DATA) !== "array") JSON_DATA = [JSON_DATA];
JSON_DATA.forEach(data => {
if (data.type !== "group") throw new Error(`Wrong format of JSON data received: Top Object should have type set to "group"`);
});
return JSON_DATA;
}
}
return {JTD_Document, JTD_Element, JTD_Element_Group};
})();