2  * Copyright 2010-2013 Ben Birch
 
   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
 
   8  *   http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  16 (function($, document) {
 
  18         var create = $.create = (function() {
 
  20                 function addAttrs( el, obj, context ) {
 
  21                         for( var attr in obj ){
 
  26                                         el.innerHTML = obj[ attr ];
 
  29                                         for( var style in obj.css ) {
 
  30                                                 $.attr( el.style, style, obj.css[ style ] );
 
  33                                 case 'text' : case 'child' : case 'children' :
 
  34                                         createNode( obj[attr], el, context );
 
  37                                         el.className = obj[attr];
 
  40                                         for( var data in obj.data ) {
 
  41                                                 $.data( el, data, obj.data[data] );
 
  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] );
 
  48                                                 $.attr( el, attr, obj[attr] );
 
  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);
 
  65                         if(typeof(obj) === 'string') {
 
  66                                 el = context.createTextNode( obj );
 
  69                         } else if(obj.nodeType === 1) {
 
  71                         } else if( obj instanceof app.ui.AbstractWidget ) {
 
  74                                 el = context.createElement( obj.tag || 'DIV' );
 
  75                                 addAttrs(el, obj, context);
 
  77                         if(parent){ parent.appendChild(el); }
 
  81                 return function(elementDef, parentNode) {
 
  82                         return createNode(elementDef, parentNode, (parentNode && parentNode.ownerDocument) || document);
 
  88         // inject create into jquery internals so object definitions are treated as first class constructors (overrides non-public methods)
 
  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 );
 
  98                 return clean( elems, context, fragment, scripts );
 
 101         $.fn.init = function( selector, context, rootjQuery ) {
 
 102                 if ( selector && ( selector.tag || selector instanceof app.ui.AbstractWidget )) {
 
 103                         selector = create( selector, null, context );
 
 105                 return init.call( this, selector, context, rootjQuery );
 
 108         $.fn.init.prototype = $.fn;
 
 110 })(jQuery, window.document);