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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; 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; } (function ($, anim) { 'use strict'; var _defaults = { exitDelay: 200, enterDelay: 0, html: null, margin: 5, inDuration: 250, outDuration: 200, position: 'bottom', transitionMovement: 10 }; /** * @class * */ var Tooltip = function (_Component) { _inherits(Tooltip, _Component); /** * Construct Tooltip instance * @constructor * @param {Element} el * @param {Object} options */ function Tooltip(el, options) { _classCallCheck(this, Tooltip); var _this = _possibleConstructorReturn(this, (Tooltip.__proto__ || Object.getPrototypeOf(Tooltip)).call(this, Tooltip, el, options)); _this.el.M_Tooltip = _this; _this.options = $.extend({}, Tooltip.defaults, options); _this.isOpen = false; _this.isHovered = false; _this.isFocused = false; _this._appendTooltipEl(); _this._setupEventHandlers(); return _this; } _createClass(Tooltip, [{ key: 'destroy', /** * Teardown component */ value: function destroy() { $(this.tooltipEl).remove(); this._removeEventHandlers(); this.el.M_Tooltip = undefined; } }, { key: '_appendTooltipEl', value: function _appendTooltipEl() { var tooltipEl = document.createElement('div'); tooltipEl.classList.add('material-tooltip'); this.tooltipEl = tooltipEl; var tooltipContentEl = document.createElement('div'); tooltipContentEl.classList.add('tooltip-content'); tooltipContentEl.innerHTML = this.options.html; tooltipEl.appendChild(tooltipContentEl); document.body.appendChild(tooltipEl); } }, { key: '_updateTooltipContent', value: function _updateTooltipContent() { this.tooltipEl.querySelector('.tooltip-content').innerHTML = this.options.html; } }, { key: '_setupEventHandlers', value: function _setupEventHandlers() { this._handleMouseEnterBound = this._handleMouseEnter.bind(this); this._handleMouseLeaveBound = this._handleMouseLeave.bind(this); this._handleFocusBound = this._handleFocus.bind(this); this._handleBlurBound = this._handleBlur.bind(this); this.el.addEventListener('mouseenter', this._handleMouseEnterBound); this.el.addEventListener('mouseleave', this._handleMouseLeaveBound); this.el.addEventListener('focus', this._handleFocusBound, true); this.el.addEventListener('blur', this._handleBlurBound, true); } }, { key: '_removeEventHandlers', value: function _removeEventHandlers() { this.el.removeEventListener('mouseenter', this._handleMouseEnterBound); this.el.removeEventListener('mouseleave', this._handleMouseLeaveBound); this.el.removeEventListener('focus', this._handleFocusBound, true); this.el.removeEventListener('blur', this._handleBlurBound, true); } }, { key: 'open', value: function open() { if (this.isOpen) { return; } this.isOpen = true; // Update tooltip content with HTML attribute options this.options = $.extend({}, this.options, this._getAttributeOptions()); this._updateTooltipContent(); this._setEnterDelayTimeout(); } }, { key: 'close', value: function close() { if (!this.isOpen) { return; } this.isOpen = false; this._setExitDelayTimeout(); } /** * Create timeout which delays when the tooltip closes */ }, { key: '_setExitDelayTimeout', value: function _setExitDelayTimeout() { var _this2 = this; clearTimeout(this._exitDelayTimeout); this._exitDelayTimeout = setTimeout(function () { if (_this2.isHovered || _this2.isFocused) { return; } _this2._animateOut(); }, this.options.exitDelay); } /** * Create timeout which delays when the toast closes */ }, { key: '_setEnterDelayTimeout', value: function _setEnterDelayTimeout() { var _this3 = this; clearTimeout(this._enterDelayTimeout); this._enterDelayTimeout = setTimeout(function () { if (!_this3.isHovered && !_this3.isFocused) { return; } _this3._animateIn(); }, this.options.enterDelay); } }, { key: '_positionTooltip', value: function _positionTooltip() { var origin = this.el, tooltip = this.tooltipEl, originHeight = origin.offsetHeight, originWidth = origin.offsetWidth, tooltipHeight = tooltip.offsetHeight, tooltipWidth = tooltip.offsetWidth, newCoordinates = void 0, margin = this.options.margin, targetTop = void 0, targetLeft = void 0; this.xMovement = 0, this.yMovement = 0; targetTop = origin.getBoundingClientRect().top + M.getDocumentScrollTop(); targetLeft = origin.getBoundingClientRect().left + M.getDocumentScrollLeft(); if (this.options.position === 'top') { targetTop += -tooltipHeight - margin; targetLeft += originWidth / 2 - tooltipWidth / 2; this.yMovement = -this.options.transitionMovement; } else if (this.options.position === 'right') { targetTop += originHeight / 2 - tooltipHeight / 2; targetLeft += originWidth + margin; this.xMovement = this.options.transitionMovement; } else if (this.options.position === 'left') { targetTop += originHeight / 2 - tooltipHeight / 2; targetLeft += -tooltipWidth - margin; this.xMovement = -this.options.transitionMovement; } else { targetTop += originHeight + margin; targetLeft += originWidth / 2 - tooltipWidth / 2; this.yMovement = this.options.transitionMovement; } newCoordinates = this._repositionWithinScreen(targetLeft, targetTop, tooltipWidth, tooltipHeight); $(tooltip).css({ top: newCoordinates.y + 'px', left: newCoordinates.x + 'px' }); } }, { key: '_repositionWithinScreen', value: function _repositionWithinScreen(x, y, width, height) { var scrollLeft = M.getDocumentScrollLeft(); var scrollTop = M.getDocumentScrollTop(); var newX = x - scrollLeft; var newY = y - scrollTop; var bounding = { left: newX, top: newY, width: width, height: height }; var offset = this.options.margin + this.options.transitionMovement; var edges = M.checkWithinContainer(document.body, bounding, offset); if (edges.left) { newX = offset; } else if (edges.right) { newX -= newX + width - window.innerWidth; } if (edges.top) { newY = offset; } else if (edges.bottom) { newY -= newY + height - window.innerHeight; } return { x: newX + scrollLeft, y: newY + scrollTop }; } }, { key: '_animateIn', value: function _animateIn() { this._positionTooltip(); this.tooltipEl.style.visibility = 'visible'; anim.remove(this.tooltipEl); anim({ targets: this.tooltipEl, opacity: 1, translateX: this.xMovement, translateY: this.yMovement, duration: this.options.inDuration, easing: 'easeOutCubic' }); } }, { key: '_animateOut', value: function _animateOut() { anim.remove(this.tooltipEl); anim({ targets: this.tooltipEl, opacity: 0, translateX: 0, translateY: 0, duration: this.options.outDuration, easing: 'easeOutCubic' }); } }, { key: '_handleMouseEnter', value: function _handleMouseEnter() { this.isHovered = true; this.open(); } }, { key: '_handleMouseLeave', value: function _handleMouseLeave() { this.isHovered = false; this.close(); } }, { key: '_handleFocus', value: function _handleFocus() { this.isFocused = true; this.open(); } }, { key: '_handleBlur', value: function _handleBlur() { this.isFocused = false; this.close(); } }, { key: '_getAttributeOptions', value: function _getAttributeOptions() { var attributeOptions = {}; var tooltipTextOption = this.el.getAttribute('data-tooltip'); var positionOption = this.el.getAttribute('data-position'); if (tooltipTextOption) { attributeOptions.html = tooltipTextOption; } if (positionOption) { attributeOptions.position = positionOption; } return attributeOptions; } }], [{ key: 'init', value: function init(els, options) { return _get(Tooltip.__proto__ || Object.getPrototypeOf(Tooltip), 'init', this).call(this, this, els, options); } /** * Get Instance */ }, { key: 'getInstance', value: function getInstance(el) { var domElem = !!el.jquery ? el[0] : el; return domElem.M_Tooltip; } }, { key: 'defaults', get: function () { return _defaults; } }]); return Tooltip; }(Component); M.Tooltip = Tooltip; if (M.jQueryLoaded) { M.initializeJqueryWrapper(Tooltip, 'tooltip', 'M_Tooltip'); } })(cash, M.anime);