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

index.js 5.2KB

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