diff --git a/_dev/js/theme/index.js b/_dev/js/theme/index.js index 39d7e149..92829dc9 100644 --- a/_dev/js/theme/index.js +++ b/_dev/js/theme/index.js @@ -1,5 +1,6 @@ import EventEmitter from 'events'; +import './windowExpose'; import './core/index'; import './vendors/bootstrap/bootstrap-imports'; import './components/dynamic-bootstrap-components'; diff --git a/_dev/js/theme/windowExpose.js b/_dev/js/theme/windowExpose.js new file mode 100644 index 00000000..df4c7de3 --- /dev/null +++ b/_dev/js/theme/windowExpose.js @@ -0,0 +1,28 @@ +import exposeToWindow from "../utils/misc/exposeToWindow"; +import { on, one, off, trigger } from "../utils/event/eventHandler"; +import useHttpRequest from "../utils/http/useHttpRequest"; +import useDefaultHttpRequest from "../utils/http/useDefaultHttpRequest"; +import useHttpController from "../utils/http/useHttpController"; +import useHttpPayloadDefinition from "../utils/http/useHttpPayloadDefinition"; +import { isElementVisible, each, DOMReady, parseToHtml } from '../utils/DOM/DOMHelpers'; +import { getAllSiblingsBeforeElement, getAllSiblingsAfterElement } from '../utils/DOM/DOMSelectorsHelper'; +import { fromSerializeObject, fromSerialize, formSerializeArray } from '../utils/form/formSerialize'; + +exposeToWindow('eventHandlerOn', on); +exposeToWindow('eventHandlerOne', one); +exposeToWindow('eventHandlerOff', off); +exposeToWindow('eventHandlerTrigger', trigger); +exposeToWindow('useHttpRequest', useHttpRequest); +exposeToWindow('useDefaultHttpRequest', useDefaultHttpRequest); +exposeToWindow('useHttpController', useHttpController); +exposeToWindow('useHttpPayloadDefinition', useHttpPayloadDefinition); +exposeToWindow('isElementVisible', isElementVisible); +exposeToWindow('each', each); +exposeToWindow('DOMReady', DOMReady); +exposeToWindow('parseToHtml', parseToHtml); +exposeToWindow('getAllSiblingsBeforeElement', getAllSiblingsBeforeElement); +exposeToWindow('getAllSiblingsAfterElement', getAllSiblingsAfterElement); +exposeToWindow('fromSerializeObject', fromSerializeObject); +exposeToWindow('fromSerialize', fromSerialize); +exposeToWindow('formSerializeArray', formSerializeArray); + diff --git a/_dev/js/utils/event/eventHandler.js b/_dev/js/utils/event/eventHandler.js index 9e7567f0..506177ca 100644 --- a/_dev/js/utils/event/eventHandler.js +++ b/_dev/js/utils/event/eventHandler.js @@ -1,8 +1,10 @@ import EventHandler from 'bootstrap/js/src/dom/event-handler'; - /** * Add event handler + * + * @description exposed to window as eventHandlerOn + * * @param element {HTMLElement|Document|Window} * @param eventName {string} * @param handlerOrDelegation {string|function} @@ -14,6 +16,9 @@ export const on = (element, eventName, handlerOrDelegation, handler) => { /** * Add event handler that will be executed only once + * + * @description exposed to window as eventHandlerOne + * * @param element {HTMLElement|Document|Window} * @param eventName {string} * @param handlerOrDelegation {string|function} @@ -25,6 +30,9 @@ export const one = (element, eventName, handlerOrDelegation, handler) => { /** * Remove event handler + * + * @description exposed to window as eventHandlerOff + * * @param element {HTMLElement|Document|Window} * @param eventName {string} * @param handlerOrDelegation {string|function} @@ -36,6 +44,9 @@ export const off = (element, eventName, handlerOrDelegation, handler) => { /** * Trigger event + * + * @description exposed to window as eventHandlerTrigger + * * @param element {HTMLElement} * @param eventName {string} * @param args {object} diff --git a/_dev/js/utils/misc/exposeToWindow.js b/_dev/js/utils/misc/exposeToWindow.js new file mode 100644 index 00000000..d7d68b29 --- /dev/null +++ b/_dev/js/utils/misc/exposeToWindow.js @@ -0,0 +1,30 @@ +/** + * A set to track exposed function names. + * @type {Set} + */ +const exposeSet = new Set(); + +/** + * Exposes a function globally by assigning it to the window object. + * + * @param {string} name - The name under which the function will be exposed globally. + * @param {Function} fnc - The function to be exposed. + * @throws Will throw an error if the specified name is already in use for global exposure. + */ +const exposeToWindow = (name, fnc) => { + /** + * Throws an error if the specified name is already in use for global exposure. + * @throws Error + */ + const throwErrorIfAlreadyExposed = () => { + if (exposeSet.has(name)) { + throw new Error(`"${name}" is already exposed`); + } + }; + + throwErrorIfAlreadyExposed(); + exposeSet.add(name); + window[name] = fnc; +}; + +export default exposeToWindow;