Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ new HtmlGenerator ([
tag: "div",
val: "Value",
att: {class: "class-name"},
events: { 'click': function(){console.log('clicked')}, 'mouseover': function(){console.log('mouseover')}},
childs: [
{
tag: "div",
Expand Down
49 changes: 29 additions & 20 deletions htmlGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
( global = global || self, global.HtmlGenerator = factory());
}( this, function () {

function _gernerateHtml( listObjectDOM, parentElement ) {
parentElement = parentElement || document.body
function _generateHtml( 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)
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);
if (e.events !== undefined) _setEvent(tag, e.events);
parentElement.appendChild(tag)
});
}
Expand All @@ -20,37 +21,45 @@
function _setAtt( tag, attributes ) {
for (const key in attributes) {
if (attributes.hasOwnProperty(key)) {
tag.setAttribute(key, attributes[key])
tag.setAttribute(key, attributes[key]);
}
}
}

function _setValue( tag, value ) {
tag.textContent = value
tag.textContent = value;
}

function _setEvent(tag, events) {
for (const key in events) {
if (events.hasOwnProperty(key)) {
tag.addEventListener(key, events[key]);
}
}
}

function _initFunctios( hg ) {
hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) }
hg.setValue = function( tag, value ) { _setValue( tag, value ) }
function _initFunctions( hg ) {
hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) };
hg.setValue = function( tag, value ) { _setValue( tag, value ) };
hg.setEvent = function( tag, events ) { _setEvent( tag, value ) };
}

function init( HtmlGenerator ) {
HtmlGenerator.prototype.generate = function ( listObjectDOM, parentElement ) {
let hg = this
_gernerateHtml( listObjectDOM, parentElement )
_initFunctios(hg)
}
let hg = this;
_generateHtml( listObjectDOM, parentElement );
_initFunctions(hg);
};
}

init(HtmlGenerator)
init(HtmlGenerator);

function HtmlGenerator( listObjectDOM, parentElement = null ) {
if (this instanceof HtmlGenerator && listObjectDOM != null){
this.generate( listObjectDOM, parentElement )
if (this instanceof HtmlGenerator && listObjectDOM != null) {
this.generate( listObjectDOM, parentElement );
}
}

return HtmlGenerator
return HtmlGenerator;

}))
}));
3 changes: 2 additions & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
{
tag: "div",
att: { class: "class-name-3" },
val: "Value 3"
val: "Value 3",
events: { 'click': function(){console.log('click')}}
}
], document.body)

Expand Down