BaseLayerPicker.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import {
  2. defined,
  3. destroyObject,
  4. DeveloperError,
  5. FeatureDetection,
  6. getElement,
  7. } from "@cesium/engine";
  8. import knockout from "../ThirdParty/knockout.js";
  9. import BaseLayerPickerViewModel from "./BaseLayerPickerViewModel.js";
  10. /**
  11. * <span style="display: block; text-align: center;">
  12. * <img src="Images/BaseLayerPicker.png" width="264" height="287" alt="" />
  13. * <br />BaseLayerPicker with its drop-panel open.
  14. * </span>
  15. * <br /><br />
  16. * The BaseLayerPicker is a single button widget that displays a panel of available imagery and
  17. * terrain providers. When imagery is selected, the corresponding imagery layer is created and inserted
  18. * as the base layer of the imagery collection; removing the existing base. When terrain is selected,
  19. * it replaces the current terrain provider. Each item in the available providers list contains a name,
  20. * a representative icon, and a tooltip to display more information when hovered. The list is initially
  21. * empty, and must be configured before use, as illustrated in the below example.
  22. *
  23. * @alias BaseLayerPicker
  24. * @constructor
  25. *
  26. * @param {Element|string} container The parent HTML container node or ID for this widget.
  27. * @param {object} options Object with the following properties:
  28. * @param {Globe} options.globe The Globe to use.
  29. * @param {ProviderViewModel[]} [options.imageryProviderViewModels=[]] The array of ProviderViewModel instances to use for imagery.
  30. * @param {ProviderViewModel} [options.selectedImageryProviderViewModel] The view model for the current base imagery layer, if not supplied the first available imagery layer is used.
  31. * @param {ProviderViewModel[]} [options.terrainProviderViewModels=[]] The array of ProviderViewModel instances to use for terrain.
  32. * @param {ProviderViewModel} [options.selectedTerrainProviderViewModel] The view model for the current base terrain layer, if not supplied the first available terrain layer is used.
  33. *
  34. * @exception {DeveloperError} Element with id "container" does not exist in the document.
  35. *
  36. *
  37. * @example
  38. * // In HTML head, include a link to the BaseLayerPicker.css stylesheet,
  39. * // and in the body, include: <div id="baseLayerPickerContainer"
  40. * // style="position:absolute;top:24px;right:24px;width:38px;height:38px;"></div>
  41. *
  42. * //Create the list of available providers we would like the user to select from.
  43. * //This example uses 3, OpenStreetMap, The Black Marble, and a single, non-streaming world image.
  44. * const imageryViewModels = [];
  45. * imageryViewModels.push(new Cesium.ProviderViewModel({
  46. * name: "Open\u00adStreet\u00adMap",
  47. * iconUrl: Cesium.buildModuleUrl("Widgets/Images/ImageryProviders/openStreetMap.png"),
  48. * tooltip: "OpenStreetMap (OSM) is a collaborative project to create a free editable \
  49. * map of the world.\nhttp://www.openstreetmap.org",
  50. * creationFunction: function() {
  51. * return new Cesium.OpenStreetMapImageryProvider({
  52. * url: "https://a.tile.openstreetmap.org/"
  53. * });
  54. * }
  55. * }));
  56. *
  57. * imageryViewModels.push(new Cesium.ProviderViewModel({
  58. * name: "Earth at Night",
  59. * iconUrl: Cesium.buildModuleUrl("Widgets/Images/ImageryProviders/blackMarble.png"),
  60. * tooltip: "The lights of cities and villages trace the outlines of civilization \
  61. * in this global view of the Earth at night as seen by NASA/NOAA's Suomi NPP satellite.",
  62. * creationFunction: function() {
  63. * return Cesium.IonImageryProvider.fromAssetId(3812);
  64. * }
  65. * }));
  66. *
  67. * imageryViewModels.push(new Cesium.ProviderViewModel({
  68. * name: "Natural Earth\u00a0II",
  69. * iconUrl: Cesium.buildModuleUrl("Widgets/Images/ImageryProviders/naturalEarthII.png"),
  70. * tooltip: "Natural Earth II, darkened for contrast.\nhttp://www.naturalearthdata.com/",
  71. * creationFunction: function() {
  72. * return Cesium.TileMapServiceImageryProvider.fromUrl(
  73. * Cesium.buildModuleUrl("Assets/Textures/NaturalEarthII")
  74. * );
  75. * }
  76. * }));
  77. *
  78. * //Create a CesiumWidget without imagery, if you haven't already done so.
  79. * const cesiumWidget = new Cesium.CesiumWidget("cesiumContainer", { baseLayer: false });
  80. *
  81. * //Finally, create the baseLayerPicker widget using our view models.
  82. * const layers = cesiumWidget.imageryLayers;
  83. * const baseLayerPicker = new Cesium.BaseLayerPicker("baseLayerPickerContainer", {
  84. * globe: cesiumWidget.scene.globe,
  85. * imageryProviderViewModels: imageryViewModels
  86. * });
  87. *
  88. * @see TerrainProvider
  89. * @see ImageryProvider
  90. * @see ImageryLayerCollection
  91. */
  92. function BaseLayerPicker(container, options) {
  93. //>>includeStart('debug', pragmas.debug);
  94. if (!defined(container)) {
  95. throw new DeveloperError("container is required.");
  96. }
  97. //>>includeEnd('debug');
  98. container = getElement(container);
  99. const viewModel = new BaseLayerPickerViewModel(options);
  100. const element = document.createElement("button");
  101. element.type = "button";
  102. element.className = "cesium-button cesium-toolbar-button";
  103. element.setAttribute(
  104. "data-bind",
  105. "\
  106. attr: { title: buttonTooltip },\
  107. click: toggleDropDown"
  108. );
  109. container.appendChild(element);
  110. const imgElement = document.createElement("img");
  111. imgElement.setAttribute("draggable", "false");
  112. imgElement.className = "cesium-baseLayerPicker-selected";
  113. imgElement.setAttribute(
  114. "data-bind",
  115. "\
  116. attr: { src: buttonImageUrl }, visible: !!buttonImageUrl"
  117. );
  118. element.appendChild(imgElement);
  119. const dropPanel = document.createElement("div");
  120. dropPanel.className = "cesium-baseLayerPicker-dropDown";
  121. dropPanel.setAttribute(
  122. "data-bind",
  123. '\
  124. css: { "cesium-baseLayerPicker-dropDown-visible" : dropDownVisible }'
  125. );
  126. container.appendChild(dropPanel);
  127. const imageryTitle = document.createElement("div");
  128. imageryTitle.className = "cesium-baseLayerPicker-sectionTitle";
  129. imageryTitle.setAttribute(
  130. "data-bind",
  131. "visible: imageryProviderViewModels.length > 0"
  132. );
  133. imageryTitle.innerHTML = "Imagery";
  134. dropPanel.appendChild(imageryTitle);
  135. const imagerySection = document.createElement("div");
  136. imagerySection.className = "cesium-baseLayerPicker-section";
  137. imagerySection.setAttribute("data-bind", "foreach: _imageryProviders");
  138. dropPanel.appendChild(imagerySection);
  139. const imageryCategories = document.createElement("div");
  140. imageryCategories.className = "cesium-baseLayerPicker-category";
  141. imagerySection.appendChild(imageryCategories);
  142. const categoryTitle = document.createElement("div");
  143. categoryTitle.className = "cesium-baseLayerPicker-categoryTitle";
  144. categoryTitle.setAttribute("data-bind", "text: name");
  145. imageryCategories.appendChild(categoryTitle);
  146. const imageryChoices = document.createElement("div");
  147. imageryChoices.className = "cesium-baseLayerPicker-choices";
  148. imageryChoices.setAttribute("data-bind", "foreach: providers");
  149. imageryCategories.appendChild(imageryChoices);
  150. const imageryProvider = document.createElement("div");
  151. imageryProvider.className = "cesium-baseLayerPicker-item";
  152. imageryProvider.setAttribute(
  153. "data-bind",
  154. '\
  155. css: { "cesium-baseLayerPicker-selectedItem" : $data === $parents[1].selectedImagery },\
  156. attr: { title: tooltip },\
  157. visible: creationCommand.canExecute,\
  158. click: function($data) { $parents[1].selectedImagery = $data; }'
  159. );
  160. imageryChoices.appendChild(imageryProvider);
  161. const providerIcon = document.createElement("img");
  162. providerIcon.className = "cesium-baseLayerPicker-itemIcon";
  163. providerIcon.setAttribute("data-bind", "attr: { src: iconUrl }");
  164. providerIcon.setAttribute("draggable", "false");
  165. imageryProvider.appendChild(providerIcon);
  166. const providerLabel = document.createElement("div");
  167. providerLabel.className = "cesium-baseLayerPicker-itemLabel";
  168. providerLabel.setAttribute("data-bind", "text: name");
  169. imageryProvider.appendChild(providerLabel);
  170. const terrainTitle = document.createElement("div");
  171. terrainTitle.className = "cesium-baseLayerPicker-sectionTitle";
  172. terrainTitle.setAttribute(
  173. "data-bind",
  174. "visible: terrainProviderViewModels.length > 0"
  175. );
  176. terrainTitle.innerHTML = "Terrain";
  177. dropPanel.appendChild(terrainTitle);
  178. const terrainSection = document.createElement("div");
  179. terrainSection.className = "cesium-baseLayerPicker-section";
  180. terrainSection.setAttribute("data-bind", "foreach: _terrainProviders");
  181. dropPanel.appendChild(terrainSection);
  182. const terrainCategories = document.createElement("div");
  183. terrainCategories.className = "cesium-baseLayerPicker-category";
  184. terrainSection.appendChild(terrainCategories);
  185. const terrainCategoryTitle = document.createElement("div");
  186. terrainCategoryTitle.className = "cesium-baseLayerPicker-categoryTitle";
  187. terrainCategoryTitle.setAttribute("data-bind", "text: name");
  188. terrainCategories.appendChild(terrainCategoryTitle);
  189. const terrainChoices = document.createElement("div");
  190. terrainChoices.className = "cesium-baseLayerPicker-choices";
  191. terrainChoices.setAttribute("data-bind", "foreach: providers");
  192. terrainCategories.appendChild(terrainChoices);
  193. const terrainProvider = document.createElement("div");
  194. terrainProvider.className = "cesium-baseLayerPicker-item";
  195. terrainProvider.setAttribute(
  196. "data-bind",
  197. '\
  198. css: { "cesium-baseLayerPicker-selectedItem" : $data === $parents[1].selectedTerrain },\
  199. attr: { title: tooltip },\
  200. visible: creationCommand.canExecute,\
  201. click: function($data) { $parents[1].selectedTerrain = $data; }'
  202. );
  203. terrainChoices.appendChild(terrainProvider);
  204. const terrainProviderIcon = document.createElement("img");
  205. terrainProviderIcon.className = "cesium-baseLayerPicker-itemIcon";
  206. terrainProviderIcon.setAttribute("data-bind", "attr: { src: iconUrl }");
  207. terrainProviderIcon.setAttribute("draggable", "false");
  208. terrainProvider.appendChild(terrainProviderIcon);
  209. const terrainProviderLabel = document.createElement("div");
  210. terrainProviderLabel.className = "cesium-baseLayerPicker-itemLabel";
  211. terrainProviderLabel.setAttribute("data-bind", "text: name");
  212. terrainProvider.appendChild(terrainProviderLabel);
  213. knockout.applyBindings(viewModel, element);
  214. knockout.applyBindings(viewModel, dropPanel);
  215. this._viewModel = viewModel;
  216. this._container = container;
  217. this._element = element;
  218. this._dropPanel = dropPanel;
  219. this._closeDropDown = function (e) {
  220. if (!(element.contains(e.target) || dropPanel.contains(e.target))) {
  221. viewModel.dropDownVisible = false;
  222. }
  223. };
  224. if (FeatureDetection.supportsPointerEvents()) {
  225. document.addEventListener("pointerdown", this._closeDropDown, true);
  226. } else {
  227. document.addEventListener("mousedown", this._closeDropDown, true);
  228. document.addEventListener("touchstart", this._closeDropDown, true);
  229. }
  230. }
  231. Object.defineProperties(BaseLayerPicker.prototype, {
  232. /**
  233. * Gets the parent container.
  234. * @memberof BaseLayerPicker.prototype
  235. *
  236. * @type {Element}
  237. */
  238. container: {
  239. get: function () {
  240. return this._container;
  241. },
  242. },
  243. /**
  244. * Gets the view model.
  245. * @memberof BaseLayerPicker.prototype
  246. *
  247. * @type {BaseLayerPickerViewModel}
  248. */
  249. viewModel: {
  250. get: function () {
  251. return this._viewModel;
  252. },
  253. },
  254. });
  255. /**
  256. * @returns {boolean} true if the object has been destroyed, false otherwise.
  257. */
  258. BaseLayerPicker.prototype.isDestroyed = function () {
  259. return false;
  260. };
  261. /**
  262. * Destroys the widget. Should be called if permanently
  263. * removing the widget from layout.
  264. */
  265. BaseLayerPicker.prototype.destroy = function () {
  266. if (FeatureDetection.supportsPointerEvents()) {
  267. document.removeEventListener("pointerdown", this._closeDropDown, true);
  268. } else {
  269. document.removeEventListener("mousedown", this._closeDropDown, true);
  270. document.removeEventListener("touchstart", this._closeDropDown, true);
  271. }
  272. knockout.cleanNode(this._element);
  273. knockout.cleanNode(this._dropPanel);
  274. this._container.removeChild(this._element);
  275. this._container.removeChild(this._dropPanel);
  276. return destroyObject(this);
  277. };
  278. export default BaseLayerPicker;