1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-09-30 07:19:13 +02:00

Extract static DATA_KEY & EVENT_KEY to base-component (#33635)

* Force each plugin that extends base-components to implement a static method `NAME()`
* Remove redundant `NAME` argument from 'Utils.defineJQueryPlugin' & fix test
This commit is contained in:
GeoSot
2021-05-11 10:49:30 +03:00
committed by GitHub
parent 7647b8fe5b
commit 9fe36edf68
16 changed files with 167 additions and 53 deletions

View File

@@ -43,8 +43,8 @@ const CLASS_NAME_SHOW = 'show'
class Alert extends BaseComponent {
// Getters
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -127,6 +127,6 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DISMISS, Alert.handleDi
* add .Alert to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Alert)
defineJQueryPlugin(Alert)
export default Alert

View File

@@ -35,7 +35,7 @@ class BaseComponent {
dispose() {
Data.remove(this._element, this.constructor.DATA_KEY)
EventHandler.off(this._element, `.${this.constructor.DATA_KEY}`)
EventHandler.off(this._element, this.constructor.EVENT_KEY)
Object.getOwnPropertyNames(this).forEach(propertyName => {
this[propertyName] = null
@@ -63,6 +63,18 @@ class BaseComponent {
static get VERSION() {
return VERSION
}
static get NAME() {
throw new Error('You have to implement the static method "NAME", for each component!')
}
static get DATA_KEY() {
return `bs.${this.NAME}`
}
static get EVENT_KEY() {
return `.${this.DATA_KEY}`
}
}
export default BaseComponent

View File

@@ -36,8 +36,8 @@ const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
class Button extends BaseComponent {
// Getters
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -90,6 +90,6 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, event => {
* add .Button to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Button)
defineJQueryPlugin(Button)
export default Button

View File

@@ -127,8 +127,8 @@ class Carousel extends BaseComponent {
return Default
}
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -598,6 +598,6 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
* add .Carousel to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Carousel)
defineJQueryPlugin(Carousel)
export default Carousel

View File

@@ -105,8 +105,8 @@ class Collapse extends BaseComponent {
return Default
}
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -390,6 +390,6 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
* add .Collapse to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Collapse)
defineJQueryPlugin(Collapse)
export default Collapse

View File

@@ -116,8 +116,8 @@ class Dropdown extends BaseComponent {
return DefaultType
}
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -530,6 +530,6 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
* add .Dropdown to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Dropdown)
defineJQueryPlugin(Dropdown)
export default Dropdown

View File

@@ -93,8 +93,8 @@ class Modal extends BaseComponent {
return Default
}
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -441,6 +441,6 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
* add .Modal to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Modal)
defineJQueryPlugin(Modal)
export default Modal

View File

@@ -78,12 +78,12 @@ class Offcanvas extends BaseComponent {
// Getters
static get Default() {
return Default
static get NAME() {
return NAME
}
static get DATA_KEY() {
return DATA_KEY
static get Default() {
return Default
}
// Public
@@ -271,6 +271,6 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
* ------------------------------------------------------------------------
*/
defineJQueryPlugin(NAME, Offcanvas)
defineJQueryPlugin(Offcanvas)
export default Offcanvas

View File

@@ -76,18 +76,10 @@ class Popover extends Tooltip {
return NAME
}
static get DATA_KEY() {
return DATA_KEY
}
static get Event() {
return Event
}
static get EVENT_KEY() {
return EVENT_KEY
}
static get DefaultType() {
return DefaultType
}
@@ -166,6 +158,6 @@ class Popover extends Tooltip {
* add .Popover to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Popover)
defineJQueryPlugin(Popover)
export default Popover

View File

@@ -87,8 +87,8 @@ class ScrollSpy extends BaseComponent {
return Default
}
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -303,6 +303,6 @@ EventHandler.on(window, EVENT_LOAD_DATA_API, () => {
* add .ScrollSpy to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, ScrollSpy)
defineJQueryPlugin(ScrollSpy)
export default ScrollSpy

View File

@@ -55,8 +55,8 @@ const SELECTOR_DROPDOWN_ACTIVE_CHILD = ':scope > .dropdown-menu .active'
class Tab extends BaseComponent {
// Getters
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -220,6 +220,6 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
* add .Tab to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Tab)
defineJQueryPlugin(Tab)
export default Tab

View File

@@ -81,8 +81,8 @@ class Toast extends BaseComponent {
return Default
}
static get DATA_KEY() {
return DATA_KEY
static get NAME() {
return NAME
}
// Public
@@ -243,6 +243,6 @@ class Toast extends BaseComponent {
* add .Toast to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Toast)
defineJQueryPlugin(Toast)
export default Toast

View File

@@ -155,18 +155,10 @@ class Tooltip extends BaseComponent {
return NAME
}
static get DATA_KEY() {
return DATA_KEY
}
static get Event() {
return Event
}
static get EVENT_KEY() {
return EVENT_KEY
}
static get DefaultType() {
return DefaultType
}
@@ -774,6 +766,6 @@ class Tooltip extends BaseComponent {
* add .Tooltip to jQuery only if jQuery is present
*/
defineJQueryPlugin(NAME, Tooltip)
defineJQueryPlugin(Tooltip)
export default Tooltip

View File

@@ -214,11 +214,12 @@ const onDOMContentLoaded = callback => {
const isRTL = () => document.documentElement.dir === 'rtl'
const defineJQueryPlugin = (name, plugin) => {
const defineJQueryPlugin = plugin => {
onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const name = plugin.NAME
const JQUERY_NO_CONFLICT = $.fn[name]
$.fn[name] = plugin.jQueryInterface
$.fn[name].Constructor = plugin