c.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. window.SINGLE_TAB = " ";
  2. window.ImgCollapsed = "images/Collapsed.gif";
  3. window.ImgExpanded = "images/Expanded.gif";
  4. window.QuoteKeys = true;
  5. function $id(id){ return document.getElementById(id); }
  6. function IsArray(obj) {
  7. return obj &&
  8. typeof obj === 'object' &&
  9. typeof obj.length === 'number' &&
  10. !(obj.propertyIsEnumerable('length'));
  11. }
  12. function Process(data){
  13. SetTab();
  14. console.log(data);
  15. window.IsCollapsible = false;
  16. var json = data;
  17. var html = "";
  18. try{
  19. if(json == "") json = "\"\"";
  20. var obj = eval("["+json+"]");
  21. html = ProcessObject(obj[0], 0, false, false, false);
  22. $id("Canvas").innerHTML = "<PRE class='CodeContainer'>"+html+"</PRE>";
  23. }catch(e){
  24. alert("JSON数据格式不正确:\n"+e.message);
  25. $id("Canvas").innerHTML = "";
  26. }
  27. }
  28. window._dateObj = new Date();
  29. window._regexpObj = new RegExp();
  30. function ProcessObject(obj, indent, addComma, isArray, isPropertyContent){
  31. var html = "";
  32. var comma = (addComma) ? "<span class='Comma'>,</span> " : "";
  33. var type = typeof obj;
  34. var clpsHtml ="";
  35. if(IsArray(obj)){
  36. if(obj.length == 0){
  37. html += GetRow(indent, "<span class='ArrayBrace'>[ ]</span>"+comma, isPropertyContent);
  38. }else{
  39. clpsHtml = window.IsCollapsible ? "<span><img src=\""+window.ImgExpanded+"\" onClick=\"ExpImgClicked(this)\" /></span><span class='collapsible'>" : "";
  40. html += GetRow(indent, "<span class='ArrayBrace'>[</span>"+clpsHtml, isPropertyContent);
  41. for(var i = 0; i < obj.length; i++){
  42. html += ProcessObject(obj[i], indent + 1, i < (obj.length - 1), true, false);
  43. }
  44. clpsHtml = window.IsCollapsible ? "</span>" : "";
  45. html += GetRow(indent, clpsHtml+"<span class='ArrayBrace'>]</span>"+comma);
  46. }
  47. }else if(type == 'object'){
  48. if (obj == null){
  49. html += FormatLiteral("null", "", comma, indent, isArray, "Null");
  50. }else if (obj.constructor == window._dateObj.constructor) {
  51. html += FormatLiteral("new Date(" + obj.getTime() + ") /*" + obj.toLocaleString()+"*/", "", comma, indent, isArray, "Date");
  52. }else if (obj.constructor == window._regexpObj.constructor) {
  53. html += FormatLiteral("new RegExp(" + obj + ")", "", comma, indent, isArray, "RegExp");
  54. }else{
  55. var numProps = 0;
  56. for(var prop in obj) numProps++;
  57. if(numProps == 0){
  58. html += GetRow(indent, "<span class='ObjectBrace'>{ }</span>"+comma, isPropertyContent);
  59. }else{
  60. clpsHtml = window.IsCollapsible ? "<span><img src=\""+window.ImgExpanded+"\" onClick=\"ExpImgClicked(this)\" /></span><span class='collapsible'>" : "";
  61. html += GetRow(indent, "<span class='ObjectBrace'>{</span>"+clpsHtml, isPropertyContent);
  62. var j = 0;
  63. for(var prop in obj){
  64. var quote = window.QuoteKeys ? "\"" : "";
  65. html += GetRow(indent + 1, "<span class='PropertyName'>"+quote+prop+quote+"</span>: "+ProcessObject(obj[prop], indent + 1, ++j < numProps, false, true));
  66. }
  67. clpsHtml = window.IsCollapsible ? "</span>" : "";
  68. html += GetRow(indent, clpsHtml+"<span class='ObjectBrace'>}</span>"+comma);
  69. }
  70. }
  71. }else if(type == 'number'){
  72. html += FormatLiteral(obj, "", comma, indent, isArray, "Number");
  73. }else if(type == 'boolean'){
  74. html += FormatLiteral(obj, "", comma, indent, isArray, "Boolean");
  75. }else if(type == 'function'){
  76. if (obj.constructor == window._regexpObj.constructor) {
  77. html += FormatLiteral("new RegExp(" + obj + ")", "", comma, indent, isArray, "RegExp");
  78. }else{
  79. obj = FormatFunction(indent, obj);
  80. html += FormatLiteral(obj, "", comma, indent, isArray, "Function");
  81. }
  82. }else if(type == 'undefined'){
  83. html += FormatLiteral("undefined", "", comma, indent, isArray, "Null");
  84. }else{
  85. html += FormatLiteral(obj.toString().split("\\").join("\\\\").split('"').join('\\"'), "\"", comma, indent, isArray, "String");
  86. }
  87. return html;
  88. }
  89. function FormatLiteral(literal, quote, comma, indent, isArray, style){
  90. if(typeof literal == 'string')
  91. literal = literal.split("<").join("&lt;").split(">").join("&gt;");
  92. var str = "<span class='"+style+"'>"+quote+literal+quote+comma+"</span>";
  93. if(isArray) str = GetRow(indent, str);
  94. return str;
  95. }
  96. function FormatFunction(indent, obj){
  97. var tabs = "";
  98. for(var i = 0; i < indent; i++) tabs += window.TAB;
  99. var funcStrArray = obj.toString().split("\n");
  100. var str = "";
  101. for(var i = 0; i < funcStrArray.length; i++){
  102. str += ((i==0)?"":tabs) + funcStrArray[i] + "\n";
  103. }
  104. return str;
  105. }
  106. function GetRow(indent, data, isPropertyContent){
  107. var tabs = "";
  108. for(var i = 0; i < indent && !isPropertyContent; i++) tabs += window.TAB;
  109. if(data != null && data.length > 0 && data.charAt(data.length-1) != "\n")
  110. data = data+"\n";
  111. return tabs+data;
  112. }
  113. function QuoteKeysClicked(){
  114. window.QuoteKeys = $id("QuoteKeys").checked;
  115. Process();
  116. }
  117. function CollapseAllClicked(){
  118. EnsureIsPopulated();
  119. TraverseChildren($id("Canvas"), function(element){
  120. if(element.className == 'collapsible'){
  121. MakeContentVisible(element, false);
  122. }
  123. }, 0);
  124. }
  125. function ExpandAllClicked(){
  126. EnsureIsPopulated();
  127. TraverseChildren($id("Canvas"), function(element){
  128. if(element.className == 'collapsible'){
  129. MakeContentVisible(element, true);
  130. }
  131. }, 0);
  132. }
  133. function MakeContentVisible(element, visible){
  134. var img = element.previousSibling.firstChild;
  135. if(!!img.tagName && img.tagName.toLowerCase() == "img"){
  136. element.style.display = visible ? 'inline' : 'none';
  137. element.previousSibling.firstChild.src = visible ? window.ImgExpanded : window.ImgCollapsed;
  138. }
  139. }
  140. function TraverseChildren(element, func, depth){
  141. for(var i = 0; i < element.childNodes.length; i++){
  142. TraverseChildren(element.childNodes[i], func, depth + 1);
  143. }
  144. func(element, depth);
  145. }
  146. function ExpImgClicked(img){
  147. var container = img.parentNode.nextSibling;
  148. if(!container) return;
  149. var disp = "none";
  150. var src = window.ImgCollapsed;
  151. if(container.style.display == "none"){
  152. disp = "inline";
  153. src = window.ImgExpanded;
  154. }
  155. container.style.display = disp;
  156. img.src = src;
  157. }
  158. function CollapseLevel(level){
  159. EnsureIsPopulated();
  160. TraverseChildren($id("Canvas"), function(element, depth){
  161. if(element.className == 'collapsible'){
  162. if(depth >= level){
  163. MakeContentVisible(element, false);
  164. }else{
  165. MakeContentVisible(element, true);
  166. }
  167. }
  168. }, 0);
  169. }
  170. function SetTab(){
  171. window.TAB = MultiplyString(parseInt(3), window.SINGLE_TAB);
  172. }
  173. function EnsureIsPopulated(){
  174. if(!$id("Canvas").innerHTML && !!$id("RawJson").value) Process();
  175. }
  176. function MultiplyString(num, str){
  177. var sb =[];
  178. for(var i = 0; i < num; i++){
  179. sb.push(str);
  180. }
  181. return sb.join("");
  182. }
  183. function SelectAllClicked(){
  184. if(!!document.selection && !!document.selection.empty) {
  185. document.selection.empty();
  186. } else if(window.getSelection) {
  187. var sel = window.getSelection();
  188. if(sel.removeAllRanges) {
  189. window.getSelection().removeAllRanges();
  190. }
  191. }
  192. var range =
  193. (!!document.body && !!document.body.createTextRange)
  194. ? document.body.createTextRange()
  195. : document.createRange();
  196. if(!!range.selectNode)
  197. range.selectNode($id("Canvas"));
  198. else if(range.moveToElementText)
  199. range.moveToElementText($id("Canvas"));
  200. if(!!range.select)
  201. range.select($id("Canvas"));
  202. else
  203. window.getSelection().addRange(range);
  204. }
  205. function LinkToJson(){
  206. var val = $id("RawJson").value;
  207. val = escape(val.split('/n').join(' ').split('/r').join(' '));
  208. $id("InvisibleLinkUrl").value = val;
  209. $id("InvisibleLink").submit();
  210. }