@ithaka/bonsai
Version:
ITHAKA core styling
61 lines (52 loc) • 1.89 kB
JavaScript
;
import $ from "jquery";
import { Tooltip } from "foundation-sites/js/foundation.tooltip";
import { BonsaiBase } from "./bonsai.base";
/** Tooltip module
* @class
* @param {jQuery} elements - jQuery object to use for initializing the tooltip.
* @param {Object} options - Optional parameters.
*
* @example
* const tooltip = new BonsaiTooltip($(".tooltip"));
*
* @returns Returns object that has initialized each tooltip matching the incoming `element`
*
*/
class BonsaiTooltip extends BonsaiBase {
constructor(elements, options = {}) {
super(elements, options);
Bonsai.Tooltips = Bonsai.hasOwnProperty("Tooltips") ? Bonsai.Tooltips : {members: {}, reflow: this.reflow};
this._initializeTooltips();
}
/**
* Iterates through all elements of the jQuery selector and creates a Tooltip object. If the Tooltip element has an ID, it
* is added to the global Bonsai.Tooltips Object for global event listening
*
* @private
*/
_initializeTooltips() {
this.elements.each((index, element) => {
let $element = $(element),
elementID = $element.attr("id"),
theTooltip = new Tooltip($element, this.options);
if (elementID) {
Bonsai.Tooltips.members[elementID] = theTooltip;
}
});
}
/**
* Reflow method to re-bind jQuery element to a Tooltip object
*
* @public
*/
reflow() {
$.each(Bonsai.Tooltips.members, (elementID, theTooltip) => {
theTooltip._destroy(); // destroy previous Tooltip element
if ($(`#${elementID}`).length) { // Some tooltips may disappear when ajax page reloads and results change
Bonsai.Tooltips.members[elementID] = new Tooltip($(`#${elementID}`));
}
});
}
}
export { BonsaiTooltip };