Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2012 00:13
Show Gist options
  • Save anonymous/4272918 to your computer and use it in GitHub Desktop.
Save anonymous/4272918 to your computer and use it in GitHub Desktop.
A version of "this is the JS I end up with" from [1] using Tim Caswell's dombuilder [2] library [1] http://qfox.nl/weblog/276 [2] https://github.com/creationix/dombuilder/
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>Table</title>
</head>
<body>
<script src="01-dombuilder.min.js"></script>
<script src="02-table.js"></script>
</body>
</html>
//////////////////////////////////////
// //
// JS domBuilder Library //
// //
// Tim Caswell <tim@creationix.com> //
// //
//////////////////////////////////////
(typeof define=="function"&&function(e){define("dombuilder",e)}||function(e){window.domBuilder=e()})(function(){"use strict";function e(n,a){if(typeof n=="string")return document.createTextNode(n);var f,l;for(var c=0,h=n.length;c<h;c++){var p=n[c];if(!f){if(typeof p=="string"){var d=p.match(o);d=d?d[0]:"div",f=document.createElement(d),l=!0;var v=p.match(r);v&&f.setAttribute("class",v.map(u).join(" "));var m=p.match(i);m&&f.setAttribute("id",m[0].substr(1));var g=p.match(s);a&&g&&(a[g[0].substr(1)]=f);continue}f=document.createDocumentFragment()}l&&typeof p=="object"&&p.__proto__===Object.prototype?t(f,p):f.appendChild(e(p,a)),l=!1}return f}function t(e,t){var r=Object.keys(t);for(var i=0,s=r.length;i<s;i++){var o=r[i],u=t[o];o==="$"?u(e):o==="css"?n(e.style,u):o.substr(0,2)==="on"?e.addEventListener(o.substr(2),u,!1):e.setAttribute(o,u)}}function n(e,t){var n=Object.keys(t);for(var r=0,i=n.length;r<i;r++){var s=n[r];e[s]=t[s]}}function u(e){return e.substr(1)}var r=/\.[^.#$]+/g,i=/#[^.#$]+/,s=/\$[^.#$]+/,o=/^[^.#$]+/;return e});
var refs = {}
var table = domBuilder(['table', ['tbody'
, ['tr'
, ['td']
, ['td', {style: 'text-align: center'}, ['button$up']]
, ['td']
]
, ['tr'
, ['td', ['button$left']]
, ['td'
, ['input$width', {'style': 'width: 50px'}]
, ' x '
, ['input$height', {'style': 'width: 50px'}]
]
, ['td', ['button$right']]
]
, ['tr'
, ['td']
, ['td', {style: 'text-align: center'}, ['button$down']]
, ['td']
]
]], refs)
// refs now contains .up .down .left .right .width .height
document.body.appendChild(table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment