Guest User

Untitled

a guest
Mar 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <section id="data"></section>
  9. </body>
  10. <script>
  11. const Info = class {
  12. constructor(json) {
  13. const { title, header, items } = json;
  14. if (typeof title != 'string' || !title) throw 'invalid title';
  15. if (!Array.isArray(header) || !header.length) throw 'invalid header';
  16. if (!Array.isArray(items) || !items.length) throw 'invalid items';
  17. items.forEach((v, idx)=>{
  18. if (!Array.isArray(v)) throw "invalid items";
  19. if (v.length != header.length) items.splice(idx, 1);
  20. });
  21. this._private = { title, header, items };
  22. }
  23.  
  24. get title() { return this._private.title; }
  25. get header() { return this._private.header; }
  26. get items() { return this._private.items; }
  27. };
  28.  
  29. const Data = class {
  30. async getData() {
  31. const json = await this._getData();
  32. return new Info(json);
  33. }
  34. };
  35.  
  36. const JsonData = class extends Data {
  37. constructor(data) {
  38. super();
  39. this._data = data;
  40. }
  41.  
  42. async _getData() {
  43. if (typeof this._data == 'string') {
  44. const response = await fetch(this._data);
  45. return await response.json();
  46. } else return this._data;
  47. }
  48. };
  49.  
  50. const Renderer = class {
  51. async render(data) {
  52. if (!(data instanceof Data)) throw 'invalid data type';
  53.  
  54. const info = await data.getData();
  55. if (!(info instanceof Info)) throw 'invalid info type';
  56.  
  57. const { title, header, items } = info;
  58. this._title = title;
  59. this._header = header;
  60. this._items = items;
  61. this._render();
  62. }
  63.  
  64. _render() {
  65. throw 'must _render overrided';
  66. }
  67. };
  68.  
  69. const TableRenderer = class extends Renderer {
  70. constructor(parent) {
  71. if (typeof parent != 'string' || !parent) throw 'invalid param';
  72. super();
  73. this._parent = parent;
  74. }
  75.  
  76. _render(){
  77. const parent = document.querySelector(this._parent);
  78. if (!parent) throw "invaild parent";
  79. parent.innerHTML = "";
  80. const [table, caption] = "table,caption".split(",").map(v => document.createElement(v));
  81. caption.innerHTML = this._title;
  82. table.appendChild(caption);
  83. table.appendChild(
  84. this._header.reduce(
  85. (thead, data) => (thead.appendChild(document.createElement("th")).innerHTML = data, thead),
  86. document.createElement("thead"))
  87. );
  88. parent.appendChild(
  89. this._items.reduce(
  90. (table, row) => (table.appendChild(
  91. row.reduce((tr, data) => (
  92. tr.appendChild(document.createElement("td")).innerHTML = data, tr), document.createElement("tr"))
  93. ), table), table)
  94. );
  95. }
  96. };
  97.  
  98. const data = new JsonData('71_1.json');
  99. const renderer = new TableRenderer('#data');
  100. renderer.render(data);
  101. </script>
  102. </html>
Add Comment
Please, Sign In to add comment