Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. return
  42. }
  43. L.Control.SideBySide = L.Control.extend({
  44. options: {
  45. thumbSize: 42,
  46. padding: 0
  47. },
  48. initialize: function (leftLayers, rightLayers, options) {
  49. this.setLeftLayers(leftLayers)
  50. this.setRightLayers(rightLayers)
  51. L.setOptions(this, options)
  52. },
  53. getPosition: noop,
  54. setPosition: noop,
  55. includes: L.Mixin.Events,
  56. addTo: function (map) {
  57. this.remove()
  58. this._map = map
  59. var container = this._container = L.DomUtil.create('div', 'leaflet-sbs', map._controlContainer)
  60. this._divider = L.DomUtil.create('div', 'leaflet-sbs-divider', container)
  61. var range = this._range = L.DomUtil.create('input', 'leaflet-sbs-range', container)
  62. range.type = 'range'
  63. range.min = 0
  64. range.max = 1
  65. range.step = 'any'
  66. range.value = 0.5
  67. range.style.paddingLeft = range.style.paddingRight = this.options.padding + 'px'
  68. this._addEvents()
  69. this._updateLayers()
  70. return this
  71. },
  72. remove: function () {
  73. if (!this._map) {
  74. return this
  75. }
  76. this._removeEvents()
  77. L.DomUtil.remove(this._container)
  78. this._map = null
  79. return this
  80. },
  81. setLeftLayers: function (leftLayers) {
  82. this._leftLayers = asArray(leftLayers)
  83. this._updateLayers()
  84. return this
  85. },
  86. setRightLayers: function (rightLayers) {
  87. this._rightLayers = asArray(rightLayers)
  88. this._updateLayers()
  89. return this
  90. },
  91. _updateClip: function () {
  92. var map = this._map
  93. var rangeValue = this._range.value
  94. var nw = map.containerPointToLayerPoint([0, 0])
  95. var se = map.containerPointToLayerPoint(map.getSize())
  96. var offset = (0.5 - rangeValue) * (2 * this.options.padding + this.options.thumbSize)
  97. var clipX = nw.x + (se.x - nw.x) * rangeValue + offset
  98. var dividerX = map.getSize().x * rangeValue + offset
  99. this._divider.style.left = dividerX + 'px'
  100. this.fire('dividermove', {x: dividerX})
  101. var clipLeft = 'rect(' + [nw.y, clipX, se.y, nw.x].join('px,') + 'px)'
  102. var clipRight = 'rect(' + [nw.y, se.x, se.y, clipX].join('px,') + 'px)'
  103. if (this._leftLayer) {
  104. this._leftLayer.getContainer().style.clip = clipLeft
  105. }
  106. if (this._rightLayer) {
  107. this._rightLayer.getContainer().style.clip = clipRight
  108. }
  109. },
  110. _updateLayers: function () {
  111. if (!this._map) {
  112. return this
  113. }
  114. var prevLeft = this._leftLayer
  115. var prevRight = this._rightLayer
  116. this._leftLayer = this._rightLayer = null
  117. this._leftLayers.forEach(function (layer) {
  118. if (this._map.hasLayer(layer)) {
  119. this._leftLayer = layer
  120. }
  121. }, this)
  122. this._rightLayers.forEach(function (layer) {
  123. if (this._map.hasLayer(layer)) {
  124. this._rightLayer = layer
  125. }
  126. }, this)
  127. if (prevLeft !== this._leftLayer) {
  128. prevLeft && this.fire('leftlayerremove', {layer: prevLeft})
  129. this._leftLayer && this.fire('leftlayeradd', {layer: this._leftLayer})
  130. }
  131. if (prevRight !== this._rightLayer) {
  132. prevRight && this.fire('rightlayerremove', {layer: prevRight})
  133. this._rightLayer && this.fire('rightlayeradd', {layer: this._rightLayer})
  134. }
  135. this._updateClip()
  136. },
  137. _addEvents: function () {
  138. var range = this._range
  139. var map = this._map
  140. if (!map || !range) return
  141. map.on('move', this._updateClip, this)
  142. map.on('layeradd layerremove', this._updateLayers, this)
  143. on(range, getRangeEvent(range), this._updateClip, this)
  144. on(range, 'mousedown touchstart', cancelMapDrag, this)
  145. on(range, 'mouseup touchend', uncancelMapDrag, this)
  146. },
  147. _removeEvents: function () {
  148. var range = this._range
  149. var map = this._map
  150. if (range) {
  151. off(range, getRangeEvent(range), this._updateClip, this)
  152. off(range, 'mousedown touchstart', cancelMapDrag, this)
  153. off(range, 'mouseup touchend', uncancelMapDrag, this)
  154. }
  155. if (map) {
  156. map.off('layeradd layerremove', this._updateLayers, this)
  157. map.off('move', this._updateClip, this)
  158. }
  159. }
  160. })
  161. L.control.sideBySide = function (leftLayers, rightLayers, options) {
  162. return new L.Control.SideBySide(leftLayers, rightLayers, options)
  163. }
  164. module.exports = L.Control.SideBySide