Source: formats/kml/controls/KmlTreeVisibility.js

  1. /*
  2. * Copyright 2003-2006, 2009, 2017, 2020 United States Government, as represented
  3. * by the Administrator of the National Aeronautics and Space Administration.
  4. * All rights reserved.
  5. *
  6. * The NASAWorldWind/WebWorldWind platform is licensed under the Apache License,
  7. * Version 2.0 (the "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License
  9. * at http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. *
  16. * NASAWorldWind/WebWorldWind also contains the following 3rd party Open Source
  17. * software:
  18. *
  19. * ES6-Promise – under MIT License
  20. * libtess.js – SGI Free Software License B
  21. * Proj4 – under MIT License
  22. * JSZip – under MIT License
  23. *
  24. * A complete listing of 3rd Party software notices and licenses included in
  25. * WebWorldWind can be found in the WebWorldWind 3rd-party notices and licenses
  26. * PDF found in code directory.
  27. */
  28. define([
  29. '../../../util/WWUtil',
  30. './KmlControls'
  31. ], function (WWUtil,
  32. KmlControls
  33. ) {
  34. "use strict";
  35. /**
  36. * This class represents the structure of Documents, Folders and Features in the document. It renders them into
  37. * some of the outside area with defined classes, so that user can specify the look and feel.
  38. * Important part of this effort is to allow user show/hide subset of the Features present in the document.
  39. * Implementing this functionality also simplifies the manual testing.
  40. * @param visualElementId {String} Id of the element into which this will be rendered.
  41. * @param wwd {WorldWindow} WorldWindow instance necessary to control the redraw in the framework.
  42. * @constructor
  43. * @augments KmlControls
  44. * @alias KmlTreeVisibility
  45. * @classdesc Class for controlling the visibility of features.
  46. */
  47. var KmlTreeVisibility = function (visualElementId, wwd) {
  48. KmlControls.apply(this);
  49. this._visualElementId = visualElementId;
  50. this._wwd = wwd;
  51. };
  52. KmlTreeVisibility.prototype = Object.create(KmlControls.prototype);
  53. /**
  54. * @inheritDoc
  55. */
  56. KmlTreeVisibility.prototype.hook = function (node, options) {
  57. if(options.isFeature) {
  58. this.createControls(node);
  59. }
  60. };
  61. // For internal use only.
  62. KmlTreeVisibility.prototype.createControls = function (node) {
  63. var name = node.kmlName || node.id || WWUtil.guid();
  64. var enabled = node.enabled && node.kmlVisibility === true;
  65. var controlsForSingleElement = document.createElement("div");
  66. var toggleVisibility = document.createElement("input");
  67. toggleVisibility.setAttribute("type", "checkbox");
  68. if (enabled) {
  69. toggleVisibility.setAttribute("checked", "checked");
  70. }
  71. toggleVisibility.addEventListener("click", toggleVisibilityOfElement, true);
  72. controlsForSingleElement.appendChild(toggleVisibility);
  73. var lookAtName;
  74. if (node.kmlAbstractView) {
  75. lookAtName = document.createElement("a");
  76. } else {
  77. lookAtName = document.createElement("span");
  78. }
  79. lookAtName.appendChild(document.createTextNode(name));
  80. lookAtName.addEventListener("click", lookAt, true);
  81. controlsForSingleElement.appendChild(lookAtName);
  82. document.getElementById(this._visualElementId).appendChild(controlsForSingleElement);
  83. var self = this;
  84. function toggleVisibilityOfElement() {
  85. enabled = !enabled;
  86. self.updateDescendants(node, enabled);
  87. }
  88. function lookAt() {
  89. if (node.kmlAbstractView) {
  90. node.kmlAbstractView.update({wwd: self._wwd});
  91. }
  92. }
  93. };
  94. // Internal use only. Updates all descendants of given Feature.
  95. KmlTreeVisibility.prototype.updateDescendants = function (node, enabled) {
  96. node.controlledVisibility = enabled;
  97. this._wwd.redraw();
  98. };
  99. return KmlTreeVisibility;
  100. });