UNPKG

fullcalendar

Version:

FullCalendar Vanilla JS package for rendering a calendar

172 lines (167 loc) • 5.31 kB
class Emitter { constructor() { this.handlers = {}; this.thisContext = null; } setThisContext(thisContext) { this.thisContext = thisContext; } setOptions(options) { this.options = options; } on(type, handler) { addToHash(this.handlers, type, handler); } off(type, handler) { removeFromHash(this.handlers, type, handler); } trigger(type, ...args) { let attachedHandlers = this.handlers[type] || []; let optionHandler = this.options && this.options[type]; let handlers = [].concat(optionHandler || [], attachedHandlers); for (let handler of handlers) { handler.apply(this.thisContext, args); } } hasHandlers(type) { return Boolean((this.handlers[type] && this.handlers[type].length) || (this.options && this.options[type])); } } function addToHash(hash, type, handler) { (hash[type] || (hash[type] = [])) .push(handler); } function removeFromHash(hash, type, handler) { if (handler) { if (hash[type]) { hash[type] = hash[type].filter((func) => func !== handler); } } else { delete hash[type]; // remove all handler funcs for this type } } /* Tracks short-lived async work that leads to rendering (animation frames, short timers, ResizeObserver deliveries), so automated tests can wait for rendering to finish instead of sleeping. Long timers (now-indicator, scroll-end debounce, long-press delay) and drag auto-scrolling, which runs for as long as the pointer stays near an edge, are deliberately not tracked. */ let pendingCnt = 0; let activityCnt = 0; /* Marks async work as pending until the returned function is called (once). */ function holdSettle() { let isReleased = false; pendingCnt++; return () => { if (!isReleased) { isReleased = true; pendingCnt--; activityCnt++; } }; } /* Records that something happened that might lead to more rendering. */ function markSettleActivity() { activityCnt++; } function settleRequestAnimationFrame(callback) { const release = holdSettle(); requestAnimationFrame(() => { try { callback(); } finally { release(); } }); } const pendingTimeouts = new Map(); function settleSetTimeout(callback, ms) { const release = holdSettle(); const id = setTimeout(() => { pendingTimeouts.delete(id); try { callback(); } finally { release(); } }, ms); pendingTimeouts.set(id, release); return id; } function settleClearTimeout(id) { const release = pendingTimeouts.get(id); if (release) { pendingTimeouts.delete(id); clearTimeout(id); release(); } } /* Resolves once nothing tracked is pending and a whole animation frame has passed without activity. ResizeObserver callbacks run after animation-frame callbacks within a frame, so activity is compared between two consecutive frames rather than checked within one. */ function whenSettled() { return new Promise((resolve) => { let prevActivityCnt = -1; function check() { if (!pendingCnt && activityCnt === prevActivityCnt) { resolve(); } else { prevActivityCnt = activityCnt; requestAnimationFrame(check); } } requestAnimationFrame(check); }); } // TODO: get rid of this in favor of options system, // tho it's really easy to access this globally rather than pass thru options. const config = {}; function getAppendableRoot(el) { const root = el.getRootNode(); if (root instanceof Document) { return root.body || root.documentElement; // pick body if available } return root; } function computeElIsRtl(el) { return getComputedStyle(el).direction === 'rtl'; } // Style // ---------------------------------------------------------------------------------------------------------------- const PIXEL_PROP_RE = /(top|left|right|bottom|width|height)$/i; function applyStyle(el, props) { for (let propName in props) { applyStyleProp(el, propName, props[propName]); } } function applyStyleProp(el, name, val) { if (val == null) { el.style[name] = ''; } else if (typeof val === 'number' && PIXEL_PROP_RE.test(name)) { el.style[name] = `${val}px`; } else { el.style[name] = val; } } // Event Handling // ---------------------------------------------------------------------------------------------------------------- // if intercepting bubbled events at the document/window/body level, // and want to see originating element (the 'target'), use this util instead // of `ev.target` because it goes within web-component boundaries. function getEventTargetViaRoot(ev) { return ev.composedPath?.()[0] ?? ev.target; } export { Emitter as E, applyStyleProp as a, settleSetTimeout as b, settleClearTimeout as c, config as d, computeElIsRtl as e, getEventTargetViaRoot as f, getAppendableRoot as g, applyStyle as h, holdSettle as i, markSettleActivity as m, settleRequestAnimationFrame as s, whenSettled as w };