2
0

setting-test.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. var setting = [
  6. {
  7. "id":"edit",
  8. "type":"parent",
  9. "children":[
  10. {
  11. "id":"font",
  12. "type":"parent",
  13. "children":[
  14. {
  15. "id":"size",
  16. "type":"number",
  17. "defualt":14,
  18. "max":100,
  19. "min":9
  20. },
  21. {
  22. "id":"family",
  23. "type":"input",
  24. "defualt":"mono"
  25. },
  26. {
  27. "id":"weight",
  28. "type":"select",
  29. "options":[300,400,500],
  30. "defualt":500
  31. }
  32. ]
  33. }
  34. ]
  35. },
  36. {
  37. "id":"channel",
  38. "type":"parent",
  39. "children":[
  40. {
  41. "id":"showname",
  42. "type":"checkbox",
  43. "defualt":true
  44. },
  45. {
  46. "id":"list",
  47. "type":"channel.list",
  48. "defualt":[1,2]
  49. }
  50. ]
  51. }
  52. ]
  53. var user = {
  54. "edit.font.size":15,
  55. "edit.font.weight":400,
  56. "channel.showname":false,
  57. }
  58. function render_setting(param){
  59. let _path=new Array();
  60. let html ="";
  61. for (const iterator of param.data) {
  62. html += render_setting_node(iterator,param.userdata,_path);
  63. }
  64. param.div.innerHTML = html;
  65. }
  66. function render_setting_node(node,userdata,path){
  67. let html = "";
  68. path.push(node);
  69. let fullpath = new Array();
  70. for (const it of path) {
  71. fullpath.push(it.id);
  72. }
  73. let srtFullPath = fullpath.join(".");
  74. html += "<div class='setting_cell'>";
  75. html += "<h3>";
  76. html += node.id;
  77. html += "</h3>";
  78. html += "<div class='subtitle'>"+srtFullPath+"</div>";
  79. html += "<div>";
  80. let nodeValue = null;
  81. if (userdata.hasOwnProperty.call(userdata, srtFullPath)) {
  82. nodeValue = userdata[srtFullPath];
  83. }else{
  84. nodeValue = node.defualt;
  85. }
  86. switch(node.type){
  87. case "input":
  88. html += "<input type='input' value='"+nodeValue+"' />";
  89. break;
  90. case "number":
  91. html += "<input type='input' value='"+nodeValue+"' />";
  92. html += "默认值:"+ node.defualt;
  93. break;
  94. case "checkbox":
  95. html += "<div>";
  96. let checked = "";
  97. if(nodeValue){
  98. checked = "checked"
  99. }
  100. html += "<input type='checkbox' "+checked+" />";
  101. break;
  102. case "select":
  103. html += "<select>";
  104. for (const op of node.options) {
  105. html += "<option value='"+op+"' ";
  106. if(op==nodeValue){
  107. html += " selected ";
  108. }
  109. html +=">";
  110. html += op;
  111. if(op==node.defualt){
  112. html += "默认";
  113. }
  114. html += "</option>";
  115. }
  116. html += "</select>";
  117. break;
  118. case "parent":
  119. for (const iterator of node.children) {
  120. html += render_setting_node(iterator,userdata,path);
  121. }
  122. break;
  123. }
  124. html += "</div>";
  125. html += "</div>";
  126. path.pop();
  127. return html;
  128. }
  129. </script>
  130. </head>
  131. <body>
  132. <div id="setting"></div>
  133. <script>
  134. render_setting(
  135. {
  136. "div":document.querySelector("#setting"),
  137. "data":setting,
  138. "userdata":user
  139. }
  140. )
  141. </script>
  142. </body>
  143. </html>