-
Notifications
You must be signed in to change notification settings - Fork 4
/
htmlGenerator.js
56 lines (46 loc) · 1.71 KB
/
htmlGenerator.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
(function ( global, factory ) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
( global = global || self, global.HtmlGenerator = factory());
}( this, function () {
function _gernerateHtml( listObjectDOM, parentElement ) {
parentElement = parentElement || document.body
if (typeof listObjectDOM === 'object' ? Array.isArray(listObjectDOM) : null) {
listObjectDOM.forEach(e => {
let tag = document.createElement(e.tag)
_setAtt(tag, e.att)
if (e.val !== undefined) _setValue(tag, e.val)
if (e.childs !== undefined) new HtmlGenerator(e.childs, tag)
parentElement.appendChild(tag)
});
}
}
function _setAtt( tag, attributes ) {
for (const key in attributes) {
if (attributes.hasOwnProperty(key)) {
tag.setAttribute(key, attributes[key])
}
}
}
function _setValue( tag, value ) {
tag.textContent = value
}
function _initFunctios( hg ) {
hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) }
hg.setValue = function( tag, value ) { _setValue( tag, value ) }
}
function init( HtmlGenerator ) {
HtmlGenerator.prototype.generate = function ( listObjectDOM, parentElement ) {
let hg = this
_gernerateHtml( listObjectDOM, parentElement )
_initFunctios(hg)
}
}
init(HtmlGenerator)
function HtmlGenerator( listObjectDOM, parentElement = null ) {
if (this instanceof HtmlGenerator && listObjectDOM != null){
this.generate( listObjectDOM, parentElement )
}
}
return HtmlGenerator
}))