maplair
Version:
JavaScript library for mobile-friendly interactive maps
52 lines (51 loc) • 1.62 kB
JavaScript
import { setOptions } from "./Util.js";
//#region src/core/Class.js
var Class = class {
static include(props) {
const parentOptions = this.prototype.options;
for (const k of getAllMethodNames(props)) this.prototype[k] = props[k];
if (props.options) {
this.prototype.options = parentOptions;
this.mergeOptions(props.options);
}
return this;
function* getAllMethodNames(obj) {
do {
if (obj === Object || obj === Object.prototype) break;
for (const k of Object.getOwnPropertyNames(obj)) yield k;
} while ((obj = Object.getPrototypeOf(obj)) !== void 0);
}
}
static setDefaultOptions(options) {
setOptions(this.prototype, options);
return this;
}
static mergeOptions(options) {
this.prototype.options ??= {};
Object.assign(this.prototype.options, options);
return this;
}
static addInitHook(fn, ...args) {
const init = typeof fn === "function" ? fn : function() {
this[fn](...args);
};
if (!Object.hasOwn(this, "_initHooks")) this._initHooks = [];
this._initHooks.push(init);
return this;
}
constructor() {
setOptions(this);
}
callInitHooks(cls = this.constructor) {
const done = this._initHooksCalledFor;
if (done && (done === cls || done.prototype instanceof cls)) return this;
const classes = [];
for (let c = cls; c && c !== Function.prototype && c !== done; c = Object.getPrototypeOf(c)) classes.unshift(c);
this._initHooksCalledFor = cls;
for (const c of classes) if (Object.hasOwn(c, "_initHooks")) for (const hook of c._initHooks) hook.call(this);
return this;
}
};
//#endregion
export { Class };
//# sourceMappingURL=Class.js.map