yqcloud-ui
Version:
An enterprise-class UI design language and React-based implementation
152 lines (128 loc) • 4.19 kB
JavaScript
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import align from 'dom-align';
import addEventListener from '../util/Dom/addEventListener';
import shallowequal from 'shallowequal';
import isWindow from './isWindow';
function buffer(fn, ms) {
var timer = void 0;
function clear() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
function bufferFn() {
clear();
timer = setTimeout(fn, ms);
}
bufferFn.clear = clear;
return bufferFn;
}
var Align = function (_Component) {
_inherits(Align, _Component);
function Align() {
var _temp, _this, _ret;
_classCallCheck(this, Align);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.forceAlign = function () {
var props = _this.props;
if (!props.disabled) {
var source = ReactDOM.findDOMNode(_this);
props.onAlign(source, align(source, props.target(), props.align));
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
Align.prototype.componentDidMount = function componentDidMount() {
var props = this.props;
// if parent ref not attached .... use document.getElementById
this.forceAlign();
if (!props.disabled && props.monitorWindowResize) {
this.startMonitorWindowResize();
}
};
Align.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
var reAlign = false;
var props = this.props;
if (!props.disabled) {
if (prevProps.disabled || !shallowequal(prevProps.align, props.align)) {
reAlign = true;
} else {
var lastTarget = prevProps.target();
var currentTarget = props.target();
if (isWindow(lastTarget) && isWindow(currentTarget)) {
reAlign = false;
} else if (lastTarget !== currentTarget) {
reAlign = true;
}
}
}
if (reAlign) {
this.forceAlign();
}
if (props.monitorWindowResize && !props.disabled) {
this.startMonitorWindowResize();
} else {
this.stopMonitorWindowResize();
}
};
Align.prototype.componentWillUnmount = function componentWillUnmount() {
this.stopMonitorWindowResize();
};
Align.prototype.startMonitorWindowResize = function startMonitorWindowResize() {
if (!this.resizeHandler) {
this.bufferMonitor = buffer(this.forceAlign, this.props.monitorBufferTime);
this.resizeHandler = addEventListener(window, 'resize', this.bufferMonitor);
}
};
Align.prototype.stopMonitorWindowResize = function stopMonitorWindowResize() {
if (this.resizeHandler) {
this.bufferMonitor.clear();
this.resizeHandler.remove();
this.resizeHandler = null;
}
};
Align.prototype.render = function render() {
var _props = this.props,
childrenProps = _props.childrenProps,
children = _props.children;
var child = React.Children.only(children);
if (childrenProps) {
var newProps = {};
for (var prop in childrenProps) {
if (childrenProps.hasOwnProperty(prop)) {
newProps[prop] = this.props[childrenProps[prop]];
}
}
return React.cloneElement(child, newProps);
}
return child;
};
return Align;
}(Component);
Align.propTypes = {
childrenProps: PropTypes.object,
align: PropTypes.object.isRequired,
target: PropTypes.func,
onAlign: PropTypes.func,
monitorBufferTime: PropTypes.number,
monitorWindowResize: PropTypes.bool,
disabled: PropTypes.bool,
children: PropTypes.any
};
Align.defaultProps = {
target: function target() {
return window;
},
onAlign: function onAlign() {},
monitorBufferTime: 50,
monitorWindowResize: false,
disabled: false
};
export default Align;