You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

148 lines
3.4 KiB

  1. import { $ } from '../../../src/js/utils/dom';
  2. import { toTitleCase } from '../../../src/js/utils/helpers';
  3. export class docsBuilder {
  4. constructor(LIB_OBJ) {
  5. this.LIB_OBJ = LIB_OBJ;
  6. }
  7. makeSection(parent, sys) {
  8. return new docSection(this.LIB_OBJ, parent, sys);
  9. }
  10. }
  11. class docSection {
  12. constructor(LIB_OBJ, parent, sys) {
  13. this.LIB_OBJ = LIB_OBJ;
  14. this.parent = parent; // should be preferably a section
  15. this.sys = sys;
  16. this.blockMap = {};
  17. this.demos = [];
  18. this.make();
  19. }
  20. make() {
  21. // const section = document.querySelector(this.parent);
  22. let s = this.sys;
  23. if(s.title) {
  24. $.create('h6', { inside: this.parent, innerHTML: s.title });
  25. }
  26. s.contentBlocks.forEach((blockConf, index) => {
  27. this.blockMap[index] = this.getBlock(blockConf);
  28. });
  29. }
  30. getBlock(blockConf) {
  31. let fnName = 'get' + toTitleCase(blockConf.type);
  32. if(this[fnName]) {
  33. return this[fnName](blockConf);
  34. } else {
  35. throw new Error(`Unknown section block type '${blockConf.type}'.`);
  36. }
  37. }
  38. getText(blockConf) {
  39. return $.create('p', {
  40. inside: this.parent,
  41. innerHTML: blockConf.content
  42. });
  43. }
  44. getCode(blockConf) {
  45. let pre = $.create('pre', { inside: this.parent });
  46. let lang = blockConf.lang || 'javascript';
  47. let code = $.create('code', {
  48. inside: pre,
  49. className: `hljs ${lang}`,
  50. innerHTML: blockConf.content
  51. });
  52. }
  53. getCustom(blockConf) {
  54. this.parent.innerHTML += blockConf.html;
  55. }
  56. getDemo(blockConf) {
  57. let bc = blockConf;
  58. let args = bc.config;
  59. let figure, row;
  60. if(!bc.sideContent) {
  61. figure = $.create('figure', { inside: this.parent });
  62. } else {
  63. row = $.create('div', {
  64. inside: this.parent,
  65. className: "row",
  66. innerHTML: `<div class="col-sm-8"></div>
  67. <div class="col-sm-4"></div>`,
  68. });
  69. figure = $.create('figure', { inside: row.querySelector('.col-sm-8') });
  70. row.querySelector('.col-sm-4').innerHTML += bc.sideContent;
  71. }
  72. let libObj = new this.LIB_OBJ(figure, args);
  73. let demoIndex = this.demos.length;
  74. this.demos.push(libObj);
  75. if(bc.postSetup) {
  76. bc.postSetup(this.demos[demoIndex], figure, row);
  77. }
  78. this.getDemoOptions(demoIndex, bc.options, args, figure);
  79. this.getDemoActions(demoIndex, bc.actions, args);
  80. }
  81. getDemoOptions(demoIndex, options=[], args={}, figure) {
  82. options.forEach(o => {
  83. const btnGroup = $.create('div', {
  84. inside: this.parent,
  85. className: `btn-group ${o.name}`
  86. });
  87. const mapKeys = o.mapKeys;
  88. if(o.type === "map") {
  89. args[o.path[0]] = {};
  90. }
  91. Object.keys(o.states).forEach(key => {
  92. let state = o.states[key];
  93. let activeClass = key === o.activeState ? 'active' : '';
  94. let button = $.create('button', {
  95. inside: btnGroup,
  96. className: `btn btn-sm btn-secondary ${activeClass}`,
  97. innerHTML: key,
  98. onClick: (e) => {
  99. // map
  100. if(o.type === "map") {
  101. mapKeys.forEach((attr, i) => {
  102. args[o.path[0]][attr] = state[i];
  103. })
  104. } else {
  105. // boolean, string, number, object
  106. args[o.path[0]] = state;
  107. }
  108. this.demos[demoIndex] = new this.LIB_OBJ(figure, args);
  109. }
  110. });
  111. if(activeClass) { button.click(); }
  112. });
  113. });
  114. }
  115. getDemoActions(demoIndex, actions=[], args={}) {
  116. actions.forEach(o => {
  117. let args = o.args || [];
  118. $.create('button', {
  119. inside: this.parent,
  120. className: `btn btn-action btn-sm btn-secondary`,
  121. innerHTML: o.name,
  122. onClick: () => {this.demos[demoIndex][o.fn](...args);}
  123. });
  124. });
  125. }
  126. }