0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-05 23:46:20 +01:00
Bootstrap/js/src/popover.js

195 lines
4.6 KiB
JavaScript
Raw Normal View History

import $ from 'jquery'
2015-05-12 23:28:11 +02:00
import Tooltip from './tooltip'
/**
* --------------------------------------------------------------------------
2017-08-11 05:56:35 +02:00
* Bootstrap (v4.0.0-beta): popover.js
2015-05-12 23:28:11 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
const Popover = (() => {
2015-05-12 23:28:11 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
const NAME = 'popover'
2017-08-11 05:56:35 +02:00
const VERSION = '4.0.0-beta'
2015-05-12 23:28:11 +02:00
const DATA_KEY = 'bs.popover'
2015-05-13 21:48:34 +02:00
const EVENT_KEY = `.${DATA_KEY}`
2015-05-12 23:28:11 +02:00
const JQUERY_NO_CONFLICT = $.fn[NAME]
2017-04-07 13:20:34 +02:00
const CLASS_PREFIX = 'bs-popover'
2017-05-24 14:09:36 +02:00
const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')
2015-05-12 23:28:11 +02:00
const Default = $.extend({}, Tooltip.Default, {
placement : 'right',
trigger : 'click',
content : '',
template : '<div class="popover" role="tooltip">'
+ '<div class="arrow"></div>'
+ '<h3 class="popover-header"></h3>'
+ '<div class="popover-body"></div></div>'
2015-05-12 23:28:11 +02:00
})
2015-05-13 23:46:50 +02:00
const DefaultType = $.extend({}, Tooltip.DefaultType, {
content : '(string|element|function)'
2015-05-13 23:46:50 +02:00
})
2015-05-12 23:28:11 +02:00
const ClassName = {
2016-10-28 00:13:17 +02:00
FADE : 'fade',
SHOW : 'show'
2015-05-12 23:28:11 +02:00
}
const Selector = {
TITLE : '.popover-header',
CONTENT : '.popover-body'
2015-05-12 23:28:11 +02:00
}
const Event = {
2015-05-13 21:48:34 +02:00
HIDE : `hide${EVENT_KEY}`,
HIDDEN : `hidden${EVENT_KEY}`,
SHOW : `show${EVENT_KEY}`,
SHOWN : `shown${EVENT_KEY}`,
INSERTED : `inserted${EVENT_KEY}`,
CLICK : `click${EVENT_KEY}`,
FOCUSIN : `focusin${EVENT_KEY}`,
FOCUSOUT : `focusout${EVENT_KEY}`,
MOUSEENTER : `mouseenter${EVENT_KEY}`,
MOUSELEAVE : `mouseleave${EVENT_KEY}`
2015-05-12 23:28:11 +02:00
}
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Popover extends Tooltip {
// getters
static get VERSION() {
return VERSION
}
static get Default() {
return Default
}
static get NAME() {
return NAME
}
static get DATA_KEY() {
return DATA_KEY
}
static get Event() {
return Event
}
2015-05-13 21:48:34 +02:00
static get EVENT_KEY() {
return EVENT_KEY
}
2015-05-13 23:46:50 +02:00
static get DefaultType() {
return DefaultType
}
2015-05-12 23:28:11 +02:00
// overrides
isWithContent() {
return this.getTitle() || this._getContent()
}
2017-04-07 13:20:34 +02:00
addAttachmentClass(attachment) {
$(this.getTipElement()).addClass(`${CLASS_PREFIX}-${attachment}`)
}
2015-05-12 23:28:11 +02:00
getTipElement() {
2017-07-27 12:58:22 +02:00
this.tip = this.tip || $(this.config.template)[0]
return this.tip
2015-05-12 23:28:11 +02:00
}
setContent() {
const $tip = $(this.getTipElement())
2015-05-12 23:28:11 +02:00
// we use append for html objects to maintain js events
this.setElementContent($tip.find(Selector.TITLE), this.getTitle())
this.setElementContent($tip.find(Selector.CONTENT), this._getContent())
2015-05-12 23:28:11 +02:00
2016-10-28 00:13:17 +02:00
$tip.removeClass(`${ClassName.FADE} ${ClassName.SHOW}`)
2015-05-12 23:28:11 +02:00
}
// private
_getContent() {
return this.element.getAttribute('data-content')
2015-08-19 04:22:46 +02:00
|| (typeof this.config.content === 'function' ?
2015-05-12 23:28:11 +02:00
this.config.content.call(this.element) :
this.config.content)
}
2017-04-07 13:20:34 +02:00
_cleanTipClass() {
const $tip = $(this.getTipElement())
const tabClass = $tip.attr('class').match(BSCLS_PREFIX_REGEX)
if (tabClass !== null && tabClass.length > 0) {
$tip.removeClass(tabClass.join(''))
}
}
2015-05-12 23:28:11 +02:00
// static
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = typeof config === 'object' ? config : null
2015-05-12 23:28:11 +02:00
if (!data && /destroy|hide/.test(config)) {
return
}
if (!data) {
data = new Popover(this, _config)
$(this).data(DATA_KEY, data)
}
if (typeof config === 'string') {
2017-07-27 12:39:55 +02:00
if (typeof data[config] === 'undefined') {
throw new Error(`No method named "${config}"`)
}
2015-05-12 23:28:11 +02:00
data[config]()
}
})
}
}
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/
$.fn[NAME] = Popover._jQueryInterface
$.fn[NAME].Constructor = Popover
$.fn[NAME].noConflict = function () {
$.fn[NAME] = JQUERY_NO_CONFLICT
return Popover._jQueryInterface
}
return Popover
2017-10-03 14:27:36 +02:00
})($)
2015-05-12 23:28:11 +02:00
export default Popover