選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. this._addEvents()
  49. this._updateLayers()
  50. this._updateClip()
  51. return this
  52. },
  53. remove: function () {
  54. if (!this._map) {
  55. return this
  56. }
  57. this._removeEvents()
  58. L.DomUtil.remove(this._container)
  59. this._map = null
  60. return this
  61. },
  62. _updateClip: function () {
  63. var map = this._map
  64. var rangeValue = this._range.value
  65. var nw = map.containerPointToLayerPoint([0, 0])
  66. var se = map.containerPointToLayerPoint(map.getSize())
  67. var offset = (0.5 - rangeValue) * 44
  68. var clipX = nw.x + (se.x - nw.x) * rangeValue + offset
  69. this._divider.style.left = map.getSize().x * rangeValue + offset + 'px'
  70. var clipLeft = 'rect(' + [nw.y, clipX, se.y, nw.x].join('px,') + 'px)'
  71. var clipRight = 'rect(' + [nw.y, se.x, se.y, clipX].join('px,') + 'px)'
  72. if (this._leftLayer) {
  73. this._leftLayer.getContainer().style.clip = clipLeft
  74. }
  75. if (this._rightLayer) {
  76. this._rightLayer.getContainer().style.clip = clipRight
  77. }
  78. },
  79. _updateLayers: function () {
  80. this._leftLayer = this._rightLayer = null
  81. this._leftLayers.forEach(function (layer) {
  82. if (this._map.hasLayer(layer)) this._leftLayer = layer
  83. }, this)
  84. this._rightLayers.forEach(function (layer) {
  85. if (this._map.hasLayer(layer)) this._rightLayer = layer
  86. }, this)
  87. this._updateClip()
  88. },
  89. _addEvents: function () {
  90. var range = this._range
  91. var map = this._map
  92. if (!map || !range) return
  93. map.on('move', this._updateClip, this)
  94. map.on('layeradd layerremove', this._updateLayers, this)
  95. on(range, getRangeEvent(range), this._updateClip, this)
  96. on(range, 'mousedown touchstart', cancelMapDrag, this)
  97. on(range, 'mouseup touchend', uncancelMapDrag, this)
  98. },
  99. _removeEvents: function () {
  100. var range = this._range
  101. var map = this._map
  102. if (range) {
  103. off(range, getRangeEvent(range), this._updateClip, this)
  104. off(range, 'mousedown touchstart', cancelMapDrag, this)
  105. off(range, 'mouseup touchend', uncancelMapDrag, this)
  106. }
  107. if (map) {
  108. map.off('layeradd layerremove', this._updateLayers, this)
  109. map.off('move', this._updateClip, this)
  110. }
  111. }
  112. })
  113. L.Control.sideBySide = function (leftLayers, rightLayers, options) {
  114. return new L.Control.SideBySide(leftLayers, rightLayers, options)
  115. }
  116. module.export = L.Control.sideBySide