Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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