Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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