fullcalendar
Version:
FullCalendar Vanilla JS package for rendering a calendar
85 lines (82 loc) • 2.72 kB
JavaScript
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
}
}
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, getEventTargetViaRoot as b, computeElIsRtl as c, applyStyle as d, getAppendableRoot as g };