UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

127 lines (122 loc) 5.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.flickConfigPropType = exports.defaultFlickConfig = exports["default"] = exports.Flick = void 0; var _util = require("@enact/core/util"); var _propTypes = _interopRequireDefault(require("prop-types")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } 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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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 Flick = exports.Flick = /*#__PURE__*/function () { function Flick() { var _this = this; _classCallCheck(this, Flick); this.begin = function (config, _ref, coords) { var onFlick = _ref.onFlick; _this.flickConfig = _objectSpread({}, config); if (_this.flickConfig.maxDuration !== null) { _this.cancelJob.startAfter(_this.flickConfig.maxDuration); } _this.tracking = !!onFlick; _this.moves.length = 0; _this.onFlick = onFlick; _this.move(coords); }; // This method will get the `onFlick` props. this.updateProps = function (_ref2) { var onFlick = _ref2.onFlick; // Check `tracking` gesture is not in progress. Check if gesture exists before updating the references to the `onFlick` if (!_this.tracking) return; // This will update the `onFlick` with the new value _this.onFlick = onFlick; }; this.move = function (_ref3) { var x = _ref3.x, y = _ref3.y; if (!_this.tracking) return; var maxMoves = _this.flickConfig.maxMoves; _this.moves.push({ x: x, y: y, t: window.performance.now() }); // track specified # of points if (_this.moves.length > maxMoves) { _this.moves.shift(); } }; this.blur = function () { _this.end(); }; this.cancel = function () { _this.tracking = false; }; this.cancelJob = new _util.Job(this.cancel); this.end = function () { if (!_this.tracking) return; var minVelocity = _this.flickConfig.minVelocity; _this.cancelJob.stop(); var moves = _this.moves; if (moves.length > 1) { // note: important to use up time to reduce flick // velocity based on time between move and up. var last = moves[moves.length - 1]; var t = window.performance.now(); var x = 0, y = 0; // take the greatest of flick between each tracked move and last move for (var i = moves.length - 2, m; m = moves[i]; i--) { // this flick (this move - last move) / (this time - last time) var dt = t - m.t; var vx = (last.x - m.x) / dt; var vy = (last.y - m.y) / dt; // if either axis is a greater flick than previously recorded use this one if (Math.abs(vx) > Math.abs(x) || Math.abs(vy) > Math.abs(y)) { x = vx; y = vy; } } var v = Math.sqrt(x * x + y * y); if (v > minVelocity) { var vertical = Math.abs(y) > Math.abs(x); // generate the flick using the start event so it has those coordinates // this.sendFlick(ti.startEvent, x, y, v); _this.onFlick({ type: 'onFlick', direction: vertical ? 'vertical' : 'horizontal', velocityX: x, velocityY: y, velocity: v }); } } _this.tracking = false; }; this.tracking = false; this.moves = []; } return _createClass(Flick, [{ key: "isTracking", value: function isTracking() { return this.tracking; } }]); }(); var defaultFlickConfig = exports.defaultFlickConfig = { maxDuration: 250, maxMoves: 5, minVelocity: 0.1 }; var flickConfigPropType = exports.flickConfigPropType = _propTypes["default"].shape({ maxDuration: _propTypes["default"].number, maxMoves: _propTypes["default"].number, maxVelocity: _propTypes["default"].number }); var _default = exports["default"] = Flick;