'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _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; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _PopperContent = require('./PopperContent'); var _PopperContent2 = _interopRequireDefault(_PopperContent); var _utils = require('./utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var propTypes = { placement: _propTypes2.default.oneOf(_utils.PopperPlacements), target: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func, _utils.DOMElement]).isRequired, container: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.func, _utils.DOMElement]), isOpen: _propTypes2.default.bool, disabled: _propTypes2.default.bool, className: _propTypes2.default.string, innerClassName: _propTypes2.default.string, placementPrefix: _propTypes2.default.string, cssModule: _propTypes2.default.object, toggle: _propTypes2.default.func, delay: _propTypes2.default.oneOfType([_propTypes2.default.shape({ show: _propTypes2.default.number, hide: _propTypes2.default.number }), _propTypes2.default.number]) }; var DEFAULT_DELAYS = { show: 0, hide: 0 }; var defaultProps = { isOpen: false, placement: 'right', placementPrefix: 'bs-popover', delay: DEFAULT_DELAYS, toggle: function toggle() {} }; var Popover = function (_React$Component) { _inherits(Popover, _React$Component); function Popover(props) { _classCallCheck(this, Popover); var _this = _possibleConstructorReturn(this, (Popover.__proto__ || Object.getPrototypeOf(Popover)).call(this, props)); _this.addTargetEvents = _this.addTargetEvents.bind(_this); _this.handleDocumentClick = _this.handleDocumentClick.bind(_this); _this.removeTargetEvents = _this.removeTargetEvents.bind(_this); _this.getRef = _this.getRef.bind(_this); _this.toggle = _this.toggle.bind(_this); _this.show = _this.show.bind(_this); _this.hide = _this.hide.bind(_this); return _this; } _createClass(Popover, [{ key: 'componentDidMount', value: function componentDidMount() { this._target = (0, _utils.getTarget)(this.props.target); this.handleProps(); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.handleProps(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.clearShowTimeout(); this.clearHideTimeout(); this.removeTargetEvents(); } }, { key: 'getRef', value: function getRef(ref) { this._popover = ref; } }, { key: 'getDelay', value: function getDelay(key) { var delay = this.props.delay; if ((typeof delay === 'undefined' ? 'undefined' : _typeof(delay)) === 'object') { return isNaN(delay[key]) ? DEFAULT_DELAYS[key] : delay[key]; } return delay; } }, { key: 'handleProps', value: function handleProps() { if (this.props.isOpen) { this.show(); } else { this.hide(); } } }, { key: 'show', value: function show() { this.clearHideTimeout(); this.addTargetEvents(); if (!this.props.isOpen) { this.clearShowTimeout(); this._showTimeout = setTimeout(this.toggle, this.getDelay('show')); } } }, { key: 'hide', value: function hide() { this.clearShowTimeout(); this.removeTargetEvents(); if (this.props.isOpen) { this.clearHideTimeout(); this._hideTimeout = setTimeout(this.toggle, this.getDelay('hide')); } } }, { key: 'clearShowTimeout', value: function clearShowTimeout() { clearTimeout(this._showTimeout); this._showTimeout = undefined; } }, { key: 'clearHideTimeout', value: function clearHideTimeout() { clearTimeout(this._hideTimeout); this._hideTimeout = undefined; } }, { key: 'handleDocumentClick', value: function handleDocumentClick(e) { if (e.target !== this._target && !this._target.contains(e.target) && e.target !== this._popover && !(this._popover && this._popover.contains(e.target))) { if (this._hideTimeout) { this.clearHideTimeout(); } if (this.props.isOpen) { this.toggle(); } } } }, { key: 'addTargetEvents', value: function addTargetEvents() { var _this2 = this; ['click', 'touchstart'].forEach(function (event) { return document.addEventListener(event, _this2.handleDocumentClick, true); }); } }, { key: 'removeTargetEvents', value: function removeTargetEvents() { var _this3 = this; ['click', 'touchstart'].forEach(function (event) { return document.removeEventListener(event, _this3.handleDocumentClick, true); }); } }, { key: 'toggle', value: function toggle(e) { if (this.props.disabled) { return e && e.preventDefault(); } return this.props.toggle(); } }, { key: 'render', value: function render() { if (!this.props.isOpen) { return null; } var attributes = (0, _utils.omit)(this.props, Object.keys(propTypes)); var classes = (0, _utils.mapToCssModules)((0, _classnames2.default)('popover-inner', this.props.innerClassName), this.props.cssModule); var popperClasses = (0, _utils.mapToCssModules)((0, _classnames2.default)('popover', 'show', this.props.className), this.props.cssModule); return _react2.default.createElement( _PopperContent2.default, { className: popperClasses, target: this.props.target, isOpen: this.props.isOpen, placement: this.props.placement, placementPrefix: this.props.placementPrefix, container: this.props.container }, _react2.default.createElement('div', _extends({}, attributes, { className: classes, ref: this.getRef })) ); } }]); return Popover; }(_react2.default.Component); Popover.propTypes = propTypes; Popover.defaultProps = defaultProps; exports.default = Popover;