智慧水务管理系统 - 精河县供水工程综合管理平台

BaseLayerPicker.js 12KB

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