tooltip_manager.js 3.7 KB
Newer Older
polesye committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
(function() {
    'use strict';
    var TooltipManager = function (element) {
        this.element = $(element);
        // If tooltip container already exist, use it.
        this.tooltip = $('div.' + this.className.split(/\s+/).join('.'));
        // Otherwise, create new one.
        if (!this.tooltip.length) {
            this.tooltip = $('<div />', {
                'class': this.className
            }).appendTo(this.element);
        }

        this.hide();
15
        _.bindAll(this, 'show', 'hide', 'showTooltip', 'moveTooltip', 'hideTooltip', 'click');
polesye committed
16 17 18 19 20 21 22 23 24 25 26 27 28
        this.bindEvents();
    };

    TooltipManager.prototype = {
        // Space separated list of class names for the tooltip container.
        className: 'tooltip',
        SELECTOR: '[data-tooltip]',

        bindEvents: function () {
            this.element.on({
                'mouseover.TooltipManager': this.showTooltip,
                'mousemove.TooltipManager': this.moveTooltip,
                'mouseout.TooltipManager': this.hideTooltip,
29
                'click.TooltipManager': this.click
polesye committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
            }, this.SELECTOR);
        },

        getCoords: function (pageX, pageY) {
            return {
                'left': pageX - 0.5 * this.tooltip.outerWidth(),
                'top': pageY - (this.tooltip.outerHeight() + 15)
            };
        },

        show: function () {
            this.tooltip.show().css('opacity', 1);
        },

        hide: function () {
            this.tooltip.hide().css('opacity', 0);
        },

        showTooltip: function(event) {
49 50 51 52 53 54 55 56 57 58 59
            this.prepareTooltip(event.currentTarget, event.pageX, event.pageY);
            if (this.tooltipTimer) {
                clearTimeout(this.tooltipTimer);
            }
            this.tooltipTimer = setTimeout(this.show, 500);
        },

        prepareTooltip: function(element, pageX, pageY) {
            pageX = typeof pageX !== 'undefined' ? pageX : element.offset().left + element.width()/2;
            pageY = typeof pageY !== 'undefined' ? pageY : element.offset().top + element.height()/2;
            var tooltipText = $(element).attr('data-tooltip');
polesye committed
60 61
            this.tooltip
                .html(tooltipText)
62 63
                .css(this.getCoords(pageX, pageY));
        },
polesye committed
64

65 66 67 68
        // To manually trigger a tooltip to reveal, other than through user mouse movement:
        openTooltip: function(element) {
            this.prepareTooltip(element);
            this.show();
polesye committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            if (this.tooltipTimer) {
                clearTimeout(this.tooltipTimer);
            }
        },

        moveTooltip: function(event) {
            this.tooltip.css(this.getCoords(event.pageX, event.pageY));
        },

        hideTooltip: function() {
            clearTimeout(this.tooltipTimer);
            // Wait for a 50ms before hiding the tooltip to avoid blinking when
            // the item contains nested elements.
            this.tooltipTimer = setTimeout(this.hide, 50);
        },

85 86 87 88 89 90 91 92 93 94 95 96
        click: function(event) {
            var showOnClick = !!$(event.currentTarget).data('tooltip-show-on-click'); // Default is false
            if (showOnClick) {
                this.show();
                if (this.tooltipTimer) {
                    clearTimeout(this.tooltipTimer);
                }
            } else {
                this.hideTooltip(event);
            }
        },

polesye committed
97 98 99 100 101 102 103 104 105 106
        destroy: function () {
            this.tooltip.remove();
            // Unbind all delegated event handlers in the ".TooltipManager"
            // namespace.
            this.element.off('.TooltipManager', this.SELECTOR);
        }
    };

    window.TooltipManager = TooltipManager;
    $(document).ready(function () {
107
        window.globalTooltipManager = new TooltipManager(document.body);
polesye committed
108 109
    });
}());