|
| 1 | +import mapboxgl from 'mapbox-gl'; |
| 2 | + |
| 3 | +const defaultOptions = { |
| 4 | + doubleClickZoom: true |
| 5 | +}; |
| 6 | + |
| 7 | +function functor(x) { |
| 8 | + return function () { |
| 9 | + return x; |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +const Map = function (options) { |
| 15 | + const evented = new mapboxgl.Evented(); |
| 16 | + this.on = evented.on; |
| 17 | + this.fire = evented.fire; |
| 18 | + this.listens = evented.listens; |
| 19 | + |
| 20 | + this.options = options; |
| 21 | + this._events = {}; |
| 22 | + this._sources = {}; |
| 23 | + this._collectResourceTiming = !!this.options.collectResourceTiming; |
| 24 | + this.zoom = this.options.zoom || 0; |
| 25 | + this._container = this.options.container || 'map'; |
| 26 | + this._layers = {}; |
| 27 | + this.getContainer = function () { |
| 28 | + return this._container; |
| 29 | + }; |
| 30 | + this.bounds = this.options.bounds; |
| 31 | + |
| 32 | + try { |
| 33 | + this.center = this.options.center ? new mapboxgl.LngLat(this.options.center.lng, this.options.center.lat) : new mapboxgl.LngLat(0, 0); |
| 34 | + } catch (e) { |
| 35 | + this.center = this.options.center ? new mapboxgl.LngLat(this.options.center[0], this.options.center[1]) : new mapboxgl.LngLat(0, 0); |
| 36 | + } |
| 37 | + this.resize = function () {}; |
| 38 | + this.style = options.style; |
| 39 | + this.setStyle = function (style, options) { |
| 40 | + for (let i = 0, list = style.layers; i < list.length; i += 1) { |
| 41 | + this._layers[layer.id] = list[i]; |
| 42 | + } |
| 43 | + this.sources = style.sources |
| 44 | + }; |
| 45 | + if (options.style) { |
| 46 | + this.setStyle(options.style); |
| 47 | + } |
| 48 | + this.transform = { |
| 49 | + angle: 0 |
| 50 | + }; |
| 51 | + this._controlCorners = { |
| 52 | + 'top-left': { |
| 53 | + appendChild: function () {} |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + const setters = [ |
| 58 | + // Camera options |
| 59 | + 'jumpTo', |
| 60 | + 'panTo', |
| 61 | + 'panBy', |
| 62 | + 'setBearing', |
| 63 | + 'setPitch', |
| 64 | + 'setZoom', |
| 65 | + 'fitBounds', |
| 66 | + 'resetNorth', |
| 67 | + 'snapToNorth', |
| 68 | + // Settings |
| 69 | + 'setMaxBounds', |
| 70 | + 'setMinZoom', |
| 71 | + 'setMaxZoom', |
| 72 | + // Layer properties |
| 73 | + 'setLayoutProperty', |
| 74 | + 'setPaintProperty' |
| 75 | + ]; |
| 76 | + const genericSetter = functor(this); |
| 77 | + for (let i = 0; i < setters.length; i++) { |
| 78 | + this[setters[i]] = genericSetter; |
| 79 | + } |
| 80 | + |
| 81 | + this.setLayoutProperty = function (layerid) {}; |
| 82 | + |
| 83 | + this.addControl = function (control) { |
| 84 | + control.onAdd(this); |
| 85 | + }; |
| 86 | + |
| 87 | + this.getStyle = function () { |
| 88 | + return { |
| 89 | + version:8, |
| 90 | + source:this._sources, |
| 91 | + layers:Object.values(this._layers) |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + this.getContainer = function () { |
| 96 | + const container = { |
| 97 | + parentNode: container, |
| 98 | + appendChild: function () {}, |
| 99 | + removeChild: function () {}, |
| 100 | + getElementsByClassName: function () { |
| 101 | + return [container]; |
| 102 | + }, |
| 103 | + addEventListener: function (name, handle) {}, |
| 104 | + removeEventListener: function () {}, |
| 105 | + classList: { |
| 106 | + add: function () {}, |
| 107 | + remove: function () {} |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + return container; |
| 112 | + }; |
| 113 | + |
| 114 | + this.getSource = function (name) { |
| 115 | + this._sources[name] |
| 116 | + }; |
| 117 | + |
| 118 | + this.loaded = function () { |
| 119 | + return true; |
| 120 | + }; |
| 121 | + |
| 122 | + this.removeControl = function () { |
| 123 | + return this; |
| 124 | + }; |
| 125 | + |
| 126 | + this.overlayLayersManager = {}; |
| 127 | + |
| 128 | + this.addSource = function (name, source) { |
| 129 | + this._sources[name] = source; |
| 130 | + }; |
| 131 | + this.removeSource = function (name) { |
| 132 | + delete this._sources[name]; |
| 133 | + }; |
| 134 | + this.off = function () {}; |
| 135 | + this.addLayer = function (layer, before) { |
| 136 | + this._layers[layer.id] = layer; |
| 137 | + if (layer.onAdd) { |
| 138 | + layer.onAdd(this); |
| 139 | + } |
| 140 | + if (layer.render) { |
| 141 | + layer.render(); |
| 142 | + } |
| 143 | + return this; |
| 144 | + }; |
| 145 | + |
| 146 | + this.addStyle = function (style, before) { |
| 147 | + return style; |
| 148 | + }; |
| 149 | + |
| 150 | + this.removeLayer = function (layerId) {}; |
| 151 | + this.moveLayer = function (layerId) {}; |
| 152 | + this.getFilter = function (layerId) {}; |
| 153 | + this.setFilter = function (layerId, filter) {}; |
| 154 | + this.getLayer = function (id) { |
| 155 | + return this._layers[layer.id] |
| 156 | + }; |
| 157 | + this.getBounds = function () { |
| 158 | + return this.bounds; |
| 159 | + }; |
| 160 | + |
| 161 | + this.getZoom = function () { |
| 162 | + return this.zoom; |
| 163 | + }; |
| 164 | + this.getBearing = functor(0); |
| 165 | + this.getPitch = functor(0); |
| 166 | + this.getCenter = function () { |
| 167 | + return this.center; |
| 168 | + }; |
| 169 | + this.setCenter = function (x) { |
| 170 | + if (x instanceof Array) { |
| 171 | + this.center = new mapboxgl.LngLat(x[0], x[1]); |
| 172 | + } else if (x instanceof Object) { |
| 173 | + this.center = new mapboxgl.LngLat(x.lng, x.lat); |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + this.getMinZoom = function () { |
| 178 | + return 0; |
| 179 | + }; |
| 180 | + this.getMaxZoom = function () { |
| 181 | + return 22; |
| 182 | + }; |
| 183 | + this.doubleClickZoom = { |
| 184 | + disable: function () {}, |
| 185 | + enable: function () {} |
| 186 | + }; |
| 187 | + |
| 188 | + this.boxZoom = { |
| 189 | + disable: function () {}, |
| 190 | + enable: function () {} |
| 191 | + }; |
| 192 | + |
| 193 | + this.dragPan = { |
| 194 | + disable: function () {}, |
| 195 | + enable: function () {} |
| 196 | + }; |
| 197 | + |
| 198 | + this.scrollZoom = { |
| 199 | + disable: function () {}, |
| 200 | + enable: function () {} |
| 201 | + }; |
| 202 | + |
| 203 | + this.dragRotate = { |
| 204 | + disable: function () {}, |
| 205 | + enable: function () {} |
| 206 | + }; |
| 207 | + |
| 208 | + this.keyboard = { |
| 209 | + disable: function () {}, |
| 210 | + enable: function () {} |
| 211 | + }; |
| 212 | + |
| 213 | + this.touchZoomRotate = { |
| 214 | + disable: function () {}, |
| 215 | + enable: function () {} |
| 216 | + }; |
| 217 | + |
| 218 | + this.project = function () { |
| 219 | + return { |
| 220 | + x: 500, |
| 221 | + y: 300 |
| 222 | + }; |
| 223 | + }; |
| 224 | + this.unproject = function (point) { |
| 225 | + return new mapboxgl.LngLat(-73.9876, 40.7661); |
| 226 | + }; |
| 227 | + |
| 228 | + this.queryRenderedFeatures = function (pointOrBox, queryParams) { |
| 229 | + return []; |
| 230 | + }; |
| 231 | + |
| 232 | + this.remove = function () { |
| 233 | + this._events = []; |
| 234 | + this.sources = []; |
| 235 | + }; |
| 236 | + |
| 237 | + this.zoomIn = function (e) { |
| 238 | + this.zoom++; |
| 239 | + return this.zoom; |
| 240 | + }; |
| 241 | + |
| 242 | + this.zoomOut = function (e) { |
| 243 | + this.zoom--; |
| 244 | + this.fire('wheel'); |
| 245 | + this.fire('zoomend', this.zoom); |
| 246 | + return this.zoom; |
| 247 | + }; |
| 248 | + this.loadImage = function (src, callback) { |
| 249 | + callback(null, [1, 2, 3]); |
| 250 | + }; |
| 251 | + this.addImage = function () {}; |
| 252 | + this.hasImage = function () { |
| 253 | + return true; |
| 254 | + }; |
| 255 | + this.getPaintProperty = function () {}; |
| 256 | + this.removeImage = function () {}; |
| 257 | + this.getCanvasContainer = () => { |
| 258 | + return { |
| 259 | + appendChild() {}, |
| 260 | + addEventListener(eventName, callback) {}, |
| 261 | + style: { |
| 262 | + cursor: null |
| 263 | + } |
| 264 | + }; |
| 265 | + }; |
| 266 | + this.getCanvas = () => { |
| 267 | + return { |
| 268 | + style: { |
| 269 | + width: 100, |
| 270 | + height: 100 |
| 271 | + } |
| 272 | + }; |
| 273 | + }; |
| 274 | + this.getCRS = () => { |
| 275 | + return { |
| 276 | + getExtent: () => jest.fn() |
| 277 | + }; |
| 278 | + }; |
| 279 | + this.setCRS = () => {}; |
| 280 | + this.flyTo = options => {}; |
| 281 | + this.setRenderWorldCopies = epsgCode => {}; |
| 282 | + this.triggerRepaint = () => {}; |
| 283 | + setTimeout(() => { |
| 284 | + this.fire('load'); |
| 285 | + }, 0); |
| 286 | +}; |
| 287 | + |
| 288 | +export default Map; |
0 commit comments