jsonformat.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var JSONFormat = (function(){
  2. var _toString = Object.prototype.toString;
  3. function format(object, indent_count){
  4. var html_fragment = '';
  5. switch(_typeof(object)){
  6. case 'Null' :0
  7. html_fragment = _format_null(object);
  8. break;
  9. case 'Boolean' :
  10. html_fragment = _format_boolean(object);
  11. break;
  12. case 'Number' :
  13. html_fragment = _format_number(object);
  14. break;
  15. case 'String' :
  16. html_fragment = _format_string(object);
  17. break;
  18. case 'Array' :
  19. html_fragment = _format_array(object, indent_count);
  20. break;
  21. case 'Object' :
  22. html_fragment = _format_object(object, indent_count);
  23. break;
  24. }
  25. return html_fragment;
  26. };
  27. function _format_null(object){
  28. return '<span class="json_null">null</span>';
  29. }
  30. function _format_boolean(object){
  31. return '<span class="json_boolean">' + object + '</span>';
  32. }
  33. function _format_number(object){
  34. return '<span class="json_number">' + object + '</span>';
  35. }
  36. function _format_string(object){
  37. if(0 <= object.search(/^http/)){
  38. object = '<a href="' + object + '" target="_blank" class="json_link">' + object + '</a>'
  39. }
  40. return '<span class="json_string">"' + object + '"</span>';
  41. }
  42. function _format_array(object, indent_count){
  43. var tmp_array = [];
  44. for(var i = 0, size = object.length; i < size; ++i){
  45. tmp_array.push(indent_tab(indent_count) + format(object[i], indent_count + 1));
  46. }
  47. return '[\n'
  48. + tmp_array.join(',\n')
  49. + '\n' + indent_tab(indent_count - 1) + ']';
  50. }
  51. function _format_object(object, indent_count){
  52. var tmp_array = [];
  53. for(var key in object){
  54. tmp_array.push( indent_tab(indent_count) + '<span class="json_key">"' + key + '"</span>:' + format(object[key], indent_count + 1));
  55. }
  56. return '{\n'
  57. + tmp_array.join(',\n')
  58. + '\n' + indent_tab(indent_count - 1) + '}';
  59. }
  60. function indent_tab(indent_count){
  61. return (new Array(indent_count + 1)).join(' ');
  62. }
  63. function _typeof(object){
  64. var tf = typeof object,
  65. ts = _toString.call(object);
  66. return null === object ? 'Null' :
  67. 'undefined' == tf ? 'Undefined' :
  68. 'boolean' == tf ? 'Boolean' :
  69. 'number' == tf ? 'Number' :
  70. 'string' == tf ? 'String' :
  71. '[object Function]' == ts ? 'Function' :
  72. '[object Array]' == ts ? 'Array' :
  73. '[object Date]' == ts ? 'Date' : 'Object';
  74. };
  75. function loadCssString(){
  76. var style = document.createElement('style');
  77. style.type = 'text/css';
  78. var code = Array.prototype.slice.apply(arguments).join('');
  79. try{
  80. style.appendChild(document.createTextNode(code));
  81. }catch(ex){
  82. style.styleSheet.cssText = code;
  83. }
  84. document.getElementsByTagName('head')[0].appendChild(style);
  85. }
  86. loadCssString(
  87. '.json_key{ color: purple;}',
  88. '.json_null{color: red;}',
  89. '.json_string{ color: #077;}',
  90. '.json_link{ color: #717171;}',
  91. '.json_array_brackets{}');
  92. var _JSONFormat = function(origin_data){
  93. this.data = 'string' != typeof origin_data ? origin_data :
  94. JSON && JSON.parse ? JSON.parse(origin_data) : eval('(' + origin_data + ')');
  95. };
  96. _JSONFormat.prototype = {
  97. constructor : JSONFormat,
  98. toString : function(){
  99. return format(this.data, 1);
  100. }
  101. }
  102. return _JSONFormat;
  103. })();
  104. function create_result_contatiner(){
  105. var $result = $('<pre id="result" style=" width: 100%; height: 100%; overflow: scroll; overflow-x: scroll; overflow-y:scroll"></pre>')
  106. var $result_container = $('<div id="result_container" style=" top: 1%; right: 8px; margin: 0; padding: 0; border:1px solid skyblue; background: #f8f8f8; line-height: 1.2em; font-size: 14px; cursor: pointer;"></div>');
  107. $result_container.append($result);
  108. $('body').append($result_container);
  109. return [$result_container, $result];
  110. }
  111. /*
  112. (function request_intercept(args){
  113. var $result_container = args[0],
  114. $result = args[1];
  115. $('form *[type="submit"]').bind('click', function(){
  116. var _form = $(this).parents('form'),
  117. _action = (_form.attr('action') || './'),
  118. _method = (_form.attr('method') || 'get').toLowerCase(),
  119. _params = {};
  120. _form.find('input').each(function(){
  121. var item = $(this);
  122. _params[item.attr('name')] = item.val();
  123. });
  124. qls.loading();
  125. $['get' == _method ? 'get' : 'post'](_action, _params, function(response){
  126. qls.close();
  127. try{
  128. var j = new JSONFormat(JSON && JSON.parse ? JSON.parse(response) : eval('(' + response + ')'));
  129. $result.html(j.toString());
  130. $.post(interfacevaluesurl,{values:j.toString(),id:$('select[name="interface"]').val()});
  131. }catch (e){
  132. $result.html(response);
  133. }
  134. //$result_container.stop(true).animate({width:'50%'}, 'slow');
  135. });
  136. return false;
  137. });
  138. })(create_result_contatiner());*/