選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

252 行
5.8 KiB

  1. function $$(expr, con) {
  2. return typeof expr === "string"? (con || document).querySelector(expr) : expr || null;
  3. }
  4. $$.create = (tag, o) => {
  5. var element = document.createElement(tag);
  6. let container = null;
  7. if (o.withLabel) {
  8. container = document.createElement('div');
  9. container.classList.add('input-wrapper');
  10. element.label = document.createElement('label');
  11. element.label.innerHTML = o.withLabel;
  12. container.appendChild(element.label);
  13. container.appendChild(element);
  14. }
  15. for (var i in o) {
  16. var val = o[i];
  17. if(i === "inside") {
  18. let child = container ? container : element;
  19. $$(val).appendChild(child);
  20. } else if (i === "around") {
  21. var ref = $$(val);
  22. ref.parentNode.insertBefore(element, ref);
  23. element.appendChild(ref);
  24. } else if (i === "onClick" ) {
  25. element.addEventListener('click', val);
  26. } else if (i === "onInput" ) {
  27. element.addEventListener('input', function(e) {
  28. val(element.value);
  29. });
  30. } else if (i === "onChange" ) {
  31. element.addEventListener('change', function(e) {
  32. val(element.value);
  33. });
  34. } else if (i === "styles") {
  35. if(typeof val === "object") {
  36. Object.keys(val).map(prop => {
  37. element.style[prop] = val[prop];
  38. });
  39. }
  40. } else if (i in element ) {
  41. element[i] = val;
  42. }
  43. else {
  44. element.setAttribute(i, val);
  45. }
  46. }
  47. return container ? container : element;
  48. };
  49. function toTitleCase(str) {
  50. return str.replace(/\w*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  51. }
  52. function scrub(text) {
  53. return text.replace(/ /g, "_").toLowerCase();
  54. };
  55. // export class demoBuilder {
  56. class demoBuilder {
  57. constructor(LIB_OBJ) {
  58. this.LIB_OBJ = LIB_OBJ;
  59. }
  60. makeSection(parent, sys) {
  61. return new docSection(this.LIB_OBJ, parent, sys);
  62. }
  63. }
  64. class docSection {
  65. constructor(LIB_OBJ, parent, sys) {
  66. this.LIB_OBJ = LIB_OBJ;
  67. this.parent = parent;
  68. this.sys = sys;
  69. this.blockMap = {};
  70. this.demos = [];
  71. this.make();
  72. }
  73. make() {
  74. // const section = document.querySelector(this.parent);
  75. let s = this.sys;
  76. if(s.title) {
  77. $$.create('h6', { inside: this.parent, innerHTML: s.title });
  78. }
  79. if(s.contentBlocks) {
  80. s.contentBlocks.forEach((blockConf, index) => {
  81. this.blockMap[index] = this.getBlock(blockConf);
  82. });
  83. } else {
  84. // TODO:
  85. this.blockMap['test'] = this.getDemo(s);
  86. }
  87. }
  88. getBlock(blockConf) {
  89. let fnName = 'get' + toTitleCase(blockConf.type);
  90. if(this[fnName]) {
  91. return this[fnName](blockConf);
  92. } else {
  93. throw new Error(`Unknown section block type '${blockConf.type}'.`);
  94. }
  95. }
  96. getText(blockConf) {
  97. return $$.create('p', {
  98. inside: this.parent,
  99. className: 'new-context',
  100. innerHTML: blockConf.content
  101. });
  102. }
  103. getCode(blockConf) {
  104. let pre = $$.create('pre', { inside: this.parent });
  105. let lang = blockConf.lang || 'javascript';
  106. let code = $$.create('code', {
  107. inside: pre,
  108. className: `hljs ${lang}`,
  109. innerHTML: blockConf.content
  110. });
  111. }
  112. getCustom(blockConf) {
  113. this.parent.innerHTML += blockConf.html;
  114. }
  115. getDemo(blockConf) {
  116. let bc = blockConf;
  117. let args = bc.config;
  118. let figure, row;
  119. if(!bc.sideContent) {
  120. figure = $$.create('figure', { inside: this.parent });
  121. } else {
  122. row = $$.create('div', {
  123. inside: this.parent,
  124. className: "row",
  125. innerHTML: `<div class="col-sm-8"></div>
  126. <div class="col-sm-4"></div>`,
  127. });
  128. figure = $$.create('figure', { inside: row.querySelector('.col-sm-8') });
  129. row.querySelector('.col-sm-4').innerHTML += bc.sideContent;
  130. }
  131. let libObj = new this.LIB_OBJ(figure, args);
  132. let demoIndex = this.demos.length;
  133. this.demos.push(libObj);
  134. if(bc.postSetup) {
  135. bc.postSetup(this.demos[demoIndex], figure, row);
  136. }
  137. this.getDemoOptions(demoIndex, bc.options, args, figure);
  138. this.getDemoActions(demoIndex, bc.actions, args);
  139. }
  140. getDemoOptions(demoIndex, options=[], args={}, figure) {
  141. options.forEach(o => {
  142. console.log("o", o);
  143. const btnGroup = $$.create('div', {
  144. inside: this.parent,
  145. className: `btn-group ${scrub(o.name)}`
  146. });
  147. const mapKeys = o.mapKeys;
  148. if(o.type === "number") {
  149. let numOpts = o.numberOptions;
  150. let activeState = o.activeState ? o.activeState : 0
  151. const inputGroup = $$.create('input', {
  152. inside: btnGroup,
  153. withLabel: o.name + ': ' + '<b>' + activeState + '</b>',
  154. className: `form-control`,
  155. type: "range",
  156. min: numOpts.min,
  157. max: numOpts.max,
  158. step: numOpts.step,
  159. value: activeState,
  160. // (max - min)/2
  161. onInput: (value) => {
  162. if(o.path[1]) {
  163. args[o.path[0]][o.path[1]] = value;
  164. } else {
  165. args[o.path[0]] = value;
  166. }
  167. let label = inputGroup.querySelector('label');
  168. if(label) {
  169. label.innerHTML = o.name + ': ' + '<b>' + value + '</b>';
  170. }
  171. this.demos[demoIndex] = new this.LIB_OBJ(figure, args);
  172. }
  173. });
  174. } else if(["Map", "String", "Boolean", "Array", "Object"].includes(o.type)) {
  175. args[o.path[0]] = {};
  176. Object.keys(o.states).forEach(key => {
  177. let state = o.states[key];
  178. let activeClass = key === o.activeState ? 'active' : '';
  179. let button = $$.create('button', {
  180. inside: btnGroup,
  181. className: `btn btn-sm btn-secondary ${activeClass}`,
  182. innerHTML: key,
  183. onClick: (e) => {
  184. // map
  185. if(o.type === "map") {
  186. mapKeys.forEach((attr, i) => {
  187. args[o.path[0]][attr] = state[i];
  188. })
  189. } else {
  190. // boolean, string, number, object
  191. args[o.path[0]] = state;
  192. }
  193. this.demos[demoIndex] = new this.LIB_OBJ(figure, args);
  194. }
  195. });
  196. if(activeClass) { button.click(); }
  197. });
  198. }
  199. });
  200. }
  201. getDemoActions(demoIndex, actions=[], args={}) {
  202. actions.forEach(o => {
  203. let args = o.args || [];
  204. $$.create('button', {
  205. inside: this.parent,
  206. className: `btn btn-action btn-sm btn-secondary`,
  207. innerHTML: o.name,
  208. onClick: () => {this.demos[demoIndex][o.fn](...args);}
  209. });
  210. });
  211. }
  212. }