Browse Source

2.0.0

gh-pages
Gregor MacLennan 9 years ago
parent
commit
b7279c0b4a
3 changed files with 50 additions and 15 deletions
  1. 48
    13
      leaflet-side-by-side.js
  2. 1
    1
      leaflet-side-by-side.min.js
  3. 1
    1
      package.json

+ 48
- 13
leaflet-side-by-side.js View File

5
 require('./range.css')
5
 require('./range.css')
6
 
6
 
7
 var mapWasDragEnabled
7
 var mapWasDragEnabled
8
+var mapWasTapEnabled
8
 
9
 
9
 // Leaflet v0.7 backwards compatibility
10
 // Leaflet v0.7 backwards compatibility
10
 function on (el, types, fn, context) {
11
 function on (el, types, fn, context) {
26
 
27
 
27
 function cancelMapDrag () {
28
 function cancelMapDrag () {
28
   mapWasDragEnabled = this._map.dragging.enabled()
29
   mapWasDragEnabled = this._map.dragging.enabled()
30
+  mapWasTapEnabled = this._map.tap && this._map.tap.enabled()
29
   this._map.dragging.disable()
31
   this._map.dragging.disable()
32
+  this._map.tap && this._map.tap.disable()
30
 }
33
 }
31
 
34
 
32
 function uncancelMapDrag (e) {
35
 function uncancelMapDrag (e) {
33
-  if (!mapWasDragEnabled) return
34
   this._refocusOnMap(e)
36
   this._refocusOnMap(e)
35
-  this._map.dragging.enable()
37
+  if (mapWasDragEnabled) {
38
+    this._map.dragging.enable()
39
+  }
40
+  if (mapWasTapEnabled) {
41
+    this._map.tap.enable()
42
+  }
43
+}
44
+
45
+// convert arg to an array - returns empty array if arg is undefined
46
+function asArray (arg) {
47
+  return (arg === 'undefined') ? [] : Array.isArray(arg) ? arg : [arg]
36
 }
48
 }
37
 
49
 
38
 function noop () {
50
 function noop () {
40
 }
52
 }
41
 
53
 
42
 L.Control.SideBySide = L.Control.extend({
54
 L.Control.SideBySide = L.Control.extend({
43
-  initialize: function (leftLayers, rightLayers) {
44
-    this._leftLayers = Array.isArray(leftLayers) ? leftLayers : [leftLayers]
45
-    this._rightLayers = Array.isArray(rightLayers) ? rightLayers : [rightLayers]
55
+  options: {
56
+    thumbSize: 42,
57
+    padding: 0
46
   },
58
   },
47
 
59
 
48
-  getPosition: noop,
60
+  initialize: function (leftLayers, rightLayers, options) {
61
+    this.setLeftLayers(leftLayers)
62
+    this.setRightLayers(rightLayers)
63
+    L.setOptions(this, options)
64
+  },
65
+
66
+  getPosition: function () {
67
+    var rangeValue = this._range.value
68
+    var offset = (0.5 - rangeValue) * (2 * this.options.padding + this.options.thumbSize)
69
+    return this._map.getSize().x * rangeValue + offset
70
+  },
49
 
71
 
50
   setPosition: noop,
72
   setPosition: noop,
51
 
73
 
64
     range.max = 1
86
     range.max = 1
65
     range.step = 'any'
87
     range.step = 'any'
66
     range.value = 0.5
88
     range.value = 0.5
89
+    range.style.paddingLeft = range.style.paddingRight = this.options.padding + 'px'
67
     this._addEvents()
90
     this._addEvents()
68
     this._updateLayers()
91
     this._updateLayers()
69
-    this._updateClip()
70
     return this
92
     return this
71
   },
93
   },
72
 
94
 
82
     return this
104
     return this
83
   },
105
   },
84
 
106
 
107
+  setLeftLayers: function (leftLayers) {
108
+    this._leftLayers = asArray(leftLayers)
109
+    this._updateLayers()
110
+    return this
111
+  },
112
+
113
+  setRightLayers: function (rightLayers) {
114
+    this._rightLayers = asArray(rightLayers)
115
+    this._updateLayers()
116
+    return this
117
+  },
118
+
85
   _updateClip: function () {
119
   _updateClip: function () {
86
     var map = this._map
120
     var map = this._map
87
-    var rangeValue = this._range.value
88
     var nw = map.containerPointToLayerPoint([0, 0])
121
     var nw = map.containerPointToLayerPoint([0, 0])
89
     var se = map.containerPointToLayerPoint(map.getSize())
122
     var se = map.containerPointToLayerPoint(map.getSize())
90
-    var offset = (0.5 - rangeValue) * 44
91
-    var clipX = nw.x + (se.x - nw.x) * rangeValue + offset
92
-    var dividerX = map.getSize().x * rangeValue + offset
123
+    var clipX = nw.x + this.getPosition()
124
+    var dividerX = this.getPosition()
93
 
125
 
94
     this._divider.style.left = dividerX + 'px'
126
     this._divider.style.left = dividerX + 'px'
95
     this.fire('dividermove', {x: dividerX})
127
     this.fire('dividermove', {x: dividerX})
104
   },
136
   },
105
 
137
 
106
   _updateLayers: function () {
138
   _updateLayers: function () {
139
+    if (!this._map) {
140
+      return this
141
+    }
107
     var prevLeft = this._leftLayer
142
     var prevLeft = this._leftLayer
108
     var prevRight = this._rightLayer
143
     var prevRight = this._rightLayer
109
     this._leftLayer = this._rightLayer = null
144
     this._leftLayer = this._rightLayer = null
154
   }
189
   }
155
 })
190
 })
156
 
191
 
157
-L.Control.sideBySide = function (leftLayers, rightLayers, options) {
192
+L.control.sideBySide = function (leftLayers, rightLayers, options) {
158
   return new L.Control.SideBySide(leftLayers, rightLayers, options)
193
   return new L.Control.SideBySide(leftLayers, rightLayers, options)
159
 }
194
 }
160
 
195
 
161
-module.export = L.Control.sideBySide
196
+module.exports = L.Control.SideBySide
162
 
197
 
163
 }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
198
 }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
164
 },{"./layout.css":2,"./range.css":4}],2:[function(require,module,exports){
199
 },{"./layout.css":2,"./range.css":4}],2:[function(require,module,exports){

+ 1
- 1
leaflet-side-by-side.min.js
File diff suppressed because it is too large
View File


+ 1
- 1
package.json View File

1
 {
1
 {
2
   "name": "leaflet-side-by-side",
2
   "name": "leaflet-side-by-side",
3
-  "version": "1.1.1",
3
+  "version": "2.0.0",
4
   "description": "Compare two Leaflet layers side by side",
4
   "description": "Compare two Leaflet layers side by side",
5
   "main": "index.js",
5
   "main": "index.js",
6
   "browserify": {
6
   "browserify": {

Loading…
Cancel
Save