HomeButton.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {
  2. defined,
  3. destroyObject,
  4. DeveloperError,
  5. getElement,
  6. } from "@cesium/engine";
  7. import knockout from "../ThirdParty/knockout.js";
  8. import HomeButtonViewModel from "./HomeButtonViewModel.js";
  9. /**
  10. * A single button widget for returning to the default camera view of the current scene.
  11. *
  12. * @alias HomeButton
  13. * @constructor
  14. *
  15. * @param {Element|string} container The DOM element or ID that will contain the widget.
  16. * @param {Scene} scene The Scene instance to use.
  17. * @param {number} [duration] The time, in seconds, it takes to complete the camera flight home.
  18. */
  19. function HomeButton(container, scene, duration) {
  20. //>>includeStart('debug', pragmas.debug);
  21. if (!defined(container)) {
  22. throw new DeveloperError("container is required.");
  23. }
  24. //>>includeEnd('debug');
  25. container = getElement(container);
  26. const viewModel = new HomeButtonViewModel(scene, duration);
  27. viewModel._svgPath =
  28. "M14,4l-10,8.75h20l-4.25-3.7188v-4.6562h-2.812v2.1875l-2.938-2.5625zm-7.0938,9.906v10.094h14.094v-10.094h-14.094zm2.1876,2.313h3.3122v4.25h-3.3122v-4.25zm5.8442,1.281h3.406v6.438h-3.406v-6.438z";
  29. const element = document.createElement("button");
  30. element.type = "button";
  31. element.className = "cesium-button cesium-toolbar-button cesium-home-button";
  32. element.setAttribute(
  33. "data-bind",
  34. "\
  35. attr: { title: tooltip },\
  36. click: command,\
  37. cesiumSvgPath: { path: _svgPath, width: 28, height: 28 }"
  38. );
  39. container.appendChild(element);
  40. knockout.applyBindings(viewModel, element);
  41. this._container = container;
  42. this._viewModel = viewModel;
  43. this._element = element;
  44. }
  45. Object.defineProperties(HomeButton.prototype, {
  46. /**
  47. * Gets the parent container.
  48. * @memberof HomeButton.prototype
  49. *
  50. * @type {Element}
  51. */
  52. container: {
  53. get: function () {
  54. return this._container;
  55. },
  56. },
  57. /**
  58. * Gets the view model.
  59. * @memberof HomeButton.prototype
  60. *
  61. * @type {HomeButtonViewModel}
  62. */
  63. viewModel: {
  64. get: function () {
  65. return this._viewModel;
  66. },
  67. },
  68. });
  69. /**
  70. * @returns {boolean} true if the object has been destroyed, false otherwise.
  71. */
  72. HomeButton.prototype.isDestroyed = function () {
  73. return false;
  74. };
  75. /**
  76. * Destroys the widget. Should be called if permanently
  77. * removing the widget from layout.
  78. */
  79. HomeButton.prototype.destroy = function () {
  80. knockout.cleanNode(this._element);
  81. this._container.removeChild(this._element);
  82. return destroyObject(this);
  83. };
  84. export default HomeButton;