From f5462d47d4af8e5728c954b1f7652b88fa4d864d Mon Sep 17 00:00:00 2001 From: GeoSot Date: Fri, 3 Dec 2021 20:15:36 +0200 Subject: [PATCH] transfer `defineJQueryPlugin` to `jquery-stuff.js` file --- js/src/alert.js | 2 +- js/src/button.js | 2 +- js/src/carousel.js | 2 +- js/src/collapse.js | 2 +- js/src/dropdown.js | 2 +- js/src/modal.js | 4 ++-- js/src/offcanvas.js | 2 +- js/src/popover.js | 2 +- js/src/scrollspy.js | 3 ++- js/src/tab.js | 3 ++- js/src/toast.js | 3 ++- js/src/tooltip.js | 3 ++- js/src/util/index.js | 21 -------------------- js/src/util/jquery-stuff.js | 20 +++++++++++++++++++ js/tests/unit/util/index.spec.js | 26 ------------------------- js/tests/unit/util/jquery-stuff.spec.js | 25 +++++++++++++++++++----- 16 files changed, 57 insertions(+), 65 deletions(-) diff --git a/js/src/alert.js b/js/src/alert.js index e7f6326f11..db50741c3d 100644 --- a/js/src/alert.js +++ b/js/src/alert.js @@ -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 diff --git a/js/src/button.js b/js/src/button.js index 08e017552e..26b81e3e3d 100644 --- a/js/src/button.js +++ b/js/src/button.js @@ -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 diff --git a/js/src/carousel.js b/js/src/carousel.js index 64f38d7e64..e016aac08c 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -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 diff --git a/js/src/collapse.js b/js/src/collapse.js index a5c9f2d695..15f2834105 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -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 diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 4a93df5b4f..cd781e8a92 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -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 diff --git a/js/src/modal.js b/js/src/modal.js index d55de90221..b251ecf9d4 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -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}` diff --git a/js/src/offcanvas.js b/js/src/offcanvas.js index 3ba93b9830..eb11c8f523 100644 --- a/js/src/offcanvas.js +++ b/js/src/offcanvas.js @@ -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 diff --git a/js/src/popover.js b/js/src/popover.js index f04a569921..0655f272e4 100644 --- a/js/src/popover.js +++ b/js/src/popover.js @@ -5,8 +5,8 @@ * -------------------------------------------------------------------------- */ -import { defineJQueryPlugin } from './util/index' import Tooltip from './tooltip' +import { defineJQueryPlugin } from './util/jquery-stuff' /** * Constants diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 06e8682d7b..bf3ad4e904 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -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 diff --git a/js/src/tab.js b/js/src/tab.js index 19ff03441f..88cf99cef9 100644 --- a/js/src/tab.js +++ b/js/src/tab.js @@ -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 diff --git a/js/src/toast.js b/js/src/toast.js index 9e29d2a009..bbb9416c8e 100644 --- a/js/src/toast.js +++ b/js/src/toast.js @@ -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 diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 67a43b224f..87dfb03e1f 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -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 diff --git a/js/src/util/index.js b/js/src/util/index.js index 25dfc11628..9017d6de55 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -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, diff --git a/js/src/util/jquery-stuff.js b/js/src/util/jquery-stuff.js index 2a0a506a51..4a61a55e90 100644 --- a/js/src/util/jquery-stuff.js +++ b/js/src/util/jquery-stuff.js @@ -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 } diff --git a/js/tests/unit/util/index.spec.js b/js/tests/unit/util/index.spec.js index 676e193e46..da583adb76 100644 --- a/js/tests/unit/util/index.spec.js +++ b/js/tests/unit/util/index.spec.js @@ -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') diff --git a/js/tests/unit/util/jquery-stuff.spec.js b/js/tests/unit/util/jquery-stuff.spec.js index 01a7226e40..e40d30f1f3 100644 --- a/js/tests/unit/util/jquery-stuff.spec.js +++ b/js/tests/unit/util/jquery-stuff.spec.js @@ -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)) }) })