UNPKG

@egjs/axes

Version:

A module used to change the information of user action entered by various input devices such as touch screen or mouse into the logical virtual coordinates. You can easily create a UI that responds to user actions.

113 lines 4.27 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { isOutside, getCirculatedPos } from "./Coordinate"; import { map, filter, every } from "./utils"; var AxisManager = (function () { function AxisManager(_axis) { var _this = this; this._axis = _axis; this._complementOptions(); this._pos = Object.keys(this._axis).reduce(function (pos, v) { pos[v] = _this._axis[v].startPos; return pos; }, {}); } AxisManager.prototype.getDelta = function (depaPos, destPos) { var fullDepaPos = this.get(depaPos); return map(this.get(destPos), function (v, k) { return v - fullDepaPos[k]; }); }; AxisManager.prototype.get = function (axes) { var _this = this; if (axes && Array.isArray(axes)) { return axes.reduce(function (acc, v) { if (v && v in _this._pos) { acc[v] = _this._pos[v]; } return acc; }, {}); } else { return __assign(__assign({}, this._pos), (axes || {})); } }; AxisManager.prototype.moveTo = function (pos, depaPos) { if (depaPos === void 0) { depaPos = this._pos; } var delta = map(this._pos, function (v, key) { return key in pos && key in depaPos ? pos[key] - depaPos[key] : 0; }); this.set(this.map(pos, function (v, opt) { return opt ? getCirculatedPos(v, opt.range, opt.circular) : 0; })); return { pos: __assign({}, this._pos), delta: delta, }; }; AxisManager.prototype.set = function (pos) { for (var k in pos) { if (k && k in this._pos) { this._pos[k] = pos[k]; } } }; AxisManager.prototype.every = function (pos, callback) { var axisOptions = this._axis; return every(pos, function (value, key) { return callback(value, axisOptions[key], key); }); }; AxisManager.prototype.filter = function (pos, callback) { var axisOptions = this._axis; return filter(pos, function (value, key) { return callback(value, axisOptions[key], key); }); }; AxisManager.prototype.map = function (pos, callback) { var axisOptions = this._axis; return map(pos, function (value, key) { return callback(value, axisOptions[key], key); }); }; AxisManager.prototype.isOutside = function (axes) { return !this.every(axes ? this.get(axes) : this._pos, function (v, opt) { return !isOutside(v, opt.range); }); }; AxisManager.prototype.getAxisOptions = function (key) { return this._axis[key]; }; AxisManager.prototype.setAxis = function (axis) { var _this = this; Object.keys(axis).forEach(function (key) { if (!_this._axis[key]) { throw new Error("Axis ".concat(key, " does not exist in Axes instance")); } _this._axis[key] = __assign(__assign({}, _this._axis[key]), axis[key]); }); this._complementOptions(); }; AxisManager.prototype._complementOptions = function () { var _this = this; Object.keys(this._axis).forEach(function (axis) { _this._axis[axis] = __assign({ range: [0, 100], startPos: _this._axis[axis].range[0], bounce: [0, 0], circular: [false, false], }, _this._axis[axis]); ["bounce", "circular"].forEach(function (v) { var axisOption = _this._axis; var key = axisOption[axis][v]; if (/string|number|boolean/.test(typeof key)) { axisOption[axis][v] = [key, key]; } }); }); }; return AxisManager; }()); export { AxisManager }; //# sourceMappingURL=AxisManager.js.map