2c9a0f54b38c75fbd8c445ae460e1d5d67fff362
[ccsdk/features.git] /
1 (function() {
2
3         var joey = this.joey = function joey(elementDef, parentNode) {
4                 return createNode( elementDef, parentNode, parentNode ? parentNode.ownerDocument : this.document );
5         };
6
7         var shortcuts = joey.shortcuts = {
8                 "text" : "textContent",
9                 "cls" : "className"
10         };
11
12         var plugins = joey.plugins = [
13                 function( obj, context ) {
14                         if( typeof obj === 'string' ) {
15                                 return context.createTextNode( obj );
16                         }
17                 },
18                 function( obj, context ) {
19                         if( "tag" in obj ) {
20                                 var el = context.createElement( obj.tag );
21                                 for( var attr in obj ) {
22                                         addAttr( el, attr, obj[ attr ], context );
23                                 }
24                                 return el;
25                         }
26                 }
27         ];
28
29         function addAttr( el, attr, value, context ) {
30                 attr = shortcuts[attr] || attr;
31                 if( attr === 'children' ) {
32                         for( var i = 0; i < value.length; i++) {
33                                 createNode( value[i], el, context );
34                         }
35                 } else if( attr === 'style' || attr === 'dataset' ) {
36                         for( var prop in value ) {
37                                 el[ attr ][ prop ] = value[ prop ];
38                         }
39                 } else if( attr.indexOf("on") === 0 ) {
40                         el.addEventListener( attr.substr(2), value, false );
41                 } else if( value !== undefined ) {
42                         el[ attr ] = value;
43                 }
44         }
45
46         function createNode( obj, parent, context ) {
47                 var el;
48                 if( obj != null ) {
49                         plugins.some( function( plug ) {
50                                 return ( el = plug( obj, context ) );
51                         });
52                         parent && parent.appendChild( el );
53                         return el;
54                 }
55         }
56
57 }());