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

303 lines
13 KiB
JavaScript
Executable File

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _classnames3 = _interopRequireDefault(require("classnames"));
var _rcHammerjs = _interopRequireDefault(require("rc-hammerjs"));
var _reactDom = _interopRequireDefault(require("react-dom"));
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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 SwipeableTabBarNode = /*#__PURE__*/function (_React$Component) {
_inherits(SwipeableTabBarNode, _React$Component);
function SwipeableTabBarNode(props) {
var _this;
_classCallCheck(this, SwipeableTabBarNode);
_this = _possibleConstructorReturn(this, _getPrototypeOf(SwipeableTabBarNode).call(this, props));
_this.onPan = function (e) {
var _this$cache = _this.cache,
vertical = _this$cache.vertical,
totalAvaliableDelta = _this$cache.totalAvaliableDelta,
totalDelta = _this$cache.totalDelta;
var speed = _this.props.speed; // calculate touch distance
var nowDelta = vertical ? e.deltaY : e.deltaX;
nowDelta *= speed / 10; // calculate distance dom need transform
var _nextDelta = nowDelta + totalDelta;
if (_this.isRtl()) {
// calculate distance from right when direction is right-to-left
if (_nextDelta <= 0) {
_nextDelta = 0;
} else if (_nextDelta >= totalAvaliableDelta) {
_nextDelta = totalAvaliableDelta;
}
} // calculate distance from left when direction is left-to-right
else if (_nextDelta >= 0) {
_nextDelta = 0;
} else if (_nextDelta <= -totalAvaliableDelta) {
_nextDelta = -totalAvaliableDelta;
}
_this.cache.totalDelta = _nextDelta;
_this.setSwipePosition(); // calculate pagination display
var _this$checkPagination = _this.checkPaginationByDelta(_this.cache.totalDelta),
hasPrevPage = _this$checkPagination.hasPrevPage,
hasNextPage = _this$checkPagination.hasNextPage;
if (hasPrevPage !== _this.state.hasPrevPage || hasNextPage !== _this.state.hasNextPage) {
_this.setState({
hasPrevPage: hasPrevPage,
hasNextPage: hasNextPage
});
}
};
var _this$checkPagination2 = _this.checkPaginationByKey(_this.props.activeKey),
_hasPrevPage = _this$checkPagination2.hasPrevPage,
_hasNextPage = _this$checkPagination2.hasNextPage;
_this.state = {
hasPrevPage: _hasPrevPage,
hasNextPage: _hasNextPage
};
return _this;
}
_createClass(SwipeableTabBarNode, [{
key: "componentDidMount",
value: function componentDidMount() {
var swipe = this.props.getRef('swipe');
var nav = this.props.getRef('nav');
var activeKey = this.props.activeKey;
this.swipeNode = _reactDom.default.findDOMNode(swipe); // dom which scroll (9999px)
this.realNode = _reactDom.default.findDOMNode(nav); // dom which visiable in screen (viewport)
this.setCache();
this.setSwipePositionByKey(activeKey);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
this.setCache();
if (this.props.activeKey && this.props.activeKey !== prevProps.activeKey || this.props.panels.length !== prevProps.panels.length || this.props.pageSize !== prevProps.pageSize) {
this.setSwipePositionByKey(this.props.activeKey);
}
}
}, {
key: "setCache",
value: function setCache() {
var _this$props = this.props,
tabBarPosition = _this$props.tabBarPosition,
pageSize = _this$props.pageSize,
panels = _this$props.panels;
var _isVertical = (0, _utils.isVertical)(tabBarPosition);
var _viewSize = (0, _utils.getStyle)(this.realNode, _isVertical ? 'height' : 'width');
var _tabWidth = _viewSize / pageSize;
this.cache = _objectSpread({}, this.cache, {
vertical: _isVertical,
totalAvaliableDelta: _tabWidth * panels.length - _viewSize,
tabWidth: _tabWidth
});
}
/**
* used for props.activeKey setting, not for swipe callback
*/
}, {
key: "getDeltaByKey",
value: function getDeltaByKey(activeKey) {
var pageSize = this.props.pageSize;
var index = this.getIndexByKey(activeKey);
var centerTabCount = Math.floor(pageSize / 2);
var tabWidth = this.cache.tabWidth;
var delta = (index - centerTabCount) * tabWidth; // in rtl direction tabs are ordered from right to left, so delta should be positive in order to
// push swiped element to righ side (start of view)
if (!this.isRtl()) {
delta *= -1;
}
return delta;
}
}, {
key: "getIndexByKey",
value: function getIndexByKey(activeKey) {
var panels = this.props.panels;
var length = panels.length;
for (var i = 0; i < length; i++) {
if (panels[i].key === activeKey) {
return i;
}
}
return -1;
}
}, {
key: "setSwipePositionByKey",
value: function setSwipePositionByKey(activeKey) {
var _this$checkPagination3 = this.checkPaginationByKey(activeKey),
hasPrevPage = _this$checkPagination3.hasPrevPage,
hasNextPage = _this$checkPagination3.hasNextPage;
var totalAvaliableDelta = this.cache.totalAvaliableDelta;
this.setState({
hasPrevPage: hasPrevPage,
hasNextPage: hasNextPage
});
var delta;
if (!hasPrevPage) {
// the first page
delta = 0;
} else if (!hasNextPage) {
// the last page
delta = this.isRtl() ? totalAvaliableDelta : -totalAvaliableDelta;
} else if (hasNextPage) {
// the middle page
delta = this.getDeltaByKey(activeKey);
}
this.cache.totalDelta = delta;
this.setSwipePosition();
}
}, {
key: "setSwipePosition",
value: function setSwipePosition() {
var _this$cache2 = this.cache,
totalDelta = _this$cache2.totalDelta,
vertical = _this$cache2.vertical;
(0, _utils.setPxStyle)(this.swipeNode, totalDelta, vertical);
}
}, {
key: "checkPaginationByKey",
value: function checkPaginationByKey(activeKey) {
var _this$props2 = this.props,
panels = _this$props2.panels,
pageSize = _this$props2.pageSize;
var index = this.getIndexByKey(activeKey);
var centerTabCount = Math.floor(pageSize / 2); // the basic rule is to make activeTab be shown in the center of TabBar viewport
return {
hasPrevPage: index - centerTabCount > 0,
hasNextPage: index + centerTabCount < panels.length
};
}
}, {
key: "checkPaginationByDelta",
value: function checkPaginationByDelta(delta) {
var totalAvaliableDelta = this.cache.totalAvaliableDelta;
return {
hasPrevPage: delta < 0,
hasNextPage: -delta < totalAvaliableDelta
};
}
}, {
key: "isRtl",
value: function isRtl() {
return this.props.direction === 'rtl';
}
}, {
key: "render",
value: function render() {
var _classnames2;
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
hammerOptions = _this$props3.hammerOptions,
tabBarPosition = _this$props3.tabBarPosition;
var _this$state = this.state,
hasPrevPage = _this$state.hasPrevPage,
hasNextPage = _this$state.hasNextPage;
var navClassName = "".concat(prefixCls, "-nav");
var navClasses = (0, _classnames3.default)(_defineProperty({}, navClassName, true));
var events = {
onPan: this.onPan
};
return _react.default.createElement("div", {
className: (0, _classnames3.default)((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefixCls, "-nav-container"), 1), _defineProperty(_classnames2, "".concat(prefixCls, "-nav-swipe-container"), 1), _defineProperty(_classnames2, "".concat(prefixCls, "-prevpage"), hasPrevPage), _defineProperty(_classnames2, "".concat(prefixCls, "-nextpage"), hasNextPage), _classnames2)),
key: "container",
ref: this.props.saveRef('container')
}, _react.default.createElement("div", {
className: "".concat(prefixCls, "-nav-wrap"),
ref: this.props.saveRef('navWrap')
}, _react.default.createElement(_rcHammerjs.default, _extends({}, events, {
direction: (0, _utils.isVertical)(tabBarPosition) ? 'DIRECTION_ALL' : 'DIRECTION_HORIZONTAL',
options: hammerOptions
}), _react.default.createElement("div", {
className: "".concat(prefixCls, "-nav-swipe"),
ref: this.props.saveRef('swipe')
}, _react.default.createElement("div", {
className: navClasses,
ref: this.props.saveRef('nav')
}, this.props.children)))));
}
}]);
return SwipeableTabBarNode;
}(_react.default.Component);
exports.default = SwipeableTabBarNode;
SwipeableTabBarNode.defaultProps = {
panels: null,
tabBarPosition: 'top',
prefixCls: '',
children: null,
hammerOptions: {},
pageSize: 5,
// per page show how many tabs
speed: 7,
// swipe speed, 1 to 10, more bigger more faster
saveRef: function saveRef() {},
getRef: function getRef() {}
};