@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
147 lines (141 loc) • 6.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.resetDefaultConfig = exports.mergeConfig = exports.getConfig = exports["default"] = exports.configure = void 0;
var _Drag = require("./Drag.js");
var _Flick = require("./Flick.js");
var _Hold = require("./Hold.js");
var _Pinch = require("./Pinch");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var allowedDragKeys = Object.keys(_Drag.defaultDragConfig);
var allowedFlickKeys = Object.keys(_Flick.defaultFlickConfig);
var allowedHoldKeys = Object.keys(_Hold.defaultHoldConfig);
var allowedPinchKeys = Object.keys(_Pinch.defaultPinchConfig);
/**
* The Global Gesture Configuration Object
*
* @private
* @memberof ui/Touchable
*/
var config = {};
// map-friendly clone method
var clone = function clone(o) {
return Object.assign({}, o);
};
// Merges two configuration objects while retaining only the allowed keys
var mergeGestureConfig = function mergeGestureConfig(current, update, allowed) {
var cfg = _objectSpread(_objectSpread({}, current), update);
Object.keys(cfg).forEach(function (key) {
if (allowed.indexOf(key) === -1) {
delete cfg[key];
}
});
return cfg;
};
// Merges the current global config with the provided `cfg` and returns the result
var mergeConfig = exports.mergeConfig = function mergeConfig(cfg) {
var merged = {
drag: mergeGestureConfig(config.drag, cfg.drag, allowedDragKeys),
flick: mergeGestureConfig(config.flick, cfg.flick, allowedFlickKeys),
hold: mergeGestureConfig(config.hold, cfg.hold, allowedHoldKeys),
pinch: mergeGestureConfig(config.pinch, cfg.pinch, allowedPinchKeys)
};
merged.hold.events = merged.hold.events.map(clone);
return merged;
};
/**
* Configures the global gesture configuration for the application.
*
* Example:
* ```
* // Updates the `maxMoves`, `moveTolerance`, and `frequency` configurations while retaining the
* // current value of all other configurations
* configure({
* flick: {
* maxMoves: 10
* },
* hold: {
* moveTolerance: 32,
* frequency: 400
* }
* });
* ```
*
* Each type of gesture has its own set of configuration properties grouped within a separate object
* keyed by the name of the gesture. Partial configurations may be passed and will be merged with
* the current configuration.
*
* `drag`
*
* * `boxSizing` - The part of the component's box model is used as the bounds of the constraint.
* Only applies when `global` is `false`.
* * `'border-box'` - the default, includes the padding and border but excludes the margin.
* * `'content-box'` - excludes the padding, border, and margin.
* * `global` - When `true`, drag gestures will continue when leaving the bounds of the component
* or blurring the component.
* * `moveTolerance` - The number of pixels from the start position of the drag that the pointer
* may move before cancelling the drag. Defaults to `16`.
*
* `flick`
*
* * `maxDuration` - The amount of time, in milliseconds, to complete a flick gesture before it
* is cancelled. Defaults to 250.
* * `maxMoves` - The number of moves tracked to determine if a flick occurred. Defaults to `5`.
* * `minVelocity` - The minimum threshold, measured as the change in pixels over the change in
* time per move, that must be exceeded to generate a `onFlick` event.
*
* `hold`
*
* * `cancelOnMove` - When `true`, the hold is cancelled when moving beyond the `moveTolerance`.
* Defaults to `false`
* * `global` - When `true`, hold gestures will continue when leaving the bounds of the component
* or blurring the component.
* * `moveTolerance` - The number of pixels from the start position of the hold that the pointer
* may move before cancelling the hold. Ignored when `cancelOnMove` is `false`. Defaults to
* `16`.
* * `frequency` - The time, in milliseconds, to poll for hold events. Defaults to `200`.
* * `events` - An array of `onHoldStart` events which each contain a `name` and `time`. The `time`
* controls the amount of time that must pass before this `onHoldStart` event is fired and should
* be a multiple of `frequency`.
*
* `pinch`
*
* * `boxSizing` - The part of the component's box model is used as the bounds of the constraint.
* Only applies when `global` is `false`.
* * `'border-box'` - the default, includes the padding and border but excludes the margin.
* * `'content-box'` - excludes the padding, border, and margin.
* * `global` - When `true`, pinch gestures will continue when leaving the bounds of the component
* or blurring the component.
* * `maxScale` - The maximum scale value. Defaults to `4`.
* * `minScale` - The minimum scale value. Defaults to `0.5`.
* * `moveTolerance` - The distance difference from the previous distance that the pointer may move
* before cancelling the scaling. Defaults to `16`.
*
* @function
* @param {Object} cfg A partial or complete configuration object
*
* @returns {undefined}
* @public
* @memberof ui/Touchable
*/
var configure = exports.configure = function configure(cfg) {
config = mergeConfig(cfg);
};
var getConfig = exports.getConfig = function getConfig() {
return config;
};
var resetDefaultConfig = exports.resetDefaultConfig = function resetDefaultConfig() {
return configure({
drag: _Drag.defaultDragConfig,
flick: _Flick.defaultFlickConfig,
hold: _Hold.defaultHoldConfig,
pinch: _Pinch.defaultPinchConfig
});
};
resetDefaultConfig();
var _default = exports["default"] = configure;