1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
4 |
|
---|
5 | var actions_dist_useGestureCoreActions = require('./actions-89e642c9.cjs.prod.js');
|
---|
6 | require('./maths-83bc6f64.cjs.prod.js');
|
---|
7 |
|
---|
8 | function _objectWithoutPropertiesLoose(source, excluded) {
|
---|
9 | if (source == null) return {};
|
---|
10 | var target = {};
|
---|
11 | var sourceKeys = Object.keys(source);
|
---|
12 | var key, i;
|
---|
13 | for (i = 0; i < sourceKeys.length; i++) {
|
---|
14 | key = sourceKeys[i];
|
---|
15 | if (excluded.indexOf(key) >= 0) continue;
|
---|
16 | target[key] = source[key];
|
---|
17 | }
|
---|
18 | return target;
|
---|
19 | }
|
---|
20 |
|
---|
21 | function _objectWithoutProperties(source, excluded) {
|
---|
22 | if (source == null) return {};
|
---|
23 | var target = _objectWithoutPropertiesLoose(source, excluded);
|
---|
24 | var key, i;
|
---|
25 | if (Object.getOwnPropertySymbols) {
|
---|
26 | var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
---|
27 | for (i = 0; i < sourceSymbolKeys.length; i++) {
|
---|
28 | key = sourceSymbolKeys[i];
|
---|
29 | if (excluded.indexOf(key) >= 0) continue;
|
---|
30 | if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
---|
31 | target[key] = source[key];
|
---|
32 | }
|
---|
33 | }
|
---|
34 | return target;
|
---|
35 | }
|
---|
36 |
|
---|
37 | const sharedConfigResolver = {
|
---|
38 | target(value) {
|
---|
39 | if (value) {
|
---|
40 | return () => 'current' in value ? value.current : value;
|
---|
41 | }
|
---|
42 | return undefined;
|
---|
43 | },
|
---|
44 | enabled(value = true) {
|
---|
45 | return value;
|
---|
46 | },
|
---|
47 | window(value = actions_dist_useGestureCoreActions.SUPPORT.isBrowser ? window : undefined) {
|
---|
48 | return value;
|
---|
49 | },
|
---|
50 | eventOptions({
|
---|
51 | passive = true,
|
---|
52 | capture = false
|
---|
53 | } = {}) {
|
---|
54 | return {
|
---|
55 | passive,
|
---|
56 | capture
|
---|
57 | };
|
---|
58 | },
|
---|
59 | transform(value) {
|
---|
60 | return value;
|
---|
61 | }
|
---|
62 | };
|
---|
63 |
|
---|
64 | const _excluded = ["target", "eventOptions", "window", "enabled", "transform"];
|
---|
65 | function resolveWith(config = {}, resolvers) {
|
---|
66 | const result = {};
|
---|
67 | for (const [key, resolver] of Object.entries(resolvers)) {
|
---|
68 | switch (typeof resolver) {
|
---|
69 | case 'function':
|
---|
70 | {
|
---|
71 | result[key] = resolver.call(result, config[key], key, config);
|
---|
72 | }
|
---|
73 | break;
|
---|
74 | case 'object':
|
---|
75 | result[key] = resolveWith(config[key], resolver);
|
---|
76 | break;
|
---|
77 | case 'boolean':
|
---|
78 | if (resolver) result[key] = config[key];
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return result;
|
---|
83 | }
|
---|
84 | function parse(newConfig, gestureKey, _config = {}) {
|
---|
85 | const _ref = newConfig,
|
---|
86 | {
|
---|
87 | target,
|
---|
88 | eventOptions,
|
---|
89 | window,
|
---|
90 | enabled,
|
---|
91 | transform
|
---|
92 | } = _ref,
|
---|
93 | rest = _objectWithoutProperties(_ref, _excluded);
|
---|
94 | _config.shared = resolveWith({
|
---|
95 | target,
|
---|
96 | eventOptions,
|
---|
97 | window,
|
---|
98 | enabled,
|
---|
99 | transform
|
---|
100 | }, sharedConfigResolver);
|
---|
101 | if (gestureKey) {
|
---|
102 | const resolver = actions_dist_useGestureCoreActions.ConfigResolverMap.get(gestureKey);
|
---|
103 | _config[gestureKey] = resolveWith(actions_dist_useGestureCoreActions._objectSpread2({
|
---|
104 | shared: _config.shared
|
---|
105 | }, rest), resolver);
|
---|
106 | } else {
|
---|
107 | for (const key in rest) {
|
---|
108 | const resolver = actions_dist_useGestureCoreActions.ConfigResolverMap.get(key);
|
---|
109 | if (resolver) {
|
---|
110 | _config[key] = resolveWith(actions_dist_useGestureCoreActions._objectSpread2({
|
---|
111 | shared: _config.shared
|
---|
112 | }, rest[key]), resolver);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return _config;
|
---|
117 | }
|
---|
118 |
|
---|
119 | class EventStore {
|
---|
120 | constructor(ctrl, gestureKey) {
|
---|
121 | actions_dist_useGestureCoreActions._defineProperty(this, "_listeners", new Set());
|
---|
122 | this._ctrl = ctrl;
|
---|
123 | this._gestureKey = gestureKey;
|
---|
124 | }
|
---|
125 | add(element, device, action, handler, options) {
|
---|
126 | const listeners = this._listeners;
|
---|
127 | const type = actions_dist_useGestureCoreActions.toDomEventType(device, action);
|
---|
128 | const _options = this._gestureKey ? this._ctrl.config[this._gestureKey].eventOptions : {};
|
---|
129 | const eventOptions = actions_dist_useGestureCoreActions._objectSpread2(actions_dist_useGestureCoreActions._objectSpread2({}, _options), options);
|
---|
130 | element.addEventListener(type, handler, eventOptions);
|
---|
131 | const remove = () => {
|
---|
132 | element.removeEventListener(type, handler, eventOptions);
|
---|
133 | listeners.delete(remove);
|
---|
134 | };
|
---|
135 | listeners.add(remove);
|
---|
136 | return remove;
|
---|
137 | }
|
---|
138 | clean() {
|
---|
139 | this._listeners.forEach(remove => remove());
|
---|
140 | this._listeners.clear();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | class TimeoutStore {
|
---|
145 | constructor() {
|
---|
146 | actions_dist_useGestureCoreActions._defineProperty(this, "_timeouts", new Map());
|
---|
147 | }
|
---|
148 | add(key, callback, ms = 140, ...args) {
|
---|
149 | this.remove(key);
|
---|
150 | this._timeouts.set(key, window.setTimeout(callback, ms, ...args));
|
---|
151 | }
|
---|
152 | remove(key) {
|
---|
153 | const timeout = this._timeouts.get(key);
|
---|
154 | if (timeout) window.clearTimeout(timeout);
|
---|
155 | }
|
---|
156 | clean() {
|
---|
157 | this._timeouts.forEach(timeout => void window.clearTimeout(timeout));
|
---|
158 | this._timeouts.clear();
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | class Controller {
|
---|
163 | constructor(handlers) {
|
---|
164 | actions_dist_useGestureCoreActions._defineProperty(this, "gestures", new Set());
|
---|
165 | actions_dist_useGestureCoreActions._defineProperty(this, "_targetEventStore", new EventStore(this));
|
---|
166 | actions_dist_useGestureCoreActions._defineProperty(this, "gestureEventStores", {});
|
---|
167 | actions_dist_useGestureCoreActions._defineProperty(this, "gestureTimeoutStores", {});
|
---|
168 | actions_dist_useGestureCoreActions._defineProperty(this, "handlers", {});
|
---|
169 | actions_dist_useGestureCoreActions._defineProperty(this, "config", {});
|
---|
170 | actions_dist_useGestureCoreActions._defineProperty(this, "pointerIds", new Set());
|
---|
171 | actions_dist_useGestureCoreActions._defineProperty(this, "touchIds", new Set());
|
---|
172 | actions_dist_useGestureCoreActions._defineProperty(this, "state", {
|
---|
173 | shared: {
|
---|
174 | shiftKey: false,
|
---|
175 | metaKey: false,
|
---|
176 | ctrlKey: false,
|
---|
177 | altKey: false
|
---|
178 | }
|
---|
179 | });
|
---|
180 | resolveGestures(this, handlers);
|
---|
181 | }
|
---|
182 | setEventIds(event) {
|
---|
183 | if (actions_dist_useGestureCoreActions.isTouch(event)) {
|
---|
184 | this.touchIds = new Set(actions_dist_useGestureCoreActions.touchIds(event));
|
---|
185 | return this.touchIds;
|
---|
186 | } else if ('pointerId' in event) {
|
---|
187 | if (event.type === 'pointerup' || event.type === 'pointercancel') this.pointerIds.delete(event.pointerId);else if (event.type === 'pointerdown') this.pointerIds.add(event.pointerId);
|
---|
188 | return this.pointerIds;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | applyHandlers(handlers, nativeHandlers) {
|
---|
192 | this.handlers = handlers;
|
---|
193 | this.nativeHandlers = nativeHandlers;
|
---|
194 | }
|
---|
195 | applyConfig(config, gestureKey) {
|
---|
196 | this.config = parse(config, gestureKey, this.config);
|
---|
197 | }
|
---|
198 | clean() {
|
---|
199 | this._targetEventStore.clean();
|
---|
200 | for (const key of this.gestures) {
|
---|
201 | this.gestureEventStores[key].clean();
|
---|
202 | this.gestureTimeoutStores[key].clean();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | effect() {
|
---|
206 | if (this.config.shared.target) this.bind();
|
---|
207 | return () => this._targetEventStore.clean();
|
---|
208 | }
|
---|
209 | bind(...args) {
|
---|
210 | const sharedConfig = this.config.shared;
|
---|
211 | const props = {};
|
---|
212 | let target;
|
---|
213 | if (sharedConfig.target) {
|
---|
214 | target = sharedConfig.target();
|
---|
215 | if (!target) return;
|
---|
216 | }
|
---|
217 | if (sharedConfig.enabled) {
|
---|
218 | for (const gestureKey of this.gestures) {
|
---|
219 | const gestureConfig = this.config[gestureKey];
|
---|
220 | const bindFunction = bindToProps(props, gestureConfig.eventOptions, !!target);
|
---|
221 | if (gestureConfig.enabled) {
|
---|
222 | const Engine = actions_dist_useGestureCoreActions.EngineMap.get(gestureKey);
|
---|
223 | new Engine(this, args, gestureKey).bind(bindFunction);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | const nativeBindFunction = bindToProps(props, sharedConfig.eventOptions, !!target);
|
---|
227 | for (const eventKey in this.nativeHandlers) {
|
---|
228 | nativeBindFunction(eventKey, '', event => this.nativeHandlers[eventKey](actions_dist_useGestureCoreActions._objectSpread2(actions_dist_useGestureCoreActions._objectSpread2({}, this.state.shared), {}, {
|
---|
229 | event,
|
---|
230 | args
|
---|
231 | })), undefined, true);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | for (const handlerProp in props) {
|
---|
235 | props[handlerProp] = actions_dist_useGestureCoreActions.chain(...props[handlerProp]);
|
---|
236 | }
|
---|
237 | if (!target) return props;
|
---|
238 | for (const handlerProp in props) {
|
---|
239 | const {
|
---|
240 | device,
|
---|
241 | capture,
|
---|
242 | passive
|
---|
243 | } = actions_dist_useGestureCoreActions.parseProp(handlerProp);
|
---|
244 | this._targetEventStore.add(target, device, '', props[handlerProp], {
|
---|
245 | capture,
|
---|
246 | passive
|
---|
247 | });
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | function setupGesture(ctrl, gestureKey) {
|
---|
252 | ctrl.gestures.add(gestureKey);
|
---|
253 | ctrl.gestureEventStores[gestureKey] = new EventStore(ctrl, gestureKey);
|
---|
254 | ctrl.gestureTimeoutStores[gestureKey] = new TimeoutStore();
|
---|
255 | }
|
---|
256 | function resolveGestures(ctrl, internalHandlers) {
|
---|
257 | if (internalHandlers.drag) setupGesture(ctrl, 'drag');
|
---|
258 | if (internalHandlers.wheel) setupGesture(ctrl, 'wheel');
|
---|
259 | if (internalHandlers.scroll) setupGesture(ctrl, 'scroll');
|
---|
260 | if (internalHandlers.move) setupGesture(ctrl, 'move');
|
---|
261 | if (internalHandlers.pinch) setupGesture(ctrl, 'pinch');
|
---|
262 | if (internalHandlers.hover) setupGesture(ctrl, 'hover');
|
---|
263 | }
|
---|
264 | const bindToProps = (props, eventOptions, withPassiveOption) => (device, action, handler, options = {}, isNative = false) => {
|
---|
265 | var _options$capture, _options$passive;
|
---|
266 | const capture = (_options$capture = options.capture) !== null && _options$capture !== void 0 ? _options$capture : eventOptions.capture;
|
---|
267 | const passive = (_options$passive = options.passive) !== null && _options$passive !== void 0 ? _options$passive : eventOptions.passive;
|
---|
268 | let handlerProp = isNative ? device : actions_dist_useGestureCoreActions.toHandlerProp(device, action, capture);
|
---|
269 | if (withPassiveOption && passive) handlerProp += 'Passive';
|
---|
270 | props[handlerProp] = props[handlerProp] || [];
|
---|
271 | props[handlerProp].push(handler);
|
---|
272 | };
|
---|
273 |
|
---|
274 | const RE_NOT_NATIVE = /^on(Drag|Wheel|Scroll|Move|Pinch|Hover)/;
|
---|
275 | function sortHandlers(_handlers) {
|
---|
276 | const native = {};
|
---|
277 | const handlers = {};
|
---|
278 | const actions = new Set();
|
---|
279 | for (let key in _handlers) {
|
---|
280 | if (RE_NOT_NATIVE.test(key)) {
|
---|
281 | actions.add(RegExp.lastMatch);
|
---|
282 | handlers[key] = _handlers[key];
|
---|
283 | } else {
|
---|
284 | native[key] = _handlers[key];
|
---|
285 | }
|
---|
286 | }
|
---|
287 | return [handlers, native, actions];
|
---|
288 | }
|
---|
289 | function registerGesture(actions, handlers, handlerKey, key, internalHandlers, config) {
|
---|
290 | if (!actions.has(handlerKey)) return;
|
---|
291 | if (!actions_dist_useGestureCoreActions.EngineMap.has(key)) {
|
---|
292 | return;
|
---|
293 | }
|
---|
294 | const startKey = handlerKey + 'Start';
|
---|
295 | const endKey = handlerKey + 'End';
|
---|
296 | const fn = state => {
|
---|
297 | let memo = undefined;
|
---|
298 | if (state.first && startKey in handlers) handlers[startKey](state);
|
---|
299 | if (handlerKey in handlers) memo = handlers[handlerKey](state);
|
---|
300 | if (state.last && endKey in handlers) handlers[endKey](state);
|
---|
301 | return memo;
|
---|
302 | };
|
---|
303 | internalHandlers[key] = fn;
|
---|
304 | config[key] = config[key] || {};
|
---|
305 | }
|
---|
306 | function parseMergedHandlers(mergedHandlers, mergedConfig) {
|
---|
307 | const [handlers, nativeHandlers, actions] = sortHandlers(mergedHandlers);
|
---|
308 | const internalHandlers = {};
|
---|
309 | registerGesture(actions, handlers, 'onDrag', 'drag', internalHandlers, mergedConfig);
|
---|
310 | registerGesture(actions, handlers, 'onWheel', 'wheel', internalHandlers, mergedConfig);
|
---|
311 | registerGesture(actions, handlers, 'onScroll', 'scroll', internalHandlers, mergedConfig);
|
---|
312 | registerGesture(actions, handlers, 'onPinch', 'pinch', internalHandlers, mergedConfig);
|
---|
313 | registerGesture(actions, handlers, 'onMove', 'move', internalHandlers, mergedConfig);
|
---|
314 | registerGesture(actions, handlers, 'onHover', 'hover', internalHandlers, mergedConfig);
|
---|
315 | return {
|
---|
316 | handlers: internalHandlers,
|
---|
317 | config: mergedConfig,
|
---|
318 | nativeHandlers
|
---|
319 | };
|
---|
320 | }
|
---|
321 |
|
---|
322 | exports.Controller = Controller;
|
---|
323 | exports.parseMergedHandlers = parseMergedHandlers;
|
---|