You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.8KB

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