Spaces:
Sleeping
Sleeping
File size: 4,167 Bytes
4a288a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | // @flow
import DOM from '../../util/dom';
import {Event} from '../../util/evented';
import type Map from '../map';
/**
* The `BoxZoomHandler` allows the user to zoom the map to fit within a bounding box.
* The bounding box is defined by clicking and holding `shift` while dragging the cursor.
*/
class BoxZoomHandler {
_map: Map;
_el: HTMLElement;
_container: HTMLElement;
_enabled: boolean;
_active: boolean;
_startPos: Point;
_lastPos: Point;
_box: HTMLElement;
_clickTolerance: number;
/**
* @private
*/
constructor(map: Map, options: {
clickTolerance: number
}) {
this._map = map;
this._el = map.getCanvasContainer();
this._container = map.getContainer();
this._clickTolerance = options.clickTolerance || 1;
}
/**
* Returns a Boolean indicating whether the "box zoom" interaction is enabled.
*
* @returns {boolean} `true` if the "box zoom" interaction is enabled.
*/
isEnabled() {
return !!this._enabled;
}
/**
* Returns a Boolean indicating whether the "box zoom" interaction is active, i.e. currently being used.
*
* @returns {boolean} `true` if the "box zoom" interaction is active.
*/
isActive() {
return !!this._active;
}
/**
* Enables the "box zoom" interaction.
*
* @example
* map.boxZoom.enable();
*/
enable() {
if (this.isEnabled()) return;
this._enabled = true;
}
/**
* Disables the "box zoom" interaction.
*
* @example
* map.boxZoom.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._enabled = false;
}
mousedown(e: MouseEvent, point: Point) {
if (!this.isEnabled()) return;
if (!(e.shiftKey && e.button === 0)) return;
DOM.disableDrag();
this._startPos = this._lastPos = point;
this._active = true;
}
mousemoveWindow(e: MouseEvent, point: Point) {
if (!this._active) return;
const pos = point;
if (this._lastPos.equals(pos) || (!this._box && pos.dist(this._startPos) < this._clickTolerance)) {
return;
}
const p0 = this._startPos;
this._lastPos = pos;
if (!this._box) {
this._box = DOM.create('div', 'mapboxgl-boxzoom', this._container);
this._container.classList.add('mapboxgl-crosshair');
this._fireEvent('boxzoomstart', e);
}
const minX = Math.min(p0.x, pos.x),
maxX = Math.max(p0.x, pos.x),
minY = Math.min(p0.y, pos.y),
maxY = Math.max(p0.y, pos.y);
DOM.setTransform(this._box, `translate(${minX}px,${minY}px)`);
this._box.style.width = `${maxX - minX}px`;
this._box.style.height = `${maxY - minY}px`;
}
mouseupWindow(e: MouseEvent, point: Point) {
if (!this._active) return;
if (e.button !== 0) return;
const p0 = this._startPos,
p1 = point;
this.reset();
DOM.suppressClick();
if (p0.x === p1.x && p0.y === p1.y) {
this._fireEvent('boxzoomcancel', e);
} else {
this._map.fire(new Event('boxzoomend', {originalEvent: e}));
return {
cameraAnimation: map => map.fitScreenCoordinates(p0, p1, this._map.getBearing(), {linear: true})
};
}
}
keydown(e: KeyboardEvent) {
if (!this._active) return;
if (e.keyCode === 27) {
this.reset();
this._fireEvent('boxzoomcancel', e);
}
}
blur() {
this.reset();
}
reset() {
this._active = false;
this._container.classList.remove('mapboxgl-crosshair');
if (this._box) {
DOM.remove(this._box);
this._box = (null: any);
}
DOM.enableDrag();
delete this._startPos;
delete this._lastPos;
}
_fireEvent(type: string, e: *) {
return this._map.fire(new Event(type, {originalEvent: e}));
}
}
export default BoxZoomHandler;
|