UNPKG

react-divx

Version:
212 lines (160 loc) 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireDefault(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } 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); } 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; } var fullAreaStyle = { height: '100%', width: '100%' }; var DivX = /*#__PURE__*/ function (_React$Component) { _inherits(DivX, _React$Component); function DivX(props) { var _this; _classCallCheck(this, DivX); _this = _possibleConstructorReturn(this, _getPrototypeOf(DivX).call(this, props)); _defineProperty(_assertThisInitialized(_this), "componentDidMount", function () { if (_this.props.onOuterClick || _this.props.onDocumentClick) document.addEventListener('click', _this.onDocumentClick); if (_this.props.onEscPress || _this.props.onDocumentKeydown) document.addEventListener('keydown', _this.onDocumentKeydown); if (_this.props.onResize) _this.onResize(); // dynamics handlers attach Object.keys(_this.getDynamicProps()).forEach(function (dynamicProp) { var key = dynamicProp.toLowerCase(); if (dynamicProp.indexOf('onWindow') === 0 && key.replace('onwindow', '').length > 0) { window.addEventListener(key.replace('onwindow', ''), _this.createDynamicHandler(dynamicProp)); } if (dynamicProp.indexOf('onDocument') === 0 && key.replace('ondocument', '').length > 0) { document.addEventListener(key.replace('ondocument', ''), _this.createDynamicHandler(dynamicProp)); } }); }); _defineProperty(_assertThisInitialized(_this), "componentDidUpdate", function () { if (_this.props.onResize) _this.onResize(); }); _defineProperty(_assertThisInitialized(_this), "componentWillUnmount", function () { if (_this.props.onOuterClick || _this.props.onDocumentClick) document.removeEventListener('click', _this.onDocumentClick); if (_this.props.onEscPress || _this.props.onDocumentKeydown) document.removeEventListener('keydown', _this.onDocumentKeydown); // dynamics handlers remove Object.keys(_this.dynamics).forEach(function (dynamicProp) { var key = dynamicProp.toLowerCase(); if (dynamicProp.indexOf('onWindow') === 0 && key.replace('onwindow', '').length > 0) { window.removeEventListener(key.replace('onwindow', ''), _this.dynamics[dynamicProp]); } if (dynamicProp.indexOf('onDocument') === 0 && key.replace('ondocument', '').length > 0) { document.removeEventListener(key.replace('ondocument', ''), _this.dynamics[dynamicProp]); } }); }); _defineProperty(_assertThisInitialized(_this), "onDocumentKeydown", function (event) { if (event.keyCode === 27) { if (typeof _this.props.onEscPress === 'function') _this.props.onEscPress(event); } if (typeof _this.props.onDocumentKeydown === 'function') _this.props.onDocumentKeydown(event); }); _defineProperty(_assertThisInitialized(_this), "onResize", function () { if (_this.container) { var _this$container = _this.container, clientHeight = _this$container.clientHeight, clientWidth = _this$container.clientWidth; if (clientWidth !== _this.clientWidth || clientHeight !== _this.clientHeight) { _this.clientHeight = clientHeight; _this.clientWidth = clientWidth; if (typeof _this.props.onResize === 'function') _this.props.onResize(clientWidth, clientHeight); } } }); _defineProperty(_assertThisInitialized(_this), "onDocumentClick", function (e) { if (_this.container && !_this.container.contains(e.target)) { if (typeof _this.props.onOuterClick === 'function') _this.props.onOuterClick(e); } if (typeof _this.props.onDocumentClick === 'function') _this.props.onDocumentClick(e); }); _defineProperty(_assertThisInitialized(_this), "getDynamicProps", function () { // eslint-disable-next-line var _this$props = _this.props, innerRef = _this$props.innerRef, onOuterClick = _this$props.onOuterClick, onResize = _this$props.onResize, onEscPress = _this$props.onEscPress, onDocumentKeydown = _this$props.onDocumentKeydown, onDocumentClick = _this$props.onDocumentClick, rest = _objectWithoutProperties(_this$props, ["innerRef", "onOuterClick", "onResize", "onEscPress", "onDocumentKeydown", "onDocumentClick"]); return rest; }); _defineProperty(_assertThisInitialized(_this), "getRealDivProps", function () { var rest = _this.getDynamicProps(); Object.keys(rest).forEach(function (dynamicProp) { var key = dynamicProp.toLowerCase(); if (key.indexOf('onwindow') === 0 || key.indexOf('ondocument') === 0) { delete rest[dynamicProp]; } }); return rest; }); _defineProperty(_assertThisInitialized(_this), "setRef", function (ref) { _this.container = ref; if (_this.props.innerRef) { _this.props.innerRef(ref); } }); _defineProperty(_assertThisInitialized(_this), "createDynamicHandler", function (dynamicProp) { _this.dynamics[dynamicProp] = function () { var _this$props2; if (_this.props[dynamicProp]) (_this$props2 = _this.props)[dynamicProp].apply(_this$props2, arguments); }; return _this.dynamics[dynamicProp]; }); _this.state = {}; _this.clientHeight = 0; _this.clientWidth = 0; _this.dynamics = {}; return _this; } _createClass(DivX, [{ key: "render", value: function render() { var _this$props3 = this.props, fullArea = _this$props3.fullArea, children = _this$props3.children, onOuterClick = _this$props3.onOuterClick, onResize = _this$props3.onResize; var realDivProps = this.getRealDivProps(); var needRealDiv = onOuterClick || onResize || Object.keys(realDivProps).length > 0; return needRealDiv ? // eslint-disable-next-line react/jsx-props-no-spreading _react["default"].createElement("div", _extends({ ref: this.setRef }, realDivProps, { style: fullArea ? fullAreaStyle : undefined }), children) : children; } }]); return DivX; }(_react["default"].Component); DivX.propTypes = { innerRef: _propTypes["default"].func, onOuterClick: _propTypes["default"].func, onEscPress: _propTypes["default"].func, onResize: _propTypes["default"].func, onDocumentKeydown: _propTypes["default"].func, onDocumentClick: _propTypes["default"].func, children: _propTypes["default"].any.isRequired, fullArea: _propTypes["default"].bool }; var _default = DivX; exports["default"] = _default;