Source: map-common/src/main/resources/META-INF/resources/js/GeoJSONBase.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. import State, {Config} from 'metal-state';
  15. /**
  16. * GeoJSONBase
  17. * Allows adding controls (called features) to the map, that produce
  18. * diverse actions. For example, a button for centering the map
  19. * view will act as a feature.
  20. * @abstract
  21. * @review
  22. */
  23. class GeoJSONBase extends State {
  24. /**
  25. * Receives an object with native features data and tries
  26. * to parse it with the implemented method _getNativeFeatures.
  27. * If the generated Array of native features is not empty, it fires
  28. * a 'featuresAdded' event.
  29. * @param {Object} nativeFeaturesData Data to be processed.
  30. * @review
  31. */
  32. addData(nativeFeaturesData) {
  33. const nativeFeatures = this._getNativeFeatures(nativeFeaturesData);
  34. if (nativeFeatures.length > 0) {
  35. this.emit('featuresAdded', {
  36. features: nativeFeatures.map(this._wrapNativeFeature),
  37. });
  38. }
  39. }
  40. /**
  41. * Callback executed when a native feature has been clicked.
  42. * It receives the feature as parameter, and emits a 'featureClick'
  43. * event with the wrapped feature as event data.
  44. * @param {Object} nativeFeature Feature to be wrapped and sent
  45. * @protected
  46. * @review
  47. */
  48. _handleFeatureClicked(nativeFeature) {
  49. this.emit('featureClick', {
  50. feature: this._wrapNativeFeature(nativeFeature),
  51. });
  52. }
  53. /**
  54. * Parses an object and return an array of the
  55. * parsed features. If no feature has been parsed it may return an
  56. * empty array.
  57. * @abstract
  58. * @param {Object} nativeFeaturesData
  59. * @protected
  60. * @return {Object[]} List of native features to be added
  61. * @review
  62. */
  63. _getNativeFeatures(
  64. /* eslint-disable no-unused-vars */
  65. nativeFeaturesData
  66. /* eslint-enable no-unused-vars */
  67. ) {
  68. throw new Error('Must be implemented');
  69. }
  70. /**
  71. * Wraps a native feature.
  72. * @abstract
  73. * @param {Object} nativeFeature
  74. * @protected
  75. * @return {Object} Wrapped native feature
  76. * @review
  77. */
  78. _wrapNativeFeature(
  79. /* eslint-disable no-unused-vars */
  80. nativeFeature
  81. /* eslint-enable no-unused-vars */
  82. ) {
  83. throw new Error('Must be implemented');
  84. }
  85. }
  86. /**
  87. * State definition.
  88. * @review
  89. * @static
  90. * @type {!Object}
  91. */
  92. GeoJSONBase.STATE = {
  93. /**
  94. * Map to be used
  95. * @review
  96. * @type {Object}
  97. */
  98. map: Config.object(),
  99. };
  100. window.Liferay = window.Liferay || {};
  101. window.Liferay.MapGeojsonBase = GeoJSONBase;
  102. export default GeoJSONBase;
  103. export {GeoJSONBase};