Files
icehrm/web/node_modules/rc-tabs/lib/SwipeableTabContent.js
2020-05-20 18:47:29 +02:00

244 lines
9.7 KiB
JavaScript
Executable File

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _rcHammerjs = _interopRequireDefault(require("rc-hammerjs"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _TabContent = _interopRequireDefault(require("./TabContent"));
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var RESISTANCE_COEF = 0.6;
function computeIndex(_ref) {
var maxIndex = _ref.maxIndex,
startIndex = _ref.startIndex,
delta = _ref.delta,
viewSize = _ref.viewSize;
var index = startIndex + -delta / viewSize;
if (index < 0) {
index = Math.exp(index * RESISTANCE_COEF) - 1;
} else if (index > maxIndex) {
index = maxIndex + 1 - Math.exp((maxIndex - index) * RESISTANCE_COEF);
}
return index;
}
function getIndexByDelta(e) {
var delta = (0, _utils.isVertical)(this.props.tabBarPosition) ? e.deltaY : e.deltaX;
var otherDelta = (0, _utils.isVertical)(this.props.tabBarPosition) ? e.deltaX : e.deltaY;
if (Math.abs(delta) < Math.abs(otherDelta)) {
return undefined;
}
var currentIndex = computeIndex({
maxIndex: this.maxIndex,
viewSize: this.viewSize,
startIndex: this.startIndex,
delta: delta
});
var showIndex = delta < 0 ? Math.floor(currentIndex + 1) : Math.floor(currentIndex);
if (showIndex < 0) {
showIndex = 0;
} else if (showIndex > this.maxIndex) {
showIndex = this.maxIndex;
}
if (this.children[showIndex].props.disabled) {
return undefined;
}
return currentIndex;
}
var SwipeableTabContent = /*#__PURE__*/function (_React$Component) {
_inherits(SwipeableTabContent, _React$Component);
function SwipeableTabContent() {
var _getPrototypeOf2;
var _this;
_classCallCheck(this, SwipeableTabContent);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(SwipeableTabContent)).call.apply(_getPrototypeOf2, [this].concat(args)));
_this.onPanStart = function () {
var _this$props = _this.props,
tabBarPosition = _this$props.tabBarPosition,
children = _this$props.children,
activeKey = _this$props.activeKey,
animated = _this$props.animated;
_this.startIndex = (0, _utils.getActiveIndex)(children, activeKey);
var _assertThisInitialize = _assertThisInitialized(_this),
startIndex = _assertThisInitialize.startIndex;
if (startIndex === -1) {
return;
}
if (animated) {
(0, _utils.setTransition)(_this.rootNode.style, 'none');
}
_this.startDrag = true;
_this.children = (0, _utils.toArray)(children);
_this.maxIndex = _this.children.length - 1;
_this.viewSize = (0, _utils.isVertical)(tabBarPosition) ? _this.rootNode.offsetHeight : _this.rootNode.offsetWidth;
};
_this.onPan = function (e) {
if (!_this.startDrag) {
return;
}
var tabBarPosition = _this.props.tabBarPosition;
var currentIndex = getIndexByDelta.call(_assertThisInitialized(_this), e);
if (currentIndex !== undefined) {
(0, _utils.setTransform)(_this.rootNode.style, (0, _utils.getTransformByIndex)(currentIndex, tabBarPosition));
}
};
_this.onPanEnd = function (e) {
if (!_this.startDrag) {
return;
}
_this.end(e);
};
_this.onSwipe = function (e) {
_this.end(e, true);
};
_this.end = function (e, swipe) {
var _this$props2 = _this.props,
tabBarPosition = _this$props2.tabBarPosition,
animated = _this$props2.animated;
_this.startDrag = false;
if (animated) {
(0, _utils.setTransition)(_this.rootNode.style, '');
}
var currentIndex = getIndexByDelta.call(_assertThisInitialized(_this), e);
var finalIndex = _this.startIndex;
if (currentIndex !== undefined) {
if (currentIndex < 0) {
finalIndex = 0;
} else if (currentIndex > _this.maxIndex) {
finalIndex = _this.maxIndex;
} else if (swipe) {
var delta = (0, _utils.isVertical)(tabBarPosition) ? e.deltaY : e.deltaX;
finalIndex = delta < 0 ? Math.ceil(currentIndex) : Math.floor(currentIndex);
} else {
var floorIndex = Math.floor(currentIndex);
if (currentIndex - floorIndex > 0.6) {
finalIndex = floorIndex + 1;
} else {
finalIndex = floorIndex;
}
}
}
if (_this.children[finalIndex].props.disabled) {
return;
}
if (_this.startIndex === finalIndex) {
if (animated) {
(0, _utils.setTransform)(_this.rootNode.style, (0, _utils.getTransformByIndex)(finalIndex, _this.props.tabBarPosition));
}
} else {
_this.props.onChange((0, _utils.getActiveKey)(_this.props.children, finalIndex));
}
};
return _this;
}
_createClass(SwipeableTabContent, [{
key: "componentDidMount",
value: function componentDidMount() {
this.rootNode = _reactDom.default.findDOMNode(this);
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
tabBarPosition = _this$props3.tabBarPosition,
hammerOptions = _this$props3.hammerOptions,
animated = _this$props3.animated;
var events = {
onSwipe: this.onSwipe,
onPanStart: this.onPanStart
};
if (animated !== false) {
events = _objectSpread({}, events, {
onPan: this.onPan,
onPanEnd: this.onPanEnd
});
}
return _react.default.createElement(_rcHammerjs.default, _extends({}, events, {
direction: (0, _utils.isVertical)(tabBarPosition) ? 'DIRECTION_ALL' : 'DIRECTION_HORIZONTAL',
options: hammerOptions
}), _react.default.createElement(_TabContent.default, this.props));
}
}]);
return SwipeableTabContent;
}(_react.default.Component);
exports.default = SwipeableTabContent;
SwipeableTabContent.defaultProps = {
animated: true
};