|
| 1 | +//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude); |
| 2 | +//>>description: Create an array of DOM elements from a JSON description |
| 3 | +//>>label: createDom |
| 4 | +//>>group: Core |
| 5 | + |
| 6 | +define( [ "jquery.mobile.core" ], function( $ ) { |
| 7 | +//>>excludeEnd("jqmBuildExclude"); |
| 8 | +(function( $, undefined ) { |
| 9 | + |
| 10 | +//el can be one of two things: |
| 11 | +// 1. { tagname: [ { attr: value, ... }, [ children ]] } |
| 12 | +// 2. "a string" |
| 13 | +function mkEl( el ) { |
| 14 | + var ret, key, idx, attr, children, child; |
| 15 | + |
| 16 | + if ( $.type( el ) === "string" ) { |
| 17 | + ret = document.createTextNode( el ); |
| 18 | + } else { |
| 19 | + for ( key in el ) { |
| 20 | + ret = document.createElement( key ); |
| 21 | + for( idx in el[ key ] ) { |
| 22 | + if ( $.type( el[ key ][ idx ] ) === "object" ) { |
| 23 | + for( attr in el[ key ][ idx ] ) { |
| 24 | + ret.setAttribute( attr, el[ key ][ idx ][ attr ] ); |
| 25 | + } |
| 26 | + } else if ( $.type( el[ key ][ idx ] ) === "array" ) { |
| 27 | + children = mkChildren( el[ key ][ idx ] ); |
| 28 | + for( child in children ) { |
| 29 | + ret.appendChild( children[ child ] ); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return ret; |
| 37 | +} |
| 38 | + |
| 39 | +function mkChildren( c ) { |
| 40 | + var ret = [], idx; |
| 41 | + |
| 42 | + for ( idx in c ) { |
| 43 | + ret.push( mkEl( c[ idx ] ) ); |
| 44 | + } |
| 45 | + |
| 46 | + return ret; |
| 47 | +} |
| 48 | + |
| 49 | +$.mobile.createDom = function( j ) { |
| 50 | + return ( ( $.type( j ) === "object" ) ? mkEl( j ) : |
| 51 | + ( ( $.type( j ) === "array" ) ? mkChildren( j ) : |
| 52 | + undefined ) ); |
| 53 | +}; |
| 54 | + |
| 55 | +})( jQuery ); |
| 56 | +//>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude); |
| 57 | +}); |
| 58 | +//>>excludeEnd("jqmBuildExclude"); |
0 commit comments