|
| 1 | +const OBSERVER_ID_PREFIX = "blazor_plugin_observer__"; |
| 2 | +let OBSERVER_ID = 1; |
| 3 | +const observerItems = new Map(); |
| 4 | +function reset() { |
| 5 | + OBSERVER_ID = 0; |
| 6 | + observerItems.clear(); |
| 7 | +} |
| 8 | +function getObserverItems() { |
| 9 | + return observerItems; |
| 10 | +} |
| 11 | +function getObserverElementId() { |
| 12 | + return `${OBSERVER_ID_PREFIX}${OBSERVER_ID++}`; |
| 13 | +} |
| 14 | +function createObserverItem(dotnetRef, callbackId, options) { |
| 15 | + const onEntry = onEntryChange(callbackId); |
| 16 | + const observer = new IntersectionObserver(onEntry, options); |
| 17 | + observerItems.set(callbackId, { dotnetRef, observer, elements: [] }); |
| 18 | + return observerItems.get(callbackId); |
| 19 | +} |
| 20 | +function observeElement(callbackId, element) { |
| 21 | + const item = observerItems.get(callbackId); |
| 22 | + if (item == null) { |
| 23 | + throw new Error(`Failed to observe element for key: ${callbackId} as the observer does not exist`); |
| 24 | + } |
| 25 | + if (item.elements.some(record => record.element == element)) { |
| 26 | + console.warn(`BlazorIntersectionObserver: The element is already being observed by observer for key ${callbackId}`); |
| 27 | + return ""; |
| 28 | + } |
| 29 | + const elementId = getObserverElementId(); |
| 30 | + item.observer.observe(element); |
| 31 | + item.elements.push({ elementId, element }); |
| 32 | + return elementId; |
| 33 | +} |
| 34 | +function create(dotnetRef, callbackId, options) { |
| 35 | + return createObserverItem(dotnetRef, callbackId, options); |
| 36 | +} |
| 37 | +function observe(dotnetRef, callbackId, node, options) { |
| 38 | + createObserverItem(dotnetRef, callbackId, options); |
| 39 | + return observeElement(callbackId, node); |
| 40 | +} |
| 41 | +function unobserve(callbackId, element) { |
| 42 | + var _a; |
| 43 | + const item = observerItems.get(callbackId); |
| 44 | + if (item == null) { |
| 45 | + throw new Error(`Failed to unobserve element for key: ${callbackId} as the observer does not exist`); |
| 46 | + } |
| 47 | + const unobserveElementId = (_a = item.elements.find((record) => record.element == element)) === null || _a === void 0 ? void 0 : _a.elementId; |
| 48 | + if (unobserveElementId == null) { |
| 49 | + console.warn(`BlazorIntersectionObserver: The record does not exist for observer: ${callbackId}`); |
| 50 | + } |
| 51 | + item.observer.unobserve(element); |
| 52 | + item.elements = item.elements.filter((record) => record.element != element); |
| 53 | + return unobserveElementId; |
| 54 | +} |
| 55 | +function disconnect(callbackId) { |
| 56 | + const item = observerItems.get(callbackId); |
| 57 | + if (item == null) { |
| 58 | + throw new Error(`Failed to disconnect for key: ${callbackId} as the observer does not exist`); |
| 59 | + } |
| 60 | + item.observer.disconnect(); |
| 61 | + item.elements = []; |
| 62 | + return true; |
| 63 | +} |
| 64 | +function remove(callbackId) { |
| 65 | + if (disconnect(callbackId)) { |
| 66 | + return observerItems.delete(callbackId); |
| 67 | + } |
| 68 | +} |
| 69 | +function toEntryObject(entry) { |
| 70 | + function toRectReadOnlyObject(obj) { |
| 71 | + return { |
| 72 | + X: obj.x, |
| 73 | + Y: obj.y, |
| 74 | + Width: obj.width, |
| 75 | + Height: obj.height, |
| 76 | + Top: obj.top, |
| 77 | + Left: obj.left, |
| 78 | + Bottom: obj.bottom, |
| 79 | + Right: obj.right, |
| 80 | + }; |
| 81 | + } |
| 82 | + return { |
| 83 | + IsIntersecting: entry.isIntersecting, |
| 84 | + IntersectionRatio: entry.intersectionRatio, |
| 85 | + Time: entry.time, |
| 86 | + BoundingClientRect: toRectReadOnlyObject(entry.boundingClientRect), |
| 87 | + IntersectionRect: toRectReadOnlyObject(entry.intersectionRect), |
| 88 | + RootBounds: toRectReadOnlyObject(entry.rootBounds), |
| 89 | + }; |
| 90 | +} |
| 91 | +function onEntryChange(callbackId) { |
| 92 | + return (entries) => { |
| 93 | + if (!observerItems.has(callbackId)) { |
| 94 | + return; |
| 95 | + } |
| 96 | + const { dotnetRef } = observerItems.get(callbackId); |
| 97 | + const mapped = entries.map((entry) => { |
| 98 | + const mappedEntry = toEntryObject(entry); |
| 99 | + return mappedEntry; |
| 100 | + }); |
| 101 | + dotnetRef.invokeMethodAsync("OnCallback", callbackId, mapped); |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +export { OBSERVER_ID_PREFIX, create, disconnect, getObserverItems, observe, observeElement, remove, reset, unobserve }; |
0 commit comments