UNPKG

@enact/ui

Version:

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

184 lines (181 loc) 7.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pinchConfigPropType = exports.defaultPinchConfig = exports["default"] = exports.Pinch = void 0; var _clamp = _interopRequireDefault(require("ramda/src/clamp")); 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 _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); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } var Pinch = exports.Pinch = /*#__PURE__*/_createClass(function Pinch() { var _this = this; _classCallCheck(this, Pinch); this.pinchConfig = null; this.startScale = 1.0; this.scale = 1.0; this.isPinching = function () { return _this.pinchConfig != null; }; this.setContainerBounds = function (node) { var _this$pinchConfig = _this.pinchConfig, isGlobal = _this$pinchConfig.global, boxSizing = _this$pinchConfig.boxSizing; var bounds = null; if (typeof window === 'undefined' || !node) return; if (isGlobal) { bounds = { minX: 0, minY: 0, maxX: window.innerWidth, maxY: window.innerHeight }; } else { bounds = node.getBoundingClientRect(); // adjust for padding when using content-box if (boxSizing === 'content-box') { var computedStyle = window.getComputedStyle(node); bounds = { minX: bounds.left + parseInt(computedStyle.paddingLeft), minY: bounds.top + parseInt(computedStyle.paddingTop), maxX: bounds.right - parseInt(computedStyle.paddingRight), maxY: bounds.bottom - parseInt(computedStyle.paddingBottom) }; } else { bounds = { minX: bounds.left, minY: bounds.top, maxX: bounds.right, maxY: bounds.bottom }; } } _this.bounds = bounds; }; this.getBoundsCoords = function (_ref) { var x = _ref.x, y = _ref.y; var _this$bounds = _this.bounds, maxX = _this$bounds.maxX, maxY = _this$bounds.maxY, minX = _this$bounds.minX, minY = _this$bounds.minY; return { x: (0, _clamp["default"])(minX, maxX, x) - minX, y: (0, _clamp["default"])(minY, maxY, y) - minY }; }; this.getDistance = function (coords) { if (Array.isArray(coords)) { var _this$getBoundsCoords = _this.getBoundsCoords(coords[0]), x1 = _this$getBoundsCoords.x, y1 = _this$getBoundsCoords.y; var _this$getBoundsCoords2 = _this.getBoundsCoords(coords[1]), x2 = _this$getBoundsCoords2.x, y2 = _this$getBoundsCoords2.y; var dx = x1 - x2; var dy = y1 - y2; return Math.sqrt(dx * dx + dy * dy); } return 0; }; this.updateScale = function (scale) { var _this$pinchConfig2 = _this.pinchConfig, maxScale = _this$pinchConfig2.maxScale, minScale = _this$pinchConfig2.minScale; var newScale = (0, _clamp["default"])(minScale, maxScale, scale); if (newScale !== _this.scale) { _this.scale = newScale; return true; } return false; }; this.begin = function (config, _ref2, coords, node) { var noResume = _ref2.noResume, onPinch = _ref2.onPinch, onPinchEnd = _ref2.onPinchEnd, onPinchStart = _ref2.onPinchStart; if (!onPinch && !onPinchStart && !onPinchEnd) { return; } _this.pinchConfig = _objectSpread(_objectSpread({}, config), {}, { node: node, resume: !noResume }); _this.setContainerBounds(node); _this.startDist = _this.getDistance(coords); _this.previousDist = _this.startDist; _this.startScale = _this.scale; _this.onPinch = onPinch; _this.onPinchStart = onPinchStart; _this.onPinchEnd = onPinchEnd; if (_this.onPinchStart) { _this.onPinchStart({ type: 'onPinchStart', coords: coords }); } }; // This method will get the `onPinch`, `onPinchEnd`, and `onPinchStart` props. this.updateProps = function (_ref3) { var onPinch = _ref3.onPinch, onPinchEnd = _ref3.onPinchEnd, onPinchStart = _ref3.onPinchStart; // Check `isPinching` gesture is not in progress. Check if gesture exists before updating the references to the `pinchConfig` if (!_this.isPinching()) return; // This will update the `pinchConfig` with the new value _this.onPinch = onPinch; _this.onPinchStart = onPinchStart; _this.onPinchEnd = onPinchEnd; }; this.move = function (coords) { if (!_this.isPinching()) return; var moveTolerance = _this.pinchConfig.moveTolerance; var currentDist = _this.getDistance(coords); var scale = currentDist / _this.startDist * _this.startScale; if (Math.abs(_this.previousDist - currentDist) > moveTolerance && _this.onPinch && _this.updateScale(scale)) { _this.onPinch({ type: 'onPinch', scale: _this.scale, coords: coords }); _this.previousDist = currentDist; } }; this.blur = function () { if (!_this.isPinching()) return; if (!_this.pinchConfig.global) { _this.end(); } }; this.end = function () { if (!_this.isPinching()) return; if (_this.onPinchEnd) { _this.onPinchEnd({ type: 'onPinchEnd' }); } _this.pinchConfig = null; }; }); var defaultPinchConfig = exports.defaultPinchConfig = { boxSizing: 'border-box', global: false, maxScale: 4, minScale: 0.5, moveTolerance: 16 }; var pinchConfigPropType = exports.pinchConfigPropType = _propTypes["default"].shape({ boxSizing: _propTypes["default"].string, global: _propTypes["default"].bool, maxScale: _propTypes["default"].number, minScale: _propTypes["default"].number, moveTolerance: _propTypes["default"].number }); var _default = exports["default"] = Pinch;