-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Interaction.js
170 lines (150 loc) · 4.89 KB
/
Interaction.js
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
/**
* @module ol/interaction/Interaction
*/
import BaseObject from '../Object.js';
import InteractionProperty from './Property.js';
import {easeOut, linear} from '../easing.js';
/***
* @template Return
* @typedef {import("../Observable").OnSignature<import("../Observable").EventTypes, import("../events/Event.js").default, Return> &
* import("../Observable").OnSignature<import("../ObjectEventType").Types|
* 'change:active', import("../Object").ObjectEvent, Return> &
* import("../Observable").CombinedOnSignature<import("../Observable").EventTypes|import("../ObjectEventType").Types|
* 'change:active', Return>} InteractionOnSignature
*/
/**
* Object literal with config options for interactions.
* @typedef {Object} InteractionOptions
* @property {function(import("../MapBrowserEvent.js").default):boolean} handleEvent
* Method called by the map to notify the interaction that a browser event was
* dispatched to the map. If the function returns a falsy value, propagation of
* the event to other interactions in the map's interactions chain will be
* prevented (this includes functions with no explicit return). The interactions
* are traversed in reverse order of the interactions collection of the map.
*/
/**
* @classdesc
* Abstract base class; normally only used for creating subclasses and not
* instantiated in apps.
* User actions that change the state of the map. Some are similar to controls,
* but are not associated with a DOM element.
* For example, {@link module:ol/interaction/KeyboardZoom~KeyboardZoom} is
* functionally the same as {@link module:ol/control/Zoom~Zoom}, but triggered
* by a keyboard event not a button element event.
* Although interactions do not have a DOM element, some of them do render
* vectors and so are visible on the screen.
* @api
*/
class Interaction extends BaseObject {
/**
* @param {InteractionOptions} [options] Options.
*/
constructor(options) {
super();
/***
* @type {InteractionOnSignature<import("../events").EventsKey>}
*/
this.on;
/***
* @type {InteractionOnSignature<import("../events").EventsKey>}
*/
this.once;
/***
* @type {InteractionOnSignature<void>}
*/
this.un;
if (options && options.handleEvent) {
this.handleEvent = options.handleEvent;
}
/**
* @private
* @type {import("../Map.js").default|null}
*/
this.map_ = null;
this.setActive(true);
}
/**
* Return whether the interaction is currently active.
* @return {boolean} `true` if the interaction is active, `false` otherwise.
* @observable
* @api
*/
getActive() {
return /** @type {boolean} */ (this.get(InteractionProperty.ACTIVE));
}
/**
* Get the map associated with this interaction.
* @return {import("../Map.js").default|null} Map.
* @api
*/
getMap() {
return this.map_;
}
/**
* Handles the {@link module:ol/MapBrowserEvent~MapBrowserEvent map browser event}.
* @param {import("../MapBrowserEvent.js").default} mapBrowserEvent Map browser event.
* @return {boolean} `false` to stop event propagation.
* @api
*/
handleEvent(mapBrowserEvent) {
return true;
}
/**
* Activate or deactivate the interaction.
* @param {boolean} active Active.
* @observable
* @api
*/
setActive(active) {
this.set(InteractionProperty.ACTIVE, active);
}
/**
* Remove the interaction from its current map and attach it to the new map.
* Subclasses may set up event handlers to get notified about changes to
* the map here.
* @param {import("../Map.js").default|null} map Map.
*/
setMap(map) {
this.map_ = map;
}
}
/**
* @param {import("../View.js").default} view View.
* @param {import("../coordinate.js").Coordinate} delta Delta.
* @param {number} [duration] Duration.
*/
export function pan(view, delta, duration) {
const currentCenter = view.getCenterInternal();
if (currentCenter) {
const center = [currentCenter[0] + delta[0], currentCenter[1] + delta[1]];
view.animateInternal({
duration: duration !== undefined ? duration : 250,
easing: linear,
center: view.getConstrainedCenter(center),
});
}
}
/**
* @param {import("../View.js").default} view View.
* @param {number} delta Delta from previous zoom level.
* @param {import("../coordinate.js").Coordinate} [anchor] Anchor coordinate in the user projection.
* @param {number} [duration] Duration.
*/
export function zoomByDelta(view, delta, anchor, duration) {
const currentZoom = view.getZoom();
if (currentZoom === undefined) {
return;
}
const newZoom = view.getConstrainedZoom(currentZoom + delta);
const newResolution = view.getResolutionForZoom(newZoom);
if (view.getAnimating()) {
view.cancelAnimations();
}
view.animate({
resolution: newResolution,
anchor: anchor,
duration: duration !== undefined ? duration : 250,
easing: easeOut,
});
}
export default Interaction;