Last active
November 30, 2019 11:32
-
-
Save davidemaser/01f5f7e158f69051127348d64b41c192 to your computer and use it in GitHub Desktop.
DOM element generator using JQUERY. Builds dom elements from JSON formatted parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Created by David Maser. | |
| */ | |
| var generator = { | |
| accept : ['button','div','section','ul','li','nav','form','radio','checkbox','footer','header'], | |
| build:function(obj){ | |
| if(typeof obj == 'object') { | |
| var _core = obj.core; | |
| for (var i in _core) { | |
| var _valid = $.inArray(_core[i].type,generator.accept); | |
| var _generatorID = _core[i].type+'-'+i; | |
| var _structure = '<'+_core[i].type+' generator-id="core-'+_generatorID+'"'; | |
| _structure += _core[i].class !== '' && _core[i].class !== undefined ? ' class="'+_core[i].class+'"' : ''; | |
| _structure += _core[i].id !== '' && _core[i].id !== undefined ? ' id="'+_core[i].id+'"' : ''; | |
| _structure += _core[i].disabled !== '' && _core[i].disabled !== undefined && _core[i].disabled == true ? ' disabled' : ''; | |
| if (_core[i].attributes !== '' && _core[i].attributes !== undefined && typeof _core[i].attributes == 'object') { | |
| $.each(_core[i].attributes, function( key, value ) { | |
| if (_core[i].attributes[key] !== '' && _core[i].attributes[key] !== undefined) { | |
| _structure += ' '+key+'='+value; | |
| } | |
| }); | |
| } | |
| if (_core[i].style !== '' && _core[i].style !== undefined && typeof _core[i].style == 'object') { | |
| var _styleString = ''; | |
| $.each(_core[i].style, function( key, value ) { | |
| if (_core[i].style !== '' && _core[i].style !== undefined) { | |
| _styleString += key + ':' +value+';'; | |
| } | |
| }); | |
| _structure += ' style="'+_styleString+'"'; | |
| } | |
| _structure += '>'; | |
| _structure += _core[i].content; | |
| _structure += '</'+_core[i].type+'>'; | |
| if(_valid > -1){ | |
| $(_core[i].parent).append(_structure); | |
| $.each(_core[i].events, function( key, value ) { | |
| $('body').on(key,'[generator-id="core-'+_generatorID+'"]',function(){ | |
| eval(value); | |
| }); | |
| }); | |
| } | |
| } | |
| } | |
| } | |
| }; | |
| new generator.build( | |
| { | |
| "core": [{ | |
| "type": "button", | |
| "class":"boo", | |
| "id":"first", | |
| "disabled":false, | |
| "attributes":{ | |
| "data-href":"", | |
| "data-prop":"" | |
| }, | |
| "content":"Click Me", | |
| "style":{ | |
| "background":"#fff", | |
| "border-right":"1px solid #000", | |
| "color":"#000" | |
| }, | |
| "events":{ | |
| "click":"console.log('this')" | |
| }, | |
| "parent":"body" | |
| },{ | |
| "type": "button", | |
| "class":"boo", | |
| "id":"second", | |
| "disabled":false, | |
| "attributes":{ | |
| "data-href":"", | |
| "data-prop":"" | |
| }, | |
| "content":"Don't Click Me", | |
| "style":{ | |
| "background":"#fff", | |
| "border-right":"1px solid #000", | |
| "color":"#000" | |
| }, | |
| "events":{ | |
| "click":"console.log('sdfsdfsdf')" | |
| }, | |
| "parent":"body" | |
| }] | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment