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.
 
 
 

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