Skip to content

Commit a4ea6ee

Browse files
committed
expose utils to window
1 parent 39844a8 commit a4ea6ee

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

_dev/js/theme/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import EventEmitter from 'events';
22

3+
import './windowExpose';
34
import './core/index';
45
import './vendors/bootstrap/bootstrap-imports';
56
import './components/dynamic-bootstrap-components';

_dev/js/theme/windowExpose.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import exposeToWindow from "../utils/misc/exposeToWindow";
2+
import { on, one, off, trigger } from "../utils/event/eventHandler";
3+
import useHttpRequest from "../utils/http/useHttpRequest";
4+
import useDefaultHttpRequest from "../utils/http/useDefaultHttpRequest";
5+
import useHttpController from "../utils/http/useHttpController";
6+
import useHttpPayloadDefinition from "../utils/http/useHttpPayloadDefinition";
7+
import { isElementVisible, each, DOMReady, parseToHtml } from '../utils/DOM/DOMHelpers';
8+
import { getAllSiblingsBeforeElement, getAllSiblingsAfterElement } from '../utils/DOM/DOMSelectorsHelper';
9+
import { fromSerializeObject, fromSerialize, formSerializeArray } from '../utils/form/formSerialize';
10+
11+
exposeToWindow('eventHandlerOn', on);
12+
exposeToWindow('eventHandlerOne', one);
13+
exposeToWindow('eventHandlerOff', off);
14+
exposeToWindow('eventHandlerTrigger', trigger);
15+
exposeToWindow('useHttpRequest', useHttpRequest);
16+
exposeToWindow('useDefaultHttpRequest', useDefaultHttpRequest);
17+
exposeToWindow('useHttpController', useHttpController);
18+
exposeToWindow('useHttpPayloadDefinition', useHttpPayloadDefinition);
19+
exposeToWindow('isElementVisible', isElementVisible);
20+
exposeToWindow('each', each);
21+
exposeToWindow('DOMReady', DOMReady);
22+
exposeToWindow('parseToHtml', parseToHtml);
23+
exposeToWindow('getAllSiblingsBeforeElement', getAllSiblingsBeforeElement);
24+
exposeToWindow('getAllSiblingsAfterElement', getAllSiblingsAfterElement);
25+
exposeToWindow('fromSerializeObject', fromSerializeObject);
26+
exposeToWindow('fromSerialize', fromSerialize);
27+
exposeToWindow('formSerializeArray', formSerializeArray);
28+

_dev/js/utils/event/eventHandler.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import EventHandler from 'bootstrap/js/src/dom/event-handler';
22

3-
43
/**
54
* Add event handler
5+
*
6+
* @description exposed to window as eventHandlerOn
7+
*
68
* @param element {HTMLElement|Document|Window}
79
* @param eventName {string}
810
* @param handlerOrDelegation {string|function}
@@ -14,6 +16,9 @@ export const on = (element, eventName, handlerOrDelegation, handler) => {
1416

1517
/**
1618
* Add event handler that will be executed only once
19+
*
20+
* @description exposed to window as eventHandlerOne
21+
*
1722
* @param element {HTMLElement|Document|Window}
1823
* @param eventName {string}
1924
* @param handlerOrDelegation {string|function}
@@ -25,6 +30,9 @@ export const one = (element, eventName, handlerOrDelegation, handler) => {
2530

2631
/**
2732
* Remove event handler
33+
*
34+
* @description exposed to window as eventHandlerOff
35+
*
2836
* @param element {HTMLElement|Document|Window}
2937
* @param eventName {string}
3038
* @param handlerOrDelegation {string|function}
@@ -36,6 +44,9 @@ export const off = (element, eventName, handlerOrDelegation, handler) => {
3644

3745
/**
3846
* Trigger event
47+
*
48+
* @description exposed to window as eventHandlerTrigger
49+
*
3950
* @param element {HTMLElement}
4051
* @param eventName {string}
4152
* @param args {object}

_dev/js/utils/misc/exposeToWindow.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* A set to track exposed function names.
3+
* @type {Set<string>}
4+
*/
5+
const exposeSet = new Set();
6+
7+
/**
8+
* Exposes a function globally by assigning it to the window object.
9+
*
10+
* @param {string} name - The name under which the function will be exposed globally.
11+
* @param {Function} fnc - The function to be exposed.
12+
* @throws Will throw an error if the specified name is already in use for global exposure.
13+
*/
14+
const exposeToWindow = (name, fnc) => {
15+
/**
16+
* Throws an error if the specified name is already in use for global exposure.
17+
* @throws Error
18+
*/
19+
const throwErrorIfAlreadyExposed = () => {
20+
if (exposeSet.has(name)) {
21+
throw new Error(`"${name}" is already exposed`);
22+
}
23+
};
24+
25+
throwErrorIfAlreadyExposed();
26+
exposeSet.add(name);
27+
window[name] = fnc;
28+
};
29+
30+
export default exposeToWindow;

0 commit comments

Comments
 (0)