Last active
July 8, 2016 11:39
-
-
Save andresgutgon/3e19c3b7f25d003125a2c605f3f04d03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | |
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | |
'use strict'; | |
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | |
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | |
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | |
(function ($) { | |
var MODIFIERS = ['shift', 'offset', 'preventOverflow', 'keepTogether', 'arrow', 'flip', 'applyStyle']; | |
// With this config Popper will try to position | |
// the popover inside the viewport and without overlap the button. | |
var AUTO_POSITION_DEFAULT = { | |
flipBehavior: ['bottom', 'top', 'left', 'right'], | |
preventOverflowOrder: ['top', 'bottom', 'left', 'right'] | |
}; | |
var POPPER_DEFAULT_OPTIONS = { | |
placement: 'bottom', | |
removeOnDestroy: true | |
}; | |
var Tooltip = function Tooltip(element, options) { | |
this.init('tooltip', element, options); | |
}; | |
Tooltip.prototype = { | |
constructor: Tooltip, | |
/** | |
* Init | |
* | |
* @param {String} type | |
* @param {DOM} element | |
* @param {Object} options | |
*/ | |
init: function init(type, element, options) { | |
this.type = type; | |
this.$element = $(element); | |
this.options = this.getOptions(options); | |
this.enabled = true; | |
var triggers = this.options.trigger.split(' '); | |
for (var i = triggers.length; i--;) { | |
var trigger = triggers[i]; | |
if (trigger === 'click') { | |
this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this)); | |
} else if (trigger !== 'manual') { | |
var eventIn = trigger === 'hover' ? 'mouseenter' : 'focus'; | |
var eventOut = trigger === 'hover' ? 'mouseleave' : 'blur'; | |
this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this)); | |
this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this)); | |
} | |
} | |
if (this.options.selector) { | |
this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' }); | |
} else { | |
this.fixTitle(); | |
} | |
this.initialUpdatePosition = _.once(this.updatePosition); | |
}, | |
/** | |
* Get options | |
* | |
* @param {Object} opt | |
*/ | |
getOptions: function getOptions(opt) { | |
var options = $.extend({}, $.fn[this.type].defaults, this.$element.data(), opt); | |
if (options.delay && typeof options.delay === 'number') { | |
options.delay = { | |
show: options.delay, | |
hide: options.delay | |
}; | |
} | |
return options; | |
}, | |
/** | |
* Enter action | |
* | |
* @param {Event} event | |
*/ | |
enter: function enter(event) { | |
var defaults = $.fn[this.type].defaults; | |
var options = {}; | |
this._options && $.each(this._options, function (key, value) { | |
if (defaults[key] !== value) { | |
options[key] = value; | |
} | |
}, this); | |
var tooltip = $(event.currentTarget)[this.type](options).data(this.type); | |
if (!tooltip.options.delay || !tooltip.options.delay.show) return tooltip.show(); | |
clearTimeout(this.timeout); | |
tooltip.hoverState = 'in'; | |
this.timeout = setTimeout(function () { | |
if (tooltip.hoverState === 'in') { | |
tooltip.show(); | |
} | |
}, tooltip.options.delay.show); | |
}, | |
/** | |
* Leave action | |
* | |
* @param {Event} event | |
*/ | |
leave: function leave(event) { | |
var tooltip = $(event.currentTarget)[this.type](this._options).data(this.type); | |
if (this.timeout) clearTimeout(this.timeout); | |
if (!tooltip.options.delay || !tooltip.options.delay.hide) return tooltip.hide(); | |
tooltip.hoverState = 'out'; | |
this.timeout = setTimeout(function () { | |
if (tooltip.hoverState === 'out') { | |
tooltip.hide(); | |
} | |
}, tooltip.options.delay.hide); | |
}, | |
/** | |
* Show action | |
*/ | |
show: function show() { | |
if (!this.hasContent() || !this.enabled) return; | |
var event = $.Event('show'); | |
this.$element.trigger(event); | |
if (event.isDefaultPrevented()) return; | |
var $tip = this.tip(); | |
var _options = this.options; | |
var animation = _options.animation; | |
var outer_css_class = _options.outer_css_class; | |
var detach = _options.detach; | |
var classes = outer_css_class + ' ' || ''; | |
classes = animation ? 'fade in' : 'in'; | |
if (detach) { | |
$tip.detach(); | |
} | |
$tip.addClass(classes).css({ top: 0, left: 0, display: 'block' }); | |
this.addToDOM($tip); | |
if (detach) { | |
this.updatePosition(); | |
} else { | |
this.initialUpdatePosition(); | |
} | |
this.$element.trigger('shown'); | |
}, | |
/** | |
* Add detached tooltip to DOM | |
* | |
* @param {jQuery} $tip | |
*/ | |
addToDOM: function addToDOM($tip) { | |
var _options2 = this.options; | |
var container = _options2.container; | |
var detach = _options2.detach; | |
if (!detach) return; | |
if (container) { | |
$tip.appendTo(container); | |
} else { | |
$tip.insertAfter(this.$element); | |
} | |
}, | |
/** | |
* Hide content | |
*/ | |
hide: function hide() { | |
var event = $.Event('hide'); | |
this.$element.trigger(event); | |
if (event.isDefaultPrevented()) return; | |
this.$element.trigger('hidden'); | |
var detach = this.options.detach; | |
var $tip = this.$tip; | |
if (!$tip) return this; | |
$tip.css('zIndex', -10000) // Under everything when not visible | |
.removeClass('in'); | |
if (!detach) return this; | |
/** | |
* remove animation | |
*/ | |
function removeWithAnimation() { | |
var timeout = setTimeout(function () { | |
$tip.off($.support.transition.end).detach(); | |
}, 300); | |
$tip.one($.support.transition.end, function () { | |
clearTimeout(timeout); | |
$tip.detach(); | |
}); | |
} | |
if ($.support.transition && $tip.hasClass('fade')) { | |
removeWithAnimation(); | |
} else { | |
$tip.detach(); | |
} | |
return this; | |
}, | |
/** | |
* Fix title | |
*/ | |
fixTitle: function fixTitle() { | |
var $element = this.$element; | |
if ($element.attr('title') || typeof $element.attr('data-original-title') !== 'string') { | |
$element.attr('data-original-title', $element.attr('title') || '').attr('title', ''); | |
} | |
}, | |
/** | |
* Check if has content | |
* | |
* @return {Boolean} | |
*/ | |
hasContent: function hasContent() { | |
return !!this.getTitle(); | |
}, | |
/** | |
* Get title | |
* | |
* @return {String} | |
*/ | |
getTitle: function getTitle() { | |
var $element = this.$element; | |
var options = this.options; | |
return $element.attr('data-original-title') || (typeof options.title === 'function' ? options.title.call($element[0]) : options.title); | |
}, | |
/** | |
* Check if popover has been shown | |
* | |
* @retun {Boolean} | |
*/ | |
isShown: function isShown() { | |
if (!this.popper_instance) return false; | |
var $tip = this.$tip || this.tip(); | |
return $tip.hasClass('in'); | |
}, | |
/** | |
* Set content 1 | |
*/ | |
setContent: function setContent() { | |
return this.getTitle(); | |
}, | |
/** | |
* Init popper instance | |
* | |
* @return {Object} | |
*/ | |
initPopperInstance: function initPopperInstance() { | |
if (this.popper_instance) return this.popper_instance; | |
var _options3 = this.options; | |
var parent = _options3.parent; | |
var popperClassNames = _options3.popperClassNames; | |
var popperExtraClassNames = _options3.popperExtraClassNames; | |
var popperArrowClassNames = _options3.popperArrowClassNames; | |
var classNames = popperClassNames || ['tooltip', 'tooltip-inner']; | |
if (popperExtraClassNames) classNames.push(popperExtraClassNames); | |
var arrowClassNames = popperArrowClassNames || ['tooltip-arrow']; | |
var content = this.setContent(); | |
var contentType = this.getContentType(content); | |
var popperObject = { | |
content: contentType === 'node' ? content[0] : content, | |
contentType: contentType, | |
classNames: classNames, | |
arrowClassNames: arrowClassNames | |
}; | |
if (parent) popperObject = _extends({}, popperObject, { parent: parent }); | |
var popperOptions = this.getPopperOptions(); | |
return new Popper(this.$element, popperObject, _extends({}, POPPER_DEFAULT_OPTIONS, popperOptions, { | |
modifiers: this.getModifiers() | |
})); | |
}, | |
/** | |
* Get popper content type | |
* | |
* @param {HTML|String} content | |
* @return {String} | |
*/ | |
getContentType: function getContentType(content) { | |
var html = this.options.html; | |
if (!html || !content) return 'text'; | |
if (_.isString(content)) return 'html'; | |
if (!!content.length) return 'node'; | |
return 'text'; | |
}, | |
/** | |
* Get popper options | |
* | |
* @return {Object} | |
*/ | |
getPopperOptions: function getPopperOptions() { | |
var _options4 = this.options; | |
var placement = _options4.placement; | |
var boundaries = _options4.boundaries; | |
var flipBehavior = _options4.flipBehavior; | |
var preventOverflowOrder = _options4.preventOverflowOrder; | |
var autoPosition = _options4.autoPosition; | |
var options = { | |
placement: this.getPlacement(placement), | |
gpuAcceleration: true }; | |
// use transform instead of let/top absolute for positioning | |
if (boundaries) options = _extends({}, options, { boundariesElement: boundaries }); | |
if (autoPosition) options = _extends({}, options, AUTO_POSITION_DEFAULT); | |
if (flipBehavior) options = _extends({}, options, { flipBehavior: flipBehavior }); | |
if (preventOverflowOrder) options = _extends({}, options, { preventOverflowOrder: preventOverflowOrder }); | |
return options; | |
}, | |
/** | |
* Maintain compatibility with old tooltips | |
* | |
* @param {Array} | |
*/ | |
getModifiers: function getModifiers() { | |
var fixToReference = this.options.fixToReference; | |
/** | |
* Check if reference is visible inside boundaries | |
* | |
* @param {Object} data | |
* @param {Object} boundariesElement | |
* @return {Boolean} | |
*/ | |
function isVisible(data, boundariesElement) { | |
var boundaries = data.boundaries; | |
var offsets = data.offsets; | |
var _offsets$reference = offsets.reference; | |
var top = _offsets$reference.top; | |
var bottom = _offsets$reference.bottom; | |
var boundariesHeight = boundaries.height; | |
var boundariesOffsetTop = $(boundariesElement).offset().top; | |
var boundariesBottom = boundariesOffsetTop + boundariesHeight; | |
return top <= boundariesBottom && bottom >= boundariesOffsetTop; | |
} | |
/** | |
* Modifier to hide popper if is outside boundaries | |
* If boundaries is not defined we skip the check | |
* | |
* @param {Object} data | |
* @return {Object} | |
*/ | |
var hideOnReferenceNotVisibleOnBoundaries = function hideOnReferenceNotVisibleOnBoundaries(data) { | |
var boundariesElement = data.instance._options.boundariesElement; | |
var boundaries = data.boundaries; | |
if (boundariesElement === 'viewport' || _.isNaN(boundaries.top)) return data; | |
var visible = isVisible(data, boundariesElement); | |
// Hide popper | |
var opacity = visible ? 1 : 0; | |
var pointerEvents = visible ? '' : 'none'; | |
$(data.instance._popper).css({ opacity: opacity, pointerEvents: pointerEvents }); | |
return data; | |
}; | |
var modifiers = [].concat(MODIFIERS, [hideOnReferenceNotVisibleOnBoundaries]); | |
if (!fixToReference) return modifiers; | |
var fixToReferenceModifier = function fixToReferenceModifier() { | |
return function (data) { | |
var _data$offsets = data.offsets; | |
var popper = _data$offsets.popper; | |
var reference = _data$offsets.reference; | |
var top = reference.top; | |
var height = reference.height; | |
popper.top = top + height; | |
return data; | |
}; | |
}; | |
return [].concat(_toConsumableArray(modifiers), [fixToReferenceModifier()]); | |
}, | |
/** | |
* Maintain compatibility with old tooltips | |
* | |
* @param {String} placement | |
*/ | |
getPlacement: function getPlacement(placement) { | |
var placementText = _.isFunction(placement) ? placement() : placement; | |
return placementText.replace('\-left', '-start').replace('\-right', '-end'); | |
}, | |
/** | |
* Return tip. Content of tooltip | |
* | |
* @return {String} | |
*/ | |
tip: function tip() { | |
if (!this.popper_instance) { | |
this.popper_instance = this.initPopperInstance(); | |
this.$tip = $(this.popper_instance._popper); | |
} | |
return this.$tip; | |
}, | |
/** | |
* Return arrow. | |
* | |
* @return {String} | |
*/ | |
arrow: function arrow() { | |
var $tip = this.tip(); | |
this.$arrow = this.$arrow || $tip && $tip().find('.tooltip-arrow'); | |
return this.$arrow; | |
}, | |
/** | |
* Update position of popper | |
*/ | |
updatePosition: function updatePosition() { | |
if (!this.popper_instance) return; | |
this.popper_instance.update(); | |
}, | |
/** | |
* Destroy popper instance | |
* | |
* @return {jQuery} | |
*/ | |
destroyPopper: function destroyPopper() { | |
if (!this.popper_instance) return this; | |
this.popper_instance.destroy(); | |
return this; | |
}, | |
/** | |
* Destroy all jQuery and popper events/elements | |
* | |
* @param {Event} event | |
*/ | |
destroy: function destroy() { | |
this.hide().destroyPopper().$element.off('.' + this.type).removeData(this.type); | |
} | |
}; | |
var old = $.fn.tooltip; | |
$.fn.tooltip = function (option) { | |
return this.each(function () { | |
var $this = $(this); | |
var options = (typeof option === 'undefined' ? 'undefined' : _typeof(option)) === 'object' && option; | |
var data = $this.data('tooltip'); | |
if (!data) $this.data('tooltip', data = new Tooltip(this, options)); | |
if (typeof option === 'string') data[option](); | |
}); | |
}; | |
$.fn.tooltip.Constructor = Tooltip; | |
$.fn.tooltip.defaults = { | |
animation: true, | |
placement: 'top', | |
selector: false, | |
template: '<div class="js-tooltip tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>', | |
trigger: 'hover focus', | |
title: '', | |
delay: 0, | |
html: false, | |
container: false, | |
detach: false | |
}; | |
$.fn.tooltip.noConflict = function () { | |
$.fn.tooltip = old; | |
return this; | |
}; | |
})(window.jQuery); | |
(function ($) { | |
var MODIFIERS = ['shift', 'offset', 'preventOverflow', 'keepTogether', 'arrow', 'flip', 'applyStyle']; | |
// With this config Popper will try to position | |
// the popover inside the viewport and without overlap the button. | |
var AUTO_POSITION_DEFAULT = { | |
flipBehavior: ['bottom', 'top', 'left', 'right'], | |
preventOverflowOrder: ['top', 'bottom', 'left', 'right'] | |
}; | |
var POPPER_DEFAULT_OPTIONS = { | |
placement: 'bottom', | |
removeOnDestroy: true | |
}; | |
var Tooltip = function Tooltip(element, options) { | |
this.init('tooltip', element, options); | |
}; | |
Tooltip.prototype = { | |
constructor: Tooltip, | |
/** | |
* Init | |
* | |
* @param {String} type | |
* @param {DOM} element | |
* @param {Object} options | |
*/ | |
init: function init(type, element, options) { | |
this.type = type; | |
this.$element = $(element); | |
this.options = this.getOptions(options); | |
this.enabled = true; | |
var triggers = this.options.trigger.split(' '); | |
for (var i = triggers.length; i--;) { | |
var trigger = triggers[i]; | |
if (trigger === 'click') { | |
this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this)); | |
} else if (trigger !== 'manual') { | |
var eventIn = trigger === 'hover' ? 'mouseenter' : 'focus'; | |
var eventOut = trigger === 'hover' ? 'mouseleave' : 'blur'; | |
this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this)); | |
this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this)); | |
} | |
} | |
if (this.options.selector) { | |
this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' }); | |
} else { | |
this.fixTitle(); | |
} | |
this.initialUpdatePosition = _.once(this.updatePosition); | |
}, | |
/** | |
* Get options | |
* | |
* @param {Object} opt | |
*/ | |
getOptions: function getOptions(opt) { | |
var options = $.extend({}, $.fn[this.type].defaults, this.$element.data(), opt); | |
if (options.delay && typeof options.delay === 'number') { | |
options.delay = { | |
show: options.delay, | |
hide: options.delay | |
}; | |
} | |
return options; | |
}, | |
/** | |
* Enter action | |
* | |
* @param {Event} event | |
*/ | |
enter: function enter(event) { | |
var defaults = $.fn[this.type].defaults; | |
var options = {}; | |
this._options && $.each(this._options, function (key, value) { | |
if (defaults[key] !== value) { | |
options[key] = value; | |
} | |
}, this); | |
var tooltip = $(event.currentTarget)[this.type](options).data(this.type); | |
if (!tooltip.options.delay || !tooltip.options.delay.show) return tooltip.show(); | |
clearTimeout(this.timeout); | |
tooltip.hoverState = 'in'; | |
this.timeout = setTimeout(function () { | |
if (tooltip.hoverState === 'in') { | |
tooltip.show(); | |
} | |
}, tooltip.options.delay.show); | |
}, | |
/** | |
* Leave action | |
* | |
* @param {Event} event | |
*/ | |
leave: function leave(event) { | |
var tooltip = $(event.currentTarget)[this.type](this._options).data(this.type); | |
if (this.timeout) clearTimeout(this.timeout); | |
if (!tooltip.options.delay || !tooltip.options.delay.hide) return tooltip.hide(); | |
tooltip.hoverState = 'out'; | |
this.timeout = setTimeout(function () { | |
if (tooltip.hoverState === 'out') { | |
tooltip.hide(); | |
} | |
}, tooltip.options.delay.hide); | |
}, | |
/** | |
* Show action | |
*/ | |
show: function show() { | |
if (!this.hasContent() || !this.enabled) return; | |
var event = $.Event('show'); | |
this.$element.trigger(event); | |
if (event.isDefaultPrevented()) return; | |
var $tip = this.tip(); | |
var _options = this.options; | |
var animation = _options.animation; | |
var outer_css_class = _options.outer_css_class; | |
var detach = _options.detach; | |
var classes = outer_css_class + ' ' || ''; | |
classes = animation ? 'fade in' : 'in'; | |
if (detach) { | |
$tip.detach(); | |
} | |
$tip.addClass(classes).css({ top: 0, left: 0, display: 'block' }); | |
this.addToDOM($tip); | |
if (detach) { | |
this.updatePosition(); | |
} else { | |
this.initialUpdatePosition(); | |
} | |
this.$element.trigger('shown'); | |
}, | |
/** | |
* Add detached tooltip to DOM | |
* | |
* @param {jQuery} $tip | |
*/ | |
addToDOM: function addToDOM($tip) { | |
var _options2 = this.options; | |
var container = _options2.container; | |
var detach = _options2.detach; | |
if (!detach) return; | |
if (container) { | |
$tip.appendTo(container); | |
} else { | |
$tip.insertAfter(this.$element); | |
} | |
}, | |
/** | |
* Hide content | |
*/ | |
hide: function hide() { | |
var event = $.Event('hide'); | |
this.$element.trigger(event); | |
if (event.isDefaultPrevented()) return; | |
this.$element.trigger('hidden'); | |
var detach = this.options.detach; | |
var $tip = this.$tip; | |
if (!$tip) return this; | |
$tip.removeClass('in'); | |
if (!detach) return this; | |
/** | |
* remove animation | |
*/ | |
function removeWithAnimation() { | |
var timeout = setTimeout(function () { | |
$tip.off($.support.transition.end).detach(); | |
}, 300); | |
$tip.one($.support.transition.end, function () { | |
clearTimeout(timeout); | |
$tip.detach(); | |
}); | |
} | |
if ($.support.transition && $tip.hasClass('fade')) { | |
removeWithAnimation(); | |
} else { | |
$tip.detach(); | |
} | |
return this; | |
}, | |
/** | |
* Fix title | |
*/ | |
fixTitle: function fixTitle() { | |
var $element = this.$element; | |
if ($element.attr('title') || typeof $element.attr('data-original-title') !== 'string') { | |
$element.attr('data-original-title', $element.attr('title') || '').attr('title', ''); | |
} | |
}, | |
/** | |
* Check if has content | |
* | |
* @return {Boolean} | |
*/ | |
hasContent: function hasContent() { | |
return !!this.getTitle(); | |
}, | |
/** | |
* Get title | |
* | |
* @return {String} | |
*/ | |
getTitle: function getTitle() { | |
var $element = this.$element; | |
var options = this.options; | |
return $element.attr('data-original-title') || (typeof options.title === 'function' ? options.title.call($element[0]) : options.title); | |
}, | |
/** | |
* Check if popover has been shown | |
* | |
* @retun {Boolean} | |
*/ | |
isShown: function isShown() { | |
if (!this.popper_instance) return false; | |
var $tip = this.$tip || this.tip(); | |
return $tip.hasClass('in'); | |
}, | |
/** | |
* Set content 1 | |
*/ | |
setContent: function setContent() { | |
return this.getTitle(); | |
}, | |
/** | |
* Init popper instance | |
* | |
* @return {Object} | |
*/ | |
initPopperInstance: function initPopperInstance() { | |
if (this.popper_instance) return this.popper_instance; | |
var _options3 = this.options; | |
var parent = _options3.parent; | |
var popperClassNames = _options3.popperClassNames; | |
var popperExtraClassNames = _options3.popperExtraClassNames; | |
var popperArrowClassNames = _options3.popperArrowClassNames; | |
var classNames = popperClassNames || ['tooltip', 'tooltip-inner']; | |
if (popperExtraClassNames) classNames.push(popperExtraClassNames); | |
var arrowClassNames = popperArrowClassNames || ['tooltip-arrow']; | |
var content = this.setContent(); | |
var contentType = this.getContentType(content); | |
var popperObject = { | |
content: contentType === 'node' ? content[0] : content, | |
contentType: contentType, | |
classNames: classNames, | |
arrowClassNames: arrowClassNames | |
}; | |
if (parent) popperObject = _extends({}, popperObject, { parent: parent }); | |
var popperOptions = this.getPopperOptions(); | |
return new Popper(this.$element, popperObject, _extends({}, POPPER_DEFAULT_OPTIONS, popperOptions, { | |
modifiers: this.getModifiers() | |
})); | |
}, | |
/** | |
* Get popper content type | |
* | |
* @param {HTML|String} content | |
* @return {String} | |
*/ | |
getContentType: function getContentType(content) { | |
var html = this.options.html; | |
if (!html || !content) return 'text'; | |
if (_.isString(content)) return 'html'; | |
if (!!content.length) return 'node'; | |
return 'text'; | |
}, | |
/** | |
* Get popper options | |
* | |
* @return {Object} | |
*/ | |
getPopperOptions: function getPopperOptions() { | |
var _options4 = this.options; | |
var placement = _options4.placement; | |
var boundaries = _options4.boundaries; | |
var flipBehavior = _options4.flipBehavior; | |
var preventOverflowOrder = _options4.preventOverflowOrder; | |
var autoPosition = _options4.autoPosition; | |
var options = { | |
placement: this.getPlacement(placement), | |
gpuAcceleration: true }; | |
// use transform instead of let/top absolute for positioning | |
if (boundaries) options = _extends({}, options, { boundariesElement: boundaries }); | |
if (autoPosition) options = _extends({}, options, AUTO_POSITION_DEFAULT); | |
if (flipBehavior) options = _extends({}, options, { flipBehavior: flipBehavior }); | |
if (preventOverflowOrder) options = _extends({}, options, { preventOverflowOrder: preventOverflowOrder }); | |
return options; | |
}, | |
/** | |
* Maintain compatibility with old tooltips | |
* | |
* @param {Array} | |
*/ | |
getModifiers: function getModifiers() { | |
var fixToReference = this.options.fixToReference; | |
/** | |
* Check if reference is visible inside boundaries | |
* | |
* @param {Object} data | |
* @param {Object} boundariesElement | |
* @return {Boolean} | |
*/ | |
function isVisible(data, boundariesElement) { | |
var boundaries = data.boundaries; | |
var offsets = data.offsets; | |
var _offsets$reference = offsets.reference; | |
var top = _offsets$reference.top; | |
var bottom = _offsets$reference.bottom; | |
var boundariesHeight = boundaries.height; | |
var boundariesOffsetTop = $(boundariesElement).offset().top; | |
var boundariesBottom = boundariesOffsetTop + boundariesHeight; | |
return top <= boundariesBottom && bottom >= boundariesOffsetTop; | |
} | |
/** | |
* Modifier to hide popper if is outside boundaries | |
* If boundaries is not defined we skip the check | |
* | |
* @param {Object} data | |
* @return {Object} | |
*/ | |
var hideOnReferenceNotVisibleOnBoundaries = function hideOnReferenceNotVisibleOnBoundaries(data) { | |
var boundariesElement = data.instance._options.boundariesElement; | |
var boundaries = data.boundaries; | |
if (boundariesElement === 'viewport' || _.isNaN(boundaries.top)) return data; | |
var visible = isVisible(data, boundariesElement); | |
// Hide popper | |
var opacity = visible ? 1 : 0; | |
var pointerEvents = visible ? '' : 'none'; | |
$(data.instance._popper).css({ opacity: opacity, pointerEvents: pointerEvents }); | |
return data; | |
}; | |
var modifiers = [].concat(MODIFIERS, [hideOnReferenceNotVisibleOnBoundaries]); | |
if (!fixToReference) return modifiers; | |
var fixToReferenceModifier = function fixToReferenceModifier() { | |
return function (data) { | |
var _data$offsets = data.offsets; | |
var popper = _data$offsets.popper; | |
var reference = _data$offsets.reference; | |
var top = reference.top; | |
var height = reference.height; | |
popper.top = top + height; | |
return data; | |
}; | |
}; | |
return [].concat(_toConsumableArray(modifiers), [fixToReferenceModifier()]); | |
}, | |
/** | |
* Maintain compatibility with old tooltips | |
* | |
* @param {String} placement | |
*/ | |
getPlacement: function getPlacement(placement) { | |
var placementText = _.isFunction(placement) ? placement() : placement; | |
return placementText.replace('\-left', '-start').replace('\-right', '-end'); | |
}, | |
/** | |
* Return tip. Content of tooltip | |
* | |
* @return {String} | |
*/ | |
tip: function tip() { | |
if (!this.popper_instance) { | |
this.popper_instance = this.initPopperInstance(); | |
this.$tip = $(this.popper_instance._popper); | |
} | |
return this.$tip; | |
}, | |
/** | |
* Return arrow. | |
* | |
* @return {String} | |
*/ | |
arrow: function arrow() { | |
var $tip = this.tip(); | |
this.$arrow = this.$arrow || $tip && $tip().find('.tooltip-arrow'); | |
return this.$arrow; | |
}, | |
/** | |
* Update position of popper | |
*/ | |
updatePosition: function updatePosition() { | |
if (!this.popper_instance) return; | |
this.popper_instance.update(); | |
}, | |
/** | |
* Destroy popper instance | |
* | |
* @return {jQuery} | |
*/ | |
destroyPopper: function destroyPopper() { | |
if (!this.popper_instance) return this; | |
this.popper_instance.destroy(); | |
return this; | |
}, | |
/** | |
* Destroy all jQuery and popper events/elements | |
* | |
* @param {Event} event | |
*/ | |
destroy: function destroy() { | |
this.hide().destroyPopper().$element.off('.' + this.type).removeData(this.type); | |
} | |
}; | |
var old = $.fn.tooltip; | |
$.fn.tooltip = function (option) { | |
return this.each(function () { | |
var $this = $(this); | |
var options = (typeof option === 'undefined' ? 'undefined' : _typeof(option)) === 'object' && option; | |
var data = $this.data('tooltip'); | |
if (!data) $this.data('tooltip', data = new Tooltip(this, options)); | |
if (typeof option === 'string') data[option](); | |
}); | |
}; | |
$.fn.tooltip.Constructor = Tooltip; | |
$.fn.tooltip.defaults = { | |
animation: true, | |
placement: 'top', | |
selector: false, | |
template: '<div class="js-tooltip tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>', | |
trigger: 'hover focus', | |
title: '', | |
delay: 0, | |
html: false, | |
container: false, | |
detach: false | |
}; | |
$.fn.tooltip.noConflict = function () { | |
$.fn.tooltip = old; | |
return this; | |
}; | |
})(window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment