'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, cssModule: _propTypes2.default.object, toggle: _propTypes2.default.func, autohide: _propTypes2.default.bool, placementPrefix: _propTypes2.default.string, delay: _propTypes2.default.oneOfType([_propTypes2.default.shape({ show: _propTypes2.default.number, hide: _propTypes2.default.number }), _propTypes2.default.number]) }; var DEFAULT_DELAYS = { show: 0, hide: 250 }; var defaultProps = { isOpen: false, placement: 'top', placementPrefix: 'bs-tooltip', delay: DEFAULT_DELAYS, autohide: true, toggle: function toggle() {} }; var Tooltip = function (_React$Component) { _inherits(Tooltip, _React$Component); function Tooltip(props) { _classCallCheck(this, Tooltip); var _this = _possibleConstructorReturn(this, (Tooltip.__proto__ || Object.getPrototypeOf(Tooltip)).call(this, props)); _this.addTargetEvents = _this.addTargetEvents.bind(_this); _this.handleDocumentClick = _this.handleDocumentClick.bind(_this); _this.removeTargetEvents = _this.removeTargetEvents.bind(_this); _this.toggle = _this.toggle.bind(_this); _this.onMouseOverTooltip = _this.onMouseOverTooltip.bind(_this); _this.onMouseLeaveTooltip = _this.onMouseLeaveTooltip.bind(_this); _this.onMouseOverTooltipContent = _this.onMouseOverTooltipContent.bind(_this); _this.onMouseLeaveTooltipContent = _this.onMouseLeaveTooltipContent.bind(_this); _this.show = _this.show.bind(_this); _this.hide = _this.hide.bind(_this); return _this; } _createClass(Tooltip, [{ key: 'componentDidMount', value: function componentDidMount() { this._target = (0, _utils.getTarget)(this.props.target); this.addTargetEvents(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.removeTargetEvents(); } }, { key: 'onMouseOverTooltip', value: function onMouseOverTooltip() { if (this._hideTimeout) { this.clearHideTimeout(); } this._showTimeout = setTimeout(this.show, this.getDelay('show')); } }, { key: 'onMouseLeaveTooltip', value: function onMouseLeaveTooltip() { if (this._showTimeout) { this.clearShowTimeout(); } this._hideTimeout = setTimeout(this.hide, this.getDelay('hide')); } }, { key: 'onMouseOverTooltipContent', value: function onMouseOverTooltipContent() { if (this.props.autohide) { return; } if (this._hideTimeout) { this.clearHideTimeout(); } } }, { key: 'onMouseLeaveTooltipContent', value: function onMouseLeaveTooltipContent() { if (this.props.autohide) { return; } if (this._showTimeout) { this.clearShowTimeout(); } this._hideTimeout = setTimeout(this.hide, this.getDelay('hide')); } }, { 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: 'show', value: function show() { if (!this.props.isOpen) { this.clearShowTimeout(); this.toggle(); } } }, { key: 'hide', value: function hide() { if (this.props.isOpen) { this.clearHideTimeout(); this.toggle(); } } }, { 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)) { if (this._hideTimeout) { this.clearHideTimeout(); } if (!this.props.isOpen) { this.toggle(); } } } }, { key: 'addTargetEvents', value: function addTargetEvents() { var _this2 = this; this._target.addEventListener('mouseover', this.onMouseOverTooltip, true); this._target.addEventListener('mouseout', this.onMouseLeaveTooltip, true); ['click', 'touchstart'].forEach(function (event) { return document.addEventListener(event, _this2.handleDocumentClick, true); }); } }, { key: 'removeTargetEvents', value: function removeTargetEvents() { var _this3 = this; this._target.removeEventListener('mouseover', this.onMouseOverTooltip, true); this._target.removeEventListener('mouseout', this.onMouseLeaveTooltip, true); ['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)('tooltip-inner', this.props.innerClassName), this.props.cssModule); var popperClasses = (0, _utils.mapToCssModules)((0, _classnames2.default)('tooltip', '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, onMouseOver: this.onMouseOverTooltipContent, onMouseLeave: this.onMouseLeaveTooltipContent })) ); } }]); return Tooltip; }(_react2.default.Component); Tooltip.propTypes = propTypes; Tooltip.defaultProps = defaultProps; exports.default = Tooltip;