UNPKG

@shakthillc/components

Version:

React generic components for shakthi products

292 lines (262 loc) 10.3 kB
import _extends from "babel-runtime/helpers/extends"; import _toConsumableArray from "babel-runtime/helpers/toConsumableArray"; import _Object$keys from "babel-runtime/core-js/object/keys"; import _Object$getPrototypeOf from "babel-runtime/core-js/object/get-prototype-of"; import _classCallCheck from "babel-runtime/helpers/classCallCheck"; import _createClass from "babel-runtime/helpers/createClass"; import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn"; import _inherits from "babel-runtime/helpers/inherits"; /**** Libraries ****/ import React from "react"; import hoistStatics from "hoist-non-react-statics"; /**** Methods ****/ import viewPort from "./viewPort"; var lastOpenedGroup = []; var popups = {}; global.closeGroupPopups = function (groupName) { var groupPopups = popups[groupName] || []; groupPopups.forEach(function (popup) { popup.state.isPopupOpen && popup.setState({ isPopupOpen: false, isPopupReady: false }); }); }; /* eslint-disable react/no-deprecated */ /* eslint-disable react/prop-types */ var Popup = function Popup(Component) { var group = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "global"; var needResizeHandling = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; var Popup = function (_React$Component) { _inherits(Popup, _React$Component); function Popup(props) { _classCallCheck(this, Popup); var _this = _possibleConstructorReturn(this, (Popup.__proto__ || _Object$getPrototypeOf(Popup)).call(this, props)); _this.state = { isPopupOpen: props.isPopupOpen || false, position: "bottomCenter", height: "0px", isPopupReady: props.isPopupOpen || false, adjustments: {} }; _this.togglePopup = _this.togglePopup.bind(_this); _this.documentClickHandler = _this.documentClickHandler.bind(_this); _this.removeClose = _this.removeClose.bind(_this); _this.documentKeyupHandler = _this.documentKeyupHandler.bind(_this); _this.openPopupOnly = _this.openPopupOnly.bind(_this); _this.closePopupOnly = _this.closePopupOnly.bind(_this); _this.getTargetRef = _this.getTargetRef.bind(_this); _this.getContainerRef = _this.getContainerRef.bind(_this); _this.handlePopupPosition = _this.handlePopupPosition.bind(_this); _this.size = null; return _this; } _createClass(Popup, [{ key: "componentDidMount", value: function componentDidMount() { var group = this.getGroup(); var groupPopups = popups[group] || []; groupPopups.push(this); popups[group] = groupPopups; if (_Object$keys(popups).length === 1 && groupPopups.length === 1) { document.addEventListener("click", this.documentClickHandler, false); document.addEventListener("keyup", this.documentKeyupHandler, false); } } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var isPopupOpen = this.state.isPopupOpen; if (typeof nextProps.isPopupOpen !== "undefined" && nextProps.isPopupOpen !== isPopupOpen) { this.setState({ isPopupOpen: nextProps.isPopupOpen }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this2 = this; var group = this.getGroup(); popups = _Object$keys(popups).reduce(function (res, groupName) { if (groupName === group) { var groupPopups = popups[group]; var newGroupPopups = groupPopups.filter(function (popup) { return popup !== _this2; }); newGroupPopups.length === 0 && lastOpenedGroup.indexOf(group) >= 0 && lastOpenedGroup.splice(lastOpenedGroup.indexOf(group), 1); res[group] = newGroupPopups; } return res; }, popups); var noPopups = true; for (var i in popups) { if (popups[i].length >= 1) { noPopups = false; break; } } if (noPopups) { document.removeEventListener("click", this.documentClickHandler); document.removeEventListener("keyup", this.documentKeyupHandler); } } }, { key: "getGroup", value: function getGroup() { var popupGroup = this.props.popupGroup; return popupGroup || group; } }, { key: "togglePopup", value: function togglePopup(e, defaultPosition) { var _this3 = this; var group = this.getGroup(); this.removeClose(e); var isPopupOpen = this.state.isPopupOpen; var groupPopups = popups[group]; lastOpenedGroup = !isPopupOpen && lastOpenedGroup.indexOf(group) === -1 ? [group].concat(_toConsumableArray(lastOpenedGroup)) : lastOpenedGroup; isPopupOpen && lastOpenedGroup.splice(0, 1); groupPopups.forEach(function (popup) { if (popup !== _this3 && popup.state.isPopupOpen) { popup.setState({ isPopupOpen: false, isPopupReady: false }); } }); if (isPopupOpen) { this.setState({ isPopupOpen: false, isPopupReady: false }); } else { this.handlePopupPosition(defaultPosition); } } }, { key: "openPopupOnly", value: function openPopupOnly(e, defaultPosition) { var group = this.getGroup(); this.removeClose(e); lastOpenedGroup = lastOpenedGroup.indexOf(group) === -1 ? [group].concat(_toConsumableArray(lastOpenedGroup)) : lastOpenedGroup; this.handlePopupPosition(defaultPosition); } }, { key: "closePopupOnly", value: function closePopupOnly(e) { this.removeClose(e); lastOpenedGroup.splice(0, 1); var isPopupOpen = this.state.isPopupOpen; if (isPopupOpen) { this.setState({ isPopupOpen: false, isPopupReady: false }); } } }, { key: "documentClickHandler", value: function documentClickHandler() { try { _Object$keys(popups).forEach(function (groupName) { var groupPopups = popups[groupName]; groupPopups.forEach(function (popup) { popup.state.isPopupOpen && (!popup.props.checkBeforeClose || popup.props.checkBeforeClose && popup.props.checkBeforeClose()) && popup.setState({ isPopupOpen: false, isPopupReady: false }); }); }); lastOpenedGroup = []; } catch (e) { // eslint-disable-next-line no-console //console.error('popup component not unmounted properly', e); } } }, { key: "documentKeyupHandler", value: function documentKeyupHandler(e) { try { if (e.keyCode === 27) { var groupPopups = lastOpenedGroup.length ? popups[lastOpenedGroup[0]] : []; lastOpenedGroup.splice(0, 1); groupPopups.forEach(function (popup) { popup.state.isPopupOpen && popup.setState({ isPopupOpen: false, isPopupReady: false }); }); } } catch (e) { // eslint-disable-next-line no-console //console.log('error', e); } } }, { key: "removeClose", value: function removeClose(e) { e && e.stopPropagation && e.stopPropagation(); e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation(); } }, { key: "handlePopupPosition", value: function handlePopupPosition() { var _this4 = this; var defaultPosition = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "bottomCenter"; var isResizeHandling = arguments[1]; // isResizeHandling --->>> Window resize and dropBox resize var placeHolderElement = this.placeHolderElement, dropElement = this.dropElement; if (!placeHolderElement && !dropElement) { this.setState({ isPopupOpen: true, isPopupReady: true }); return; } var setPosition = function setPosition() { requestAnimationFrame(function () { var placeHolderElement = _this4.placeHolderElement, dropElement = _this4.dropElement; var _state = _this4.state, position = _state.position, isPopupReady = _state.isPopupReady; var scrollContainer = placeHolderElement.closest("[data-scroll=true]") || document.body; var betterPosition = viewPort.betterView(dropElement, placeHolderElement, defaultPosition, scrollContainer); var _ref = betterPosition || {}, view = _ref.view, adjustments = _ref.adjustments; if (position !== view || !isPopupReady) { _this4.setState({ isPopupReady: true, position: view, adjustments: adjustments }); } }); }; if (isResizeHandling) { setPosition(); } else { this.defaultPosition = defaultPosition; this.setState({ isPopupOpen: true, isPopupReady: false }, setPosition); } } }, { key: "getTargetRef", value: function getTargetRef(el) { this.placeHolderElement = el; } }, { key: "getContainerRef", value: function getContainerRef(el) { this.dropElement = el; } }, { key: "render", value: function render() { return React.createElement(Component, _extends({}, this.props, this.state, { openPopupOnly: this.openPopupOnly, closePopupOnly: this.closePopupOnly, togglePopup: this.togglePopup, removeClose: this.removeClose, getTargetRef: this.getTargetRef, getContainerRef: this.getContainerRef })); } }]); return Popup; }(React.Component); Popup.displayName = Component.displayName || Component.name || Popup.name; return hoistStatics(Popup, Component); }; export default Popup;