choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
350 lines (289 loc) • 11.4 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var _tslib = require("tslib");
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _classnames = _interopRequireDefault(require("classnames"));
var _shallowequal = _interopRequireDefault(require("shallowequal"));
var _omit = _interopRequireDefault(require("lodash/omit"));
var _noop = _interopRequireDefault(require("lodash/noop"));
var _getScroll = _interopRequireDefault(require("../_util/getScroll"));
var _throttleByAnimationFrame = require("../_util/throttleByAnimationFrame");
var _addEventListener = _interopRequireDefault(require("../_util/addEventListener"));
var _ConfigContext = _interopRequireDefault(require("../config-provider/ConfigContext"));
function getTargetRect(target) {
return target !== window ? target.getBoundingClientRect() : {
top: 0,
left: 0,
bottom: 0
};
}
function getOffset(element, target) {
var elemRect = element.getBoundingClientRect();
var targetRect = getTargetRect(target);
var scrollTop = (0, _getScroll["default"])(target, true);
var scrollLeft = (0, _getScroll["default"])(target, false);
var docElem = window.document.body;
var clientTop = docElem.clientTop || 0;
var clientLeft = docElem.clientLeft || 0;
return {
top: elemRect.top - targetRect.top + scrollTop - clientTop,
left: elemRect.left - targetRect.left + scrollLeft - clientLeft,
width: elemRect.width,
height: elemRect.height
};
}
function getDefaultTarget() {
return typeof window !== 'undefined' ? window : null;
}
var Affix = /*#__PURE__*/function (_Component) {
(0, _inherits2["default"])(Affix, _Component);
var _super = (0, _createSuper2["default"])(Affix);
function Affix() {
var _this;
(0, _classCallCheck2["default"])(this, Affix);
_this = _super.apply(this, arguments);
_this.state = {
affixStyle: undefined,
placeholderStyle: undefined
};
_this.eventHandlers = {};
_this.events = ['resize', 'scroll', 'touchstart', 'touchmove', 'touchend', 'pageshow', 'load'];
_this.saveFixedNode = function (node) {
_this.fixedNode = node;
};
_this.savePlaceholderNode = function (node) {
_this.placeholderNode = node;
};
return _this;
}
(0, _createClass2["default"])(Affix, [{
key: "setAffixStyle",
value: function setAffixStyle(e, affixStyle) {
var _this2 = this;
var _this$props = this.props,
_this$props$onChange = _this$props.onChange,
onChange = _this$props$onChange === void 0 ? _noop["default"] : _this$props$onChange,
_this$props$target = _this$props.target,
target = _this$props$target === void 0 ? getDefaultTarget : _this$props$target;
var originalAffixStyle = this.state.affixStyle;
var isWindow = target() === window;
if (e.type === 'scroll' && originalAffixStyle && affixStyle && isWindow) {
return;
}
if ((0, _shallowequal["default"])(affixStyle, originalAffixStyle)) {
return;
}
this.setState({
affixStyle: affixStyle
}, function () {
if (affixStyle && !originalAffixStyle || !affixStyle && originalAffixStyle) {
var state = _this2.state;
onChange(!!state.affixStyle);
}
});
}
}, {
key: "setPlaceholderStyle",
value: function setPlaceholderStyle(placeholderStyle) {
var originalPlaceholderStyle = this.state.placeholderStyle;
if ((0, _shallowequal["default"])(placeholderStyle, originalPlaceholderStyle)) {
return;
}
this.setState({
placeholderStyle: placeholderStyle
});
}
}, {
key: "syncPlaceholderStyle",
value: function syncPlaceholderStyle(e) {
var affixStyle = this.state.affixStyle;
if (!affixStyle) {
return;
}
this.placeholderNode.style.cssText = '';
this.setAffixStyle(e, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, affixStyle), {}, {
width: this.placeholderNode.offsetWidth
}));
this.setPlaceholderStyle({
width: this.placeholderNode.offsetWidth
});
}
}, {
key: "updatePosition",
value: function updatePosition(e) {
var _this$props2 = this.props,
offsetBottom = _this$props2.offsetBottom,
offset = _this$props2.offset,
_this$props2$target = _this$props2.target,
target = _this$props2$target === void 0 ? getDefaultTarget : _this$props2$target;
var offsetTop = this.props.offsetTop;
var targetNode = target(); // Backwards support
offsetTop = typeof offsetTop === 'undefined' ? offset : offsetTop;
var scrollTop = (0, _getScroll["default"])(targetNode, true);
var affixNode = (0, _reactDom.findDOMNode)(this);
var elemOffset = getOffset(affixNode, targetNode);
var elemSize = {
width: this.fixedNode.offsetWidth,
height: this.fixedNode.offsetHeight
};
var offsetMode = {
top: false,
bottom: false
}; // Default to `offsetTop=0`.
if (typeof offsetTop !== 'number' && typeof offsetBottom !== 'number') {
offsetMode.top = true;
offsetTop = 0;
} else {
offsetMode.top = typeof offsetTop === 'number';
offsetMode.bottom = typeof offsetBottom === 'number';
}
var targetRect = getTargetRect(targetNode);
var targetInnerHeight = targetNode.innerHeight || targetNode.clientHeight;
if (scrollTop > elemOffset.top - offsetTop && offsetMode.top) {
// Fixed Top
var width = elemOffset.width;
var top = targetRect.top + offsetTop;
this.setAffixStyle(e, {
position: 'fixed',
top: top,
left: targetRect.left + elemOffset.left,
width: width
});
this.setPlaceholderStyle({
width: width,
height: elemSize.height
});
} else if (scrollTop < elemOffset.top + elemSize.height + offsetBottom - targetInnerHeight && offsetMode.bottom) {
// Fixed Bottom
var targetBottomOffet = targetNode === window ? 0 : window.innerHeight - targetRect.bottom;
var _width = elemOffset.width;
this.setAffixStyle(e, {
position: 'fixed',
bottom: targetBottomOffet + offsetBottom,
left: targetRect.left + elemOffset.left,
width: _width
});
this.setPlaceholderStyle({
width: _width,
height: elemOffset.height
});
} else {
var affixStyle = this.state.affixStyle;
if (e.type === 'resize' && affixStyle && affixStyle.position === 'fixed' && affixNode.offsetWidth) {
this.setAffixStyle(e, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, affixStyle), {}, {
width: affixNode.offsetWidth
}));
} else {
this.setAffixStyle(e, null);
}
this.setPlaceholderStyle(null);
}
if (e.type === 'resize') {
this.syncPlaceholderStyle(e);
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var _this3 = this;
var props = this.props;
var target = props.target || getDefaultTarget; // Wait for parent component ref has its value
this.timeout = window.setTimeout(function () {
_this3.setTargetEventListeners(target); // Mock Event object.
_this3.updatePosition({});
});
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
var _this$props3 = this.props,
offsetTop = _this$props3.offsetTop,
offsetBottom = _this$props3.offsetBottom,
target = _this$props3.target;
if (target !== nextProps.target) {
this.clearEventListeners();
this.setTargetEventListeners(nextProps.target); // Mock Event object.
this.updatePosition({});
}
if (offsetTop !== nextProps.offsetTop || offsetBottom !== nextProps.offsetBottom) {
this.updatePosition({});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.clearEventListeners();
clearTimeout(this.timeout);
this.updatePosition.cancel();
}
}, {
key: "setTargetEventListeners",
value: function setTargetEventListeners(getTarget) {
var _this4 = this;
var target = getTarget();
if (!target) {
return;
}
this.clearEventListeners();
this.events.forEach(function (eventName) {
_this4.eventHandlers[eventName] = (0, _addEventListener["default"])(target, eventName, _this4.updatePosition);
});
}
}, {
key: "clearEventListeners",
value: function clearEventListeners() {
var _this5 = this;
this.events.forEach(function (eventName) {
var handler = _this5.eventHandlers[eventName];
if (handler && handler.remove) {
handler.remove();
}
});
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
prefixCls = _this$props4.prefixCls,
style = _this$props4.style,
children = _this$props4.children;
var _this$state = this.state,
affixStyle = _this$state.affixStyle,
placeholderStyle = _this$state.placeholderStyle;
var getPrefixCls = this.context.getPrefixCls;
var className = (0, _classnames["default"])((0, _defineProperty2["default"])({}, getPrefixCls('affix', prefixCls), affixStyle));
var props = (0, _omit["default"])(this.props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target', 'onChange']);
return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({}, props, {
style: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, placeholderStyle), style),
ref: this.savePlaceholderNode
}), /*#__PURE__*/_react["default"].createElement("div", {
className: className,
ref: this.saveFixedNode,
style: affixStyle
}, children));
}
}], [{
key: "contextType",
get: function get() {
return _ConfigContext["default"];
}
}]);
return Affix;
}(_react.Component);
exports["default"] = Affix;
Affix.displayName = 'Affix';
(0, _tslib.__decorate)([(0, _throttleByAnimationFrame.throttleByAnimationFrameDecorator)()], Affix.prototype, "updatePosition", null);
//# sourceMappingURL=index.js.map