@thednp/shorty 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +340 -0
- package/dist/shorty.esm.js +2540 -0
- package/dist/shorty.esm.min.js +2 -0
- package/dist/shorty.js +2563 -0
- package/dist/shorty.min.js +2 -0
- package/package.json +59 -0
- package/src/attr/getAttribute.js +9 -0
- package/src/attr/getAttributeNS.js +10 -0
- package/src/attr/hasAttribute.js +9 -0
- package/src/attr/hasAttributeNS.js +10 -0
- package/src/attr/removeAttribute.js +9 -0
- package/src/attr/removeAttributeNS.js +10 -0
- package/src/attr/setAttribute.js +10 -0
- package/src/attr/setAttributeNS.js +11 -0
- package/src/blocks/documentBody.js +5 -0
- package/src/blocks/documentElement.js +5 -0
- package/src/blocks/documentHead.js +5 -0
- package/src/boolean/isApple.js +13 -0
- package/src/boolean/isFirefox.js +9 -0
- package/src/boolean/isMobile.js +20 -0
- package/src/boolean/support3DTransform.js +8 -0
- package/src/boolean/supportAnimation.js +8 -0
- package/src/boolean/supportPassive.js +28 -0
- package/src/boolean/supportTouch.js +6 -0
- package/src/boolean/supportTransform.js +8 -0
- package/src/boolean/supportTransition.js +8 -0
- package/src/class/addClass.js +10 -0
- package/src/class/hasClass.js +10 -0
- package/src/class/removeClass.js +10 -0
- package/src/event/off.js +13 -0
- package/src/event/on.js +13 -0
- package/src/event/one.js +26 -0
- package/src/get/getBoundingClientRect.js +33 -0
- package/src/get/getDocument.js +11 -0
- package/src/get/getDocumentBody.js +10 -0
- package/src/get/getDocumentElement.js +11 -0
- package/src/get/getDocumentHead.js +10 -0
- package/src/get/getElementAnimationDelay.js +21 -0
- package/src/get/getElementAnimationDelayLegacy.js +21 -0
- package/src/get/getElementAnimationDuration.js +20 -0
- package/src/get/getElementAnimationDurationLegacy.js +21 -0
- package/src/get/getElementStyle.js +18 -0
- package/src/get/getElementTransitionDelay.js +21 -0
- package/src/get/getElementTransitionDelayLegacy.js +21 -0
- package/src/get/getElementTransitionDuration.js +20 -0
- package/src/get/getElementTransitionDurationLegacy.js +21 -0
- package/src/get/getNodeScroll.js +16 -0
- package/src/get/getParentNode.js +24 -0
- package/src/get/getRectRelativeToOffsetParent.js +30 -0
- package/src/get/getUID.js +34 -0
- package/src/get/getWindow.js +20 -0
- package/src/index.js +475 -0
- package/src/is/isArray.js +9 -0
- package/src/is/isCustomElement.js +8 -0
- package/src/is/isDocument.js +9 -0
- package/src/is/isElement.js +9 -0
- package/src/is/isElementInScrollRange.js +18 -0
- package/src/is/isElementInViewport.js +22 -0
- package/src/is/isElementsArray.js +10 -0
- package/src/is/isFunction.js +9 -0
- package/src/is/isHTMLCollection.js +8 -0
- package/src/is/isHTMLElement.js +8 -0
- package/src/is/isHTMLImageElement.js +8 -0
- package/src/is/isMedia.js +12 -0
- package/src/is/isNode.js +8 -0
- package/src/is/isNodeList.js +8 -0
- package/src/is/isRTL.js +9 -0
- package/src/is/isSVGElement.js +8 -0
- package/src/is/isScaledElement.js +15 -0
- package/src/is/isShadowRoot.js +12 -0
- package/src/is/isString.js +8 -0
- package/src/is/isTableElement.js +7 -0
- package/src/is/isWindow.js +9 -0
- package/src/misc/ArrayFrom.js +8 -0
- package/src/misc/Float32ArrayFrom.js +7 -0
- package/src/misc/Float64ArrayFrom.js +7 -0
- package/src/misc/ObjectAssign.js +7 -0
- package/src/misc/ObjectKeys.js +7 -0
- package/src/misc/ObjectValues.js +7 -0
- package/src/misc/OriginalEvent.js +18 -0
- package/src/misc/createElement.js +25 -0
- package/src/misc/createElementNS.js +26 -0
- package/src/misc/data.js +78 -0
- package/src/misc/dispatchEvent.js +8 -0
- package/src/misc/distinct.js +10 -0
- package/src/misc/emulateAnimationEnd.js +37 -0
- package/src/misc/emulateAnimationEndLegacy.js +38 -0
- package/src/misc/emulateTransitionEnd.js +37 -0
- package/src/misc/emulateTransitionEndLegacy.js +38 -0
- package/src/misc/focus.js +8 -0
- package/src/misc/noop.js +3 -0
- package/src/misc/normalizeOptions.js +49 -0
- package/src/misc/normalizeValue.js +32 -0
- package/src/misc/passiveHandler.js +7 -0
- package/src/misc/passiveHandlerLegacy.js +9 -0
- package/src/misc/reflow.js +9 -0
- package/src/misc/setElementStyle.js +10 -0
- package/src/misc/timer.js +78 -0
- package/src/misc/toLowerCase.js +8 -0
- package/src/misc/toUpperCase.js +8 -0
- package/src/misc/tryWrapper.js +11 -0
- package/src/misc/version.js +9 -0
- package/src/selectors/closest.js +16 -0
- package/src/selectors/documentAll.js +8 -0
- package/src/selectors/elementNodes.js +5 -0
- package/src/selectors/getCustomElements.js +18 -0
- package/src/selectors/getElementById.js +11 -0
- package/src/selectors/getElementsByClassName.js +15 -0
- package/src/selectors/getElementsByTagName.js +15 -0
- package/src/selectors/matches.js +10 -0
- package/src/selectors/matchesLegacy.js +23 -0
- package/src/selectors/parentNodes.js +5 -0
- package/src/selectors/querySelector.js +15 -0
- package/src/selectors/querySelectorAll.js +14 -0
- package/src/strings/DOMContentLoadedEvent.js +6 -0
- package/src/strings/DOMMouseScrollEvent.js +6 -0
- package/src/strings/abortEvent.js +6 -0
- package/src/strings/addEventListener.js +6 -0
- package/src/strings/animationDelay.js +6 -0
- package/src/strings/animationDelayLegacy.js +8 -0
- package/src/strings/animationDuration.js +6 -0
- package/src/strings/animationDurationLegacy.js +8 -0
- package/src/strings/animationEndEvent.js +6 -0
- package/src/strings/animationEndEventLegacy.js +8 -0
- package/src/strings/animationName.js +6 -0
- package/src/strings/animationNameLegacy.js +8 -0
- package/src/strings/ariaChecked.js +6 -0
- package/src/strings/ariaDescribedBy.js +6 -0
- package/src/strings/ariaDescription.js +6 -0
- package/src/strings/ariaExpanded.js +6 -0
- package/src/strings/ariaHasPopup.js +6 -0
- package/src/strings/ariaHidden.js +6 -0
- package/src/strings/ariaLabel.js +6 -0
- package/src/strings/ariaLabelledBy.js +6 -0
- package/src/strings/ariaModal.js +6 -0
- package/src/strings/ariaPressed.js +6 -0
- package/src/strings/ariaSelected.js +6 -0
- package/src/strings/ariaValueMax.js +6 -0
- package/src/strings/ariaValueMin.js +6 -0
- package/src/strings/ariaValueNow.js +6 -0
- package/src/strings/ariaValueText.js +6 -0
- package/src/strings/beforeunloadEvent.js +6 -0
- package/src/strings/bezierEasings.js +33 -0
- package/src/strings/blurEvent.js +6 -0
- package/src/strings/changeEvent.js +6 -0
- package/src/strings/contextmenuEvent.js +6 -0
- package/src/strings/errorEvent.js +6 -0
- package/src/strings/focusEvent.js +6 -0
- package/src/strings/focusEvents.js +6 -0
- package/src/strings/focusinEvent.js +6 -0
- package/src/strings/focusoutEvent.js +6 -0
- package/src/strings/gesturechangeEvent.js +6 -0
- package/src/strings/gestureendEvent.js +6 -0
- package/src/strings/gesturestartEvent.js +6 -0
- package/src/strings/keyAlt.js +7 -0
- package/src/strings/keyArrowDown.js +7 -0
- package/src/strings/keyArrowLeft.js +7 -0
- package/src/strings/keyArrowRight.js +7 -0
- package/src/strings/keyArrowUp.js +7 -0
- package/src/strings/keyBackspace.js +7 -0
- package/src/strings/keyCapsLock.js +7 -0
- package/src/strings/keyControl.js +7 -0
- package/src/strings/keyDelete.js +7 -0
- package/src/strings/keyEnter.js +7 -0
- package/src/strings/keyEscape.js +7 -0
- package/src/strings/keyInsert.js +7 -0
- package/src/strings/keyMeta.js +7 -0
- package/src/strings/keyPause.js +7 -0
- package/src/strings/keyScrollLock.js +7 -0
- package/src/strings/keyShift.js +7 -0
- package/src/strings/keySpace.js +7 -0
- package/src/strings/keyTab.js +7 -0
- package/src/strings/keyboardEventKeys.js +27 -0
- package/src/strings/keydownEvent.js +6 -0
- package/src/strings/keypressEvent.js +6 -0
- package/src/strings/keyupEvent.js +6 -0
- package/src/strings/loadEvent.js +6 -0
- package/src/strings/loadstartEvent.js +6 -0
- package/src/strings/mouseClickEvents.js +6 -0
- package/src/strings/mouseHoverEvents.js +6 -0
- package/src/strings/mouseSwipeEvents.js +8 -0
- package/src/strings/mouseclickEvent.js +6 -0
- package/src/strings/mousedblclickEvent.js +6 -0
- package/src/strings/mousedownEvent.js +6 -0
- package/src/strings/mouseenterEvent.js +6 -0
- package/src/strings/mousehoverEvent.js +6 -0
- package/src/strings/mouseinEvent.js +6 -0
- package/src/strings/mouseleaveEvent.js +6 -0
- package/src/strings/mousemoveEvent.js +6 -0
- package/src/strings/mouseoutEvent.js +6 -0
- package/src/strings/mouseoverEvent.js +6 -0
- package/src/strings/mouseupEvent.js +6 -0
- package/src/strings/mousewheelEvent.js +6 -0
- package/src/strings/moveEvent.js +6 -0
- package/src/strings/nativeEvents.js +108 -0
- package/src/strings/offsetHeight.js +6 -0
- package/src/strings/offsetWidth.js +6 -0
- package/src/strings/orientationchangeEvent.js +6 -0
- package/src/strings/pointercancelEvent.js +6 -0
- package/src/strings/pointerdownEvent.js +6 -0
- package/src/strings/pointerleaveEvent.js +6 -0
- package/src/strings/pointermoveEvent.js +6 -0
- package/src/strings/pointerupEvent.js +6 -0
- package/src/strings/readystatechangeEvent.js +6 -0
- package/src/strings/removeEventListener.js +6 -0
- package/src/strings/resetEvent.js +6 -0
- package/src/strings/resizeEvent.js +6 -0
- package/src/strings/scrollEvent.js +6 -0
- package/src/strings/scrollHeight.js +6 -0
- package/src/strings/scrollWidth.js +6 -0
- package/src/strings/selectEvent.js +6 -0
- package/src/strings/selectendEvent.js +6 -0
- package/src/strings/selectstartEvent.js +6 -0
- package/src/strings/submitEvent.js +6 -0
- package/src/strings/touchEvents.js +8 -0
- package/src/strings/touchcancelEvent.js +6 -0
- package/src/strings/touchendEvent.js +6 -0
- package/src/strings/touchmoveEvent.js +6 -0
- package/src/strings/touchstartEvent.js +6 -0
- package/src/strings/transitionDelay.js +6 -0
- package/src/strings/transitionDelayLegacy.js +8 -0
- package/src/strings/transitionDuration.js +6 -0
- package/src/strings/transitionDurationLegacy.js +8 -0
- package/src/strings/transitionEndEvent.js +6 -0
- package/src/strings/transitionEndEventLegacy.js +8 -0
- package/src/strings/transitionProperty.js +7 -0
- package/src/strings/transitionPropertyLegacy.js +12 -0
- package/src/strings/unloadEvent.js +6 -0
- package/src/strings/userAgent.js +7 -0
- package/src/strings/userAgentData.js +8 -0
- package/types/index.d.ts +252 -0
- package/types/module/shorty.ts +249 -0
- package/types/shorty.d.ts +2317 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import getWindow from '../get/getWindow';
|
|
2
|
+
/**
|
|
3
|
+
* Check if target is a `ShadowRoot`.
|
|
4
|
+
*
|
|
5
|
+
* @param {any} element target
|
|
6
|
+
* @returns {boolean} the query result
|
|
7
|
+
*/
|
|
8
|
+
const isShadowRoot = (element) => {
|
|
9
|
+
const OwnElement = getWindow(element).ShadowRoot;
|
|
10
|
+
return element instanceof OwnElement || element instanceof ShadowRoot;
|
|
11
|
+
};
|
|
12
|
+
export default isShadowRoot;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if a target element is a `<table>`, `<td>` or `<th>`.
|
|
3
|
+
* @param {any} element the target element
|
|
4
|
+
* @returns {boolean} the query result
|
|
5
|
+
*/
|
|
6
|
+
const isTableElement = (element) => ['TABLE', 'TD', 'TH'].includes(element.tagName);
|
|
7
|
+
export default isTableElement;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Float32Array.from()` static method.
|
|
3
|
+
* @param {any[] | HTMLCollection | NodeList} arr array-like iterable object
|
|
4
|
+
* @returns {Float32Array}
|
|
5
|
+
*/
|
|
6
|
+
const Float32ArrayFrom = (arr) => Float32Array.from(Array.from(arr));
|
|
7
|
+
export default Float32ArrayFrom;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Float64Array.from()` static method.
|
|
3
|
+
* @param {any[] | HTMLCollection | NodeList} arr array-like iterable object
|
|
4
|
+
* @returns {Float64Array}
|
|
5
|
+
*/
|
|
6
|
+
const Float64ArrayFrom = (arr) => Float64Array.from(Array.from(arr));
|
|
7
|
+
export default Float64ArrayFrom;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import ObjectAssign from './ObjectAssign';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a namespaced `CustomEvent` specific to each component.
|
|
5
|
+
* @param {string} EventType Event.type
|
|
6
|
+
* @param {Record<string, any>=} config Event.options | Event.properties
|
|
7
|
+
* @returns {SHORTER.OriginalEvent} a new namespaced event
|
|
8
|
+
*/
|
|
9
|
+
export default function OriginalEvent(EventType, config) {
|
|
10
|
+
const OriginalCustomEvent = new CustomEvent(EventType, {
|
|
11
|
+
cancelable: true, bubbles: true,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (config instanceof Object) {
|
|
15
|
+
ObjectAssign(OriginalCustomEvent, config);
|
|
16
|
+
}
|
|
17
|
+
return OriginalCustomEvent;
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import getDocument from '../get/getDocument';
|
|
2
|
+
import ObjectAssign from './ObjectAssign';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This is a shortie for `document.createElement` method
|
|
6
|
+
* which allows you to create a new `HTMLElement` for a given `tagName`
|
|
7
|
+
* or based on an object with specific non-readonly attributes:
|
|
8
|
+
* `id`, `className`, `textContent`, `style`, etc.
|
|
9
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
|
|
10
|
+
*
|
|
11
|
+
* @param {Record<string, string> | string} param `tagName` or object
|
|
12
|
+
* @return {HTMLElement | Element} a new `HTMLElement` or `Element`
|
|
13
|
+
*/
|
|
14
|
+
export default function createElement(param) {
|
|
15
|
+
if (typeof param === 'string') {
|
|
16
|
+
return getDocument().createElement(param);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const { tagName } = param;
|
|
20
|
+
const attr = { ...param };
|
|
21
|
+
const newElement = createElement(tagName);
|
|
22
|
+
delete attr.tagName;
|
|
23
|
+
ObjectAssign(newElement, attr);
|
|
24
|
+
return newElement;
|
|
25
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import getDocument from '../get/getDocument';
|
|
2
|
+
import ObjectAssign from './ObjectAssign';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This is a shortie for `document.createElementNS` method
|
|
6
|
+
* which allows you to create a new `HTMLElement` for a given `tagName`
|
|
7
|
+
* or based on an object with specific non-readonly attributes:
|
|
8
|
+
* `id`, `className`, `textContent`, `style`, etc.
|
|
9
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS
|
|
10
|
+
*
|
|
11
|
+
* @param {string} namespace `namespaceURI` to associate with the new `HTMLElement`
|
|
12
|
+
* @param {Record<string, string> | string} param `tagName` or object
|
|
13
|
+
* @return {HTMLElement | Element} a new `HTMLElement` or `Element`
|
|
14
|
+
*/
|
|
15
|
+
export default function createElementNS(namespace, param) {
|
|
16
|
+
if (typeof param === 'string') {
|
|
17
|
+
return getDocument().createElementNS(namespace, param);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { tagName } = param;
|
|
21
|
+
const attr = { ...param };
|
|
22
|
+
const newElement = createElementNS(namespace, tagName);
|
|
23
|
+
delete attr.tagName;
|
|
24
|
+
ObjectAssign(newElement, attr);
|
|
25
|
+
return newElement;
|
|
26
|
+
}
|
package/src/misc/data.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import querySelector from '../selectors/querySelector';
|
|
2
|
+
|
|
3
|
+
/** @type {Map<string, Map<HTMLElement | Element, Record<string, any>>>} */
|
|
4
|
+
const componentData = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* An interface for web components background data.
|
|
7
|
+
* @see https://github.com/thednp/bootstrap.native/blob/master/src/components/base-component.js
|
|
8
|
+
*/
|
|
9
|
+
const Data = {
|
|
10
|
+
/**
|
|
11
|
+
* Sets web components data.
|
|
12
|
+
* @param {HTMLElement | Element | string} target target element
|
|
13
|
+
* @param {string} component the component's name or a unique key
|
|
14
|
+
* @param {Record<string, any>} instance the component instance
|
|
15
|
+
*/
|
|
16
|
+
set: (target, component, instance) => {
|
|
17
|
+
const element = querySelector(target);
|
|
18
|
+
if (!element) return;
|
|
19
|
+
|
|
20
|
+
if (!componentData.has(component)) {
|
|
21
|
+
componentData.set(component, new Map());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const instanceMap = componentData.get(component);
|
|
25
|
+
// @ts-ignore - not undefined, but defined right above
|
|
26
|
+
instanceMap.set(element, instance);
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns all instances for specified component.
|
|
31
|
+
* @param {string} component the component's name or a unique key
|
|
32
|
+
* @returns {Map<HTMLElement | Element, Record<string, any>>?} all the component instances
|
|
33
|
+
*/
|
|
34
|
+
getAllFor: (component) => {
|
|
35
|
+
const instanceMap = componentData.get(component);
|
|
36
|
+
|
|
37
|
+
return instanceMap || null;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Returns the instance associated with the target.
|
|
42
|
+
* @param {HTMLElement | Element | string} target target element
|
|
43
|
+
* @param {string} component the component's name or a unique key
|
|
44
|
+
* @returns {Record<string, any>?} the instance
|
|
45
|
+
*/
|
|
46
|
+
get: (target, component) => {
|
|
47
|
+
const element = querySelector(target);
|
|
48
|
+
const allForC = Data.getAllFor(component);
|
|
49
|
+
const instance = element && allForC && allForC.get(element);
|
|
50
|
+
|
|
51
|
+
return instance || null;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Removes web components data.
|
|
56
|
+
* @param {HTMLElement | Element | string} target target element
|
|
57
|
+
* @param {string} component the component's name or a unique key
|
|
58
|
+
*/
|
|
59
|
+
remove: (target, component) => {
|
|
60
|
+
const element = querySelector(target);
|
|
61
|
+
const instanceMap = componentData.get(component);
|
|
62
|
+
if (!instanceMap || !element) return;
|
|
63
|
+
|
|
64
|
+
instanceMap.delete(element);
|
|
65
|
+
|
|
66
|
+
if (instanceMap.size === 0) {
|
|
67
|
+
componentData.delete(component);
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* An alias for `Data.get()`.
|
|
74
|
+
* @type {SHORTER.getInstance<any>}
|
|
75
|
+
*/
|
|
76
|
+
export const getInstance = (target, component) => Data.get(target, component);
|
|
77
|
+
|
|
78
|
+
export default Data;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for the `Element.dispatchEvent(Event)` method.
|
|
3
|
+
*
|
|
4
|
+
* @param {HTMLElement | Element} element is the target
|
|
5
|
+
* @param {Event} event is the `Event` object
|
|
6
|
+
*/
|
|
7
|
+
const dispatchEvent = (element, event) => element.dispatchEvent(event);
|
|
8
|
+
export default dispatchEvent;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JavaScript `Array` distinct.
|
|
3
|
+
* @see https://codeburst.io/javascript-array-distinct-5edc93501dc4
|
|
4
|
+
* @param {any} value
|
|
5
|
+
* @param {number} index
|
|
6
|
+
* @param {any} self
|
|
7
|
+
* @returns {boolean}
|
|
8
|
+
*/
|
|
9
|
+
const distinct = (value, index, self) => self.indexOf(value) === index;
|
|
10
|
+
export default distinct;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import animationEndEvent from '../strings/animationEndEvent';
|
|
2
|
+
import getElementAnimationDelay from '../get/getElementAnimationDelay';
|
|
3
|
+
import getElementAnimationDuration from '../get/getElementAnimationDuration';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Utility to make sure callbacks are consistently
|
|
7
|
+
* called when animation ends.
|
|
8
|
+
*
|
|
9
|
+
* @param {HTMLElement | Element} element target
|
|
10
|
+
* @param {EventListener} handler `animationend` callback
|
|
11
|
+
*/
|
|
12
|
+
export default function emulateAnimationEnd(element, handler) {
|
|
13
|
+
let called = 0;
|
|
14
|
+
const endEvent = new Event(animationEndEvent);
|
|
15
|
+
const duration = getElementAnimationDuration(element);
|
|
16
|
+
const delay = getElementAnimationDelay(element);
|
|
17
|
+
|
|
18
|
+
if (duration) {
|
|
19
|
+
/**
|
|
20
|
+
* Wrap the handler in on -> off callback
|
|
21
|
+
* @type {EventListener}
|
|
22
|
+
*/
|
|
23
|
+
const animationEndWrapper = (e) => {
|
|
24
|
+
if (e.target === element) {
|
|
25
|
+
handler.apply(element, [e]);
|
|
26
|
+
element.removeEventListener(animationEndEvent, animationEndWrapper);
|
|
27
|
+
called = 1;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
element.addEventListener(animationEndEvent, animationEndWrapper);
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
if (!called) element.dispatchEvent(endEvent);
|
|
33
|
+
}, duration + delay + 17);
|
|
34
|
+
} else {
|
|
35
|
+
handler.apply(element, [endEvent]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import supportAnimation from '../boolean/supportAnimation';
|
|
2
|
+
import animationEndEvent from '../strings/animationEndEventLegacy';
|
|
3
|
+
import getElementAnimationDelay from '../get/getElementAnimationDelayLegacy';
|
|
4
|
+
import getElementAnimationDuration from '../get/getElementAnimationDurationLegacy';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Utility to make sure callbacks are consistently
|
|
8
|
+
* called when animation ends.
|
|
9
|
+
*
|
|
10
|
+
* @param {HTMLElement | Element} element target
|
|
11
|
+
* @param {EventListener} handler `animationend` callback
|
|
12
|
+
*/
|
|
13
|
+
export default function emulateAnimationEnd(element, handler) {
|
|
14
|
+
let called = 0;
|
|
15
|
+
const endEvent = new Event(animationEndEvent);
|
|
16
|
+
const duration = getElementAnimationDuration(element);
|
|
17
|
+
const delay = getElementAnimationDelay(element);
|
|
18
|
+
|
|
19
|
+
if (supportAnimation && duration) {
|
|
20
|
+
/**
|
|
21
|
+
* Wrap the handler in on -> off callback
|
|
22
|
+
* @param {Event} e Event object
|
|
23
|
+
*/
|
|
24
|
+
const animationEndWrapper = (e) => {
|
|
25
|
+
if (e.target === element) {
|
|
26
|
+
handler.apply(element, [e]);
|
|
27
|
+
element.removeEventListener(animationEndEvent, animationEndWrapper);
|
|
28
|
+
called = 1;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
element.addEventListener(animationEndEvent, animationEndWrapper);
|
|
32
|
+
setTimeout(() => {
|
|
33
|
+
if (!called) element.dispatchEvent(endEvent);
|
|
34
|
+
}, duration + delay + 17);
|
|
35
|
+
} else {
|
|
36
|
+
handler.apply(element, [endEvent]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import transitionEndEvent from '../strings/transitionEndEvent';
|
|
2
|
+
import getElementTransitionDelay from '../get/getElementTransitionDelay';
|
|
3
|
+
import getElementTransitionDuration from '../get/getElementTransitionDuration';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Utility to make sure callbacks are consistently
|
|
7
|
+
* called when transition ends.
|
|
8
|
+
*
|
|
9
|
+
* @param {HTMLElement | Element} element target
|
|
10
|
+
* @param {EventListener} handler `transitionend` callback
|
|
11
|
+
*/
|
|
12
|
+
export default function emulateTransitionEnd(element, handler) {
|
|
13
|
+
let called = 0;
|
|
14
|
+
const endEvent = new Event(transitionEndEvent);
|
|
15
|
+
const duration = getElementTransitionDuration(element);
|
|
16
|
+
const delay = getElementTransitionDelay(element);
|
|
17
|
+
|
|
18
|
+
if (duration) {
|
|
19
|
+
/**
|
|
20
|
+
* Wrap the handler in on -> off callback
|
|
21
|
+
* @type {EventListener} e Event object
|
|
22
|
+
*/
|
|
23
|
+
const transitionEndWrapper = (e) => {
|
|
24
|
+
if (e.target === element) {
|
|
25
|
+
handler.apply(element, [e]);
|
|
26
|
+
element.removeEventListener(transitionEndEvent, transitionEndWrapper);
|
|
27
|
+
called = 1;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
element.addEventListener(transitionEndEvent, transitionEndWrapper);
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
if (!called) element.dispatchEvent(endEvent);
|
|
33
|
+
}, duration + delay + 17);
|
|
34
|
+
} else {
|
|
35
|
+
handler.apply(element, [endEvent]);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import supportTransition from '../boolean/supportTransition';
|
|
2
|
+
import transitionEndEvent from '../strings/transitionEndEventLegacy';
|
|
3
|
+
import getElementTransitionDelay from '../get/getElementTransitionDelayLegacy';
|
|
4
|
+
import getElementTransitionDuration from '../get/getElementTransitionDurationLegacy';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Utility to make sure callbacks are consistently
|
|
8
|
+
* called when transition ends.
|
|
9
|
+
*
|
|
10
|
+
* @param {HTMLElement | Element} element target
|
|
11
|
+
* @param {EventListener} handler `transitionend` callback
|
|
12
|
+
*/
|
|
13
|
+
export default function emulateTransitionEnd(element, handler) {
|
|
14
|
+
let called = 0;
|
|
15
|
+
const endEvent = new Event(transitionEndEvent);
|
|
16
|
+
const duration = getElementTransitionDuration(element);
|
|
17
|
+
const delay = getElementTransitionDelay(element);
|
|
18
|
+
|
|
19
|
+
if (supportTransition && duration) {
|
|
20
|
+
/**
|
|
21
|
+
* Wrap the handler in on -> off callback
|
|
22
|
+
* @param {Event} e Event object
|
|
23
|
+
*/
|
|
24
|
+
const transitionEndWrapper = (e) => {
|
|
25
|
+
if (e.target === element) {
|
|
26
|
+
handler.apply(element, [e]);
|
|
27
|
+
element.removeEventListener(transitionEndEvent, transitionEndWrapper);
|
|
28
|
+
called = 1;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
element.addEventListener(transitionEndEvent, transitionEndWrapper);
|
|
32
|
+
setTimeout(() => {
|
|
33
|
+
if (!called) element.dispatchEvent(endEvent);
|
|
34
|
+
}, duration + delay + 17);
|
|
35
|
+
} else {
|
|
36
|
+
handler.apply(element, [endEvent]);
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/misc/noop.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import getAttribute from '../attr/getAttribute';
|
|
2
|
+
import normalizeValue from './normalizeValue';
|
|
3
|
+
import ObjectKeys from './ObjectKeys';
|
|
4
|
+
import toLowerCase from './toLowerCase';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Utility to normalize component options.
|
|
8
|
+
*
|
|
9
|
+
* @param {HTMLElement | Element} element target
|
|
10
|
+
* @param {Record<string, any>} defaultOps component default options
|
|
11
|
+
* @param {Record<string, any>} inputOps component instance options
|
|
12
|
+
* @param {string=} ns component namespace
|
|
13
|
+
* @return {Record<string, any>} normalized component options object
|
|
14
|
+
*/
|
|
15
|
+
export default function normalizeOptions(element, defaultOps, inputOps, ns) {
|
|
16
|
+
// @ts-ignore -- our targets are always `HTMLElement`
|
|
17
|
+
const data = { ...element.dataset };
|
|
18
|
+
/** @type {Record<string, any>} */
|
|
19
|
+
const normalOps = {};
|
|
20
|
+
/** @type {Record<string, any>} */
|
|
21
|
+
const dataOps = {};
|
|
22
|
+
const title = 'title';
|
|
23
|
+
|
|
24
|
+
ObjectKeys(data).forEach((k) => {
|
|
25
|
+
const key = ns && k.includes(ns)
|
|
26
|
+
? k.replace(ns, '').replace(/[A-Z]/, (match) => toLowerCase(match))
|
|
27
|
+
: k;
|
|
28
|
+
|
|
29
|
+
dataOps[key] = normalizeValue(data[k]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
ObjectKeys(inputOps).forEach((k) => {
|
|
33
|
+
inputOps[k] = normalizeValue(inputOps[k]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
ObjectKeys(defaultOps).forEach((k) => {
|
|
37
|
+
if (k in inputOps) {
|
|
38
|
+
normalOps[k] = inputOps[k];
|
|
39
|
+
} else if (k in dataOps) {
|
|
40
|
+
normalOps[k] = dataOps[k];
|
|
41
|
+
} else {
|
|
42
|
+
normalOps[k] = k === title
|
|
43
|
+
? getAttribute(element, title)
|
|
44
|
+
: defaultOps[k];
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return normalOps;
|
|
49
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The raw value or a given component option.
|
|
3
|
+
*
|
|
4
|
+
* @typedef {string | HTMLElement | Function | number | boolean | null} niceValue
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Utility to normalize component options
|
|
9
|
+
*
|
|
10
|
+
* @param {any} value the input value
|
|
11
|
+
* @return {niceValue} the normalized value
|
|
12
|
+
*/
|
|
13
|
+
export default function normalizeValue(value) {
|
|
14
|
+
if (value === 'true') { // boolean
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (value === 'false') { // boolean
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!Number.isNaN(+value)) { // number
|
|
23
|
+
return +value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (value === '' || value === 'null') { // null
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// string / function / HTMLElement / object
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import supportPassive from '../boolean/supportPassive';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A global namespace for most scroll event listeners in legacy browsers.
|
|
5
|
+
* @type {Partial<AddEventListenerOptions> | boolean}
|
|
6
|
+
*/
|
|
7
|
+
const passiveHandler = supportPassive ? { passive: true } : false;
|
|
8
|
+
|
|
9
|
+
export default passiveHandler;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility to force re-paint of an `HTMLElement` target.
|
|
3
|
+
*
|
|
4
|
+
* @param {HTMLElement | Element} element is the target
|
|
5
|
+
* @return {number} the `Element.offsetHeight` value
|
|
6
|
+
*/
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
const reflow = (element) => element.offsetHeight;
|
|
9
|
+
export default reflow;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import ObjectAssign from './ObjectAssign';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shortcut for multiple uses of `HTMLElement.style.propertyName` method.
|
|
5
|
+
* @param {HTMLElement | Element} element target element
|
|
6
|
+
* @param {Partial<CSSStyleDeclaration>} styles attribute value
|
|
7
|
+
*/
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
const setElementStyle = (element, styles) => { ObjectAssign(element.style, styles); };
|
|
10
|
+
export default setElementStyle;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import querySelector from '../selectors/querySelector';
|
|
2
|
+
|
|
3
|
+
/** @type {Map<HTMLElement | Element, any>} */
|
|
4
|
+
const TimeCache = new Map();
|
|
5
|
+
/**
|
|
6
|
+
* An interface for one or more `TimerHandler`s per `Element`.
|
|
7
|
+
* @see https://github.com/thednp/navbar.js/
|
|
8
|
+
*/
|
|
9
|
+
const Timer = {
|
|
10
|
+
/**
|
|
11
|
+
* Sets a new timeout timer for an element, or element -> key association.
|
|
12
|
+
* @param {HTMLElement | Element | string} target target element
|
|
13
|
+
* @param {ReturnType<TimerHandler>} callback the callback
|
|
14
|
+
* @param {number} delay the execution delay
|
|
15
|
+
* @param {string=} key a unique key
|
|
16
|
+
*/
|
|
17
|
+
set: (target, callback, delay, key) => {
|
|
18
|
+
const element = querySelector(target);
|
|
19
|
+
|
|
20
|
+
if (!element) return;
|
|
21
|
+
|
|
22
|
+
if (key && key.length) {
|
|
23
|
+
if (!TimeCache.has(element)) {
|
|
24
|
+
TimeCache.set(element, new Map());
|
|
25
|
+
}
|
|
26
|
+
const keyTimers = TimeCache.get(element);
|
|
27
|
+
keyTimers.set(key, setTimeout(callback, delay));
|
|
28
|
+
} else {
|
|
29
|
+
TimeCache.set(element, setTimeout(callback, delay));
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns the timer associated with the target.
|
|
35
|
+
* @param {HTMLElement | Element | string} target target element
|
|
36
|
+
* @param {string=} key a unique
|
|
37
|
+
* @returns {number?} the timer
|
|
38
|
+
*/
|
|
39
|
+
get: (target, key) => {
|
|
40
|
+
const element = querySelector(target);
|
|
41
|
+
|
|
42
|
+
if (!element) return null;
|
|
43
|
+
const keyTimers = TimeCache.get(element);
|
|
44
|
+
|
|
45
|
+
if (key && key.length && keyTimers && keyTimers.get) {
|
|
46
|
+
return keyTimers.get(key) || null;
|
|
47
|
+
}
|
|
48
|
+
return keyTimers || null;
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Clears the element's timer.
|
|
53
|
+
* @param {HTMLElement | Element | string} target target element
|
|
54
|
+
* @param {string=} key a unique key
|
|
55
|
+
*/
|
|
56
|
+
clear: (target, key) => {
|
|
57
|
+
const element = querySelector(target);
|
|
58
|
+
|
|
59
|
+
if (!element) return;
|
|
60
|
+
|
|
61
|
+
if (key && key.length) {
|
|
62
|
+
const keyTimers = TimeCache.get(element);
|
|
63
|
+
|
|
64
|
+
if (keyTimers && keyTimers.get) {
|
|
65
|
+
clearTimeout(keyTimers.get(key));
|
|
66
|
+
keyTimers.delete(key);
|
|
67
|
+
if (keyTimers.size === 0) {
|
|
68
|
+
TimeCache.delete(element);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
clearTimeout(TimeCache.get(element));
|
|
73
|
+
TimeCache.delete(element);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export default Timer;
|