choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
248 lines (213 loc) • 7.16 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import { cloneElement, Component, isValidElement } from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import noop from 'lodash/noop';
import { getDocument } from '../../pro/es/_util/DocumentUtils';
import domAlign from './domAlign';
import EventManager from '../_util/EventManager';
import TaskRunner from '../_util/TaskRunner';
function isWindow(obj) {
return obj != null && obj === obj.window;
}
function isChildrenFunction(fn) {
return typeof fn === 'function';
}
var Align =
/*#__PURE__*/
function (_Component) {
_inherits(Align, _Component);
var _super = _createSuper(Align);
function Align() {
var _this;
_classCallCheck(this, Align);
_this = _super.apply(this, arguments);
_this.saveSourceRef = function (node) {
_this.source = node;
var childrenRef = _this.props.childrenRef;
if (childrenRef) {
childrenRef(node);
}
};
return _this;
}
_createClass(Align, [{
key: "forceAlign",
value: function forceAlign() {
var _this$props = this.props,
hidden = _this$props.hidden,
_this$props$onAlign = _this$props.onAlign,
onAlign = _this$props$onAlign === void 0 ? noop : _this$props$onAlign,
_this$props$target = _this$props.target,
target = _this$props$target === void 0 ? function () {
return getDocument(window).body;
} : _this$props$target,
align = _this$props.align;
if (!hidden) {
var _this$source = this.source,
source = _this$source === void 0 ? findDOMNode(this) : _this$source;
var ref = target();
var result = domAlign(source, ref, align);
var translate = {
x: 0,
y: 0
};
var points = result.points,
_result$overflow = result.overflow,
adjustX = _result$overflow.adjustX,
adjustY = _result$overflow.adjustY;
if (source && ref && (adjustX || adjustY) && (points.includes('bc') || points.includes('tc'))) {
var r1 = source.getBoundingClientRect();
var r2 = ref.getBoundingClientRect();
if (adjustX) {
translate.x = r1.left + r1.width / 2 - r2.left - r2.width / 2;
}
if (adjustY) {
translate.y = r1.top + r1.height / 2 - r2.top - r2.height / 2;
}
}
onAlign(source, result, ref, translate);
}
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var _this$props2 = this.props,
hidden = _this$props2.hidden,
monitorWindowResize = _this$props2.monitorWindowResize;
this.forceAlign();
if (!hidden && monitorWindowResize) {
this.startMonitorWindowResize();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props3 = this.props,
hidden = _this$props3.hidden,
align = _this$props3.align,
_this$props3$target = _this$props3.target,
target = _this$props3$target === void 0 ? function () {
return window;
} : _this$props3$target,
monitorWindowResize = _this$props3.monitorWindowResize;
var preHidden = prevProps.hidden,
preAlign = prevProps.align,
preTarget = prevProps.target;
var reAlign = false;
if (!hidden) {
if (preHidden || preAlign !== align) {
reAlign = true;
} else {
var lastTarget = preTarget();
var currentTarget = target();
if (isWindow(lastTarget) && isWindow(currentTarget)) {
reAlign = false;
} else if (lastTarget !== currentTarget) {
reAlign = true;
}
}
}
if (reAlign) {
this.forceAlign();
}
if (monitorWindowResize && !hidden) {
this.startMonitorWindowResize();
} else {
this.stopMonitorWindowResize();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.stopMonitorWindowResize();
}
}, {
key: "startMonitorWindowResize",
value: function startMonitorWindowResize() {
var monitorBufferTime = this.props.monitorBufferTime;
if (!this.resizeHandler) {
this.resizeHandler = new EventManager(window);
this.bufferMonitor = new TaskRunner();
this.resizeHandler.addEventListener('resize', this.bufferMonitor.delay.bind(this.bufferMonitor, monitorBufferTime, this.forceAlign.bind(this)));
}
}
}, {
key: "stopMonitorWindowResize",
value: function stopMonitorWindowResize() {
if (this.resizeHandler) {
if (this.bufferMonitor) {
this.bufferMonitor.cancel();
}
this.resizeHandler.clear();
this.resizeHandler = null;
}
}
}, {
key: "render",
value: function render() {
var props = this.props;
var childrenProps = props.childrenProps,
children = props.children;
var node = isChildrenFunction(children) ? children(this.saveSourceRef) : children;
if (childrenProps && isValidElement(node)) {
var newProps = {};
Object.keys(childrenProps).forEach(function (prop) {
if ({}.hasOwnProperty.call(childrenProps, prop)) {
newProps[prop] = props[childrenProps[prop]];
}
});
return cloneElement(node, newProps);
}
return node;
}
}]);
return Align;
}(Component);
export { Align as default };
Align.displayName = 'Align';
Align.propTypes = {
childrenProps: PropTypes.object,
childrenRef: PropTypes.func,
align: PropTypes.object.isRequired,
target: PropTypes.func,
onAlign: PropTypes.func,
monitorBufferTime: PropTypes.number,
monitorWindowResize: PropTypes.bool,
hidden: PropTypes.bool,
children: PropTypes.any
};
Align.defaultProps = {
monitorBufferTime: 50,
monitorWindowResize: false,
hidden: true
};
//# sourceMappingURL=Align.js.map