UNPKG

@zohodesk/popups

Version:

popups popupover modal dialog alert notifications popup handling for whole app

323 lines (266 loc) 13.7 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.closeGroupPopups = void 0; var _react = _interopRequireDefault(require("react")); var _reactDom = _interopRequireDefault(require("react-dom")); var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics")); var _ViewPort = _interopRequireDefault(require("../ViewPort")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } var popups = {}; var closeGroupPopups = function closeGroupPopups() { var groupName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'global'; var groupPopups = popups[groupName] || []; groupPopups.forEach(function (popup) { popup.state.isPopupOpen && resetPopupState(popup); }); }; exports.closeGroupPopups = closeGroupPopups; var resetPopupState = function resetPopupState(popup) { popup.setState({ isPopupOpen: false, isPopupReady: false, position: 'bottom' }); }; var resetAllPopupState = function resetAllPopupState(popup, filterGroupName) { Object.keys(popups).forEach(closeGroupPopups); }; var Popup = function Popup(Component) { var group = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'global'; var customStyles = arguments.length > 2 ? arguments[2] : undefined; var popupStyles = Object.assign({}, customStyles); group = group || 'global'; var Popup = /*#__PURE__*/function (_React$Component) { _inherits(Popup, _React$Component); var _super = _createSuper(Popup); function Popup(props) { var _this; _classCallCheck(this, Popup); _this = _super.call(this, props); _this.state = { isPopupOpen: props.isPopupOpen || false, isPopupReady: props.isPopupReady || false, position: props.position || 'bottom', fixedPosition: {} }; _this.popupGroup = props.group || group; _this.childGroup = props.childGroup; _this.stopCloseEvent = _this.stopCloseEvent.bind(_assertThisInitialized(_this)); _this.documentKeyupHandler = _this.documentKeyupHandler.bind(_assertThisInitialized(_this)); _this.documentClickHandler = _this.documentClickHandler.bind(_assertThisInitialized(_this)); _this.togglePopup = _this.togglePopup.bind(_assertThisInitialized(_this)); _this.openPopupOnly = _this.openPopupOnly.bind(_assertThisInitialized(_this)); _this.closePopupOnly = _this.closePopupOnly.bind(_assertThisInitialized(_this)); _this.setRef = _this.setRef.bind(_assertThisInitialized(_this)); //this.popupName = popupName; return _this; } _createClass(Popup, [{ key: "setRef", value: function setRef(el) { this.elementRef = el; } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { var state = null; if (typeof nextProps.isPopupOpen !== "undefined" && nextProps.isPopupOpen !== this.state.isPopupOpen) { state = state || {}; state.isPopupOpen = nextProps.isPopupOpen; } if (typeof nextProps.isPopupReady !== "undefined" && nextProps.isPopupReady !== this.state.isPopupReady) { state = state || {}; state.isPopupReady = nextProps.isPopupReady; } if (typeof nextProps.position !== "undefined" && nextProps.position !== this.state.position) { state = state || {}; state.position = nextProps.position; } if (state) { this.setState(state); } } }, { key: "componentDidMount", value: function componentDidMount() { var groupPopups = popups[this.popupGroup] || []; groupPopups.push(this); popups[this.popupGroup] = groupPopups; if (groupPopups.length == 1) { global.document.addEventListener('click', this.documentClickHandler, false); global.document.addEventListener('keyup', this.documentKeyupHandler, false); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this2 = this; popups = Object.keys(popups).reduce(function (res, groupName) { if (groupName == _this2.popupGroup) { var groupPopups = popups[_this2.popupGroup]; var newGroupPopups = groupPopups.filter(function (popup) { return popup !== _this2; }); res[_this2.popupGroup] = newGroupPopups; } return res; }, popups); if (popups.length == 0) { global.document.removeEventListener('click', this.documentClickHandler); global.document.removeEventListener('keyup', this.documentKeyupHandler); } } }, { key: "togglePopup", value: function togglePopup(e, dropElement, placeHoldeEl, isPopupOpen) { var _this3 = this; this.stopCloseEvent(e); //Close the all other opes var groupPopups = popups[this.popupGroup]; groupPopups.forEach(function (popup) { if (popup != _this3) { popup.state.isPopupOpen && popup.setState({ isPopupOpen: false, position: 'bottom' }); } }); this.setState({ isPopupOpen: isPopupOpen ? isPopupOpen : !this.state.isPopupOpen, isPopupReady: false, position: 'bottom' }, function () { if (!dropElement || !placeHoldeEl) { return; } ; requestAnimationFrame(function () { var frame = _this3.props.frameId ? document.getElementById(_this3.props.frameId) : document.getElementById("container"); var defaultPosition = _this3.props.defaultPosition || "bottom"; var betterPosition = _ViewPort["default"].betterView(dropElement, placeHoldeEl, defaultPosition, frame); //Auto predict views if (betterPosition.view) { _this3.setState({ isPopupReady: true, position: betterPosition.view, fixedPosition: betterPosition.fixedPosition }); } else { _this3.setState({ isPopupReady: true, position: 'bottom' }); } }); }); } }, { key: "openPopupOnly", value: function openPopupOnly(e, dropElement, placeHoldeEl) { this.togglePopup(e, dropElement, placeHoldeEl, true); /*this.stopCloseEvent(e); let groupPopups = popups[this.popupGroup]; groupPopups.forEach(popup => { if (popup != this) { popup.state.isPopupOpen && popup.setState({ isPopupOpen: false, position: 'bottom' }); } }); this.setState({ isPopupOpen: true , isPopupReady : false , position: 'bottom' },()=>{ if( !dropElement || !placeHoldeEl ){ return; }; requestAnimationFrame(()=>{ var frame = this.props.frameId ? document.getElementById(this.props.frameId) : document.getElementById("container"); let defaultPosition = this.props.defaultPosition || "bottomCenter"; let betterPosition = viewPort.betterView( dropElement , placeHoldeEl , defaultPosition , frame); //Auto predict views if( betterPosition.view ) { this.setState({ isPopupReady : true , position: betterPosition.view }); }else{ this.setState({ isPopupReady : true , position: defaultPosition }); } }) });*/ } }, { key: "closePopupOnly", value: function closePopupOnly(e) { this.stopCloseEvent(e); resetPopupState(this); } }, { key: "documentClickHandler", value: function documentClickHandler(e) { try { resetAllPopupState(); } catch (e) { console.error('error', e); } } }, { key: "documentKeyupHandler", value: function documentKeyupHandler(e) { try { if (e.keyCode == 27) { resetAllPopupState(); } } catch (e) { console.log('error', e); } } }, { key: "stopCloseEvent", value: function stopCloseEvent(e) { e.stopPropagation && e.stopPropagation(); e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation(); if (this.childGroup) { closeGroupPopups(this.childGroup); } } }, { key: "render", value: function render() { var _this$state = this.state, position = _this$state.position, isPopupOpen = _this$state.isPopupOpen, isPopupReady = _this$state.isPopupReady; var positionStyle = position ? popupStyles[position] || '' : ''; var arrowStyle = "".concat(popupStyles.arrow, " ").concat(positionStyle); var popupOpenStyle = isPopupOpen ? "".concat(popupStyles.isOpen, " ").concat(positionStyle) : "".concat(popupStyles.isHide); var popupReadyStyle = isPopupReady ? "".concat(popupStyles.isReady) : ''; var contentStyle = "".concat(popupStyles.content, " ").concat(popupOpenStyle, " ").concat(popupReadyStyle); return /*#__PURE__*/_react["default"].createElement(Component, _extends({ ref: this.setRef }, this.props, this.state, { openPopupOnly: this.openPopupOnly, closePopupOnly: this.closePopupOnly, togglePopup: this.togglePopup, isPopupOpen: this.state.isPopupOpen, removeClose: this.stopCloseEvent, popupStyle: { arrow: arrowStyle, content: contentStyle }, popupStyles: popupStyles })); } }]); return Popup; }(_react["default"].Component); return (0, _hoistNonReactStatics["default"])(Popup, Component); }; var _default = Popup; exports["default"] = _default;