InspectorShared.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { Check, defaultValue, defined } from "@cesium/engine";
  2. /**
  3. * A static class with helper functions used by CesiumInspector, Cesium3DTilesInspector, and VoxelInspector
  4. * @private
  5. */
  6. const InspectorShared = {};
  7. /**
  8. * Creates a checkbox component
  9. * @param {string} labelText The text to display in the checkbox label
  10. * @param {string} checkedBinding The name of the variable used for checked binding
  11. * @param {string} [enableBinding] The name of the variable used for enable binding
  12. * @return {Element}
  13. */
  14. InspectorShared.createCheckbox = function (
  15. labelText,
  16. checkedBinding,
  17. enableBinding
  18. ) {
  19. //>>includeStart('debug', pragmas.debug);
  20. Check.typeOf.string("labelText", labelText);
  21. Check.typeOf.string("checkedBinding", checkedBinding);
  22. //>>includeEnd('debug');
  23. const checkboxContainer = document.createElement("div");
  24. const checkboxLabel = document.createElement("label");
  25. const checkboxInput = document.createElement("input");
  26. checkboxInput.type = "checkbox";
  27. let binding = `checked: ${checkedBinding}`;
  28. if (defined(enableBinding)) {
  29. binding += `, enable: ${enableBinding}`;
  30. }
  31. checkboxInput.setAttribute("data-bind", binding);
  32. checkboxLabel.appendChild(checkboxInput);
  33. checkboxLabel.appendChild(document.createTextNode(labelText));
  34. checkboxContainer.appendChild(checkboxLabel);
  35. return checkboxContainer;
  36. };
  37. /**
  38. * Creates a section element
  39. * @param {Element} panel The parent element
  40. * @param {string} headerText The text to display at the top of the section
  41. * @param {string} sectionVisibleBinding The name of the variable used for visible binding
  42. * @param {string} toggleSectionVisibilityBinding The name of the function used to toggle visibility
  43. * @return {Element}
  44. */
  45. InspectorShared.createSection = function (
  46. panel,
  47. headerText,
  48. sectionVisibleBinding,
  49. toggleSectionVisibilityBinding
  50. ) {
  51. //>>includeStart('debug', pragmas.debug);
  52. Check.defined("panel", panel);
  53. Check.typeOf.string("headerText", headerText);
  54. Check.typeOf.string("sectionVisibleBinding", sectionVisibleBinding);
  55. Check.typeOf.string(
  56. "toggleSectionVisibilityBinding",
  57. toggleSectionVisibilityBinding
  58. );
  59. //>>includeEnd('debug');
  60. const section = document.createElement("div");
  61. section.className = "cesium-cesiumInspector-section";
  62. section.setAttribute(
  63. "data-bind",
  64. `css: { "cesium-cesiumInspector-section-collapsed": !${sectionVisibleBinding} }`
  65. );
  66. panel.appendChild(section);
  67. const sectionHeader = document.createElement("h3");
  68. sectionHeader.className = "cesium-cesiumInspector-sectionHeader";
  69. sectionHeader.appendChild(document.createTextNode(headerText));
  70. sectionHeader.setAttribute(
  71. "data-bind",
  72. `click: ${toggleSectionVisibilityBinding}`
  73. );
  74. section.appendChild(sectionHeader);
  75. const sectionContent = document.createElement("div");
  76. sectionContent.className = "cesium-cesiumInspector-sectionContent";
  77. section.appendChild(sectionContent);
  78. return sectionContent;
  79. };
  80. /**
  81. * Creates a range input
  82. * @param {string} rangeText The text to display
  83. * @param {string} sliderValueBinding The name of the variable used for slider value binding
  84. * @param {number} min The minimum value
  85. * @param {number} max The maximum value
  86. * @param {number} [step] The step size. Defaults to "any".
  87. * @param {string} [inputValueBinding] The name of the variable used for input value binding
  88. * @return {Element}
  89. */
  90. InspectorShared.createRangeInput = function (
  91. rangeText,
  92. sliderValueBinding,
  93. min,
  94. max,
  95. step,
  96. inputValueBinding
  97. ) {
  98. //>>includeStart('debug', pragmas.debug);
  99. Check.typeOf.string("rangeText", rangeText);
  100. Check.typeOf.string("sliderValueBinding", sliderValueBinding);
  101. Check.typeOf.number("min", min);
  102. Check.typeOf.number("max", max);
  103. //>>includeEnd('debug');
  104. inputValueBinding = defaultValue(inputValueBinding, sliderValueBinding);
  105. const input = document.createElement("input");
  106. input.setAttribute("data-bind", `value: ${inputValueBinding}`);
  107. input.type = "number";
  108. const slider = document.createElement("input");
  109. slider.type = "range";
  110. slider.min = min;
  111. slider.max = max;
  112. slider.step = defaultValue(step, "any");
  113. slider.setAttribute(
  114. "data-bind",
  115. `valueUpdate: "input", value: ${sliderValueBinding}`
  116. );
  117. const wrapper = document.createElement("div");
  118. wrapper.appendChild(slider);
  119. const container = document.createElement("div");
  120. container.className = "cesium-cesiumInspector-slider";
  121. container.appendChild(document.createTextNode(rangeText));
  122. container.appendChild(input);
  123. container.appendChild(wrapper);
  124. return container;
  125. };
  126. /**
  127. * Creates a button component
  128. * @param {string} buttonText The button text
  129. * @param {string} clickedBinding The name of the variable used for clicked binding
  130. * @param {string} [activeBinding] The name of the variable used for active binding
  131. * @return {Element}
  132. */
  133. InspectorShared.createButton = function (
  134. buttonText,
  135. clickedBinding,
  136. activeBinding
  137. ) {
  138. //>>includeStart('debug', pragmas.debug);
  139. Check.typeOf.string("buttonText", buttonText);
  140. Check.typeOf.string("clickedBinding", clickedBinding);
  141. //>>includeEnd('debug');
  142. const button = document.createElement("button");
  143. button.type = "button";
  144. button.textContent = buttonText;
  145. button.className = "cesium-cesiumInspector-pickButton";
  146. let binding = `click: ${clickedBinding}`;
  147. if (defined(activeBinding)) {
  148. binding += `, css: {"cesium-cesiumInspector-pickButtonHighlight" : ${activeBinding}}`;
  149. }
  150. button.setAttribute("data-bind", binding);
  151. return button;
  152. };
  153. export default InspectorShared;