fda0b1ca7d5bdf9a5d3c45bd4e2a8e3c8972a864
[ccsdk/features.git] /
1 /**
2  * Copyright 2010-2013 Ben Birch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this software except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 (function($, document) {
17
18         var create = $.create = (function() {
19
20                 function addAttrs( el, obj, context ) {
21                         for( var attr in obj ){
22                                 switch( attr ) {
23                                 case 'tag' :
24                                         break;
25                                 case 'html' :
26                                         el.innerHTML = obj[ attr ];
27                                         break;
28                                 case 'css' :
29                                         for( var style in obj.css ) {
30                                                 $.attr( el.style, style, obj.css[ style ] );
31                                         }
32                                         break;
33                                 case 'text' : case 'child' : case 'children' :
34                                         createNode( obj[attr], el, context );
35                                         break;
36                                 case 'cls' :
37                                         el.className = obj[attr];
38                                         break;
39                                 case 'data' :
40                                         for( var data in obj.data ) {
41                                                 $.data( el, data, obj.data[data] );
42                                         }
43                                         break;
44                                 default :
45                                         if( attr.indexOf("on") === 0 && $.isFunction(obj[attr]) ) {
46                                                 $.event.add( el, attr.substr(2).replace(/^[A-Z]/, function(a) { return a.toLowerCase(); }), obj[attr] );
47                                         } else {
48                                                 $.attr( el, attr, obj[attr] );
49                                         }
50                                 }
51                         }
52                 }
53
54                 function createNode(obj, parent, context) {
55                         if(obj && ($.isArray(obj) || obj instanceof $)) {
56                                 for(var ret = [], i = 0; i < obj.length; i++) {
57                                         var newNode = createNode(obj[i], parent, context);
58                                         if(newNode) {
59                                                 ret.push(newNode);
60                                         }
61                                 }
62                                 return ret;
63                         }
64                         var el;
65                         if(typeof(obj) === 'string') {
66                                 el = context.createTextNode( obj );
67                         } else if(!obj) {
68                                 return undefined;
69                         } else if(obj.nodeType === 1) {
70                                 el = obj;
71                         } else if( obj instanceof app.ui.AbstractWidget ) {
72                                 el = obj.el[0];
73                         } else {
74                                 el = context.createElement( obj.tag || 'DIV' );
75                                 addAttrs(el, obj, context);
76                         }
77                         if(parent){ parent.appendChild(el); }
78                         return el;
79                 }
80
81                 return function(elementDef, parentNode) {
82                         return createNode(elementDef, parentNode, (parentNode && parentNode.ownerDocument) || document);
83                 };
84                 
85         })();
86         
87
88         // inject create into jquery internals so object definitions are treated as first class constructors (overrides non-public methods)
89         var clean = $.clean,
90                 init = $.fn.init;
91
92         $.clean = function( elems, context, fragment, scripts ) {
93                 for(var i = 0; i < elems.length; i++) {
94                         if( elems[i].tag || elems[i] instanceof app.ui.AbstractWidget ) {
95                                 elems[i] = create( elems[i], null, context );
96                         }
97                 }
98                 return clean( elems, context, fragment, scripts );
99         };
100
101         $.fn.init = function( selector, context, rootjQuery ) {
102                 if ( selector && ( selector.tag || selector instanceof app.ui.AbstractWidget )) {
103                         selector = create( selector, null, context );
104                 }
105                 return init.call( this, selector, context, rootjQuery );
106         };
107
108         $.fn.init.prototype = $.fn;
109
110 })(jQuery, window.document);
111