@enact/core 3.4.9-experimental-3 → 3.4.9-experimental-7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/{build/dispatcher → dispatcher}/dispatcher.d.ts +0 -0
- package/dispatcher/dispatcher.js +76 -57
- package/dispatcher/listeners.js +31 -19
- package/dispatcher/tests/dispatcher-specs.js +75 -100
- package/{build/handle → handle}/handle.d.ts +0 -0
- package/handle/handle.js +260 -212
- package/handle/tests/handle-specs.js +476 -601
- package/{build/hoc → hoc}/hoc.d.ts +0 -0
- package/hoc/hoc.js +47 -33
- package/hoc/tests/hoc-specs.js +85 -126
- package/{build/index.d.ts → index.d.ts} +0 -0
- package/index.js +1 -0
- package/{build/internal → internal}/ApiDecorator/ApiDecorator.d.ts +0 -0
- package/internal/ApiDecorator/ApiDecorator.js +107 -61
- package/internal/ApiDecorator/tests/ApiDecorator-specs.js +106 -97
- package/{build/internal → internal}/Registry/Registry.d.ts +0 -0
- package/internal/Registry/Registry.js +68 -51
- package/internal/Registry/tests/Registry-specs.js +201 -143
- package/{build/internal → internal}/deprecate/deprecate.d.ts +0 -0
- package/internal/deprecate/deprecate.js +85 -60
- package/{build/internal → internal}/prop-types/prop-types.d.ts +0 -0
- package/internal/prop-types/prop-types.js +68 -61
- package/{build/keymap → keymap}/index.d.ts +0 -0
- package/keymap/index.js +27 -15
- package/keymap/keymap.js +67 -52
- package/keymap/tests/keymap-specs.js +75 -104
- package/kind/computed.js +29 -16
- package/{build/kind → kind}/kind.d.ts +0 -0
- package/kind/kind.js +150 -114
- package/kind/styles.js +62 -47
- package/kind/tests/computed-specs.js +87 -86
- package/kind/tests/kind-specs.js +216 -214
- package/kind/tests/styles-specs.js +170 -223
- package/kind/util.js +15 -7
- package/package.json +1 -1
- package/{build/platform → platform}/platform.d.ts +0 -0
- package/platform/platform.js +263 -169
- package/platform/tests/platform-specs.js +92 -90
- package/{build/snapshot → snapshot}/snapshot.d.ts +0 -0
- package/snapshot/snapshot.js +34 -27
- package/useChainRefs/tests/useChainRefs-specs.js +46 -30
- package/{build/useChainRefs → useChainRefs}/useChainRefs.d.ts +0 -0
- package/useChainRefs/useChainRefs.js +49 -29
- package/useClass/tests/useClass-specs.js +51 -46
- package/{build/useClass → useClass}/useClass.d.ts +0 -0
- package/useClass/useClass.js +27 -9
- package/useHandlers/Handlers.js +55 -30
- package/useHandlers/tests/useHandlers-specs.js +79 -95
- package/{build/useHandlers → useHandlers}/useHandlers.d.ts +0 -0
- package/useHandlers/useHandlers.js +18 -11
- package/util/Job.js +203 -241
- package/util/tests/Job-specs.js +216 -204
- package/util/tests/util-specs.js +123 -146
- package/{build/util → util}/util.d.ts +0 -0
- package/util/util.js +146 -103
- package/build/CHANGELOG.md +0 -699
- package/build/README.md +0 -33
- package/build/dispatcher/dispatcher.js +0 -160
- package/build/dispatcher/listeners.js +0 -37
- package/build/dispatcher/package.json +0 -3
- package/build/dispatcher/tests/dispatcher-specs.js +0 -75
- package/build/handle/handle.js +0 -846
- package/build/handle/package.json +0 -3
- package/build/handle/tests/handle-specs.js +0 -476
- package/build/hoc/hoc.js +0 -102
- package/build/hoc/package.json +0 -3
- package/build/hoc/tests/hoc-specs.js +0 -90
- package/build/index.js +0 -2
- package/build/internal/ApiDecorator/ApiDecorator.js +0 -143
- package/build/internal/ApiDecorator/package.json +0 -3
- package/build/internal/ApiDecorator/tests/ApiDecorator-specs.js +0 -106
- package/build/internal/Registry/Registry.js +0 -76
- package/build/internal/Registry/package.json +0 -3
- package/build/internal/Registry/tests/Registry-specs.js +0 -201
- package/build/internal/deprecate/deprecate.js +0 -116
- package/build/internal/deprecate/package.json +0 -3
- package/build/internal/prop-types/package.json +0 -3
- package/build/internal/prop-types/prop-types.js +0 -90
- package/build/keymap/index.js +0 -54
- package/build/keymap/keymap.js +0 -201
- package/build/keymap/package.json +0 -3
- package/build/keymap/tests/keymap-specs.js +0 -75
- package/build/kind/computed.js +0 -60
- package/build/kind/kind.js +0 -266
- package/build/kind/package.json +0 -3
- package/build/kind/styles.js +0 -105
- package/build/kind/tests/computed-specs.js +0 -87
- package/build/kind/tests/kind-specs.js +0 -216
- package/build/kind/tests/styles-specs.js +0 -170
- package/build/kind/util.js +0 -17
- package/build/package-lock.json +0 -206
- package/build/package.json +0 -38
- package/build/platform/package.json +0 -3
- package/build/platform/platform.js +0 -306
- package/build/platform/tests/platform-specs.js +0 -92
- package/build/snapshot/package.json +0 -3
- package/build/snapshot/snapshot.js +0 -89
- package/build/useChainRefs/package.json +0 -3
- package/build/useChainRefs/tests/useChainRefs-specs.js +0 -57
- package/build/useChainRefs/useChainRefs.js +0 -78
- package/build/useClass/package.json +0 -3
- package/build/useClass/tests/useClass-specs.js +0 -51
- package/build/useClass/useClass.js +0 -59
- package/build/useHandlers/Handlers.js +0 -55
- package/build/useHandlers/package.json +0 -3
- package/build/useHandlers/tests/useHandlers-specs.js +0 -84
- package/build/useHandlers/useHandlers.js +0 -36
- package/build/util/Job.js +0 -211
- package/build/util/package.json +0 -3
- package/build/util/tests/Job-specs.js +0 -216
- package/build/util/tests/util-specs.js +0 -123
- package/build/util/util.js +0 -322
package/build/handle/handle.js
DELETED
|
@@ -1,846 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.stopImmediate = exports.stop = exports.returnsTrue = exports.preventDefault = exports.oneOf = exports.not = exports.log = exports.handle = exports.forProp = exports.forKeyCode = exports.forKey = exports.forEventProp = exports.forwardWithPrevent = exports.forwardCustom = exports.forward = exports.callOnEvent = exports.call = exports.adaptEvent = exports["default"] = void 0;
|
|
7
|
-
|
|
8
|
-
var _cond = _interopRequireDefault(require("ramda/src/cond"));
|
|
9
|
-
|
|
10
|
-
var _curry = _interopRequireDefault(require("ramda/src/curry"));
|
|
11
|
-
|
|
12
|
-
var _keymap = require("../keymap");
|
|
13
|
-
|
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* `core/handle` provides a set of utilities to support handling events for `kind()`s and
|
|
18
|
-
* `React.Component`s. The default export, `handle()`, generates an event handler function from a
|
|
19
|
-
* set of input functions. The input functions either process or filter the event. If an input
|
|
20
|
-
* function returns `true`, `handle()` will continue processing the event by calling the next input
|
|
21
|
-
* function in the chain. If it returns `false` (or any falsy value like `null` or `undefined`),
|
|
22
|
-
* the event handling chain stops at that input function.
|
|
23
|
-
*
|
|
24
|
-
* Example:
|
|
25
|
-
* ```
|
|
26
|
-
* import {forKey, forward, handle, preventDefault} from '@enact/core/handle';
|
|
27
|
-
*
|
|
28
|
-
* // logEnter will contain a function that accepts an event, a props object, and a context object
|
|
29
|
-
* const logEnter = handle(
|
|
30
|
-
* forward('onKeyDown'), // forwards the event to the function passed in the onKeyDown prop
|
|
31
|
-
* forKey('enter'), // if the event.keyCode maps to the enter key, allows event processing to continue
|
|
32
|
-
* preventDefault, // calls event.preventDefault() to prevent the `keypress` event
|
|
33
|
-
* (ev, props) => { // custom event handler -- in this case, logging some text
|
|
34
|
-
* // since it doesn't return `true`, no further input functions would be called after this one
|
|
35
|
-
* console.log('The Enter key was pressed down');
|
|
36
|
-
* }
|
|
37
|
-
* ).finally(() => {
|
|
38
|
-
* console.log('This will log at the end no matter what happens within the handler above')
|
|
39
|
-
* });
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* `handle()` can also be bound to a component instance which allows it to access the instance
|
|
43
|
-
* `props` and `context`. This allows you to write consistent event handlers for components created
|
|
44
|
-
* either with `kind()` or ES6 classes without worrying about from where the props are sourced.
|
|
45
|
-
*
|
|
46
|
-
* Handlers can either be bound directly using the native `bind()` method or using the `bindAs()`
|
|
47
|
-
* utility method that is appended to the handler.
|
|
48
|
-
*
|
|
49
|
-
* Example:
|
|
50
|
-
* ```
|
|
51
|
-
* import {forKey, forward, handle, preventDefault} from '@enact/core/handle';
|
|
52
|
-
* import React from 'react';
|
|
53
|
-
*
|
|
54
|
-
* class MyComponent extends React.Component {
|
|
55
|
-
* // bind handle() to the instance
|
|
56
|
-
* constructor () {
|
|
57
|
-
* super();
|
|
58
|
-
*
|
|
59
|
-
* // logEnter will be bound to `this` and set as this.handleKeyDown
|
|
60
|
-
* //
|
|
61
|
-
* // Equivalent to the following with the advantage of set the function name to be displayed in
|
|
62
|
-
* // development tool call stacks
|
|
63
|
-
* //
|
|
64
|
-
* // this.handleKeyDown = logEnter.bind(this)
|
|
65
|
-
* logEnter.bindAs(this, 'handleKeyDown');
|
|
66
|
-
* }
|
|
67
|
-
*
|
|
68
|
-
* render () {
|
|
69
|
-
* return (
|
|
70
|
-
* <div onKeyDown={this.handleKeyDown} />
|
|
71
|
-
* );
|
|
72
|
-
* }
|
|
73
|
-
* }
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @module core/handle
|
|
77
|
-
* @exports adaptEvent
|
|
78
|
-
* @exports call
|
|
79
|
-
* @exports callOnEvent
|
|
80
|
-
* @exports forward
|
|
81
|
-
* @exports forwardWithPrevent
|
|
82
|
-
* @exports forEventProp
|
|
83
|
-
* @exports forKey
|
|
84
|
-
* @exports forKeyCode
|
|
85
|
-
* @exports forProp
|
|
86
|
-
* @exports handle
|
|
87
|
-
* @exports log
|
|
88
|
-
* @exports oneOf
|
|
89
|
-
* @exports preventDefault
|
|
90
|
-
* @exports returnsTrue
|
|
91
|
-
* @exports stop
|
|
92
|
-
* @exports stopImmediate
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The signature for event handlers
|
|
97
|
-
*
|
|
98
|
-
* @callback EventHandler
|
|
99
|
-
* @memberof core/handle
|
|
100
|
-
* @param {any} event
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* The signature for event handling functions supported by `handle` and related functions
|
|
105
|
-
*
|
|
106
|
-
* @callback HandlerFunction
|
|
107
|
-
* @memberof core/handle
|
|
108
|
-
* @param {any} event
|
|
109
|
-
* @param {Object<string, any>} props
|
|
110
|
-
* @param {Object<string, any>} context
|
|
111
|
-
*/
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* The signature for {@link core/handle.adaptEvent} parameter `adapter`
|
|
115
|
-
*
|
|
116
|
-
* @callback EventAdapter
|
|
117
|
-
* @memberof core/handle
|
|
118
|
-
* @param {any} event
|
|
119
|
-
* @param {Object<string, any>} props
|
|
120
|
-
* @param {Object<string, any>} context
|
|
121
|
-
* @returns {any}
|
|
122
|
-
*/
|
|
123
|
-
// Accepts an array of handlers, sanitizes them, and returns a handler function
|
|
124
|
-
// compose(allPass, map(makeSafeHandler));
|
|
125
|
-
var makeHandler = function makeHandler(handlers) {
|
|
126
|
-
// allowing shadowing here to provide a meaningful function name in dev tools
|
|
127
|
-
// eslint-disable-next-line no-shadow
|
|
128
|
-
return function handle() {
|
|
129
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
130
|
-
args[_key] = arguments[_key];
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
for (var i = 0; i < handlers.length; i++) {
|
|
134
|
-
var fn = handlers[i];
|
|
135
|
-
|
|
136
|
-
if (typeof fn !== 'function' || fn.apply(this, args)) {
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return true;
|
|
144
|
-
};
|
|
145
|
-
}; // Loose check to determine if obj is component-ish if it has both props and context members
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
var hasPropsAndContext = function hasPropsAndContext(obj) {
|
|
149
|
-
return obj && Object.prototype.hasOwnProperty.call(obj, 'props') && Object.prototype.hasOwnProperty.call(obj, 'context');
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
var named = function named(fn, name) {
|
|
153
|
-
if (process.env.NODE_ENV !== "production") {
|
|
154
|
-
try {
|
|
155
|
-
Object.defineProperty(fn, 'name', {
|
|
156
|
-
value: name,
|
|
157
|
-
writeable: false,
|
|
158
|
-
enumerable: false
|
|
159
|
-
});
|
|
160
|
-
} catch (err) {// unable to set name of function
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return fn;
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
var bindAs = function bindAs(fn, obj, name) {
|
|
168
|
-
var namedFunction = name ? named(fn, name) : fn;
|
|
169
|
-
var bound = namedFunction.bind(obj);
|
|
170
|
-
|
|
171
|
-
if (name) {
|
|
172
|
-
obj[name] = bound;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return bound;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
var decorateHandleFunction = function decorateHandleFunction(fn) {
|
|
179
|
-
fn.named = function (name) {
|
|
180
|
-
return named(fn, name);
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
fn.bindAs = function (obj, name) {
|
|
184
|
-
return bindAs(fn, obj, name);
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
return fn;
|
|
188
|
-
};
|
|
189
|
-
/**
|
|
190
|
-
* Allows generating event handlers by chaining input functions to filter or short-circuit the
|
|
191
|
-
* handling flow. Any input function that returns a falsy value will stop the chain.
|
|
192
|
-
*
|
|
193
|
-
* The returned handler function has a `finally()` member that accepts a function and returns a new
|
|
194
|
-
* handler function. The accepted function is called once the original handler completes regardless
|
|
195
|
-
* of the returned value.
|
|
196
|
-
*
|
|
197
|
-
* @method handle
|
|
198
|
-
* @param {...HandlerFunction} handlers List of handlers to process the event.
|
|
199
|
-
*
|
|
200
|
-
* @returns {EventHandler} A function that accepts an event which is dispatched to each of the
|
|
201
|
-
* provided handlers.
|
|
202
|
-
* @memberof core/handle
|
|
203
|
-
* @public
|
|
204
|
-
*/
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
var handle = function handle() {
|
|
208
|
-
for (var _len2 = arguments.length, handlers = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
209
|
-
handlers[_key2] = arguments[_key2];
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
var h = makeHandler(handlers); // In order to support binding either handle (handle.bind(this)) or a handler
|
|
213
|
-
// (a = handle(), a.bind(this)), we cache `this` here and use it as the fallback for props and
|
|
214
|
-
// context if fn() doesn't have its own `this`.
|
|
215
|
-
|
|
216
|
-
var _outer = this;
|
|
217
|
-
|
|
218
|
-
var fn = function prepareHandleArgs(ev, props, context) {
|
|
219
|
-
var caller = null; // if handle() was bound to a class, use its props and context. otherwise, we accept
|
|
220
|
-
// incoming props/context as would be provided by computed/handlers from kind()
|
|
221
|
-
|
|
222
|
-
if (hasPropsAndContext(this)) {
|
|
223
|
-
caller = this;
|
|
224
|
-
props = this.props;
|
|
225
|
-
context = this.context;
|
|
226
|
-
} else if (hasPropsAndContext(_outer)) {
|
|
227
|
-
caller = _outer;
|
|
228
|
-
props = _outer.props;
|
|
229
|
-
context = _outer.context;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return h.call(caller, ev, props, context);
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
fn["finally"] = function (cleanup) {
|
|
236
|
-
return decorateHandleFunction(function handleWithFinally(ev, props, context) {
|
|
237
|
-
var result = false;
|
|
238
|
-
|
|
239
|
-
if (hasPropsAndContext(this)) {
|
|
240
|
-
props = this.props;
|
|
241
|
-
context = this.context;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
try {
|
|
245
|
-
result = fn.call(this, ev, props, context);
|
|
246
|
-
} finally {
|
|
247
|
-
cleanup.call(this, ev, props, context);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return result;
|
|
251
|
-
});
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
return decorateHandleFunction(fn);
|
|
255
|
-
};
|
|
256
|
-
/**
|
|
257
|
-
* Calls the first handler whose condition passes. Each branch must be passed as an array with the
|
|
258
|
-
* first element being the condition function and the second being the handler function. The same
|
|
259
|
-
* arguments are passed to both the condition function and the handler function. The value returned
|
|
260
|
-
* from the handler is returned.
|
|
261
|
-
*
|
|
262
|
-
* Example:
|
|
263
|
-
* ```
|
|
264
|
-
* const handler = oneOf(
|
|
265
|
-
* [forKey('enter'), handleEnter],
|
|
266
|
-
* [forKey('left'), handleLeft],
|
|
267
|
-
* [forKey('right'), handleRight]
|
|
268
|
-
* );
|
|
269
|
-
* ```
|
|
270
|
-
*
|
|
271
|
-
* @method oneOf
|
|
272
|
-
* @param {...[HandlerFunction, HandlerFunction]} handlers List of conditions and handlers to process the event
|
|
273
|
-
*
|
|
274
|
-
* @returns {HandlerFunction} A function that accepts an event which is dispatched to each of the
|
|
275
|
-
* conditions and, if it passes, onto the provided handler.
|
|
276
|
-
* @memberof core/handle
|
|
277
|
-
* @public
|
|
278
|
-
*/
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
exports.handle = handle;
|
|
282
|
-
|
|
283
|
-
var oneOf = handle.oneOf = function () {
|
|
284
|
-
for (var _len3 = arguments.length, handlers = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
|
285
|
-
handlers[_key3] = arguments[_key3];
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return handle.call(this, (0, _cond["default"])(handlers));
|
|
289
|
-
};
|
|
290
|
-
/**
|
|
291
|
-
* A function that always returns `true`. Optionally accepts a `handler` function which is called
|
|
292
|
-
* before returning `true`.
|
|
293
|
-
*
|
|
294
|
-
* Example:
|
|
295
|
-
* ```
|
|
296
|
-
* // Used to coerce an existing function into a handler
|
|
297
|
-
* const coercedHandler = handle(
|
|
298
|
-
* returnsTrue(doesSomething),
|
|
299
|
-
* willAlwaysBeCalled
|
|
300
|
-
* );
|
|
301
|
-
*
|
|
302
|
-
* // Used to emulate if/else blocks with `oneOf`
|
|
303
|
-
* const ifElseHandler = oneOf(
|
|
304
|
-
* [forKey('enter'), handleEnter],
|
|
305
|
-
* [returnsTrue, handleOtherwise]
|
|
306
|
-
* );
|
|
307
|
-
* ```
|
|
308
|
-
*
|
|
309
|
-
* @method returnsTrue
|
|
310
|
-
* @param {Function} [handler] Handler function called before returning `true`.
|
|
311
|
-
*
|
|
312
|
-
* @returns {HandlerFunction} A function that returns `true`
|
|
313
|
-
* @memberof core/handle
|
|
314
|
-
* @public
|
|
315
|
-
*/
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
exports.oneOf = oneOf;
|
|
319
|
-
|
|
320
|
-
var returnsTrue = handle.returnsTrue = function (handler) {
|
|
321
|
-
if (handler && typeof handler === 'function') {
|
|
322
|
-
return named(function () {
|
|
323
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
324
|
-
args[_key4] = arguments[_key4];
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
handler.apply(this, args);
|
|
328
|
-
return true;
|
|
329
|
-
}, 'returnsTrue');
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return true;
|
|
333
|
-
};
|
|
334
|
-
/**
|
|
335
|
-
* Calls a named function on the event and returns `true`.
|
|
336
|
-
*
|
|
337
|
-
* Example:
|
|
338
|
-
* ```
|
|
339
|
-
* import {callOnEvent, handle} from '@enact/core/handle';
|
|
340
|
-
*
|
|
341
|
-
* const callsCustomMethod = handle(
|
|
342
|
-
* callOnEvent('customMethod'),
|
|
343
|
-
* (ev) => console.log('ev.customMethod() was called', ev)
|
|
344
|
-
* );
|
|
345
|
-
* ```
|
|
346
|
-
*
|
|
347
|
-
* @method callOnEvent
|
|
348
|
-
* @param {String} methodName Name of the method to call on the event
|
|
349
|
-
* @param {Object} ev Event payload
|
|
350
|
-
*
|
|
351
|
-
* @returns {true} Always returns `true`
|
|
352
|
-
* @curried
|
|
353
|
-
* @memberof core/handle
|
|
354
|
-
* @private
|
|
355
|
-
*/
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
exports.returnsTrue = returnsTrue;
|
|
359
|
-
var callOnEvent = handle.callOnEvent = (0, _curry["default"])(function (methodName, ev) {
|
|
360
|
-
if (ev[methodName]) {
|
|
361
|
-
ev[methodName]();
|
|
362
|
-
} else if (ev.nativeEvent && ev.nativeEvent[methodName]) {
|
|
363
|
-
// In some cases (notably stopImmediatePropagation), React doesn't include a desired method
|
|
364
|
-
// on its proxy so we check the native event as well.
|
|
365
|
-
ev.nativeEvent[methodName]();
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
return true;
|
|
369
|
-
});
|
|
370
|
-
/**
|
|
371
|
-
* Allows handling to continue if the value of `prop` on the event strictly equals `value`
|
|
372
|
-
*
|
|
373
|
-
* Example:
|
|
374
|
-
* ```
|
|
375
|
-
* import {forEventProp, handle} from '@enact/core/handle';
|
|
376
|
-
*
|
|
377
|
-
* const logWhenXEqualsZero = handle(
|
|
378
|
-
* forEventProp('x', 0),
|
|
379
|
-
* (ev) => console.log('ev.x was equal to zero', ev)
|
|
380
|
-
* );
|
|
381
|
-
* ```
|
|
382
|
-
*
|
|
383
|
-
* @method forEventProp
|
|
384
|
-
* @param {String} prop Name of property on event
|
|
385
|
-
* @param {*} value Value of property
|
|
386
|
-
* @param {Object} ev Event payload
|
|
387
|
-
*
|
|
388
|
-
* @returns {Boolean} Returns `true` if `prop` on `event` strictly equals `value`
|
|
389
|
-
* @curried
|
|
390
|
-
* @memberof core/handle
|
|
391
|
-
* @public
|
|
392
|
-
*/
|
|
393
|
-
|
|
394
|
-
exports.callOnEvent = callOnEvent;
|
|
395
|
-
var forEventProp = handle.forEventProp = (0, _curry["default"])(function (prop, value, ev) {
|
|
396
|
-
return ev[prop] === value;
|
|
397
|
-
});
|
|
398
|
-
/**
|
|
399
|
-
* Forwards the event to a function at `name` on `props`. If the specified prop is `undefined` or
|
|
400
|
-
* is not a function, it is ignored. The return value of the forwarded function is ignored and
|
|
401
|
-
* `true` is always returned instead.
|
|
402
|
-
*
|
|
403
|
-
* Example:
|
|
404
|
-
* ```
|
|
405
|
-
* import {forward, handle} from '@enact/core/handle';
|
|
406
|
-
*
|
|
407
|
-
* const forwardAndLog = handle(
|
|
408
|
-
* forward('onClick'),
|
|
409
|
-
* (ev) => console.log('event forwarded to onClick from props', ev)
|
|
410
|
-
* );
|
|
411
|
-
* ```
|
|
412
|
-
*
|
|
413
|
-
* @method forward
|
|
414
|
-
* @param {String} name Name of method on the `props`
|
|
415
|
-
* @param {Object} ev Event payload
|
|
416
|
-
* @param {Object} props Props object
|
|
417
|
-
*
|
|
418
|
-
* @returns {true} Always returns `true`
|
|
419
|
-
* @curried
|
|
420
|
-
* @memberof core/handle
|
|
421
|
-
* @public
|
|
422
|
-
*/
|
|
423
|
-
|
|
424
|
-
exports.forEventProp = forEventProp;
|
|
425
|
-
var forward = handle.forward = (0, _curry["default"])(named(function (name, ev, props) {
|
|
426
|
-
var fn = props && props[name];
|
|
427
|
-
|
|
428
|
-
if (typeof fn === 'function') {
|
|
429
|
-
fn(ev);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
return true;
|
|
433
|
-
}, 'forward'));
|
|
434
|
-
/**
|
|
435
|
-
* Calls `event.preventDefault()` and returns `true`.
|
|
436
|
-
*
|
|
437
|
-
* Example:
|
|
438
|
-
* ```
|
|
439
|
-
* import {handle, preventDefault} from '@enact/core/handle';
|
|
440
|
-
*
|
|
441
|
-
* const preventAndLog = handle(
|
|
442
|
-
* preventDefault,
|
|
443
|
-
* (ev) => console.log('preventDefault called', ev)
|
|
444
|
-
* );
|
|
445
|
-
* ```
|
|
446
|
-
*
|
|
447
|
-
* @method preventDefault
|
|
448
|
-
* @param {Object} ev Event payload
|
|
449
|
-
*
|
|
450
|
-
* @returns {true} Always returns `true`
|
|
451
|
-
* @memberof core/handle
|
|
452
|
-
* @public
|
|
453
|
-
*/
|
|
454
|
-
|
|
455
|
-
exports.forward = forward;
|
|
456
|
-
|
|
457
|
-
var _preventDefault = handle.preventDefault = callOnEvent('preventDefault');
|
|
458
|
-
/**
|
|
459
|
-
* Forwards the event to a function at `name` on `props` with capability to prevent default
|
|
460
|
-
* behavior. If the specified prop is `undefined` or is not a function, it is ignored. Returns
|
|
461
|
-
* `false` when `event.preventDefault()` has been called in a handler.
|
|
462
|
-
*
|
|
463
|
-
* Example:
|
|
464
|
-
* ```
|
|
465
|
-
* import {forwardWithPrevent, handle} from '@enact/core/handle';
|
|
466
|
-
*
|
|
467
|
-
* const forwardPreventDefault = handle(
|
|
468
|
-
* forwardWithPrevent('onClick'),
|
|
469
|
-
* (ev) => console.log('default action', ev)
|
|
470
|
-
* );
|
|
471
|
-
* ```
|
|
472
|
-
*
|
|
473
|
-
* @method forwardWithPrevent
|
|
474
|
-
* @param {String} name Name of method on the `props`
|
|
475
|
-
* @param {Object} ev Event payload
|
|
476
|
-
* @param {Object} props Props object
|
|
477
|
-
*
|
|
478
|
-
* @returns {Boolean} Returns `true` if default action is prevented
|
|
479
|
-
* @curried
|
|
480
|
-
* @memberof core/handle
|
|
481
|
-
* @private
|
|
482
|
-
*/
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
exports.preventDefault = _preventDefault;
|
|
486
|
-
var forwardWithPrevent = handle.forwardWithPrevent = (0, _curry["default"])(named(function (name, ev, props) {
|
|
487
|
-
var prevented = false;
|
|
488
|
-
var wrappedEvent = Object.assign({}, ev, {
|
|
489
|
-
preventDefault: function preventDefault() {
|
|
490
|
-
prevented = true;
|
|
491
|
-
|
|
492
|
-
_preventDefault(ev);
|
|
493
|
-
}
|
|
494
|
-
});
|
|
495
|
-
forward(name, wrappedEvent, props);
|
|
496
|
-
return !prevented;
|
|
497
|
-
}, 'forwardWithPrevent'));
|
|
498
|
-
/**
|
|
499
|
-
* Calls `event.stopPropagation()` and returns `true`
|
|
500
|
-
*
|
|
501
|
-
* Example:
|
|
502
|
-
* ```
|
|
503
|
-
* import {handle, stop} from '@enact/core/handle';
|
|
504
|
-
*
|
|
505
|
-
* const stopAndLog = handle(
|
|
506
|
-
* stop,
|
|
507
|
-
* (ev) => console.log('stopPropagation called', ev)
|
|
508
|
-
* );
|
|
509
|
-
* ```
|
|
510
|
-
*
|
|
511
|
-
* @method stop
|
|
512
|
-
* @param {Object} ev Event payload
|
|
513
|
-
*
|
|
514
|
-
* @returns {true} Always returns `true`
|
|
515
|
-
* @curried
|
|
516
|
-
* @memberof core/handle
|
|
517
|
-
* @public
|
|
518
|
-
*/
|
|
519
|
-
|
|
520
|
-
exports.forwardWithPrevent = forwardWithPrevent;
|
|
521
|
-
var stop = handle.stop = named(callOnEvent('stopPropagation'), 'stop');
|
|
522
|
-
/**
|
|
523
|
-
* Calls `event.stopImmediatePropagation()` and returns `true`
|
|
524
|
-
*
|
|
525
|
-
* Example:
|
|
526
|
-
* ```
|
|
527
|
-
* import {handle, stopImmediate} from '@enact/core/handle';
|
|
528
|
-
*
|
|
529
|
-
* const stopImmediateAndLog = handle(
|
|
530
|
-
* stopImmediate,
|
|
531
|
-
* (ev) => console.log('stopImmediatePropagation called', ev)
|
|
532
|
-
* );
|
|
533
|
-
* ```
|
|
534
|
-
*
|
|
535
|
-
* @method stopImmediate
|
|
536
|
-
* @param {Object} ev Event payload
|
|
537
|
-
*
|
|
538
|
-
* @returns {true} Always returns `true`
|
|
539
|
-
* @curried
|
|
540
|
-
* @memberof core/handle
|
|
541
|
-
* @public
|
|
542
|
-
*/
|
|
543
|
-
|
|
544
|
-
exports.stop = stop;
|
|
545
|
-
var stopImmediate = handle.stopImmediate = callOnEvent('stopImmediatePropagation');
|
|
546
|
-
/**
|
|
547
|
-
* Allows event handling to continue if `event.keyCode === value`.
|
|
548
|
-
*
|
|
549
|
-
* Example:
|
|
550
|
-
* ```
|
|
551
|
-
* import {forKeyCode, handle} from '@enact/core/handle';
|
|
552
|
-
*
|
|
553
|
-
* const logForEscapeKey = handle(
|
|
554
|
-
* forKeyCode(27),
|
|
555
|
-
* (ev) => console.log('Escape key pressed down', ev)
|
|
556
|
-
* );
|
|
557
|
-
* ```
|
|
558
|
-
*
|
|
559
|
-
* @method forKeyCode
|
|
560
|
-
* @param {Number} value `keyCode` to test
|
|
561
|
-
* @param {Object} ev Event payload
|
|
562
|
-
*
|
|
563
|
-
* @returns {Boolean} Returns `true` if `event.keyCode` strictly equals `value`
|
|
564
|
-
* @curried
|
|
565
|
-
* @memberof core/handle
|
|
566
|
-
* @public
|
|
567
|
-
*/
|
|
568
|
-
|
|
569
|
-
exports.stopImmediate = stopImmediate;
|
|
570
|
-
var forKeyCode = handle.forKeyCode = forEventProp('keyCode');
|
|
571
|
-
/**
|
|
572
|
-
* Allows handling to continue if the event's keyCode is mapped to `name` within
|
|
573
|
-
* {@link core/keymap}.
|
|
574
|
-
*
|
|
575
|
-
* Example:
|
|
576
|
-
* ```
|
|
577
|
-
* import {forKey, handle} from '@enact/core/handle';
|
|
578
|
-
*
|
|
579
|
-
* const logForEnterKey = handle(
|
|
580
|
-
* forKey('enter'),
|
|
581
|
-
* (ev) => console.log('Enter key pressed down', ev)
|
|
582
|
-
* );
|
|
583
|
-
* ```
|
|
584
|
-
*
|
|
585
|
-
* @see {@link core/keymap}
|
|
586
|
-
* @method forKey
|
|
587
|
-
* @param {String} name Name from {@link core/keymap}
|
|
588
|
-
* @param {Object} ev Event payload
|
|
589
|
-
*
|
|
590
|
-
* @returns {Boolean} Returns `true` if `event.keyCode` is mapped to `name`
|
|
591
|
-
* @curried
|
|
592
|
-
* @memberof core/handle
|
|
593
|
-
* @public
|
|
594
|
-
*/
|
|
595
|
-
|
|
596
|
-
exports.forKeyCode = forKeyCode;
|
|
597
|
-
var forKey = handle.forKey = (0, _curry["default"])(function (name, ev) {
|
|
598
|
-
return (0, _keymap.is)(name, ev.keyCode);
|
|
599
|
-
});
|
|
600
|
-
/**
|
|
601
|
-
* Allows handling to continue if the value of `prop` on the props strictly equals `value`.
|
|
602
|
-
*
|
|
603
|
-
* Example:
|
|
604
|
-
* ```
|
|
605
|
-
* import {forProp, handle} from '@enact/core/handle';
|
|
606
|
-
*
|
|
607
|
-
* const logWhenChecked = handle(
|
|
608
|
-
* forProp('checked', true),
|
|
609
|
-
* (ev) => console.log('checked prop is true', ev)
|
|
610
|
-
* );
|
|
611
|
-
* ```
|
|
612
|
-
*
|
|
613
|
-
* @method forProp
|
|
614
|
-
* @param {String} prop Name of property on props object
|
|
615
|
-
* @param {*} value Value of property
|
|
616
|
-
* @param {Object} ev Event payload
|
|
617
|
-
* @param {Object} props Props object
|
|
618
|
-
*
|
|
619
|
-
* @returns {Boolean} Returns `true` if the value of `props[prop]` strictly equals `value`
|
|
620
|
-
* @curried
|
|
621
|
-
* @memberof core/handle
|
|
622
|
-
* @public
|
|
623
|
-
*/
|
|
624
|
-
|
|
625
|
-
exports.forKey = forKey;
|
|
626
|
-
var forProp = handle.forProp = (0, _curry["default"])(function (prop, value, ev, props) {
|
|
627
|
-
return props[prop] === value;
|
|
628
|
-
});
|
|
629
|
-
/**
|
|
630
|
-
* Logs the event, props, and context optionally preceded by a custom message. Will only log in
|
|
631
|
-
* development mode.
|
|
632
|
-
*
|
|
633
|
-
* Example:
|
|
634
|
-
* ```
|
|
635
|
-
* import {forProp, handle, log} from '@enact/core/handle';
|
|
636
|
-
*
|
|
637
|
-
* const logWhenChecked = handle(
|
|
638
|
-
* forProp('checked', true),
|
|
639
|
-
* log('checked props is true')
|
|
640
|
-
* );
|
|
641
|
-
* ```
|
|
642
|
-
*
|
|
643
|
-
* @method log
|
|
644
|
-
* @param {String} message Custom message
|
|
645
|
-
* @param {Object} ev Event payload
|
|
646
|
-
* @param {...*} [args] Any args passed are logged
|
|
647
|
-
*
|
|
648
|
-
* @returns {true} Always returns `true`
|
|
649
|
-
* @curried
|
|
650
|
-
* @memberof core/handle
|
|
651
|
-
* @public
|
|
652
|
-
*/
|
|
653
|
-
|
|
654
|
-
exports.forProp = forProp;
|
|
655
|
-
var log = handle.log = (0, _curry["default"])(function (message, ev) {
|
|
656
|
-
if (process.env.NODE_ENV !== "production") {
|
|
657
|
-
var _console;
|
|
658
|
-
|
|
659
|
-
for (var _len5 = arguments.length, args = new Array(_len5 > 2 ? _len5 - 2 : 0), _key5 = 2; _key5 < _len5; _key5++) {
|
|
660
|
-
args[_key5 - 2] = arguments[_key5];
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
// eslint-disable-next-line no-console
|
|
664
|
-
(_console = console).log.apply(_console, [message, ev].concat(args));
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
return true;
|
|
668
|
-
});
|
|
669
|
-
/**
|
|
670
|
-
* Invokes a method by name on the component to which {@link core/handle.handle} is bound.
|
|
671
|
-
*
|
|
672
|
-
* If the methods exists on the object, it is called with the event, props, and context and its
|
|
673
|
-
* return value is returned.
|
|
674
|
-
*
|
|
675
|
-
* If the method does not exist or handle isn't bound to an instance, it returns `false`.
|
|
676
|
-
*
|
|
677
|
-
* Example:
|
|
678
|
-
* ```
|
|
679
|
-
* import {call, handle, forProp} from '@enact/core/handle';
|
|
680
|
-
*
|
|
681
|
-
* const incrementIfEnabled = handle(
|
|
682
|
-
* forProp('disabled', false),
|
|
683
|
-
* call('increment')
|
|
684
|
-
* );
|
|
685
|
-
*
|
|
686
|
-
* class Counter extends React.Component {
|
|
687
|
-
* constructor () {
|
|
688
|
-
* super();
|
|
689
|
-
*
|
|
690
|
-
* this.handleIncrement = incrementIfEnabled.bind(this);
|
|
691
|
-
* }
|
|
692
|
-
*
|
|
693
|
-
* render () {
|
|
694
|
-
* // ...
|
|
695
|
-
* }
|
|
696
|
-
* }
|
|
697
|
-
* ```
|
|
698
|
-
*
|
|
699
|
-
* @method call
|
|
700
|
-
* @param {String} method Name of method
|
|
701
|
-
*
|
|
702
|
-
* @returns {HandlerFunction} Returns the value returned by `method`, or `false` if the method
|
|
703
|
-
* does not exist
|
|
704
|
-
* @memberof core/handle
|
|
705
|
-
* @public
|
|
706
|
-
*/
|
|
707
|
-
|
|
708
|
-
exports.log = log;
|
|
709
|
-
|
|
710
|
-
var call = function call(method) {
|
|
711
|
-
return named(function () {
|
|
712
|
-
if (this && this[method]) {
|
|
713
|
-
return this[method].apply(this, arguments);
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
return false;
|
|
717
|
-
}, 'call');
|
|
718
|
-
};
|
|
719
|
-
/**
|
|
720
|
-
* Adapts an event with `adapter` before calling `handler`.
|
|
721
|
-
*
|
|
722
|
-
* The `adapter` function receives the same arguments as any handler. The value returned from
|
|
723
|
-
* `adapter` is passed as the first argument to `handler` with the remaining arguments kept the
|
|
724
|
-
* same. This is often useful to generate a custom event payload before forwarding on to a callback.
|
|
725
|
-
*
|
|
726
|
-
* Example:
|
|
727
|
-
* ```
|
|
728
|
-
* import {adaptEvent, forward} from '@enact/core/handle';
|
|
729
|
-
*
|
|
730
|
-
* // calls the onChange callback with an event payload containing a type and value member
|
|
731
|
-
* const incrementAndChange = adaptEvent(
|
|
732
|
-
* (ev, props) => ({
|
|
733
|
-
* type: 'onChange',
|
|
734
|
-
* value: props.value + 1
|
|
735
|
-
* }),
|
|
736
|
-
* forward('onChange')
|
|
737
|
-
* )
|
|
738
|
-
* ```
|
|
739
|
-
*
|
|
740
|
-
* @method adaptEvent
|
|
741
|
-
* @param {EventAdapter} adapter Function to adapt the event payload
|
|
742
|
-
* @param {HandlerFunction} handler Handler to call with the handler function
|
|
743
|
-
*
|
|
744
|
-
* @returns {HandlerFunction} Returns an [event handler]{@link core/handle.HandlerFunction} (suitable for passing to handle) that returns the result of `handler`
|
|
745
|
-
* @curried
|
|
746
|
-
* @memberof core/handle
|
|
747
|
-
* @public
|
|
748
|
-
*/
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
exports.call = call;
|
|
752
|
-
var adaptEvent = handle.adaptEvent = (0, _curry["default"])(function (adapter, handler) {
|
|
753
|
-
return named(function (ev) {
|
|
754
|
-
for (var _len6 = arguments.length, args = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) {
|
|
755
|
-
args[_key6 - 1] = arguments[_key6];
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
return handler.call.apply(handler, [this, adapter.call.apply(adapter, [this, ev].concat(args))].concat(args));
|
|
759
|
-
}, 'adaptEvent');
|
|
760
|
-
});
|
|
761
|
-
/**
|
|
762
|
-
* Creates a handler that will forward the event to a function at `name` on `props`.
|
|
763
|
-
*
|
|
764
|
-
* If `adapter` is not specified, a new event payload will be generated with a `type` member with
|
|
765
|
-
* the `name` of the custom event. If `adapter` is specified, the `type` member is added to the
|
|
766
|
-
* value returned by `adapter`.
|
|
767
|
-
*
|
|
768
|
-
* The `adapter` function receives the same arguments as any handler. The value returned from
|
|
769
|
-
* `adapter` is passed as the first argument to `handler` with the remaining arguments kept the
|
|
770
|
-
* same. This is often useful to generate a custom event payload before forwarding on to a callback.
|
|
771
|
-
*
|
|
772
|
-
* Example:
|
|
773
|
-
* ```
|
|
774
|
-
* import {forwardCustom} from '@enact/core/handle';
|
|
775
|
-
*
|
|
776
|
-
* // calls the onChange callback with the event: {type: 'onChange'}
|
|
777
|
-
* const forwardChange = forwardCustom('onChange');
|
|
778
|
-
*
|
|
779
|
-
* // calls the onChange callback with the event: {type: 'onChange', index}
|
|
780
|
-
* const forwardChangeWithIndex = forwardCustom('onChange', (ev, {index}) => ({index}));
|
|
781
|
-
* ```
|
|
782
|
-
*
|
|
783
|
-
* @method forwardCustom
|
|
784
|
-
* @param {String} name Name of method on the `props`
|
|
785
|
-
* @param {EventAdapter} [adapter] Function to adapt the event payload
|
|
786
|
-
*
|
|
787
|
-
* @returns {HandlerFunction} Returns an [event handler]{@link core/handle.EventHandler}
|
|
788
|
-
* (suitable for passing to handle or used directly within
|
|
789
|
-
* `handlers` in [kind]{@link core/kind}) that will forward the
|
|
790
|
-
* custom event.
|
|
791
|
-
* @memberof core/handle
|
|
792
|
-
* @public
|
|
793
|
-
*/
|
|
794
|
-
|
|
795
|
-
exports.adaptEvent = adaptEvent;
|
|
796
|
-
|
|
797
|
-
var forwardCustom = handle.forwardCustom = function (name, adapter) {
|
|
798
|
-
return handle(adaptEvent(function () {
|
|
799
|
-
var ev = adapter ? adapter.apply(void 0, arguments) : null; // Handle either no adapter or a non-object return from the adapter
|
|
800
|
-
|
|
801
|
-
if (!ev || typeof ev !== 'object') {
|
|
802
|
-
ev = {};
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
ev.type = name;
|
|
806
|
-
return ev;
|
|
807
|
-
}, forward(name))).named('forwardCustom');
|
|
808
|
-
};
|
|
809
|
-
/**
|
|
810
|
-
* Accepts a handler and returns the logical complement of the value returned from the handler.
|
|
811
|
-
*
|
|
812
|
-
* Example:
|
|
813
|
-
* ```
|
|
814
|
-
* import {forProp, forward, not, handle} from '@enact/core/handle';
|
|
815
|
-
*
|
|
816
|
-
* // calls the onChange callback when disabled is not true
|
|
817
|
-
* const handleChange = handle(
|
|
818
|
-
* not(forProp('disabled', true)),
|
|
819
|
-
* forward('onChange')
|
|
820
|
-
* )
|
|
821
|
-
* ```
|
|
822
|
-
*
|
|
823
|
-
* @method not
|
|
824
|
-
* @param {HandlerFunction} handler Handler to complement
|
|
825
|
-
*
|
|
826
|
-
* @returns {HandlerFunction} Returns an [event
|
|
827
|
-
* handler]{@link core/handle.HandlerFunction} (suitable for
|
|
828
|
-
* passing to handle) that returns the complement of the
|
|
829
|
-
* return value of `handler`
|
|
830
|
-
* @curried
|
|
831
|
-
* @memberof core/handle
|
|
832
|
-
* @public
|
|
833
|
-
*/
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
exports.forwardCustom = forwardCustom;
|
|
837
|
-
|
|
838
|
-
var not = handle.not = function (handler) {
|
|
839
|
-
return function () {
|
|
840
|
-
return !handler.apply(void 0, arguments);
|
|
841
|
-
};
|
|
842
|
-
};
|
|
843
|
-
|
|
844
|
-
exports.not = not;
|
|
845
|
-
var _default = handle;
|
|
846
|
-
exports["default"] = _default;
|