0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-24 21:54:24 +01:00
Bootstrap/js/src/popover.js

129 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-05-12 14:28:11 -07:00
/**
* --------------------------------------------------------------------------
2021-10-09 09:33:12 +03:00
* Bootstrap (v5.1.3): popover.js
2020-06-16 21:41:47 +03:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
2015-05-12 14:28:11 -07:00
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin } from './util/index'
import Tooltip from './tooltip'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/
2019-02-26 13:20:34 +02:00
const NAME = 'popover'
const DATA_KEY = 'bs.popover'
const EVENT_KEY = `.${DATA_KEY}`
const CLASS_PREFIX = 'bs-popover'
2018-09-26 10:39:01 +02:00
const Default = {
...Tooltip.Default,
2019-02-26 13:20:34 +02:00
placement: 'right',
offset: [0, 8],
2019-02-26 13:20:34 +02:00
trigger: 'click',
content: '',
template: '<div class="popover" role="tooltip">' +
2019-02-11 11:27:14 +01:00
'<div class="popover-arrow"></div>' +
'<h3 class="popover-header"></h3>' +
2020-12-02 06:45:15 +02:00
'<div class="popover-body"></div>' +
'</div>'
2018-09-26 10:39:01 +02:00
}
const DefaultType = {
...Tooltip.DefaultType,
2019-02-26 13:20:34 +02:00
content: '(string|element|function)'
2018-09-26 10:39:01 +02:00
}
const Event = {
2019-02-26 13:20: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}`
2018-09-26 10:39:01 +02:00
}
const SELECTOR_TITLE = '.popover-header'
const SELECTOR_CONTENT = '.popover-body'
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
*/
class Popover extends Tooltip {
// Getters
static get Default() {
return Default
}
2015-05-13 14:46:50 -07:00
2018-09-26 10:39:01 +02:00
static get NAME() {
return NAME
2015-05-12 14:28:11 -07:00
}
2018-09-26 10:39:01 +02:00
static get Event() {
return Event
2015-05-12 14:28:11 -07:00
}
2018-09-26 10:39:01 +02:00
static get DefaultType() {
return DefaultType
}
2015-05-12 14:28:11 -07:00
2018-09-26 10:39:01 +02:00
// Overrides
2015-05-12 14:28:11 -07:00
2018-09-26 10:39:01 +02:00
isWithContent() {
return this.getTitle() || this._getContent()
}
2015-05-12 14:28:11 -07:00
setContent(tip) {
this._sanitizeAndSetContent(tip, this.getTitle(), SELECTOR_TITLE)
this._sanitizeAndSetContent(tip, this._getContent(), SELECTOR_CONTENT)
2018-09-26 10:39:01 +02:00
}
2015-05-13 14:46:50 -07:00
2020-06-26 18:40:54 +03:00
// Private
2018-09-26 10:39:01 +02:00
_getContent() {
return this._resolvePossibleFunction(this._config.content)
2018-09-26 10:39:01 +02:00
}
2015-05-12 14:28:11 -07:00
_getBasicClassPrefix() {
return CLASS_PREFIX
2018-09-26 10:39:01 +02:00
}
2017-04-07 13:20:34 +02:00
2018-09-26 10:39:01 +02:00
// Static
2015-05-12 14:28:11 -07:00
2019-07-28 15:24:46 +02:00
static jQueryInterface(config) {
2018-09-26 10:39:01 +02:00
return this.each(function () {
const data = Popover.getOrCreateInstance(this, config)
2015-05-12 14:28:11 -07:00
2018-09-26 10:39:01 +02:00
if (typeof config === 'string') {
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
2015-05-12 14:28:11 -07:00
}
2019-02-26 13:20:34 +02:00
2018-09-26 10:39:01 +02:00
data[config]()
}
})
2015-05-12 14:28:11 -07:00
}
2018-09-26 10:39:01 +02:00
}
2015-05-12 14:28:11 -07:00
2018-09-26 10:39:01 +02:00
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
2020-11-01 15:49:51 +02:00
* add .Popover to jQuery only if jQuery is present
2018-09-26 10:39:01 +02:00
*/
defineJQueryPlugin(Popover)
2015-05-12 14:28:11 -07:00
export default Popover