Home Reference Source

cables_dev/cables_ui/src/ui/utils/handlebars.js

  1. import { Logger } from "cables-shared-client";
  2. import { gui } from "../gui.js";
  3. import { platform } from "../platform.js";
  4. /**
  5. * Handlebars template helper functions
  6. */
  7. const handleBarsPrecompiled = {};
  8. const log = new Logger("handlebarsjs");
  9. export function handleBarPrecompiled(name)
  10. {
  11. let template = handleBarsPrecompiled[name];
  12. if (template) return template;
  13. const source = document.getElementById(name);
  14. if (!source || !source.innerHTML)
  15. {
  16. log.warn("template not found", "template " + name + " not found...");
  17. return;
  18. }
  19. const p = handleBarsPrecompiled[name] = Handlebars.compile(source.innerHTML);
  20. return p;
  21. }
  22. /**
  23. * @param {string} name
  24. * @param {object} obj
  25. */
  26. export function getHandleBarHtml(name, obj)
  27. {
  28. let perf = gui.uiProfiler.start("getHandleBarHtml");
  29. const template = handleBarPrecompiled(name);
  30. if (!template) return "error: missing handlebars template: " + name;
  31. obj = obj || {};
  32. obj.frontendOptions = platform.frontendOptions;
  33. obj.cablesUrl = platform.getCablesUrl();
  34. obj.cablesDocsUrl = obj.cablesUrl;
  35. if (platform.getCablesDocsUrl)obj.cablesDocsUrl = platform.getCablesDocsUrl();
  36. const html = template(obj, { "allowProtoMethodsByDefault": true, "allowProtoPropertiesByDefault": true });
  37. if (perf)perf.finish();
  38. return html;
  39. }