UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

114 lines (112 loc) 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleIncrementByWheel = exports.handleIncrement = exports.handleDecrementByWheel = exports.handleDecrement = exports.forwardSpotlightEvents = exports.emitChange = void 0; var _handle = require("@enact/core/handle"); var _keymap = require("@enact/core/keymap"); var _util = require("@enact/core/util"); var _utils = require("@enact/ui/Slider/utils"); var nop = function nop() {}; var handleAcceleratedKeyDown = function handleAcceleratedKeyDown(ev, prop, _ref) { var spotlightAccelerator = _ref.current; if (!spotlightAccelerator) { return true; } if (!ev.repeat) { spotlightAccelerator.reset(); } if (spotlightAccelerator.processKey(ev, nop)) { return false; } return true; }; var calcStep = function calcStep(knobStep, step) { var s; if (knobStep != null) { s = knobStep; } else if (step != null) { s = step; } // default to a step of 1 if neither are set or are set to 0 // otherwise, increment/decrement would be no-ops return s || 1; }; var isIncrementByWheel = function isIncrementByWheel(_ref2) { var deltaY = _ref2.deltaY; return deltaY < 0; }; var isDecrementByWheel = function isDecrementByWheel(_ref3) { var deltaY = _ref3.deltaY; return deltaY > 0; }; var isIncrement = function isIncrement(_ref4, _ref5) { var keyCode = _ref4.keyCode; var orientation = _ref5.orientation; return orientation === 'vertical' ? (0, _keymap.is)('up', keyCode) : (0, _keymap.is)('right', keyCode); }; var isDecrement = function isDecrement(_ref6, _ref7) { var keyCode = _ref6.keyCode; var orientation = _ref7.orientation; return orientation === 'vertical' ? (0, _keymap.is)('down', keyCode) : (0, _keymap.is)('left', keyCode); }; var isNotMax = function isNotMax(ev, _ref8) { var value = _ref8.value, max = _ref8.max; return value !== max; }; var isNotMin = function isNotMin(ev, _ref9) { var min = _ref9.min, _ref9$value = _ref9.value, value = _ref9$value === void 0 ? min : _ref9$value; return value !== min; }; var checkInterval = function checkInterval(ev, _ref10, context) { var wheelInterval = _ref10.wheelInterval; if (ev.timeStamp - context.lastWheelTimeStamp < wheelInterval) { return false; } context.lastWheelTimeStamp = ev.timeStamp; return true; }; var emitChange = exports.emitChange = function emitChange(direction) { return (0, _handle.forwardCustom)('onChange', function (ev, _ref11) { var knobStep = _ref11.knobStep, max = _ref11.max, min = _ref11.min, step = _ref11.step, _ref11$value = _ref11.value, value = _ref11$value === void 0 ? min : _ref11$value; var newValue = (0, _util.clamp)(min, max, value + calcStep(knobStep, step) * direction); return { value: newValue, proportion: (0, _utils.calcProportion)(min, max, newValue) }; }); }; var isActive = function isActive(ev, props) { return props.active || !props.activateOnSelect; }; var handleIncrement = exports.handleIncrement = (0, _handle.handle)(isActive, isIncrement, _handle.preventDefault, _handle.stop, handleAcceleratedKeyDown, isNotMax, emitChange(1)); var handleDecrement = exports.handleDecrement = (0, _handle.handle)(isActive, isDecrement, _handle.preventDefault, _handle.stop, handleAcceleratedKeyDown, isNotMin, emitChange(-1)); var handleIncrementByWheel = exports.handleIncrementByWheel = (0, _handle.handle)(isActive, isIncrementByWheel, _handle.preventDefault, _handle.stop, isNotMax, checkInterval, emitChange(1)); var handleDecrementByWheel = exports.handleDecrementByWheel = (0, _handle.handle)(isActive, isDecrementByWheel, _handle.preventDefault, _handle.stop, isNotMin, checkInterval, emitChange(-1)); var either = function either(a, b) { return function () { return a.apply(void 0, arguments) || b.apply(void 0, arguments); }; }; var atMinimum = function atMinimum(ev, _ref12) { var min = _ref12.min, _ref12$value = _ref12.value, value = _ref12$value === void 0 ? min : _ref12$value; return value <= min; }; var atMaximum = function atMaximum(ev, _ref13) { var max = _ref13.max, min = _ref13.min, _ref13$value = _ref13.value, value = _ref13$value === void 0 ? min : _ref13$value; return value >= max; }; var forwardSpotlightEvents = exports.forwardSpotlightEvents = (0, _handle.oneOf)([(0, _handle.forKey)('left'), (0, _handle.handle)(either((0, _handle.forProp)('orientation', 'vertical'), atMinimum), (0, _handle.forwardCustom)('onSpotlightLeft'))], [(0, _handle.forKey)('right'), (0, _handle.handle)(either((0, _handle.forProp)('orientation', 'vertical'), atMaximum), (0, _handle.forwardCustom)('onSpotlightRight'))], [(0, _handle.forKey)('down'), (0, _handle.handle)(either((0, _handle.forProp)('orientation', 'horizontal'), atMinimum), (0, _handle.forwardCustom)('onSpotlightDown'))], [(0, _handle.forKey)('up'), (0, _handle.handle)(either((0, _handle.forProp)('orientation', 'horizontal'), atMaximum), (0, _handle.forwardCustom)('onSpotlightUp'))]);