Source: formats/kml/styles/KmlStyle.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/Color',
  30. '../../../util/Font',
  31. './KmlStyleSelector',
  32. './../KmlElements',
  33. './KmlPolyStyle',
  34. './KmlIconStyle',
  35. './KmlLabelStyle',
  36. './KmlLineStyle',
  37. './KmlListStyle',
  38. './KmlBalloonStyle',
  39. '../../../util/Offset',
  40. '../../../util/Promise',
  41. '../../../shapes/ShapeAttributes',
  42. '../../../shapes/TextAttributes'
  43. ], function (
  44. Color,
  45. Font,
  46. KmlStyleSelector,
  47. KmlElements,
  48. KmlPolyStyle,
  49. KmlIconStyle,
  50. KmlLabelStyle,
  51. KmlLineStyle,
  52. KmlListStyle,
  53. KmlBalloonStyle,
  54. Offset,
  55. Promise,
  56. ShapeAttributes,
  57. TextAttributes
  58. ) {
  59. "use strict";
  60. /**
  61. * Constructs an KmlStyle. Application usually don't call this constructor. It is called by {@link KmlFile} as
  62. * Objects from KmlFile are read. It is concrete implementation.
  63. * Style can contain any amount of different styles. At most one from each of these styles.
  64. * Possible children styles: IconStyle, LabelStyle, LineStyle, PolyStyle, BalloonStyle
  65. * @alias KmlStyle
  66. * @constructor
  67. * @classdesc Contains the data associated with Kml style
  68. * @param options {Object}
  69. * @param options.objectNode {Node} Node representing the Kml style.
  70. * @throws {ArgumentError} If either the node is null or undefined.
  71. * @see https://developers.google.com/kml/documentation/kmlreference#style
  72. * @augments KmlStyleSelector
  73. */
  74. var KmlStyle = function (options) {
  75. KmlStyleSelector.call(this, options);
  76. };
  77. KmlStyle.prototype = Object.create(KmlStyleSelector.prototype);
  78. Object.defineProperties(KmlStyle.prototype, {
  79. /**
  80. * Style used for icons in current node and all children nodes.
  81. * @memberof KmlStyle.prototype
  82. * @readonly
  83. * @type {KmlIconStyle|null}
  84. */
  85. kmlIconStyle: {
  86. get: function() {
  87. return this._factory.any(this, {
  88. name: KmlIconStyle.prototype.getTagNames()
  89. });
  90. }
  91. },
  92. /**
  93. * Style used for labels in current node and all children nodes.
  94. * @memberof KmlStyle.prototype
  95. * @readonly
  96. * @type {KmlLabelStyle|null}
  97. */
  98. kmlLabelStyle: {
  99. get: function() {
  100. return this._factory.any(this, {
  101. name: KmlLabelStyle.prototype.getTagNames()
  102. });
  103. }
  104. },
  105. /**
  106. * Style used for line in current node and all children nodes.
  107. * @memberof KmlStyle.prototype
  108. * @readonly
  109. * @type {KmlLineStyle|null}
  110. */
  111. kmlLineStyle: {
  112. get: function() {
  113. return this._factory.any(this, {
  114. name: KmlLineStyle.prototype.getTagNames()
  115. });
  116. }
  117. },
  118. /**
  119. * Style used for polygon in current node and all children nodes.
  120. * @memberof KmlStyle.prototype
  121. * @readonly
  122. * @type {KmlPolyStyle|null}
  123. */
  124. kmlPolyStyle: {
  125. get: function() {
  126. return this._factory.any(this, {
  127. name: KmlPolyStyle.prototype.getTagNames()
  128. });
  129. }
  130. },
  131. /**
  132. * Style used for balloons in current node and all children nodes.
  133. * @memberof KmlStyle.prototype
  134. * @readonly
  135. * @type {KmlBalloonStyle|null}
  136. */
  137. kmlBalloonStyle: {
  138. get: function() {
  139. return this._factory.any(this, {
  140. name: KmlBalloonStyle.prototype.getTagNames()
  141. });
  142. }
  143. },
  144. /**
  145. * Style used for lists in current node and all children nodes.
  146. * @memberof KmlStyle.prototype
  147. * @readonly
  148. * @type {KmlListStyle|null}
  149. */
  150. kmlListStyle: {
  151. get: function() {
  152. return this._factory.any(this, {
  153. name: KmlListStyle.prototype.getTagNames()
  154. });
  155. }
  156. }
  157. });
  158. KmlStyle.prototype.generate = function(options, fileCache) {
  159. options = options || {};
  160. var style = this || {};
  161. if(style.kmlIconStyle) {
  162. KmlIconStyle.update(style.kmlIconStyle, options, fileCache);
  163. }
  164. if(style.kmlListStyle) {
  165. KmlListStyle.update(style.kmlListStyle, options);
  166. }
  167. if(style.kmlBalloonStyle) {
  168. KmlBalloonStyle.update(style.kmlBalloonStyle, options);
  169. }
  170. if(style.kmlLabelStyle) {
  171. KmlLabelStyle.update(style.kmlLabelStyle, options);
  172. }
  173. if(style.kmlPolyStyle) {
  174. KmlPolyStyle.update(style.kmlPolyStyle, options);
  175. }
  176. if(style.kmlLineStyle) {
  177. KmlLineStyle.update(style.kmlLineStyle, options);
  178. }
  179. return options;
  180. };
  181. /**
  182. * @inheritDoc
  183. */
  184. KmlStyle.prototype.getStyle = function() {
  185. var self = this;
  186. return new Promise(function(resolve){
  187. window.setTimeout(function(){
  188. resolve(self);
  189. }, 0);
  190. });
  191. };
  192. /**
  193. * @inheritDoc
  194. */
  195. KmlStyle.prototype.getTagNames = function () {
  196. return ['Style'];
  197. };
  198. KmlElements.addKey(KmlStyle.prototype.getTagNames()[0], KmlStyle);
  199. /**
  200. * Prepare default values for the placemark Attributes.
  201. * @param attributes
  202. * @returns {Object}
  203. */
  204. KmlStyle.placemarkAttributes = function(attributes) {
  205. attributes = attributes || {};
  206. // These are all documented with their property accessors below.
  207. attributes._imageColor = attributes._imageColor || new Color(1, 1, 1, 1);
  208. attributes._imageOffset = attributes._imageOffset||
  209. new Offset(WorldWind.OFFSET_FRACTION, 0.5, WorldWind.OFFSET_FRACTION, 0.5);
  210. attributes._imageScale = attributes._imageScale || 1;
  211. attributes._imageSource = attributes._imageSource || null;
  212. attributes._depthTest = attributes._depthTest || true;
  213. attributes._labelAttributes = attributes._labelAttributes || new TextAttributes(KmlStyle.textAttributes());
  214. attributes._drawLeaderLine = attributes._drawLeaderLine || false;
  215. attributes._leaderLineAttributes = attributes._leaderLineAttributes || new ShapeAttributes(KmlStyle.shapeAttributes());
  216. return attributes;
  217. };
  218. /**
  219. * Prepare default values for text attributes
  220. * @param attributes
  221. * @returns {Object}
  222. */
  223. KmlStyle.textAttributes = function(attributes) {
  224. attributes = attributes || {};
  225. attributes._color = attributes._color || new Color(1, 1, 1, 1);
  226. attributes._font = attributes._font || new Font(14);
  227. attributes._offset = attributes._offset || new Offset(WorldWind.OFFSET_FRACTION, 0.5, WorldWind.OFFSET_FRACTION, 0.0);
  228. attributes._scale = attributes._scale || 1;
  229. attributes._depthTest = attributes._depthTest || false;
  230. attributes._outlineColor = attributes._outlineColor || Color.RED;
  231. return attributes;
  232. };
  233. /**
  234. * Prepare default values for shape attributes
  235. * @param attributes
  236. * @returns {*|{}}
  237. */
  238. KmlStyle.shapeAttributes = function(attributes) {
  239. attributes = attributes || {};
  240. // All these are documented with their property accessors below.
  241. attributes._drawInterior = attributes._drawInterior || true;
  242. attributes._drawOutline = attributes._drawOutline || true;
  243. attributes._enableLighting = attributes._enableLighting || false;
  244. attributes._interiorColor = attributes._interiorColor || Color.WHITE;
  245. attributes._outlineColor = attributes._outlineColor || Color.RED;
  246. attributes._outlineWidth = attributes._outlineWidth || 1.0;
  247. attributes._outlineStippleFactor = attributes._outlineStippleFactor || 0;
  248. attributes._outlineStipplePattern = attributes._outlineStipplePattern || 0xF0F0;
  249. attributes._imageSource = attributes._imageSource || null;
  250. attributes._depthTest = attributes._depthTest || true;
  251. attributes._drawVerticals = attributes._drawVerticals || false;
  252. attributes._applyLighting = attributes._applyLighting || false;
  253. return attributes;
  254. };
  255. /**
  256. * It returns default KmlStyle, which doesn't contain any custom information.
  257. * @returns {KmlStyle}
  258. */
  259. KmlStyle.default = function() {
  260. return new KmlStyle({
  261. objectNode: document.createElement('Style')
  262. });
  263. };
  264. return KmlStyle;
  265. });