Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var L = require('leaflet')
  2. require('./layout.css')
  3. require('./range.css')
  4. var mapWasDragEnabled
  5. // Leaflet v0.7 backwards compatibility
  6. function on (el, types, fn, context) {
  7. types.split(' ').forEach(function (type) {
  8. L.DomEvent.on(el, type, fn, context)
  9. })
  10. }
  11. // Leaflet v0.7 backwards compatibility
  12. function off (el, types, fn, context) {
  13. types.split(' ').forEach(function (type) {
  14. L.DomEvent.off(el, type, fn, context)
  15. })
  16. }
  17. function getRangeEvent (rangeInput) {
  18. return 'oninput' in rangeInput ? 'input' : 'change'
  19. }
  20. function cancelMapDrag () {
  21. mapWasDragEnabled = this._map.dragging.enabled()
  22. this._map.dragging.disable()
  23. }
  24. function uncancelMapDrag (e) {
  25. if (!mapWasDragEnabled) return
  26. this._refocusOnMap(e)
  27. this._map.dragging.enable()
  28. }
  29. function noop () {
  30. return
  31. }
  32. L.Control.SideBySide = L.Control.extend({
  33. initialize: function (leftLayers, rightLayers) {
  34. this._leftLayers = Array.isArray(leftLayers) ? leftLayers : [leftLayers]
  35. this._rightLayers = Array.isArray(rightLayers) ? rightLayers : [rightLayers]
  36. },
  37. getPosition: noop,
  38. setPosition: noop,
  39. includes: L.Mixin.Events,
  40. addTo: function (map) {
  41. this.remove()
  42. this._map = map
  43. var container = this._container = L.DomUtil.create('div', 'leaflet-sbs', map._controlContainer)
  44. this._divider = L.DomUtil.create('div', 'leaflet-sbs-divider', container)
  45. var range = this._range = L.DomUtil.create('input', 'leaflet-sbs-range', container)
  46. range.type = 'range'
  47. range.min = 0
  48. range.max = 1
  49. range.step = 'any'
  50. range.value = 0.5
  51. this._addEvents()
  52. this._updateLayers()
  53. this._updateClip()
  54. return this
  55. },
  56. remove: function () {
  57. if (!this._map) {
  58. return this
  59. }
  60. this._removeEvents()
  61. L.DomUtil.remove(this._container)
  62. this._map = null
  63. return this
  64. },
  65. _updateClip: function () {
  66. var map = this._map
  67. var rangeValue = this._range.value
  68. var nw = map.containerPointToLayerPoint([0, 0])
  69. var se = map.containerPointToLayerPoint(map.getSize())
  70. var offset = (0.5 - rangeValue) * 44
  71. var clipX = nw.x + (se.x - nw.x) * rangeValue + offset
  72. var dividerX = map.getSize().x * rangeValue + offset
  73. this._divider.style.left = dividerX + 'px'
  74. this.fire('dividermove', {x: dividerX})
  75. var clipLeft = 'rect(' + [nw.y, clipX, se.y, nw.x].join('px,') + 'px)'
  76. var clipRight = 'rect(' + [nw.y, se.x, se.y, clipX].join('px,') + 'px)'
  77. if (this._leftLayer) {
  78. this._leftLayer.getContainer().style.clip = clipLeft
  79. }
  80. if (this._rightLayer) {
  81. this._rightLayer.getContainer().style.clip = clipRight
  82. }
  83. },
  84. _updateLayers: function () {
  85. var prevLeft = this._leftLayer
  86. var prevRight = this._rightLayer
  87. this._leftLayer = this._rightLayer = null
  88. this._leftLayers.forEach(function (layer) {
  89. if (this._map.hasLayer(layer)) {
  90. this._leftLayer = layer
  91. }
  92. }, this)
  93. this._rightLayers.forEach(function (layer) {
  94. if (this._map.hasLayer(layer)) {
  95. this._rightLayer = layer
  96. }
  97. }, this)
  98. if (prevLeft !== this._leftLayer) {
  99. prevLeft && this.fire('leftlayerremove', {layer: prevLeft})
  100. this._leftLayer && this.fire('leftlayeradd', {layer: this._leftLayer})
  101. }
  102. if (prevRight !== this._rightLayer) {
  103. prevRight && this.fire('rightlayerremove', {layer: prevRight})
  104. this._rightLayer && this.fire('rightlayeradd', {layer: this._rightLayer})
  105. }
  106. this._updateClip()
  107. },
  108. _addEvents: function () {
  109. var range = this._range
  110. var map = this._map
  111. if (!map || !range) return
  112. map.on('move', this._updateClip, this)
  113. map.on('layeradd layerremove', this._updateLayers, this)
  114. on(range, getRangeEvent(range), this._updateClip, this)
  115. on(range, 'mousedown touchstart', cancelMapDrag, this)
  116. on(range, 'mouseup touchend', uncancelMapDrag, this)
  117. },
  118. _removeEvents: function () {
  119. var range = this._range
  120. var map = this._map
  121. if (range) {
  122. off(range, getRangeEvent(range), this._updateClip, this)
  123. off(range, 'mousedown touchstart', cancelMapDrag, this)
  124. off(range, 'mouseup touchend', uncancelMapDrag, this)
  125. }
  126. if (map) {
  127. map.off('layeradd layerremove', this._updateLayers, this)
  128. map.off('move', this._updateClip, this)
  129. }
  130. }
  131. })
  132. L.Control.sideBySide = function (leftLayers, rightLayers, options) {
  133. return new L.Control.SideBySide(leftLayers, rightLayers, options)
  134. }
  135. module.export = L.Control.sideBySide