JsonView.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. o2.widget = o2.widget || {};
  2. //o2.require("o2.widget.Panel", null, false);
  3. o2.widget.JsonView = new Class({
  4. Implements: [Options, Events],
  5. Extends: o2.widget.Common,
  6. options: {
  7. "title": "JsonView",
  8. "style": "default",
  9. "width": 400,
  10. "height": 400,
  11. "top": 0,
  12. "left": 0,
  13. "isMove": true,
  14. "isClose": true,
  15. "isMax": true,
  16. "isExpand": true,
  17. "isResize": true,
  18. "target": null
  19. },
  20. initialize: function(json, options){
  21. this.setOptions(options);
  22. this.json = json;
  23. this.jsonString = JSON.encode(json);
  24. this.node = new Element("div");
  25. },
  26. load: function(){
  27. if (this.fireEvent("queryLoad")){
  28. o2.require("o2.widget.Panel", function(){
  29. this.jsonObjectNode = this.createJsonObjectNode();
  30. this.jsonStringNode = this.createJsonStringNode();
  31. o2.require("o2.widget.Tab", function(){
  32. this.tab = new o2.widget.Tab(this.node, {"style": "moduleList"});
  33. this.tab.load();
  34. this.objectTabPage = this.tab.addTab(this.jsonObjectNode, "JSON", false);
  35. this.stringTabPage = this.tab.addTab(this.jsonStringNode, "Text", false);
  36. this.objectTabPage.showTab();
  37. }.bind(this));
  38. this.options["onResize"] = function(){
  39. this.setPanelSize();
  40. }.bind(this);
  41. this.jsonPanel = new o2.widget.Panel(this.node, this.options);
  42. this.jsonPanel.load();
  43. this.jsonPanel.show();
  44. this.setPanelSize();
  45. }.bind(this));
  46. this.fireEvent("postLoad");
  47. }
  48. },
  49. setPanelSize: function(){
  50. var contentSize = this.jsonPanel.content.getSize();
  51. var paddingTop = this.jsonPanel.content.getStyle("padding-top").toFloat();
  52. var paddingBottom = this.jsonPanel.content.getStyle("padding-bottom").toFloat();
  53. var tabSize = this.tab.tabNodeContainer.getSize();
  54. var marginTop = this.tab.tabNodeContainer.getStyle("margin-top").toFloat();
  55. var marginBottom = this.tab.tabNodeContainer.getStyle("margin-bottom").toFloat();
  56. var contentMarginTop = this.stringTabPage.contentNodeArea.getStyle("margin-top").toFloat();
  57. var contentMarginBottom = this.stringTabPage.contentNodeArea.getStyle("margin-bottom").toFloat();
  58. var contentHeight = contentSize.y-paddingTop-paddingBottom-tabSize.y-marginTop-marginBottom-contentMarginTop-contentMarginBottom-5;
  59. if (contentHeight<10) contentHeight = 10;
  60. this.jsonStringNode.setStyle("height", contentHeight);
  61. // this.jsonStringNode.setStyle("width", contentSize.x-32);
  62. this.jsonObjectNode.setStyle("height", contentHeight-20);
  63. },
  64. createJsonObjectNode: function(){
  65. this.jsonObjectNode = new Element("div", {
  66. "styles": {
  67. "overflow": "hideen",
  68. "margin-top": "0px",
  69. "height": "auto"
  70. }
  71. });
  72. this.loadObjectTree();
  73. return this.jsonObjectNode;
  74. },
  75. loadObjectTree: function(){
  76. if (this.objectTree){
  77. this.objectTree.node.destroy();
  78. this.objectTree = null;
  79. }
  80. o2.require("o2.widget.Tree", function(){
  81. this.objectTree = new o2.widget.Tree(this.jsonObjectNode, {"style": "jsonview"});
  82. this.objectTree.load();
  83. this.parseJsonObject(this.objectTree, "JSON", this.json, true);
  84. // var topNode = this.objectTree.appendChild({
  85. // "expand": true,
  86. // "title": "",
  87. // "text": "JSON",
  88. // "action": "",
  89. // "icon": "object.png"
  90. // });
  91. //
  92. // for (p in this.json){
  93. // this.parseJsonObject(topNode, p, this.json[p]);
  94. // }
  95. //
  96. }.bind(this));
  97. },
  98. parseJsonObject: function(treeNode, p, v, expand){
  99. var o = {
  100. "expand": expand,
  101. "title": "",
  102. "text": "",
  103. "action": "",
  104. "icon": ""
  105. };
  106. switch (typeOf(v)){
  107. case "object":
  108. o.text = p;
  109. o.icon = "object.png";
  110. var node = treeNode.appendChild(o);
  111. for (i in v){
  112. this.parseJsonObject(node, i, v[i], false);
  113. }
  114. break;
  115. case "array":
  116. o.text = p;
  117. o.icon = "array.png";
  118. var node = treeNode.appendChild(o);
  119. v.each(function(item, idx){
  120. this.parseJsonObject(node, "["+idx+"]", item,false);
  121. }.bind(this));
  122. break;
  123. default:
  124. o.text = p + " : "+v;
  125. o.icon = "string.png";
  126. var node = treeNode.appendChild(o);
  127. }
  128. },
  129. createJsonStringNode: function(){
  130. var jsonStringNode = new Element("textarea", {
  131. "readonly": false,
  132. "styles": {
  133. "width": "99%",
  134. //"width": "95%",
  135. "overflow": "auto",
  136. "border": "0px"
  137. }
  138. });
  139. jsonStringNode.set("text", JSON.format(this.json));
  140. return jsonStringNode;
  141. }
  142. });