Created
September 17, 2017 12:51
-
-
Save LeonardoCiaccio/31abfe13d113475eb6d318cc9158e008 to your computer and use it in GitHub Desktop.
Trasforma un oggetto JSON valido in elementi HTML validi, utilizzato da TRIMGLE : https://chrome.google.com/webstore/detail/trimgle-web-monitor/fkmafgfjkhkkdooapodflknnhmfefljg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Leonardo Ciaccio - [email protected] | |
Scritto per servire TRIMGLE : https://chrome.google.com/webstore/detail/trimgle-web-monitor/fkmafgfjkhkkdooapodflknnhmfefljg | |
*/ | |
( function(){ | |
"use strict"; | |
var | |
_isObj = function( _obj ){ | |
return ( typeof _obj === "object" && JSON.stringify( _obj ).indexOf( "{" ) == 0 ); | |
} | |
; | |
window.j2h = function( text, signature ){ | |
signature = signature || ""; | |
if( typeof text !== "string" )return null; | |
text = text.trim(); | |
var textJson ; | |
try{ | |
textJson = JSON.parse( text ); | |
}catch( e ){ | |
var newlistitem = document.createElement( "li" ); | |
if( signature != "" )newlistitem.setAttribute( "class", signature ); | |
newlistitem.textContent = text; | |
return newlistitem; | |
} | |
var self = window.j2h; | |
// --> Fatti i controlli possiamo procedere | |
if( Array.isArray( textJson ) === true ){ | |
var newlist = document.createElement( "ul" ); | |
textJson.forEach( function( arrEle, index ){ | |
var newlistitem = document.createElement( "li" ); | |
if( ( Array.isArray( arrEle ) === true ) || ( _isObj( arrEle ) === true ) ){ | |
var tmp = self( JSON.stringify( arrEle ) ); | |
if( !tmp )return true; | |
newlistitem = tmp; | |
}else{ | |
newlistitem.textContent = arrEle; | |
} | |
if( signature != "" )newlistitem.setAttribute( "class", signature ); | |
newlist.appendChild( newlistitem ); | |
} ); // <-- forEach | |
return ( newlist.childNodes.length > 0 ) ? newlist : null; | |
}else if( _isObj( textJson ) === true ){ | |
var newlist = document.createElement( "ul" ); | |
for( var x in textJson ){ | |
var newlistitem = document.createElement( "li" ); | |
if( signature != "" )newlistitem.setAttribute( "class", signature ); | |
newlistitem.textContent = x; | |
newlist.appendChild( newlistitem ); | |
var tmp = self( JSON.stringify( textJson[ x ] ), x.replace( /[^\w]/gi, "") ); | |
if( !tmp )continue; | |
newlist.appendChild( tmp ); | |
} | |
return ( newlist.childNodes.length > 0 ) ? newlist : null; | |
}else{ | |
var | |
newlist = document.createElement( "ul" ) | |
,newlistitem = document.createElement( "li" ) | |
; | |
if( signature != "" )newlistitem.setAttribute( "class", signature ); | |
newlistitem.textContent = textJson; | |
newlist.appendChild( newlistitem ); | |
return newlist; | |
} | |
return null; | |
}; | |
} )(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment