/* Tooltipster 3.3.0 | 2014-11-08 A rockin' custom tooltip jQuery plugin Developed by Caleb Jacob under the MIT license http://opensource.org/licenses/MIT THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ ;(function ($, window, document) { var pluginName = "tooltipster", defaults = { animation: 'fade', arrow: true, arrowColor: '', autoClose: true, content: null, contentAsHTML: false, contentCloning: true, debug: true, delay: 200, minWidth: 0, maxWidth: null, functionInit: function(origin, content) {}, functionBefore: function(origin, continueTooltip) { continueTooltip(); }, functionReady: function(origin, tooltip) {}, functionAfter: function(origin) {}, hideOnClick: false, icon: '(?)', iconCloning: true, iconDesktop: false, iconTouch: false, iconTheme: 'tooltipster-icon', interactive: false, interactiveTolerance: 350, multiple: false, offsetX: 0, offsetY: 0, onlyOne: false, position: 'top', positionTracker: false, positionTrackerCallback: function(origin){ // the default tracker callback will close the tooltip when the trigger is // 'hover' (see https://github.com/iamceege/tooltipster/pull/253) if(this.option('trigger') == 'hover' && this.option('autoClose')) { this.hide(); } }, restoration: 'current', speed: 350, timer: 0, theme: 'tooltipster-default', touchDevices: true, trigger: 'hover', updateAnimation: true }; function Plugin(element, options) { // list of instance variables this.bodyOverflowX; // stack of custom callbacks provided as parameters to API methods this.callbacks = { hide: [], show: [] }; this.checkInterval = null; // this will be the user content shown in the tooltip. A capital "C" is used because there is also a method called content() this.Content; // this is the original element which is being applied the tooltipster plugin this.$el = $(element); // this will be the element which triggers the appearance of the tooltip on hover/click/custom events. // it will be the same as this.$el if icons are not used (see in the options), otherwise it will correspond to the created icon this.$elProxy; this.elProxyPosition; this.enabled = true; this.options = $.extend({}, defaults, options); this.mouseIsOverProxy = false; // a unique namespace per instance, for easy selective unbinding this.namespace = 'tooltipster-'+ Math.round(Math.random()*100000); // Status (capital S) can be either : appearing, shown, disappearing, hidden this.Status = 'hidden'; this.timerHide = null; this.timerShow = null; // this will be the tooltip element (jQuery wrapped HTML element) this.$tooltip; // for backward compatibility this.options.iconTheme = this.options.iconTheme.replace('.', ''); this.options.theme = this.options.theme.replace('.', ''); // launch this._init(); } Plugin.prototype = { _init: function() { var self = this; // disable the plugin on old browsers (including IE7 and lower) if (document.querySelector) { // note : the content is null (empty) by default and can stay that way if the plugin remains initialized but not fed any content. The tooltip will just not appear. // let's save the initial value of the title attribute for later restoration if need be. var initialTitle = null; // it will already have been saved in case of multiple tooltips if (self.$el.data('tooltipster-initialTitle') === undefined) { initialTitle = self.$el.attr('title'); // we do not want initialTitle to have the value "undefined" because of how jQuery's .data() method works if (initialTitle === undefined) initialTitle = null; self.$el.data('tooltipster-initialTitle', initialTitle); } // if content is provided in the options, its has precedence over the title attribute. // Note : an empty string is considered content, only 'null' represents the absence of content. // Also, an existing title="" attribute will result in an empty string content if (self.options.content !== null){ self._content_set(self.options.content); } else { self._content_set(initialTitle); } var c = self.options.functionInit.call(self.$el, self.$el, self.Content); if(typeof c !== 'undefined') self._content_set(c); self.$el // strip the title off of the element to prevent the default tooltips from popping up .removeAttr('title') // to be able to find all instances on the page later (upon window events in particular) .addClass('tooltipstered'); // detect if we're changing the tooltip origin to an icon // note about this condition : if the device has touch capability and self.options.iconTouch is false, you'll have no icons event though you may consider your device as a desktop if it also has a mouse. Not sure why someone would have this use case though. if ((!deviceHasTouchCapability && self.options.iconDesktop) || (deviceHasTouchCapability && self.options.iconTouch)) { // TODO : the tooltip should be automatically be given an absolute position to be near the origin. Otherwise, when the origin is floating or what, it's going to be nowhere near it and disturb the position flow of the page elements. It will imply that the icon also detects when its origin moves, to follow it : not trivial. // Until it's done, the icon feature does not really make sense since the user still has most of the work to do by himself // if the icon provided is in the form of a string if(typeof self.options.icon === 'string'){ // wrap it in a span with the icon class self.$elProxy = $(''); self.$elProxy.text(self.options.icon); } // if it is an object (sensible choice) else { // (deep) clone the object if iconCloning == true, to make sure every instance has its own proxy. We use the icon without wrapping, no need to. We do not give it a class either, as the user will undoubtedly style the object on his own and since our css properties may conflict with his own if (self.options.iconCloning) self.$elProxy = self.options.icon.clone(true); else self.$elProxy = self.options.icon; } self.$elProxy.insertAfter(self.$el); } else { self.$elProxy = self.$el; } // for 'click' and 'hover' triggers : bind on events to open the tooltip. Closing is now handled in _showNow() because of its bindings. // Notes about touch events : // - mouseenter, mouseleave and clicks happen even on pure touch devices because they are emulated. deviceIsPureTouch() is a simple attempt to detect them. // - on hybrid devices, we do not prevent touch gesture from opening tooltips. It would be too complex to differentiate real mouse events from emulated ones. // - we check deviceIsPureTouch() at each event rather than prior to binding because the situation may change during browsing if (self.options.trigger == 'hover') { // these binding are for mouse interaction only self.$elProxy .on('mouseenter.'+ self.namespace, function() { if (!deviceIsPureTouch() || self.options.touchDevices) { self.mouseIsOverProxy = true; self._show(); } }) .on('mouseleave.'+ self.namespace, function() { if (!deviceIsPureTouch() || self.options.touchDevices) { self.mouseIsOverProxy = false; } }); // for touch interaction only if (deviceHasTouchCapability && self.options.touchDevices) { // for touch devices, we immediately display the tooltip because we cannot rely on mouseleave to handle the delay self.$elProxy.on('touchstart.'+ self.namespace, function() { self._showNow(); }); } } else if (self.options.trigger == 'click') { // note : for touch devices, we do not bind on touchstart, we only rely on the emulated clicks (triggered by taps) self.$elProxy.on('click.'+ self.namespace, function() { if (!deviceIsPureTouch() || self.options.touchDevices) { self._show(); } }); } } }, // this function will schedule the opening of the tooltip after the delay, if there is one _show: function() { var self = this; if (self.Status != 'shown' && self.Status != 'appearing') { if (self.options.delay) { self.timerShow = setTimeout(function(){ // for hover trigger, we check if the mouse is still over the proxy, otherwise we do not show anything if (self.options.trigger == 'click' || (self.options.trigger == 'hover' && self.mouseIsOverProxy)) { self._showNow(); } }, self.options.delay); } else self._showNow(); } }, // this function will open the tooltip right away _showNow: function(callback) { var self = this; // call our constructor custom function before continuing self.options.functionBefore.call(self.$el, self.$el, function() { // continue only if the tooltip is enabled and has any content if (self.enabled && self.Content !== null) { // save the method callback and cancel hide method callbacks if (callback) self.callbacks.show.push(callback); self.callbacks.hide = []; //get rid of any appearance timer clearTimeout(self.timerShow); self.timerShow = null; clearTimeout(self.timerHide); self.timerHide = null; // if we only want one tooltip open at a time, close all auto-closing tooltips currently open and not already disappearing if (self.options.onlyOne) { $('.tooltipstered').not(self.$el).each(function(i,el) { var $el = $(el), nss = $el.data('tooltipster-ns'); // iterate on all tooltips of the element $.each(nss, function(i, ns){ var instance = $el.data(ns), // we have to use the public methods here s = instance.status(), ac = instance.option('autoClose'); if (s !== 'hidden' && s !== 'disappearing' && ac) { instance.hide(); } }); }); } var finish = function() { self.Status = 'shown'; // trigger any show method custom callbacks and reset them $.each(self.callbacks.show, function(i,c) { c.call(self.$el); }); self.callbacks.show = []; }; // if this origin already has its tooltip open if (self.Status !== 'hidden') { // the timer (if any) will start (or restart) right now var extraTime = 0; // if it was disappearing, cancel that if (self.Status === 'disappearing') { self.Status = 'appearing'; if (supportsTransitions()) { self.$tooltip .clearQueue() .removeClass('tooltipster-dying') .addClass('tooltipster-'+ self.options.animation +'-show'); if (self.options.speed > 0) self.$tooltip.delay(self.options.speed); self.$tooltip.queue(finish); } else { // in case the tooltip was currently fading out, bring it back to life self.$tooltip .stop() .fadeIn(finish); } } // if the tooltip is already open, we still need to trigger the method custom callback else if(self.Status === 'shown') { finish(); } } // if the tooltip isn't already open, open that sucker up! else { self.Status = 'appearing'; // the timer (if any) will start when the tooltip has fully appeared after its transition var extraTime = self.options.speed; // disable horizontal scrollbar to keep overflowing tooltips from jacking with it and then restore it to its previous value self.bodyOverflowX = $('body').css('overflow-x'); $('body').css('overflow-x', 'hidden'); // get some other settings related to building the tooltip var animation = 'tooltipster-' + self.options.animation, animationSpeed = '-webkit-transition-duration: '+ self.options.speed +'ms; -webkit-animation-duration: '+ self.options.speed +'ms; -moz-transition-duration: '+ self.options.speed +'ms; -moz-animation-duration: '+ self.options.speed +'ms; -o-transition-duration: '+ self.options.speed +'ms; -o-animation-duration: '+ self.options.speed +'ms; -ms-transition-duration: '+ self.options.speed +'ms; -ms-animation-duration: '+ self.options.speed +'ms; transition-duration: '+ self.options.speed +'ms; animation-duration: '+ self.options.speed +'ms;', minWidth = self.options.minWidth ? 'min-width:'+ Math.round(self.options.minWidth) +'px;' : '', maxWidth = self.options.maxWidth ? 'max-width:'+ Math.round(self.options.maxWidth) +'px;' : '', pointerEvents = self.options.interactive ? 'pointer-events: auto;' : ''; // build the base of our tooltip self.$tooltip = $('
'); // only add the animation class if the user has a browser that supports animations if (supportsTransitions()) self.$tooltip.addClass(animation); // insert the content self._content_insert(); // attach self.$tooltip.appendTo('body'); // do all the crazy calculations and positioning self.reposition(); // call our custom callback since the content of the tooltip is now part of the DOM self.options.functionReady.call(self.$el, self.$el, self.$tooltip); // animate in the tooltip if (supportsTransitions()) { self.$tooltip.addClass(animation + '-show'); if(self.options.speed > 0) self.$tooltip.delay(self.options.speed); self.$tooltip.queue(finish); } else { self.$tooltip.css('display', 'none').fadeIn(self.options.speed, finish); } // will check if our tooltip origin is removed while the tooltip is shown self._interval_set(); // reposition on scroll (otherwise position:fixed element's tooltips will move away form their origin) and on resize (in case position can/has to be changed) $(window).on('scroll.'+ self.namespace +' resize.'+ self.namespace, function() { self.reposition(); }); // auto-close bindings if (self.options.autoClose) { // in case a listener is already bound for autoclosing (mouse or touch, hover or click), unbind it first $('body').off('.'+ self.namespace); // here we'll have to set different sets of bindings for both touch and mouse if (self.options.trigger == 'hover') { // if the user touches the body, hide if (deviceHasTouchCapability) { // timeout 0 : explanation below in click section setTimeout(function() { // we don't want to bind on click here because the initial touchstart event has not yet triggered its click event, which is thus about to happen $('body').on('touchstart.'+ self.namespace, function() { self.hide(); }); }, 0); } // if we have to allow interaction if (self.options.interactive) { // touch events inside the tooltip must not close it if (deviceHasTouchCapability) { self.$tooltip.on('touchstart.'+ self.namespace, function(event) { event.stopPropagation(); }); } // as for mouse interaction, we get rid of the tooltip only after the mouse has spent some time out of it var tolerance = null; self.$elProxy.add(self.$tooltip) // hide after some time out of the proxy and the tooltip .on('mouseleave.'+ self.namespace + '-autoClose', function() { clearTimeout(tolerance); tolerance = setTimeout(function(){ self.hide(); }, self.options.interactiveTolerance); }) // suspend timeout when the mouse is over the proxy or the tooltip .on('mouseenter.'+ self.namespace + '-autoClose', function() { clearTimeout(tolerance); }); } // if this is a non-interactive tooltip, get rid of it if the mouse leaves else { self.$elProxy.on('mouseleave.'+ self.namespace + '-autoClose', function() { self.hide(); }); } // close the tooltip when the proxy gets a click (common behavior of native tooltips) if (self.options.hideOnClick) { self.$elProxy.on('click.'+ self.namespace + '-autoClose', function() { self.hide(); }); } } // here we'll set the same bindings for both clicks and touch on the body to hide the tooltip else if(self.options.trigger == 'click'){ // use a timeout to prevent immediate closing if the method was called on a click event and if options.delay == 0 (because of bubbling) setTimeout(function() { $('body').on('click.'+ self.namespace +' touchstart.'+ self.namespace, function() { self.hide(); }); }, 0); // if interactive, we'll stop the events that were emitted from inside the tooltip to stop autoClosing if (self.options.interactive) { // note : the touch events will just not be used if the plugin is not enabled on touch devices self.$tooltip.on('click.'+ self.namespace +' touchstart.'+ self.namespace, function(event) { event.stopPropagation(); }); } } } } // if we have a timer set, let the countdown begin if (self.options.timer > 0) { self.timerHide = setTimeout(function() { self.timerHide = null; self.hide(); }, self.options.timer + extraTime); } } }); }, _interval_set: function() { var self = this; self.checkInterval = setInterval(function() { // if the tooltip and/or its interval should be stopped if ( // if the origin has been removed $('body').find(self.$el).length === 0 // if the elProxy has been removed || $('body').find(self.$elProxy).length === 0 // if the tooltip has been closed || self.Status == 'hidden' // if the tooltip has somehow been removed || $('body').find(self.$tooltip).length === 0 ) { // remove the tooltip if it's still here if (self.Status == 'shown' || self.Status == 'appearing') self.hide(); // clear this interval as it is no longer necessary self._interval_cancel(); } // if everything is alright else { // compare the former and current positions of the elProxy to reposition the tooltip if need be if(self.options.positionTracker){ var p = self._repositionInfo(self.$elProxy), identical = false; // compare size first (a change requires repositioning too) if(areEqual(p.dimension, self.elProxyPosition.dimension)){ // for elements with a fixed position, we track the top and left properties (relative to window) if(self.$elProxy.css('position') === 'fixed'){ if(areEqual(p.position, self.elProxyPosition.position)) identical = true; } // otherwise, track total offset (relative to document) else { if(areEqual(p.offset, self.elProxyPosition.offset)) identical = true; } } if(!identical){ self.reposition(); self.options.positionTrackerCallback.call(self, self.$el); } } } }, 200); }, _interval_cancel: function() { clearInterval(this.checkInterval); // clean delete this.checkInterval = null; }, _content_set: function(content) { // clone if asked. Cloning the object makes sure that each instance has its own version of the content (in case a same object were provided for several instances) // reminder : typeof null === object if (typeof content === 'object' && content !== null && this.options.contentCloning) { content = content.clone(true); } this.Content = content; }, _content_insert: function() { var self = this, $d = this.$tooltip.find('.tooltipster-content'); if (typeof self.Content === 'string' && !self.options.contentAsHTML) { $d.text(self.Content); } else { $d .empty() .append(self.Content); } }, _update: function(content) { var self = this; // change the content self._content_set(content); if (self.Content !== null) { // update the tooltip if it is open if (self.Status !== 'hidden') { // reset the content in the tooltip self._content_insert(); // reposition and resize the tooltip self.reposition(); // if we want to play a little animation showing the content changed if (self.options.updateAnimation) { if (supportsTransitions()) { self.$tooltip.css({ 'width': '', '-webkit-transition': 'all ' + self.options.speed + 'ms, width 0ms, height 0ms, left 0ms, top 0ms', '-moz-transition': 'all ' + self.options.speed + 'ms, width 0ms, height 0ms, left 0ms, top 0ms', '-o-transition': 'all ' + self.options.speed + 'ms, width 0ms, height 0ms, left 0ms, top 0ms', '-ms-transition': 'all ' + self.options.speed + 'ms, width 0ms, height 0ms, left 0ms, top 0ms', 'transition': 'all ' + self.options.speed + 'ms, width 0ms, height 0ms, left 0ms, top 0ms' }).addClass('tooltipster-content-changing'); // reset the CSS transitions and finish the change animation setTimeout(function() { if(self.Status != 'hidden'){ self.$tooltip.removeClass('tooltipster-content-changing'); // after the changing animation has completed, reset the CSS transitions setTimeout(function() { if(self.Status !== 'hidden'){ self.$tooltip.css({ '-webkit-transition': self.options.speed + 'ms', '-moz-transition': self.options.speed + 'ms', '-o-transition': self.options.speed + 'ms', '-ms-transition': self.options.speed + 'ms', 'transition': self.options.speed + 'ms' }); } }, self.options.speed); } }, self.options.speed); } else { self.$tooltip.fadeTo(self.options.speed, 0.5, function() { if(self.Status != 'hidden'){ self.$tooltip.fadeTo(self.options.speed, 1); } }); } } } } else { self.hide(); } }, _repositionInfo: function($el) { return { dimension: { height: $el.outerHeight(false), width: $el.outerWidth(false) }, offset: $el.offset(), position: { left: parseInt($el.css('left')), top: parseInt($el.css('top')) } }; }, hide: function(callback) { var self = this; // save the method custom callback and cancel any show method custom callbacks if (callback) self.callbacks.hide.push(callback); self.callbacks.show = []; // get rid of any appearance timeout clearTimeout(self.timerShow); self.timerShow = null; clearTimeout(self.timerHide); self.timerHide = null; var finishCallbacks = function() { // trigger any hide method custom callbacks and reset them $.each(self.callbacks.hide, function(i,c) { c.call(self.$el); }); self.callbacks.hide = []; }; // hide if (self.Status == 'shown' || self.Status == 'appearing') { self.Status = 'disappearing'; var finish = function() { self.Status = 'hidden'; // detach our content object first, so the next jQuery's remove() call does not unbind its event handlers if (typeof self.Content == 'object' && self.Content !== null) { self.Content.detach(); } self.$tooltip.remove(); self.$tooltip = null; // unbind orientationchange, scroll and resize listeners $(window).off('.'+ self.namespace); $('body') // unbind any auto-closing click/touch listeners .off('.'+ self.namespace) .css('overflow-x', self.bodyOverflowX); // unbind any auto-closing click/touch listeners $('body').off('.'+ self.namespace); // unbind any auto-closing hover listeners self.$elProxy.off('.'+ self.namespace + '-autoClose'); // call our constructor custom callback function self.options.functionAfter.call(self.$el, self.$el); // call our method custom callbacks functions finishCallbacks(); }; if (supportsTransitions()) { self.$tooltip .clearQueue() .removeClass('tooltipster-' + self.options.animation + '-show') // for transitions only .addClass('tooltipster-dying'); if(self.options.speed > 0) self.$tooltip.delay(self.options.speed); self.$tooltip.queue(finish); } else { self.$tooltip .stop() .fadeOut(self.options.speed, finish); } } // if the tooltip is already hidden, we still need to trigger the method custom callback else if(self.Status == 'hidden') { finishCallbacks(); } return self; }, // the public show() method is actually an alias for the private showNow() method show: function(callback) { this._showNow(callback); return this; }, // 'update' is deprecated in favor of 'content' but is kept for backward compatibility update: function(c) { return this.content(c); }, content: function(c) { // getter method if(typeof c === 'undefined'){ return this.Content; } // setter method else { this._update(c); return this; } }, reposition: function() { var self = this; // in case the tooltip has been removed from DOM manually if ($('body').find(self.$tooltip).length !== 0) { // reset width self.$tooltip.css('width', ''); // find variables to determine placement self.elProxyPosition = self._repositionInfo(self.$elProxy); var arrowReposition = null, windowWidth = $(window).width(), // shorthand proxy = self.elProxyPosition, tooltipWidth = self.$tooltip.outerWidth(false), tooltipInnerWidth = self.$tooltip.innerWidth() + 1, // this +1 stops FireFox from sometimes forcing an additional text line tooltipHeight = self.$tooltip.outerHeight(false); // if this is an tag inside a