Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LayerGroup.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-27 19:50:32
  4. */
  5. import { Util } from '../../core/utils'
  6. import State from '../state/State'
  7. import { LayerGroupEventType, LayerGroupEvent } from '../event'
  8. import Layer from './Layer'
  9. class LayerGroup {
  10. constructor(id) {
  11. this._id = id || Util.uuid()
  12. this._cache = {}
  13. this._show = true
  14. this._viewer = undefined
  15. this._layerGroupEvent = new LayerGroupEvent()
  16. this._layerGroupEvent.on(LayerGroupEventType.ADD, this._onAdd, this)
  17. this._layerGroupEvent.on(LayerGroupEventType.REMOVE, this._onRemove, this)
  18. this.type = Layer.getLayerType('layer_group')
  19. this._state = State.INITIALIZED
  20. }
  21. get id() {
  22. return this._id
  23. }
  24. set show(show) {
  25. this._show = show
  26. Object.keys(this._cache).forEach(key => {
  27. this._cache[key].show = this._show
  28. })
  29. }
  30. get show() {
  31. return this._show
  32. }
  33. get layerGroupEvent() {
  34. return this._layerGroupEvent
  35. }
  36. get state() {
  37. return this._state
  38. }
  39. /**
  40. *
  41. * @param viewer
  42. * @private
  43. */
  44. _onAdd(viewer) {
  45. this._viewer = viewer
  46. Object.keys(this._cache).forEach(key => {
  47. this._viewer.addLayer(this._cache[key])
  48. })
  49. this._state = State.ADDED
  50. }
  51. /**
  52. *
  53. * @private
  54. */
  55. _onRemove() {
  56. Object.keys(this._cache).forEach(key => {
  57. this._viewer && this._viewer.removeLayer(this._cache[key])
  58. })
  59. this._cache = {}
  60. this._state = State.REMOVED
  61. }
  62. /**
  63. * Adds a layer
  64. * @param layer
  65. * @returns {LayerGroup}
  66. */
  67. addLayer(layer) {
  68. if (!Object(this._cache).hasOwnProperty(layer.id)) {
  69. this._cache[layer.id] = layer
  70. this._viewer && this._viewer.addLayer(layer)
  71. }
  72. return this
  73. }
  74. /**
  75. * Removes a layer
  76. * @param layer
  77. * @returns {LayerGroup}
  78. */
  79. removeLayer(layer) {
  80. if (Object(this._cache).hasOwnProperty(layer.id)) {
  81. this._viewer && this._viewer.removeLayer(layer)
  82. delete this._cache[layer.id]
  83. }
  84. return this
  85. }
  86. /**
  87. * Returns a layer by id
  88. * @param id
  89. * @returns {*|undefined}
  90. */
  91. getLayer(id) {
  92. return this._cache[id] || undefined
  93. }
  94. /**
  95. * Returns all layers
  96. * @returns {[]}
  97. */
  98. getLayers() {
  99. let result = []
  100. Object.keys(this._cache).forEach(key => {
  101. result.push(this._cache[key])
  102. })
  103. return result
  104. }
  105. /**
  106. * Adds to the viewer
  107. * @param viewer
  108. * @returns {LayerGroup}
  109. */
  110. addTo(viewer) {
  111. if (viewer && viewer.addLayerGroup) {
  112. viewer.addLayerGroup(this)
  113. }
  114. return this
  115. }
  116. /**
  117. *
  118. * @returns {LayerGroup}
  119. */
  120. remove() {
  121. this._viewer && this._viewer.removeLayerGroup(this)
  122. return this
  123. }
  124. }
  125. Layer.registerType('layer_group')
  126. export default LayerGroup