0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-26 23:54:23 +01:00

transfer defineJQueryPlugin to jquery-stuff.js file

This commit is contained in:
GeoSot 2021-12-03 20:15:36 +02:00
parent 30fd460c6d
commit f5462d47d4
16 changed files with 57 additions and 65 deletions

View File

@ -5,10 +5,10 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin } from './util/index'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
import { enableDismissTrigger } from './util/component-functions'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -5,9 +5,9 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin } from './util/index'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -6,7 +6,6 @@
*/
import {
defineJQueryPlugin,
getElementFromSelector,
getNextActiveElement,
isRTL,
@ -19,6 +18,7 @@ import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
import Swipe from './util/swipe'
import BaseComponent from './base-component'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -6,7 +6,6 @@
*/
import {
defineJQueryPlugin,
getElement,
getElementFromSelector,
getSelectorFromElement,
@ -15,6 +14,7 @@ import {
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -7,7 +7,6 @@
import * as Popper from '@popperjs/core'
import {
defineJQueryPlugin,
getElement,
getNextActiveElement,
isDisabled,
@ -20,6 +19,7 @@ import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -5,7 +5,7 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin, getElementFromSelector, isRTL, isVisible, reflow, typeCheckConfig } from './util/index'
import { getElementFromSelector, isRTL, isVisible, reflow } from './util/index'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
import ScrollBarHelper from './util/scrollbar'
@ -13,6 +13,7 @@ import BaseComponent from './base-component'
import Backdrop from './util/backdrop'
import FocusTrap from './util/focustrap'
import { enableDismissTrigger } from './util/component-functions'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants
@ -30,7 +31,6 @@ const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_RESIZE = `resize${EVENT_KEY}`
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

View File

@ -6,7 +6,6 @@
*/
import {
defineJQueryPlugin,
getElementFromSelector,
isDisabled,
isVisible
@ -18,6 +17,7 @@ import SelectorEngine from './dom/selector-engine'
import Backdrop from './util/backdrop'
import FocusTrap from './util/focustrap'
import { enableDismissTrigger } from './util/component-functions'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -5,8 +5,8 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin } from './util/index'
import Tooltip from './tooltip'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -5,10 +5,11 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin, getElement, isDisabled, isVisible } from './util/index'
import { getElement, isDisabled, isVisible } from './util/index'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -5,10 +5,11 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin, getElementFromSelector, getNextActiveElement, isDisabled } from './util/index'
import { getElementFromSelector, getNextActiveElement, isDisabled } from './util/index'
import EventHandler from './dom/event-handler'
import SelectorEngine from './dom/selector-engine'
import BaseComponent from './base-component'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -5,10 +5,11 @@
* --------------------------------------------------------------------------
*/
import { defineJQueryPlugin, reflow } from './util/index'
import { reflow } from './util/index'
import EventHandler from './dom/event-handler'
import BaseComponent from './base-component'
import { enableDismissTrigger } from './util/component-functions'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -6,12 +6,13 @@
*/
import * as Popper from '@popperjs/core'
import { defineJQueryPlugin, findShadowRoot, getElement, getUID, isRTL, noop } from './util/index'
import { findShadowRoot, getElement, getUID, isRTL, noop } from './util/index'
import { DefaultAllowlist } from './util/sanitizer'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
import BaseComponent from './base-component'
import TemplateFactory from './util/template-factory'
import { defineJQueryPlugin } from './util/jquery-stuff'
/**
* Constants

View File

@ -4,8 +4,6 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import { getjQuery, getJqueryInterfaceForPlugin } from './jquery-stuff'
const MAX_UID = 1_000_000
const MILLISECONDS_MULTIPLIER = 1000
const TRANSITION_END = 'transitionend'
@ -225,24 +223,6 @@ const onDOMContentLoaded = callback => {
const isRTL = () => document.documentElement.dir === 'rtl'
const defineJQueryPlugin = plugin => {
onDOMContentLoaded(() => {
const $ = getjQuery()
const callback = getJqueryInterfaceForPlugin(plugin)
/* istanbul ignore if */
if ($) {
const name = plugin.NAME
const JQUERY_NO_CONFLICT = $.fn[name]
$.fn[name] = callback
$.fn[name].Constructor = plugin
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT
return callback
}
}
})
}
const execute = callback => {
if (typeof callback === 'function') {
callback()
@ -307,7 +287,6 @@ const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed
}
export {
defineJQueryPlugin,
execute,
executeAfterTransition,
findShadowRoot,

View File

@ -4,6 +4,7 @@
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
import { onDOMContentLoaded } from './index'
const getjQuery = () => {
const { jQuery } = window
@ -15,6 +16,24 @@ const getjQuery = () => {
return null
}
const defineJQueryPlugin = plugin => {
onDOMContentLoaded(() => {
const $ = getjQuery()
/* istanbul ignore if */
if ($) {
const callback = getJqueryInterfaceForPlugin(plugin)
const name = plugin.NAME
const JQUERY_NO_CONFLICT = $.fn[name]
$.fn[name] = callback
$.fn[name].Constructor = plugin
$.fn[name].noConflict = () => {
$.fn[name] = JQUERY_NO_CONFLICT
return callback
}
}
})
}
const defaultJQueryInterface = plugin => {
return function (config) {
return this.each(function () {
@ -36,6 +55,7 @@ const defaultJQueryInterface = plugin => {
const getJqueryInterfaceForPlugin = plugin => plugin.jQueryInterface || defaultJQueryInterface(plugin)
export {
defineJQueryPlugin,
getjQuery,
getJqueryInterfaceForPlugin
}

View File

@ -566,32 +566,6 @@ describe('Util', () => {
})
})
describe('defineJQueryPlugin', () => {
const fakejQuery = { fn: {} }
beforeEach(() => {
Object.defineProperty(window, 'jQuery', {
value: fakejQuery,
writable: true
})
})
afterEach(() => {
window.jQuery = undefined
})
it('should define a plugin on the jQuery instance', () => {
const pluginMock = Util.noop
pluginMock.NAME = 'test'
pluginMock.jQueryInterface = Util.noop
Util.defineJQueryPlugin(pluginMock)
expect(fakejQuery.fn.test).toEqual(pluginMock.jQueryInterface)
expect(fakejQuery.fn.test.Constructor).toEqual(pluginMock)
expect(fakejQuery.fn.test.noConflict).toEqual(jasmine.any(Function))
})
})
describe('execute', () => {
it('should execute if arg is function', () => {
const spy = jasmine.createSpy('spy')

View File

@ -1,4 +1,5 @@
import * as jQueryUtil from '../../../src/util/jquery-stuff'
import { noop } from '../../../src/util/index'
describe('Jquery Stuff', () => {
const fakejQuery = { fn: {} }
@ -15,7 +16,9 @@ describe('Jquery Stuff', () => {
})
describe('getjQuery', () => {
const fakejQuery = { trigger() {} }
const fakejQuery = {
trigger: noop
}
beforeEach(() => {
Object.defineProperty(window, 'jQuery', {
@ -47,20 +50,32 @@ describe('Jquery Stuff', () => {
})
})
describe('defineJQueryPlugin', () => {
it('should define a plugin on the jQuery instance', () => {
const pluginMock = noop
pluginMock.NAME = 'test'
pluginMock.jQueryInterface = noop
jQueryUtil.defineJQueryPlugin(pluginMock)
expect(fakejQuery.fn.test).toEqual(pluginMock.jQueryInterface)
expect(fakejQuery.fn.test.Constructor).toEqual(pluginMock)
expect(fakejQuery.fn.test.noConflict).toEqual(jasmine.any(Function))
})
})
describe('getJqueryInterfaceForPlugin', () => {
it('should return a plugin jQueryInterface if exists', () => {
const pluginMock = function () {}
const pluginMock = noop
pluginMock.NAME = 'test'
pluginMock.jQueryInterface = function () {}
pluginMock.jQueryInterface = noop
expect(jQueryUtil.getJqueryInterfaceForPlugin(pluginMock)).toEqual(pluginMock.jQueryInterface)
})
it('should return the default `defaultJQueryInterface`, if plugin jQueryInterface doesn\'t exists', () => {
const pluginMock = function () {}
const pluginMock = noop
pluginMock.NAME = 'test'
expect(jQueryUtil.getJqueryInterfaceForPlugin(pluginMock)).not.toEqual(pluginMock.jQueryInterface)
expect(jQueryUtil.getJqueryInterfaceForPlugin(pluginMock)).toEqual(jasmine.any(Function))
})
})