BaseLayerPicker.js 12 KB

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