From c35ddb3b83005effe87177128ac112d1df334ab3 Mon Sep 17 00:00:00 2001 From: zero-custom Date: Tue, 2 Jul 2024 18:17:59 +0800 Subject: [PATCH 1/3] Update htmlGenerator.js fix typo --- htmlGenerator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/htmlGenerator.js b/htmlGenerator.js index 8c52176..9ae5f60 100644 --- a/htmlGenerator.js +++ b/htmlGenerator.js @@ -4,7 +4,7 @@ ( global = global || self, global.HtmlGenerator = factory()); }( this, function () { - function _gernerateHtml( listObjectDOM, parentElement ) { + function _generateHtml( listObjectDOM, parentElement ) { parentElement = parentElement || document.body if (typeof listObjectDOM === 'object' ? Array.isArray(listObjectDOM) : null) { listObjectDOM.forEach(e => { @@ -30,7 +30,7 @@ } - function _initFunctios( hg ) { + function _initFunctions( hg ) { hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) } hg.setValue = function( tag, value ) { _setValue( tag, value ) } } @@ -38,8 +38,8 @@ function init( HtmlGenerator ) { HtmlGenerator.prototype.generate = function ( listObjectDOM, parentElement ) { let hg = this - _gernerateHtml( listObjectDOM, parentElement ) - _initFunctios(hg) + _generateHtml( listObjectDOM, parentElement ) + _initFunctions(hg) } } From 049122c2b1e4a470f43979610d6a131535048ea0 Mon Sep 17 00:00:00 2001 From: zero-custom Date: Wed, 3 Jul 2024 01:23:04 +0800 Subject: [PATCH 2/3] new option 'event' set EventListener --- README.md | 1 + htmlGenerator.js | 45 +++++++++++++++++++++++++++------------------ test.html | 3 ++- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 36793ab..8f81938 100644 --- a/README.md +++ b/README.md @@ -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", diff --git a/htmlGenerator.js b/htmlGenerator.js index 9ae5f60..b32e32d 100644 --- a/htmlGenerator.js +++ b/htmlGenerator.js @@ -5,13 +5,14 @@ }( this, function () { function _generateHtml( listObjectDOM, parentElement ) { - parentElement = parentElement || document.body + 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) }); } @@ -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 _initFunctions( hg ) { - hg.setAttributes = function( tag, attributes ) { _setAtt( tag, attributes ) } - hg.setValue = function( tag, value ) { _setValue( tag, value ) } + 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 - _generateHtml( listObjectDOM, parentElement ) - _initFunctions(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; -})) +})); diff --git a/test.html b/test.html index 2c3a751..28a2b19 100644 --- a/test.html +++ b/test.html @@ -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) From 70449a2db07fceadd2ab35ac5013d759906fdab0 Mon Sep 17 00:00:00 2001 From: zero-custom Date: Wed, 3 Jul 2024 08:51:33 +0800 Subject: [PATCH 3/3] Update htmlGenerator.js --- htmlGenerator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htmlGenerator.js b/htmlGenerator.js index b32e32d..0f13406 100644 --- a/htmlGenerator.js +++ b/htmlGenerator.js @@ -12,7 +12,7 @@ _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); + if (e.events !== undefined) _setEvent(tag, e.events); parentElement.appendChild(tag) }); }