UNPKG

carbon-components

Version:

Carbon Components is a component library for IBM Cloud

123 lines (101 loc) 5.08 kB
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; }; }(); 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; } import settings from '../../globals/js/settings'; import mixin from '../../globals/js/misc/mixin'; import createComponent from '../../globals/js/mixins/create-component'; import initComponentByEvent from '../../globals/js/mixins/init-component-by-event'; import eventedShowHideState from '../../globals/js/mixins/evented-show-hide-state'; import handles from '../../globals/js/mixins/handles'; import FloatingMenu from '../floating-menu/floating-menu'; import getLaunchingDetails from '../../globals/js/misc/get-launching-details'; import on from '../../globals/js/misc/on'; var Tooltip = function (_mixin) { _inherits(Tooltip, _mixin); /** * Tooltip. * @extends CreateComponent * @extends InitComponentBySearch * @extends Handles */ function Tooltip(element, options) { _classCallCheck(this, Tooltip); var _this = _possibleConstructorReturn(this, (Tooltip.__proto__ || Object.getPrototypeOf(Tooltip)).call(this, element, options)); ['mouseover', 'mouseout', 'focus', 'blur', 'touchleave', 'touchcancel'].forEach(function (name) { _this.manage(on(_this.element, name, function (event) { _this._handleHover(event); })); }); return _this; } /** * A method called when this widget is created upon events. * @param {Event} event The event triggering the creation. */ _createClass(Tooltip, [{ key: 'createdByEvent', value: function createdByEvent(event) { this._handleHover(event); } /** * Changes the shown/hidden state. * @param {string} state The new state. * @param {Object} detail The detail of the event trigging this action. * @param {Function} callback Callback called when change in state completes. // */ }, { key: 'changeState', value: function changeState(state, detail, callback) { if (!this.tooltip) { var tooltip = this.element.ownerDocument.querySelector(this.element.getAttribute(this.options.attribTooltipTarget)); if (!tooltip) { throw new Error('Cannot find the target tooltip.'); } // Lazily create a component instance for tooltip this.tooltip = FloatingMenu.create(tooltip, { refNode: this.element, classShown: this.options.classShown, offset: this.options.objMenuOffset }); this.children.push(this.tooltip); } // Delegates the action of changing state to the tooltip. // (And thus the before/after shown/hidden events are fired from the tooltip) this.tooltip.changeState(state, Object.assign(detail, { delegatorNode: this.element }), callback); } /** * Handles hover/focus events. * @param {Event} event The event. * @private */ }, { key: '_handleHover', value: function _handleHover(event) { var state = { mouseover: 'shown', mouseout: 'hidden', focus: 'shown', blur: 'hidden', touchleave: 'hidden', touchcancel: 'hidden' }[event.type]; this.changeState(state, getLaunchingDetails(event)); } }], [{ key: 'options', get: function get() { var prefix = settings.prefix; return { selectorInit: '[data-tooltip-trigger]', classShown: prefix + '--tooltip--shown', attribTooltipTarget: 'data-tooltip-target', objMenuOffset: { top: 10, left: 0 }, initEventNames: ['mouseover', 'focus'] }; } }]); return Tooltip; }(mixin(createComponent, initComponentByEvent, eventedShowHideState, handles)); Tooltip.components = new WeakMap(); export default Tooltip;