Home Reference Source

cables_dev/cables_ui/src/ui/components/opparampanel/op_params_htmlgen.js

  1. import { Patch, Port, utils } from "cables";
  2. import { CablesConstants } from "cables-shared-client";
  3. import { gui } from "../../gui.js";
  4. import namespace from "../../namespaceutils.js";
  5. import opNames from "../../opnameutils.js";
  6. import { platform } from "../../platform.js";
  7. import { GuiText } from "../../text.js";
  8. import { handleBarPrecompiled } from "../../utils/handlebars.js";
  9. import { UserSettings, userSettings } from "../usersettings.js";
  10. class PortHtmlGenerator
  11. {
  12. constructor(panelId)
  13. {
  14. this._panelId = panelId;
  15. this._templateHead = handleBarPrecompiled("params_op_head");
  16. this._templatePortGeneral = handleBarPrecompiled("params_port_general");
  17. this._templatePortGeneralEnd = handleBarPrecompiled("params_port_general_end");
  18. this._templatePortInput = handleBarPrecompiled("params_port_input");
  19. this._templatePortOutput = handleBarPrecompiled("params_port_output");
  20. this._templatePortsHead = handleBarPrecompiled("params_ports_head");
  21. }
  22. getHtmlOpHeader(op)
  23. {
  24. let isBookmarked = false;
  25. let oldversion = false;
  26. let newestVersion = false;
  27. let hasExample = false;
  28. let doc = null;
  29. if (op) isBookmarked = gui.bookmarks.hasBookmarkWithId(op.id);
  30. const canEditOp = gui.serverOps.canEditOp(gui.user, op.objName);
  31. if (namespace.isDeprecatedOp(op.objName))
  32. {
  33. op.isDeprecated = true;
  34. const notDeprecatedName = op.objName.replace("Deprecated.", "");
  35. const alt = Patch.getOpClass(notDeprecatedName);
  36. if (alt) op.isDeprecatedAlternative = notDeprecatedName;
  37. }
  38. if (namespace.isDevOp(op.objName)) op.isExperimental = true;
  39. if (gui.opDocs)
  40. {
  41. op.summary = gui.opDocs.getSummary(op.objName);
  42. doc = gui.opDocs.getOpDocByName(op.objName);
  43. }
  44. if (doc)
  45. {
  46. hasExample = doc.hasExample;
  47. if (doc.oldVersion) oldversion = doc.oldVersion;
  48. newestVersion = doc.newestVersion;
  49. }
  50. let opChanged = false;
  51. if (gui.serverOps.opIdsChangedOnServer[op.opId])
  52. opChanged = true;
  53. const o = {
  54. "op": op,
  55. "panelid": this._panelId,
  56. "frontendOptions": platform.frontendOptions,
  57. "isBookmarked": isBookmarked,
  58. "colorClass": opNames.getNamespaceClassName(op.objName),
  59. "texts": GuiText,
  60. "user": gui.user,
  61. "optitle": op.getTitle(),
  62. "canEditOp": canEditOp,
  63. "opChanged": opChanged,
  64. "oldVersion": oldversion,
  65. "minified": userSettings.get(UserSettings.PREF_MINIFIED_OP_HEAD),
  66. "newestVersion": newestVersion,
  67. "cablesUrl": platform.getCablesUrl(),
  68. "hasExample": hasExample,
  69. };
  70. o.cablesDocsUrl = platform.getCablesDocsUrl();
  71. return this._templateHead(o, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  72. }
  73. /**
  74. * @param {string} dir
  75. * @param {string} title
  76. */
  77. getHtmlHeaderPorts(dir, title)
  78. {
  79. return this._templatePortsHead({
  80. "dirStr": dir,
  81. "title": title,
  82. "texts": GuiText,
  83. }, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  84. }
  85. /**
  86. * @param {Array<Port>} ports
  87. * @returns {string}
  88. */
  89. getHtmlInputPorts(ports)
  90. {
  91. let html = "";
  92. let lastGroup = null;
  93. for (let i = 0; i < ports.length; i++)
  94. {
  95. const opGroup = ports[i].uiAttribs.group;
  96. let startGroup = null;
  97. let groupSpacer = false;
  98. const portGroupNames = [];
  99. if (!ports[i].uiAttribs.hideParam)
  100. {
  101. if (lastGroup != opGroup && !opGroup) groupSpacer = true;
  102. if (lastGroup != opGroup)
  103. {
  104. groupSpacer = true;
  105. lastGroup = opGroup;
  106. startGroup = lastGroup;
  107. for (let j = 0; j < ports.length; j++)
  108. {
  109. if (ports[j].uiAttribs.group == startGroup)
  110. {
  111. portGroupNames.push(ports[j].name);
  112. }
  113. }
  114. }
  115. }
  116. ports[i].tempData.watchId = "in_" + i;
  117. const tmplData = {
  118. "port": ports[i],
  119. "panelid": this._panelId,
  120. "startGroup": startGroup,
  121. "groupSpacer": groupSpacer,
  122. "portGroupNames": portGroupNames,
  123. "dirStr": "in",
  124. "cablesUrl": platform.getCablesUrl(),
  125. "openLocalFiles": platform.frontendOptions.openLocalFiles,
  126. "portnum": i,
  127. "isInput": true,
  128. "op": ports[i].op,
  129. "texts": GuiText,
  130. "vars": ports[i].op.patch.getVars(ports[i].type)
  131. };
  132. if (ports[i].uiAttribs.display === "file")
  133. {
  134. const url = String(ports[i].value || "");
  135. const patchAsset = url.startsWith("/assets/" + gui.project()._id);
  136. let fileType = "";
  137. if (patchAsset)
  138. {
  139. const fileName = utils.filename(url);
  140. const suffix = "." + fileName.split(".").pop();
  141. for (let key in CablesConstants.FILETYPES)
  142. {
  143. const typeSuffixes = CablesConstants.FILETYPES[key];
  144. if (typeSuffixes.includes(suffix))
  145. {
  146. fileType = key;
  147. break;
  148. }
  149. }
  150. const editableFiles = CablesConstants.EDITABLE_FILETYPES || [];
  151. tmplData.fileEditable = !platform.isElectron() && editableFiles.includes(fileType);
  152. tmplData.fileType = fileType;
  153. tmplData.fileName = fileName;
  154. }
  155. }
  156. html += this._templatePortGeneral(tmplData, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  157. html += this._templatePortInput(tmplData, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  158. html += this._templatePortGeneralEnd(tmplData, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  159. }
  160. return html;
  161. }
  162. /**
  163. * @param {Array<Port>} ports
  164. * @returns {string}
  165. */
  166. getHtmlOutputPorts(ports)
  167. {
  168. let lastGroup = null;
  169. let html = "";
  170. for (const i in ports)
  171. {
  172. if (
  173. ports[i].getType() == Port.TYPE_NUMBER ||
  174. ports[i].getType() == Port.TYPE_ARRAY ||
  175. ports[i].getType() == Port.TYPE_STRING ||
  176. ports[i].getType() == Port.TYPE_OBJECT) ports[i].tempData.watchId = "out_" + i;
  177. let startGroup = null;
  178. let groupSpacer = false;
  179. const portGroupNames = [];
  180. const opGroup = ports[i].uiAttribs.group;
  181. if (lastGroup != opGroup && !opGroup) groupSpacer = true;
  182. if (lastGroup != opGroup)
  183. {
  184. groupSpacer = true;
  185. lastGroup = opGroup;
  186. startGroup = lastGroup;
  187. for (let j = 0; j < ports.length; j++)
  188. {
  189. if (ports[j].uiAttribs.group == startGroup)
  190. portGroupNames.push(ports[j].name);
  191. }
  192. }
  193. const tmplData = {
  194. "port": ports[i],
  195. "dirStr": "out",
  196. "panelid": this._panelId,
  197. "groupSpacer": groupSpacer,
  198. "startGroup": startGroup,
  199. "portGroupNames": portGroupNames,
  200. "portnum": i,
  201. "isInput": false,
  202. "op": ports[i].op
  203. };
  204. html += this._templatePortGeneral(tmplData, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  205. html += this._templatePortOutput(tmplData, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  206. html += this._templatePortGeneralEnd(tmplData, { "allowProtoPropertiesByDefault": true, "allowProtoMethodsByDefault": true });
  207. }
  208. return html;
  209. }
  210. }
  211. export { PortHtmlGenerator };