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

98 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-05-12 14:28:11 -07:00
/**
* --------------------------------------------------------------------------
* Bootstrap 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 Tooltip from './tooltip.js'
import { defineJQueryPlugin } from './util/index.js'
2018-09-26 10:39:01 +02:00
/**
* Constants
*/
2019-02-26 13:20:34 +02:00
const NAME = 'popover'
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
const Default = {
...Tooltip.Default,
2019-02-26 13:20:34 +02:00
content: '',
2022-05-19 15:35:44 +02:00
offset: [0, 8],
placement: 'right',
2019-02-26 13:20:34 +02:00
template: '<div class="popover" role="tooltip">' +
2022-05-19 15:35:44 +02:00
'<div class="popover-arrow"></div>' +
'<h3 class="popover-header"></h3>' +
'<div class="popover-body"></div>' +
'</div>',
trigger: 'click'
2018-09-26 10:39:01 +02:00
}
const DefaultType = {
...Tooltip.DefaultType,
content: '(null|string|element|function)'
2018-09-26 10:39:01 +02:00
}
/**
* Class definition
2018-09-26 10:39:01 +02:00
*/
class Popover extends Tooltip {
// Getters
static get Default() {
return Default
}
2015-05-13 14:46:50 -07:00
static get DefaultType() {
return DefaultType
}
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
// Overrides
_isWithContent() {
return this._getTitle() || this._getContent()
2018-09-26 10:39:01 +02:00
}
2015-05-12 14:28:11 -07:00
// Private
_getContentForTemplate() {
return {
[SELECTOR_TITLE]: this._getTitle(),
[SELECTOR_CONTENT]: this._getContent()
}
2018-09-26 10:39:01 +02:00
}
2015-05-13 14:46:50 -07:00
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
2018-09-26 10:39:01 +02:00
// Static
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
2021-11-25 20:33:42 +02:00
if (typeof config !== 'string') {
return
}
2019-02-26 13:20:34 +02:00
2021-11-25 20:33:42 +02:00
if (typeof data[config] === 'undefined') {
throw new TypeError(`No method named "${config}"`)
2018-09-26 10:39:01 +02:00
}
2021-11-25 20:33:42 +02:00
data[config]()
2018-09-26 10:39:01 +02:00
})
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
*/
defineJQueryPlugin(Popover)
2015-05-12 14:28:11 -07:00
export default Popover