JsonTemplate.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. o2.widget = o2.widget || {};
  2. o2.widget.JsonTemplate = new Class({
  3. Implements: [Options, Events],
  4. options: {},
  5. initialize: function(json, html, options){
  6. this.setOptions(options);
  7. this.json = json;
  8. this.html = html;
  9. },
  10. load: function(){
  11. if (this.fireEvent("queryLoad")){
  12. this.setContentText();
  13. this.setContentFun();
  14. //this.setContentBoolean();
  15. //this.setContentNumber();
  16. this.setContentEach();
  17. this.fireEvent("postLoad");
  18. }
  19. return this.html;
  20. },
  21. //setContentBoolean: function(){
  22. // var regexp = /(boolean\{).+?\}/g;
  23. // var r = this.html.match(regexp);
  24. // if(r){
  25. // if (r.length){
  26. // for (var i=0; i<r.length; i++){
  27. // var text = r[i].substr(0,r[i].lastIndexOf("}"));
  28. // text = text.substr(text.indexOf("{")+1,text.length);
  29. //
  30. // var value = this.getJsonContentBoolean(this.json ,text);
  31. // this.html = this.html.replace(/(boolean\{).+\}/,value);
  32. // }
  33. // }
  34. // }
  35. //},
  36. //setContentNumber: function(){
  37. // var regexp = /(number\{).+?\}/g;
  38. // var r = this.html.match(regexp);
  39. // if(r){
  40. // if (r.length){
  41. // for (var i=0; i<r.length; i++){
  42. // var text = r[i].substr(0,r[i].lastIndexOf("}"));
  43. // text = text.substr(text.indexOf("{")+1,text.length);
  44. //
  45. // var value = this.getJsonContentNumber(this.json ,text);
  46. // this.html = this.html.replace(/(number\{).+\}/,value);
  47. // }
  48. // }
  49. // }
  50. //},
  51. setContentText: function(){
  52. var regexp = /(text\{).+?\}/g;
  53. var r = this.html.match(regexp);
  54. if(r){
  55. if (r.length){
  56. for (var i=0; i<r.length; i++){
  57. var text = r[i].substr(0,r[i].lastIndexOf("}"));
  58. text = text.substr(text.indexOf("{")+1,text.length);
  59. var value = this.getJsonContent(this.json ,text);
  60. this.html = this.html.replace(/(text\{).+?\}/,value);
  61. }
  62. }
  63. }
  64. },
  65. setContentFun: function(){
  66. var regexp = /(fun\{).+?\}/g;
  67. var r = this.html.match(regexp);
  68. if(r){
  69. if (r.length){
  70. for (var i=0; i<r.length; i++){
  71. var text = r[i].substr(0,r[i].lastIndexOf("}"));
  72. text = text.substr(text.indexOf("{")+1,text.length);
  73. var value = this.getJsonFunContent(this.json ,text);
  74. this.html = this.html.replace(/(fun\{).+?\}/,value);
  75. }
  76. }
  77. }
  78. },
  79. //getJsonContentBoolean: function(json, text){
  80. // debugger;
  81. // o2.tempContent = json;
  82. // var jsonStr = text.replace(/\$/g, "o2.tempContent");
  83. //
  84. // o2.tempReturn = "";
  85. // Browser.exec("o2.tempReturn ="+jsonStr);
  86. // return (o2.tempReturn) ? true : false;
  87. //},
  88. //getJsonContentNumber: function(json, text){
  89. // o2.tempContent = json;
  90. // var jsonStr = text.replace(/\$/g, "o2.tempContent");
  91. //
  92. // o2.tempReturn = "";
  93. // Browser.exec("o2.tempReturn ="+jsonStr);
  94. // return (o2.tempReturn).toInt();
  95. //},
  96. getJsonContent: function(json, text){
  97. //o2.tempContent = json;
  98. //var jsonStr = text.replace(/\$/g, "o2.tempContent");
  99. //o2.tempReturn = "";
  100. //Browser.exec("o2.tempReturn ="+jsonStr);
  101. //return o2.tempReturn || "";
  102. var $ = json;
  103. var f = eval("(x = function($){\n return "+text+";\n})");
  104. returnValue = f.apply(json, [$]);
  105. if (returnValue===undefined || returnValue===null) returnValue="";
  106. returnValue = returnValue.toString();
  107. return returnValue || "";
  108. },
  109. getJsonFunContent: function(json, text){
  110. var $ = json;
  111. var f = eval("(x = function($){\n "+text+";\n})");
  112. returnValue = f.apply(json, [$]);
  113. return returnValue || "";
  114. },
  115. setContentEach: function(){
  116. var regexp = /(\{each\([\s\S]+\)\})[\s\S]+?(\{endEach\})/g;
  117. //var regexp = /(each\([\s\S]+\)\{)[\s\S]+\}/g;
  118. var r = this.html.match(regexp);
  119. if(r){
  120. if (r.length){
  121. for (var i=0; i<r.length; i++){
  122. var eachItemsStr = r[i].substr(0,r[i].indexOf(")"));
  123. eachItemsStr = eachItemsStr.substr(eachItemsStr.indexOf("(")+1,eachItemsStr.length);
  124. var pars = eachItemsStr.split(/,[\s]*/g);
  125. eachItemsPar = pars[0];
  126. eachItemsCount = pars[1].toInt();
  127. var eachItems = this.getJsonContent(this.json ,eachItemsPar);
  128. if (eachItems) if (eachItemsCount==0) eachItemsCount = eachItems.length;
  129. var eachContentStr = r[i].substr(0,r[i].lastIndexOf("{endEach}"));
  130. eachContentStr = eachContentStr.substr(eachContentStr.indexOf("}")+1,eachContentStr.length);
  131. var eachContent = [];
  132. if (eachItems){
  133. for (var n=0; n<Math.min(eachItems.length, eachItemsCount); n++){
  134. var item = eachItems[n];
  135. if (item){
  136. var tmpEachContentStr = eachContentStr;
  137. var textReg = /(eachText\{).+?\}/g;
  138. texts = tmpEachContentStr.match(textReg);
  139. if (texts){
  140. if (texts.length){
  141. for (var j=0; j<texts.length; j++){
  142. var text = texts[j].substr(0,texts[j].lastIndexOf("}"));
  143. text = text.substr(text.indexOf("{")+1,text.length);
  144. var value = this.getJsonContent(item ,text);
  145. tmpEachContentStr = tmpEachContentStr.replace(/(eachText\{).+?\}/,value);
  146. }
  147. }
  148. }
  149. eachContent.push(tmpEachContentStr);
  150. }
  151. }
  152. }
  153. this.html = this.html.replace(/(\{each\([\s\S]+\)\})[\s\S]+?(\{endEach\})/,eachContent.join(""));
  154. }
  155. }
  156. }
  157. }
  158. });