1c621687a87368acd5fb5c6648ec7d78671b92c4
[ccsdk/features.git] /
1 (function($, document) {
2
3         var create = $.create = (function() {
4
5                 function addAttrs( el, obj, context ) {
6                         for( var attr in obj ){
7                                 switch( attr ) {
8                                 case 'tag' :
9                                         break;
10                                 case 'html' :
11                                         el.innerHTML = obj[ attr ];
12                                         break;
13                                 case 'css' :
14                                         for( var style in obj.css ) {
15                                                 $.attr( el.style, style, obj.css[ style ] );
16                                         }
17                                         break;
18                                 case 'text' : case 'child' : case 'children' :
19                                         createNode( obj[attr], el, context );
20                                         break;
21                                 case 'cls' :
22                                         el.className = obj[attr];
23                                         break;
24                                 case 'data' :
25                                         for( var data in obj.data ) {
26                                                 $.data( el, data, obj.data[data] );
27                                         }
28                                         break;
29                                 default :
30                                         if( attr.indexOf("on") === 0 && $.isFunction(obj[attr]) ) {
31                                                 $.event.add( el, attr.substr(2).replace(/^[A-Z]/, function(a) { return a.toLowerCase(); }), obj[attr] );
32                                         } else {
33                                                 $.attr( el, attr, obj[attr] );
34                                         }
35                                 }
36                         }
37                 }
38
39                 function createNode(obj, parent, context) {
40                         if(obj && ($.isArray(obj) || obj instanceof $)) {
41                                 for(var ret = [], i = 0; i < obj.length; i++) {
42                                         var newNode = createNode(obj[i], parent, context);
43                                         if(newNode) {
44                                                 ret.push(newNode);
45                                         }
46                                 }
47                                 return ret;
48                         }
49                         var el;
50                         if(typeof(obj) === 'string') {
51                                 el = context.createTextNode( obj );
52                         } else if(!obj) {
53                                 return undefined;
54                         } else if(obj.nodeType === 1) {
55                                 el = obj;
56                         } else if( obj instanceof app.ui.AbstractWidget ) {
57                                 el = obj.el[0];
58                         } else {
59                                 el = context.createElement( obj.tag || 'DIV' );
60                                 addAttrs(el, obj, context);
61                         }
62                         if(parent){ parent.appendChild(el); }
63                         return el;
64                 }
65
66                 return function(elementDef, parentNode) {
67                         return createNode(elementDef, parentNode, (parentNode && parentNode.ownerDocument) || document);
68                 };
69                 
70         })();
71         
72
73         // inject create into jquery internals so object definitions are treated as first class constructors (overrides non-public methods)
74         var clean = $.clean,
75                 init = $.fn.init;
76
77         $.clean = function( elems, context, fragment, scripts ) {
78                 for(var i = 0; i < elems.length; i++) {
79                         if( elems[i].tag || elems[i] instanceof app.ui.AbstractWidget ) {
80                                 elems[i] = create( elems[i], null, context );
81                         }
82                 }
83                 return clean( elems, context, fragment, scripts );
84         };
85
86         $.fn.init = function( selector, context, rootjQuery ) {
87                 if ( selector && ( selector.tag || selector instanceof app.ui.AbstractWidget )) {
88                         selector = create( selector, null, context );
89                 }
90                 return init.call( this, selector, context, rootjQuery );
91         };
92
93         $.fn.init.prototype = $.fn;
94
95 })(jQuery, window.document);
96