@studiometa/js-toolkit 3.0.0-alpha.2 → 3.0.0-alpha.3
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/Base/Base.js +2 -1
- package/Base/Base.js.map +2 -2
- package/Base/features.d.ts +1 -1
- package/Base/features.js +1 -1
- package/Base/features.js.map +2 -2
- package/Base/index.d.ts +1 -0
- package/Base/index.js +4 -0
- package/Base/index.js.map +2 -2
- package/Base/managers/AbstractManager.d.ts +3 -3
- package/Base/managers/ChildrenManager.d.ts +1 -1
- package/Base/utils.d.ts +2 -0
- package/Base/utils.js +14 -2
- package/Base/utils.js.map +2 -2
- package/README.md +6 -4
- package/decorators/index.d.ts +1 -0
- package/decorators/index.js +1 -0
- package/decorators/index.js.map +2 -2
- package/decorators/withName.d.ts +6 -0
- package/decorators/withName.js +12 -0
- package/decorators/withName.js.map +7 -0
- package/helpers/createApp.d.ts +3 -5
- package/helpers/createApp.js +14 -9
- package/helpers/createApp.js.map +2 -2
- package/package.json +1 -1
- package/services/resize.js +2 -3
- package/services/resize.js.map +2 -2
package/Base/Base.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getComponentElements, getEventTarget, addToQueue } from "./utils.js";
|
|
1
|
+
import { getComponentElements, getEventTarget, addToQueue, registerInstance } from "./utils.js";
|
|
2
2
|
import {
|
|
3
3
|
ChildrenManager,
|
|
4
4
|
RefsManager,
|
|
@@ -182,6 +182,7 @@ class Base extends EventTarget {
|
|
|
182
182
|
}
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
+
registerInstance(this);
|
|
185
186
|
this.$id = `${__config.name}-${id}`;
|
|
186
187
|
id += 1;
|
|
187
188
|
this.$el = element;
|
package/Base/Base.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/Base/Base.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define */\nimport { getComponentElements, getEventTarget, addToQueue } from './utils.js';\nimport {\n ChildrenManager,\n RefsManager,\n ServicesManager,\n EventsManager,\n OptionsManager,\n} from './managers/index.js';\nimport { noop, isDev, isFunction, isArray } from '../utils/index.js';\n\nlet id = 0;\n\nexport type BaseEl = HTMLElement & { __base__?: WeakMap<BaseConstructor, Base | 'terminated'> };\nexport type BaseConstructor<T extends Base = Base> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: Base;\n} & Pick<typeof Base, keyof typeof Base>;\nexport type BaseAsyncConstructor<T extends Base = Base> = (\n instance: Base,\n) => Promise<BaseConstructor<T> | { default: BaseConstructor<T> }>;\nexport type BaseOptions = { [name: string]: unknown };\nexport type BaseRefs = { [name: string]: HTMLElement | HTMLElement[] };\nexport type BaseChildren = { [nameOrSelector: string]: Base[] | Promise<Base>[] };\nexport type BaseConfigComponents = {\n [nameOrSelector: string]: BaseConstructor | BaseAsyncConstructor;\n};\n\nexport type BaseConfig = {\n name: string;\n debug?: boolean;\n log?: boolean;\n refs?: string[];\n emits?: string[];\n components?: BaseConfigComponents;\n options?: import('./managers/OptionsManager').OptionsSchema;\n};\n\nexport type BaseProps = {\n $el?: HTMLElement;\n $options?: BaseOptions;\n $refs?: BaseRefs;\n $children?: BaseChildren;\n $parent?: Base;\n};\n\nexport type Managers = {\n ChildrenManager: typeof ChildrenManager;\n EventsManager: typeof EventsManager;\n OptionsManager: typeof OptionsManager;\n RefsManager: typeof RefsManager;\n ServicesManager: typeof ServicesManager;\n};\n\n/**\n * Base class.\n */\nexport class Base<T extends BaseProps = BaseProps> extends EventTarget {\n /**\n * This is a Base instance.\n */\n static readonly $isBase = true as const;\n\n /**\n * The instance parent.\n */\n $parent: T['$parent'] & Base = null;\n\n /**\n * The instance id.\n */\n $id: string;\n\n /**\n * The root element.\n */\n $el: T['$el'] & BaseEl;\n\n /**\n * The state of the component.\n */\n $isMounted = false;\n\n /**\n * Store the event handlers.\n */\n __eventHandlers: Map<string, Set<EventListenerOrEventListenerObject>> = new Map();\n\n /**\n * Get the root instance of the app.\n */\n get $root(): Base {\n if (!this.$parent) {\n return this;\n }\n\n let parent = this.$parent;\n let root = this.$parent;\n\n while (parent) {\n if (!parent.$parent) {\n root = parent;\n }\n\n parent = parent.$parent;\n }\n\n return root;\n }\n\n /**\n * Merge configuration with the parents' configurations.\n */\n get __config(): BaseConfig {\n let proto = Object.getPrototypeOf(this);\n let { config } = proto.constructor;\n\n while (proto.constructor.config && proto.constructor.$isBase) {\n config = { ...proto.constructor.config, ...config };\n\n if (proto.constructor.config.options) {\n config.options = { ...proto.constructor.config.options, ...config.options };\n }\n\n if (proto.constructor.config.emits && config.emits) {\n config.emits = [...proto.constructor.config.emits, ...config.emits];\n }\n\n proto = Object.getPrototypeOf(proto);\n }\n\n config.options = config.options ?? {};\n config.refs = config.refs ?? [];\n config.components = config.components ?? {};\n\n return config;\n }\n\n static config: BaseConfig = {\n name: 'Base',\n emits: [\n // hook events\n 'before-mounted',\n 'mounted',\n 'updated',\n 'destroyed',\n 'terminated',\n // default services' events\n 'ticked',\n 'scrolled',\n 'resized',\n 'moved',\n 'loaded',\n 'keyed',\n ],\n };\n\n __services: ServicesManager;\n\n get $services(): ServicesManager {\n return this.__services;\n }\n\n __refs: RefsManager & T['$refs'] & BaseRefs;\n\n get $refs(): RefsManager & T['$refs'] & BaseRefs {\n return this.__refs;\n }\n\n __options: OptionsManager & T['$options'] & BaseOptions;\n\n get $options(): OptionsManager & T['$options'] & BaseOptions {\n return this.__options;\n }\n\n __children: ChildrenManager & T['$children'] & BaseChildren;\n\n get $children(): ChildrenManager & T['$children'] & BaseChildren {\n return this.__children;\n }\n\n __events: EventsManager;\n\n /**\n * Small helper to log stuff.\n */\n get $log(): (...args: unknown[]) => void {\n return this.__options.log ? window.console.log.bind(window, `[${this.__config.name}]`) : noop;\n }\n\n /**\n * Small helper to make warning easier.\n */\n get $warn(): (...args: unknown[]) => void {\n return this.__options.log ? window.console.warn.bind(window, `[${this.__config.name}]`) : noop;\n }\n\n /**\n * Small helper to debug information.\n */\n get __debug(): (...args: unknown[]) => void {\n return isDev && this.__options.debug\n ? window.console.log.bind(window, `[debug] [${this.$id}]`)\n : noop;\n }\n\n /**\n * Get manager constructors.\n */\n get __managers(): Managers {\n return {\n ChildrenManager,\n EventsManager,\n OptionsManager,\n RefsManager,\n ServicesManager,\n };\n }\n\n /**\n * Call an instance method and emit corresponding events.\n */\n __callMethod(method: string, ...args: unknown[]): unknown {\n if (isDev) {\n this.__debug('callMethod', method, ...args);\n }\n\n this.$emit(method, ...args);\n\n // We always emit an event, but we do not call the method if it does not exist\n if (!isFunction(this[method])) {\n return null;\n }\n\n if (isDev) {\n this.__debug(method, this, ...args);\n }\n\n return this[method].call(this, ...args);\n }\n\n /**\n * Test if the given event has been bound to the instance.\n *\n * @param {string} event The event's name.\n * @returns {boolean} Wether the given event has been bound or not.\n */\n __hasEvent(event: string): boolean {\n const eventHandlers = this.__eventHandlers.get(event);\n return eventHandlers && eventHandlers.size > 0;\n }\n\n /**\n * Class constructor where all the magic takes place.\n *\n * @param {HTMLElement} element The component's root element dd.\n */\n constructor(element: HTMLElement) {\n super();\n\n if (!element) {\n if (isDev) {\n throw new Error('The root element must be defined.');\n }\n return;\n }\n\n const { __config } = this;\n\n if (__config.name === 'Base') {\n if (isDev) {\n throw new Error('The `config.name` property is required.');\n }\n return;\n }\n\n this.$id = `${__config.name}-${id}`;\n id += 1;\n\n this.$el = element;\n\n if (!this.$el.__base__) {\n this.$el.__base__ = new WeakMap();\n }\n\n this.$el.__base__.set(this.__ctor, this);\n\n for (const service of ['Options', 'Services', 'Events', 'Refs', 'Children']) {\n this[`__${service.toLowerCase()}`] = new this.__managers[`${service}Manager`](this);\n }\n\n if (isDev) {\n this.__debug('constructor', this);\n }\n }\n\n /**\n * Trigger the `mounted` callback.\n */\n $mount(): this {\n if (this.$isMounted) {\n return this;\n }\n\n this.$emit('before-mounted');\n\n if (isDev) {\n this.__debug('$mount');\n }\n\n addToQueue(() => this.$children.registerAll());\n addToQueue(() => this.$refs.registerAll());\n addToQueue(() => this.__events.bindRootElement());\n addToQueue(() => this.$services.enableAll());\n addToQueue(() => this.$children.mountAll());\n // eslint-disable-next-line no-return-assign\n addToQueue(() => (this.$isMounted = true));\n addToQueue(() => this.__callMethod('mounted'));\n\n return this;\n }\n\n /**\n * Update the instance children.\n */\n $update(): this {\n if (isDev) {\n this.__debug('$update');\n }\n\n // Undo\n addToQueue(() => this.$refs.unregisterAll());\n addToQueue(() => this.$services.disableAll());\n // Redo\n addToQueue(() => this.$children.registerAll());\n addToQueue(() => this.$refs.registerAll());\n addToQueue(() => this.$services.enableAll());\n // Update\n addToQueue(() => this.$children.updateAll());\n addToQueue(() => this.__callMethod('updated'));\n\n return this;\n }\n\n /**\n * Trigger the `destroyed` callback.\n */\n $destroy(): this {\n if (!this.$isMounted) {\n return this;\n }\n\n if (isDev) {\n this.__debug('$destroy');\n }\n\n // eslint-disable-next-line no-return-assign\n addToQueue(() => (this.$isMounted = false));\n addToQueue(() => this.__events.unbindRootElement());\n addToQueue(() => this.$refs.unregisterAll());\n addToQueue(() => this.$services.disableAll());\n addToQueue(() => this.$children.destroyAll());\n addToQueue(() => this.__callMethod('destroyed'));\n\n return this;\n }\n\n /**\n * Terminate a child instance when it is not needed anymore.\n */\n $terminate(): void {\n if (isDev) {\n this.__debug('$terminate');\n }\n\n // First, destroy the component.\n addToQueue(() => this.$destroy());\n // Execute the `terminated` hook if it exists\n addToQueue(() => this.__callMethod('terminated'));\n // Delete instance\n addToQueue(() => this.$el.__base__.set(this.__ctor, 'terminated'));\n }\n\n /**\n * Add an emitted event.\n *\n * @param {string} event The event name.\n * @returns {void}\n */\n __addEmits(event: string): void {\n const ctor = this.__ctor;\n if (isArray(ctor.config.emits)) {\n ctor.config.emits.push(event);\n } else {\n ctor.config.emits = [event];\n }\n }\n\n /**\n * Remove an emitted event.\n *\n * @param {string} event The event name.\n * @returns {void}\n */\n __removeEmits(event: string): void {\n const ctor = this.__ctor;\n const index = ctor.config.emits.indexOf(event);\n ctor.config.emits.splice(index, 1);\n }\n\n /**\n * Get the instance constructor.\n */\n get __ctor(): BaseConstructor {\n return this.constructor as BaseConstructor;\n }\n\n /**\n * Bind a listener function to an event.\n *\n * @param {string} event\n * Name of the event.\n * @param {EventListenerOrEventListenerObject} listener\n * Function to be called.\n * @param {boolean|AddEventListenerOptions} [options]\n * Options for the `removeEventListener` method.\n * @returns {() => void}\n * A function to unbind the listener.\n */\n $on(\n event: string,\n listener: EventListenerOrEventListenerObject,\n options?: boolean | AddEventListenerOptions,\n ): () => void {\n if (isDev) {\n this.__debug('$on', event, listener, options);\n }\n\n let set = this.__eventHandlers.get(event);\n\n if (!set) {\n set = new Set();\n this.__eventHandlers.set(event, set);\n }\n\n set.add(listener);\n\n const target = getEventTarget(this, event, this.__config);\n target.addEventListener(event, listener, options);\n\n return () => {\n this.$off(event, listener, options);\n };\n }\n\n /**\n * Unbind a listener function from an event.\n *\n * @param {string} event\n * Name of the event.\n * @param {EventListenerOrEventListenerObject} listener\n * Function to be removed.\n * @param {boolean|EventListenerOptions} [options]\n * Options for the `removeEventListener` method.\n * @returns {void}\n */\n $off(\n event: string,\n listener: EventListenerOrEventListenerObject,\n options?: boolean | EventListenerOptions,\n ) {\n if (isDev) {\n this.__debug('$off', event, listener);\n }\n\n this.__eventHandlers.get(event).delete(listener);\n\n const target = getEventTarget(this, event, this.__config);\n target.removeEventListener(event, listener, options);\n }\n\n /**\n * Emits an event.\n *\n * @param {string} event Name of the event.\n * @param {any[]} args The arguments to apply to the functions bound to this event.\n * @returns {void}\n */\n $emit(event: string, ...args: unknown[]) {\n if (isDev) {\n this.__debug('$emit', event, args);\n }\n\n this.dispatchEvent(new CustomEvent(event, { detail: args }));\n }\n\n /**\n * Register and mount all instances of the component.\n */\n static $register(nameOrSelector?: string): Base[] {\n return getComponentElements(nameOrSelector ?? this.config.name).map((el) =>\n new this(el).$mount(),\n );\n }\n}\n"],
|
|
5
|
-
"mappings": "AACA,SAAS,sBAAsB,gBAAgB,
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-use-before-define */\nimport { getComponentElements, getEventTarget, addToQueue, registerInstance } from './utils.js';\nimport {\n ChildrenManager,\n RefsManager,\n ServicesManager,\n EventsManager,\n OptionsManager,\n} from './managers/index.js';\nimport { noop, isDev, isFunction, isArray } from '../utils/index.js';\n\nlet id = 0;\n\nexport type BaseEl = HTMLElement & { __base__?: WeakMap<BaseConstructor, Base | 'terminated'> };\nexport type BaseConstructor<T extends Base = Base> = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n new (...args: any[]): T;\n prototype: Base;\n} & Pick<typeof Base, keyof typeof Base>;\nexport type BaseAsyncConstructor<T extends Base = Base> = (\n instance: Base,\n) => Promise<BaseConstructor<T> | { default: BaseConstructor<T> }>;\nexport type BaseOptions = { [name: string]: unknown };\nexport type BaseRefs = { [name: string]: HTMLElement | HTMLElement[] };\nexport type BaseChildren = { [nameOrSelector: string]: Base[] | Promise<Base>[] };\nexport type BaseConfigComponents = {\n [nameOrSelector: string]: BaseConstructor | BaseAsyncConstructor;\n};\n\nexport type BaseConfig = {\n name: string;\n debug?: boolean;\n log?: boolean;\n refs?: string[];\n emits?: string[];\n components?: BaseConfigComponents;\n options?: import('./managers/OptionsManager').OptionsSchema;\n};\n\nexport type BaseProps = {\n $el?: HTMLElement;\n $options?: BaseOptions;\n $refs?: BaseRefs;\n $children?: BaseChildren;\n $parent?: Base;\n};\n\nexport type Managers = {\n ChildrenManager: typeof ChildrenManager;\n EventsManager: typeof EventsManager;\n OptionsManager: typeof OptionsManager;\n RefsManager: typeof RefsManager;\n ServicesManager: typeof ServicesManager;\n};\n\n/**\n * Base class.\n */\nexport class Base<T extends BaseProps = BaseProps> extends EventTarget {\n /**\n * This is a Base instance.\n */\n static readonly $isBase = true as const;\n\n /**\n * The instance parent.\n */\n $parent: T['$parent'] & Base = null;\n\n /**\n * The instance id.\n */\n $id: string;\n\n /**\n * The root element.\n */\n $el: T['$el'] & BaseEl;\n\n /**\n * The state of the component.\n */\n $isMounted = false;\n\n /**\n * Store the event handlers.\n */\n __eventHandlers: Map<string, Set<EventListenerOrEventListenerObject>> = new Map();\n\n /**\n * Get the root instance of the app.\n */\n get $root(): Base {\n if (!this.$parent) {\n return this;\n }\n\n let parent = this.$parent;\n let root = this.$parent;\n\n while (parent) {\n if (!parent.$parent) {\n root = parent;\n }\n\n parent = parent.$parent;\n }\n\n return root;\n }\n\n /**\n * Merge configuration with the parents' configurations.\n */\n get __config(): BaseConfig {\n let proto = Object.getPrototypeOf(this);\n let { config } = proto.constructor;\n\n while (proto.constructor.config && proto.constructor.$isBase) {\n config = { ...proto.constructor.config, ...config };\n\n if (proto.constructor.config.options) {\n config.options = { ...proto.constructor.config.options, ...config.options };\n }\n\n if (proto.constructor.config.emits && config.emits) {\n config.emits = [...proto.constructor.config.emits, ...config.emits];\n }\n\n proto = Object.getPrototypeOf(proto);\n }\n\n config.options = config.options ?? {};\n config.refs = config.refs ?? [];\n config.components = config.components ?? {};\n\n return config;\n }\n\n static config: BaseConfig = {\n name: 'Base',\n emits: [\n // hook events\n 'before-mounted',\n 'mounted',\n 'updated',\n 'destroyed',\n 'terminated',\n // default services' events\n 'ticked',\n 'scrolled',\n 'resized',\n 'moved',\n 'loaded',\n 'keyed',\n ],\n };\n\n __services: ServicesManager;\n\n get $services(): ServicesManager {\n return this.__services;\n }\n\n __refs: RefsManager & T['$refs'] & BaseRefs;\n\n get $refs(): RefsManager & T['$refs'] & BaseRefs {\n return this.__refs;\n }\n\n __options: OptionsManager & T['$options'] & BaseOptions;\n\n get $options(): OptionsManager & T['$options'] & BaseOptions {\n return this.__options;\n }\n\n __children: ChildrenManager & T['$children'] & BaseChildren;\n\n get $children(): ChildrenManager & T['$children'] & BaseChildren {\n return this.__children;\n }\n\n __events: EventsManager;\n\n /**\n * Small helper to log stuff.\n */\n get $log(): (...args: unknown[]) => void {\n return this.__options.log ? window.console.log.bind(window, `[${this.__config.name}]`) : noop;\n }\n\n /**\n * Small helper to make warning easier.\n */\n get $warn(): (...args: unknown[]) => void {\n return this.__options.log ? window.console.warn.bind(window, `[${this.__config.name}]`) : noop;\n }\n\n /**\n * Small helper to debug information.\n */\n get __debug(): (...args: unknown[]) => void {\n return isDev && this.__options.debug\n ? window.console.log.bind(window, `[debug] [${this.$id}]`)\n : noop;\n }\n\n /**\n * Get manager constructors.\n */\n get __managers(): Managers {\n return {\n ChildrenManager,\n EventsManager,\n OptionsManager,\n RefsManager,\n ServicesManager,\n };\n }\n\n /**\n * Call an instance method and emit corresponding events.\n */\n __callMethod(method: string, ...args: unknown[]): unknown {\n if (isDev) {\n this.__debug('callMethod', method, ...args);\n }\n\n this.$emit(method, ...args);\n\n // We always emit an event, but we do not call the method if it does not exist\n if (!isFunction(this[method])) {\n return null;\n }\n\n if (isDev) {\n this.__debug(method, this, ...args);\n }\n\n return this[method].call(this, ...args);\n }\n\n /**\n * Test if the given event has been bound to the instance.\n *\n * @param {string} event The event's name.\n * @returns {boolean} Wether the given event has been bound or not.\n */\n __hasEvent(event: string): boolean {\n const eventHandlers = this.__eventHandlers.get(event);\n return eventHandlers && eventHandlers.size > 0;\n }\n\n /**\n * Class constructor where all the magic takes place.\n *\n * @param {HTMLElement} element The component's root element dd.\n */\n constructor(element: HTMLElement) {\n super();\n\n if (!element) {\n if (isDev) {\n throw new Error('The root element must be defined.');\n }\n return;\n }\n\n const { __config } = this;\n\n if (__config.name === 'Base') {\n if (isDev) {\n throw new Error('The `config.name` property is required.');\n }\n return;\n }\n\n registerInstance(this);\n\n this.$id = `${__config.name}-${id}`;\n id += 1;\n\n this.$el = element;\n\n if (!this.$el.__base__) {\n this.$el.__base__ = new WeakMap();\n }\n\n this.$el.__base__.set(this.__ctor, this);\n\n for (const service of ['Options', 'Services', 'Events', 'Refs', 'Children']) {\n this[`__${service.toLowerCase()}`] = new this.__managers[`${service}Manager`](this);\n }\n\n if (isDev) {\n this.__debug('constructor', this);\n }\n }\n\n /**\n * Trigger the `mounted` callback.\n */\n $mount(): this {\n if (this.$isMounted) {\n return this;\n }\n\n this.$emit('before-mounted');\n\n if (isDev) {\n this.__debug('$mount');\n }\n\n addToQueue(() => this.$children.registerAll());\n addToQueue(() => this.$refs.registerAll());\n addToQueue(() => this.__events.bindRootElement());\n addToQueue(() => this.$services.enableAll());\n addToQueue(() => this.$children.mountAll());\n // eslint-disable-next-line no-return-assign\n addToQueue(() => (this.$isMounted = true));\n addToQueue(() => this.__callMethod('mounted'));\n\n return this;\n }\n\n /**\n * Update the instance children.\n */\n $update(): this {\n if (isDev) {\n this.__debug('$update');\n }\n\n // Undo\n addToQueue(() => this.$refs.unregisterAll());\n addToQueue(() => this.$services.disableAll());\n // Redo\n addToQueue(() => this.$children.registerAll());\n addToQueue(() => this.$refs.registerAll());\n addToQueue(() => this.$services.enableAll());\n // Update\n addToQueue(() => this.$children.updateAll());\n addToQueue(() => this.__callMethod('updated'));\n\n return this;\n }\n\n /**\n * Trigger the `destroyed` callback.\n */\n $destroy(): this {\n if (!this.$isMounted) {\n return this;\n }\n\n if (isDev) {\n this.__debug('$destroy');\n }\n\n // eslint-disable-next-line no-return-assign\n addToQueue(() => (this.$isMounted = false));\n addToQueue(() => this.__events.unbindRootElement());\n addToQueue(() => this.$refs.unregisterAll());\n addToQueue(() => this.$services.disableAll());\n addToQueue(() => this.$children.destroyAll());\n addToQueue(() => this.__callMethod('destroyed'));\n\n return this;\n }\n\n /**\n * Terminate a child instance when it is not needed anymore.\n */\n $terminate(): void {\n if (isDev) {\n this.__debug('$terminate');\n }\n\n // First, destroy the component.\n addToQueue(() => this.$destroy());\n // Execute the `terminated` hook if it exists\n addToQueue(() => this.__callMethod('terminated'));\n // Delete instance\n addToQueue(() => this.$el.__base__.set(this.__ctor, 'terminated'));\n }\n\n /**\n * Add an emitted event.\n *\n * @param {string} event The event name.\n * @returns {void}\n */\n __addEmits(event: string): void {\n const ctor = this.__ctor;\n if (isArray(ctor.config.emits)) {\n ctor.config.emits.push(event);\n } else {\n ctor.config.emits = [event];\n }\n }\n\n /**\n * Remove an emitted event.\n *\n * @param {string} event The event name.\n * @returns {void}\n */\n __removeEmits(event: string): void {\n const ctor = this.__ctor;\n const index = ctor.config.emits.indexOf(event);\n ctor.config.emits.splice(index, 1);\n }\n\n /**\n * Get the instance constructor.\n */\n get __ctor(): BaseConstructor {\n return this.constructor as BaseConstructor;\n }\n\n /**\n * Bind a listener function to an event.\n *\n * @param {string} event\n * Name of the event.\n * @param {EventListenerOrEventListenerObject} listener\n * Function to be called.\n * @param {boolean|AddEventListenerOptions} [options]\n * Options for the `removeEventListener` method.\n * @returns {() => void}\n * A function to unbind the listener.\n */\n $on(\n event: string,\n listener: EventListenerOrEventListenerObject,\n options?: boolean | AddEventListenerOptions,\n ): () => void {\n if (isDev) {\n this.__debug('$on', event, listener, options);\n }\n\n let set = this.__eventHandlers.get(event);\n\n if (!set) {\n set = new Set();\n this.__eventHandlers.set(event, set);\n }\n\n set.add(listener);\n\n const target = getEventTarget(this, event, this.__config);\n target.addEventListener(event, listener, options);\n\n return () => {\n this.$off(event, listener, options);\n };\n }\n\n /**\n * Unbind a listener function from an event.\n *\n * @param {string} event\n * Name of the event.\n * @param {EventListenerOrEventListenerObject} listener\n * Function to be removed.\n * @param {boolean|EventListenerOptions} [options]\n * Options for the `removeEventListener` method.\n * @returns {void}\n */\n $off(\n event: string,\n listener: EventListenerOrEventListenerObject,\n options?: boolean | EventListenerOptions,\n ) {\n if (isDev) {\n this.__debug('$off', event, listener);\n }\n\n this.__eventHandlers.get(event).delete(listener);\n\n const target = getEventTarget(this, event, this.__config);\n target.removeEventListener(event, listener, options);\n }\n\n /**\n * Emits an event.\n *\n * @param {string} event Name of the event.\n * @param {any[]} args The arguments to apply to the functions bound to this event.\n * @returns {void}\n */\n $emit(event: string, ...args: unknown[]) {\n if (isDev) {\n this.__debug('$emit', event, args);\n }\n\n this.dispatchEvent(new CustomEvent(event, { detail: args }));\n }\n\n /**\n * Register and mount all instances of the component.\n */\n static $register(nameOrSelector?: string): Base[] {\n return getComponentElements(nameOrSelector ?? this.config.name).map((el) =>\n new this(el).$mount(),\n );\n }\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,sBAAsB,gBAAgB,YAAY,wBAAwB;AACnF;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,MAAM,OAAO,YAAY,eAAe;AAEjD,IAAI,KAAK;AA+CF,MAAM,aAA8C,YAAY;AAAA;AAAA;AAAA;AAAA,EAIrE,OAAgB,UAAU;AAAA;AAAA;AAAA;AAAA,EAK1B,UAA+B;AAAA;AAAA;AAAA;AAAA,EAK/B;AAAA;AAAA;AAAA;AAAA,EAKA;AAAA;AAAA;AAAA;AAAA,EAKA,aAAa;AAAA;AAAA;AAAA;AAAA,EAKb,kBAAwE,oBAAI,IAAI;AAAA;AAAA;AAAA;AAAA,EAKhF,IAAI,QAAc;AAChB,QAAI,CAAC,KAAK,SAAS;AACjB,aAAO;AAAA,IACT;AAEA,QAAI,SAAS,KAAK;AAClB,QAAI,OAAO,KAAK;AAEhB,WAAO,QAAQ;AACb,UAAI,CAAC,OAAO,SAAS;AACnB,eAAO;AAAA,MACT;AAEA,eAAS,OAAO;AAAA,IAClB;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,WAAuB;AACzB,QAAI,QAAQ,OAAO,eAAe,IAAI;AACtC,QAAI,EAAE,OAAO,IAAI,MAAM;AAEvB,WAAO,MAAM,YAAY,UAAU,MAAM,YAAY,SAAS;AAC5D,eAAS,EAAE,GAAG,MAAM,YAAY,QAAQ,GAAG,OAAO;AAElD,UAAI,MAAM,YAAY,OAAO,SAAS;AACpC,eAAO,UAAU,EAAE,GAAG,MAAM,YAAY,OAAO,SAAS,GAAG,OAAO,QAAQ;AAAA,MAC5E;AAEA,UAAI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
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/Base/features.ts"],
|
|
4
|
-
"sourcesContent": ["export type Features = {\n
|
|
5
|
-
"mappings": "AAUO,MAAM,WAAW,oBAAI,IAA8C;AAAA,EACxE,CAAC,
|
|
4
|
+
"sourcesContent": ["export type Features = {\n blocking: boolean;\n breakpoints: Record<string, string>;\n};\n\ninterface FeaturesMap extends Map<keyof Features, Features[keyof Features]> {\n get<T extends keyof Features>(key: T): Features[T];\n set<T extends keyof Features>(key: T, value: Features[T]): this;\n}\n\nexport const features = new Map<keyof Features, Features[keyof Features]>([\n ['blocking', false],\n [\n 'breakpoints',\n {\n xxs: '0rem',\n xs: '30rem', // 480px\n s: '48rem', // 768px\n m: '64rem', // 1024px\n l: '80rem', // 1280px\n xl: '90rem', // 1440px\n xxl: '120rem', // 1920px\n xxxl: '160rem', // 2560px\n },\n ],\n]) as FeaturesMap;\n"],
|
|
5
|
+
"mappings": "AAUO,MAAM,WAAW,oBAAI,IAA8C;AAAA,EACxE,CAAC,YAAY,KAAK;AAAA,EAClB;AAAA,IACE;AAAA,IACA;AAAA,MACE,KAAK;AAAA,MACL,IAAI;AAAA;AAAA,MACJ,GAAG;AAAA;AAAA,MACH,GAAG;AAAA;AAAA,MACH,GAAG;AAAA;AAAA,MACH,IAAI;AAAA;AAAA,MACJ,KAAK;AAAA;AAAA,MACL,MAAM;AAAA;AAAA,IACR;AAAA,EACF;AACF,CAAC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/Base/index.d.ts
CHANGED
package/Base/index.js
CHANGED
package/Base/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/Base/index.ts"],
|
|
4
|
-
"sourcesContent": ["export * from './Base.js';\n"],
|
|
5
|
-
"mappings": "AAAA,cAAc;",
|
|
4
|
+
"sourcesContent": ["export * from './Base.js';\nexport { getInstances } from './utils.js';\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;AACd,SAAS,oBAAoB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -11,16 +11,16 @@ export declare class AbstractManager {
|
|
|
11
11
|
* Get the base instance root element.
|
|
12
12
|
*/
|
|
13
13
|
get __element(): HTMLElement & {
|
|
14
|
-
__base__?: WeakMap<import("Base
|
|
14
|
+
__base__?: WeakMap<import("../Base.js").BaseConstructor<Base<import("../Base.js").BaseProps>>, Base<import("../Base.js").BaseProps> | "terminated">;
|
|
15
15
|
};
|
|
16
16
|
/**
|
|
17
17
|
* Get the base instance config.
|
|
18
18
|
*/
|
|
19
|
-
get __config(): import("Base
|
|
19
|
+
get __config(): import("../Base.js").BaseConfig;
|
|
20
20
|
/**
|
|
21
21
|
* Get the events manager.
|
|
22
22
|
*/
|
|
23
|
-
get __eventsManager(): import("
|
|
23
|
+
get __eventsManager(): import("./EventsManager.js").EventsManager;
|
|
24
24
|
/**
|
|
25
25
|
* Class constructor.
|
|
26
26
|
*/
|
|
@@ -49,5 +49,5 @@ export declare class ChildrenManager extends AbstractManager {
|
|
|
49
49
|
* @private
|
|
50
50
|
*/
|
|
51
51
|
__register(name: string, component: BaseConstructor | BaseAsyncConstructor): void;
|
|
52
|
-
__getValue(name: string, component: BaseConstructor | BaseAsyncConstructor): (Base<import("Base
|
|
52
|
+
__getValue(name: string, component: BaseConstructor | BaseAsyncConstructor): (Base<import("../Base.js").BaseProps> | "terminated" | Promise<Base<import("../Base.js").BaseProps> | "terminated">)[];
|
|
53
53
|
}
|
package/Base/utils.d.ts
CHANGED
|
@@ -28,3 +28,5 @@ export declare function eventIsNative(event: string, element: HTMLElement): bool
|
|
|
28
28
|
* @todo Return false in v3 if event is not defined or not native to prevent adding a listener.
|
|
29
29
|
*/
|
|
30
30
|
export declare function getEventTarget(instance: Base, event: string, config: BaseConfig): Base | Base['$el'];
|
|
31
|
+
export declare function getInstances<T extends typeof Base = typeof Base>(ctor: T): Set<InstanceType<T>>;
|
|
32
|
+
export declare function registerInstance(instance: any): void;
|
package/Base/utils.js
CHANGED
|
@@ -2,7 +2,7 @@ import { isArray, isDefined, isDev, SmartQueue } from "../utils/index.js";
|
|
|
2
2
|
import { features } from "./features.js";
|
|
3
3
|
let queue;
|
|
4
4
|
function addToQueue(fn) {
|
|
5
|
-
if (
|
|
5
|
+
if (features.get("blocking")) {
|
|
6
6
|
fn();
|
|
7
7
|
return;
|
|
8
8
|
}
|
|
@@ -63,11 +63,23 @@ function getEventTarget(instance, event, config) {
|
|
|
63
63
|
}
|
|
64
64
|
return instance;
|
|
65
65
|
}
|
|
66
|
+
const instances = /* @__PURE__ */ new Map();
|
|
67
|
+
function getInstances(ctor) {
|
|
68
|
+
if (!instances.has(ctor)) {
|
|
69
|
+
instances.set(ctor, /* @__PURE__ */ new Set());
|
|
70
|
+
}
|
|
71
|
+
return instances.get(ctor);
|
|
72
|
+
}
|
|
73
|
+
function registerInstance(instance) {
|
|
74
|
+
getInstances(instance.constructor).add(instance);
|
|
75
|
+
}
|
|
66
76
|
export {
|
|
67
77
|
addToQueue,
|
|
68
78
|
eventIsDefinedInConfig,
|
|
69
79
|
eventIsNative,
|
|
70
80
|
getComponentElements,
|
|
71
|
-
getEventTarget
|
|
81
|
+
getEventTarget,
|
|
82
|
+
getInstances,
|
|
83
|
+
registerInstance
|
|
72
84
|
};
|
|
73
85
|
//# sourceMappingURL=utils.js.map
|
package/Base/utils.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/Base/utils.ts"],
|
|
4
|
-
"sourcesContent": ["import { isArray, isDefined, isDev, SmartQueue } from '../utils/index.js';\nimport type { Base, BaseConfig } from './index.js';\nimport { features } from './features.js';\n\nlet queue: SmartQueue;\n\n/**\n * Add a task to the main queue.\n */\nexport function addToQueue(fn: () => unknown): void {\n if (
|
|
5
|
-
"mappings": "AAAA,SAAS,SAAS,WAAW,OAAO,kBAAkB;AAEtD,SAAS,gBAAgB;AAEzB,IAAI;AAKG,SAAS,WAAW,IAAyB;AAClD,MAAI,
|
|
4
|
+
"sourcesContent": ["import { isArray, isDefined, isDev, SmartQueue } from '../utils/index.js';\nimport type { Base, BaseConfig } from './index.js';\nimport { features } from './features.js';\n\nlet queue: SmartQueue;\n\n/**\n * Add a task to the main queue.\n */\nexport function addToQueue(fn: () => unknown): void {\n if (features.get('blocking')) {\n fn();\n return;\n }\n\n if (!queue) {\n queue = new SmartQueue();\n }\n\n queue.add(fn);\n}\n\nconst selectors = new Map();\n\n// Separator used for multi-component declaration in `data-component` attributeS.\nconst separator = ' ';\n\n/**\n * Get the selector for a given component.\n */\nfunction getSelector(nameOrSelector: string): string {\n if (!selectors.has(nameOrSelector)) {\n const parts = [\n // Single selector\n `[data-component=\"${nameOrSelector}\"]`,\n // Selector in the middle of a list of selectors\n `[data-component*=\"${separator}${nameOrSelector}${separator}\"]`,\n // Selector at the end of a list of selectors\n `[data-component$=\"${separator}${nameOrSelector}\"]`,\n // Selector at the beginning of a list of selectors\n `[data-component^=\"${nameOrSelector}${separator}\"]`,\n ];\n selectors.set(nameOrSelector, parts.join(','));\n }\n\n return selectors.get(nameOrSelector);\n}\n\n/**\n * Get a list of elements based on the name of a component.\n *\n * @param {string} nameOrSelector\n * The name or selector to used for this component.\n * @param {HTMLElement|Document} element\n * The root element on which to query the selector, defaults to `document`.\n * @returns {Array<HTMLElement>}\n * A list of elements on which the component should be mounted.\n */\nexport function getComponentElements(\n nameOrSelector: string,\n element: HTMLElement | Document = document,\n): HTMLElement[] {\n const selector = getSelector(nameOrSelector);\n let elements = [];\n\n try {\n elements = Array.from(element.querySelectorAll(selector));\n // eslint-disable-next-line no-empty\n } catch {}\n\n // If no child component found with the default selector, and if the selector does not\n // start with an uppercase letter, matching a component's name, try a classic DOM selector.\n if (elements.length === 0 && nameOrSelector[0] === nameOrSelector[0].toLowerCase()) {\n elements = Array.from(element.querySelectorAll(nameOrSelector));\n }\n\n return elements;\n}\n\n/**\n * Test if an event is defined in the given config.\n */\nexport function eventIsDefinedInConfig(event: string, config: BaseConfig): boolean {\n return isArray(config.emits) && config.emits.includes(event);\n}\n\n/**\n * Test if an event can be used on the given element.\n */\nexport function eventIsNative(event: string, element: HTMLElement): boolean {\n return isDefined(element[`on${event}`]);\n}\n\n/**\n * Get the target of a given event.\n *\n * @todo Return false in v3 if event is not defined or not native to prevent adding a listener.\n */\nexport function getEventTarget(\n instance: Base,\n event: string,\n config: BaseConfig,\n): Base | Base['$el'] {\n if (eventIsDefinedInConfig(event, config)) {\n return instance;\n }\n\n if (eventIsNative(event, instance.$el)) {\n return instance.$el;\n }\n\n if (isDev) {\n console.warn(\n `[${config.name}]`,\n `The \"${event}\" event is missing from the configuration and is not a native`,\n `event for the root element of type \\`${instance.$el.constructor.name}\\`.`,\n );\n }\n\n // @todo v3 return false or null\n return instance;\n}\n\nconst instances = new Map<typeof Base, Set<Base>>();\n\nexport function getInstances<T extends typeof Base = typeof Base>(ctor: T): Set<InstanceType<T>> {\n if (!instances.has(ctor)) {\n instances.set(ctor, new Set<InstanceType<T>>());\n }\n\n return instances.get(ctor) as Set<InstanceType<T>>;\n}\n\nexport function registerInstance(instance) {\n getInstances(instance.constructor).add(instance);\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,SAAS,WAAW,OAAO,kBAAkB;AAEtD,SAAS,gBAAgB;AAEzB,IAAI;AAKG,SAAS,WAAW,IAAyB;AAClD,MAAI,SAAS,IAAI,UAAU,GAAG;AAC5B,OAAG;AACH;AAAA,EACF;AAEA,MAAI,CAAC,OAAO;AACV,YAAQ,IAAI,WAAW;AAAA,EACzB;AAEA,QAAM,IAAI,EAAE;AACd;AAEA,MAAM,YAAY,oBAAI,IAAI;AAG1B,MAAM,YAAY;AAKlB,SAAS,YAAY,gBAAgC;AACnD,MAAI,CAAC,UAAU,IAAI,cAAc,GAAG;AAClC,UAAM,QAAQ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AAI,CAAC,UAAU,IAAI,IAAI,GAAG;AACxB,cAAU,IAAI,MAAM,oBAAI,IAAqB,CAAC;AAAA,EAChD;AAEA,SAAO,UAAU,IAAI,IAAI;AAC3B;AAEO,SAAS,iBAAiB,UAAU;AACzC,eAAa,SAAS,WAAW,EAAE,IAAI,QAAQ;AACjD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# JS Toolkit
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@studiometa/js-toolkit/)
|
|
4
|
+
[](https://www.npmjs.com/package/@studiometa/js-toolkit/v/next)
|
|
5
|
+
[](https://www.npmjs.com/package/@studiometa/js-toolkit/)
|
|
6
|
+
[](https://bundlephobia.com/package/@studiometa/js-toolkit)
|
|
7
|
+
[](https://david-dm.org/studiometa/js-toolkit)
|
|
8
|
+

|
|
7
9
|
|
|
8
10
|
> A JavaScript data-attributes driven micro-framework shipped with plenty of useful utility functions to boost your project.
|
|
9
11
|
|
package/decorators/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './withIntersectionObserver.js';
|
|
|
7
7
|
export * from './withMountOnMediaQuery.js';
|
|
8
8
|
export * from './withMountWhenInView.js';
|
|
9
9
|
export * from './withMountWhenPrefersMotion.js';
|
|
10
|
+
export * from './withName.js';
|
|
10
11
|
export * from './withRelativePointer.js';
|
|
11
12
|
export * from './withResponsiveOptions.js';
|
|
12
13
|
export * from './withScrolledInView/index.js';
|
package/decorators/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export * from "./withIntersectionObserver.js";
|
|
|
7
7
|
export * from "./withMountOnMediaQuery.js";
|
|
8
8
|
export * from "./withMountWhenInView.js";
|
|
9
9
|
export * from "./withMountWhenPrefersMotion.js";
|
|
10
|
+
export * from "./withName.js";
|
|
10
11
|
export * from "./withRelativePointer.js";
|
|
11
12
|
export * from "./withResponsiveOptions.js";
|
|
12
13
|
export * from "./withScrolledInView/index.js";
|
package/decorators/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/decorators/index.ts"],
|
|
4
|
-
"sourcesContent": ["export * from './withBreakpointManager.js';\nexport * from './withBreakpointObserver.js';\nexport * from './withDrag.js';\nexport * from './withExtraConfig.js';\nexport * from './withFreezedOptions.js';\nexport * from './withIntersectionObserver.js';\nexport * from './withMountOnMediaQuery.js';\nexport * from './withMountWhenInView.js';\nexport * from './withMountWhenPrefersMotion.js';\nexport * from './withRelativePointer.js';\nexport * from './withResponsiveOptions.js';\nexport * from './withScrolledInView/index.js';\n"],
|
|
5
|
-
"mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
4
|
+
"sourcesContent": ["export * from './withBreakpointManager.js';\nexport * from './withBreakpointObserver.js';\nexport * from './withDrag.js';\nexport * from './withExtraConfig.js';\nexport * from './withFreezedOptions.js';\nexport * from './withIntersectionObserver.js';\nexport * from './withMountOnMediaQuery.js';\nexport * from './withMountWhenInView.js';\nexport * from './withMountWhenPrefersMotion.js';\nexport * from './withName.js';\nexport * from './withRelativePointer.js';\nexport * from './withResponsiveOptions.js';\nexport * from './withScrolledInView/index.js';\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BaseDecorator, BaseInterface } from '../Base/types.js';
|
|
2
|
+
import type { Base, BaseProps } from '../Base/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Override the name of the given component.
|
|
5
|
+
*/
|
|
6
|
+
export declare function withName<S extends Base = Base>(BaseClass: typeof Base, name: string): BaseDecorator<BaseInterface, S, BaseProps>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../packages/js-toolkit/decorators/withName.ts"],
|
|
4
|
+
"sourcesContent": ["import type { BaseDecorator, BaseInterface } from '../Base/types.js';\nimport type { Base, BaseProps, BaseConfig } from '../Base/index.js';\n\n/**\n * Override the name of the given component.\n */\nexport function withName<S extends Base = Base>(\n BaseClass: typeof Base,\n name: string,\n): BaseDecorator<BaseInterface, S, BaseProps> {\n // @ts-ignore\n return class extends BaseClass {\n static config: BaseConfig = {\n ...BaseClass.config,\n name,\n };\n };\n}\n"],
|
|
5
|
+
"mappings": "AAMO,SAAS,SACd,WACA,MAC4C;AAE5C,SAAO,cAAc,UAAU;AAAA,IAC7B,OAAO,SAAqB;AAAA,MAC1B,GAAG,UAAU;AAAA,MACb;AAAA,IACF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/helpers/createApp.d.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import type { Base, BaseConstructor, BaseProps } from '../Base/index.js';
|
|
2
2
|
import type { Features } from '../Base/features.js';
|
|
3
|
-
export type CreateAppOptions = {
|
|
4
|
-
root
|
|
5
|
-
features: Partial<Features>;
|
|
6
|
-
breakpoints?: Record<string, string>;
|
|
3
|
+
export type CreateAppOptions = Partial<Features> & {
|
|
4
|
+
root?: HTMLElement;
|
|
7
5
|
};
|
|
8
6
|
/**
|
|
9
7
|
* Instantiate and mount the given component on the given root element when the page has been loaded
|
|
10
8
|
* and return a function to use the app instance when it is ready.
|
|
11
9
|
*/
|
|
12
|
-
export default function createApp<S extends BaseConstructor<Base>, T extends BaseProps = BaseProps>(App: S,
|
|
10
|
+
export default function createApp<S extends BaseConstructor<Base>, T extends BaseProps = BaseProps>(App: S, options?: HTMLElement | CreateAppOptions): () => Promise<S & Base<T>>;
|
package/helpers/createApp.js
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import { features } from "../Base/features.js";
|
|
2
|
-
import {
|
|
3
|
-
function createApp(App,
|
|
2
|
+
import { isBoolean, isObject } from "../utils/index.js";
|
|
3
|
+
function createApp(App, options = {}) {
|
|
4
4
|
let app;
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
const {
|
|
6
|
+
root = document.body,
|
|
7
|
+
breakpoints = null,
|
|
8
|
+
blocking = null
|
|
9
|
+
} = options instanceof HTMLElement ? { root: options } : options;
|
|
10
|
+
if (isObject(breakpoints)) {
|
|
11
|
+
features.set("breakpoints", breakpoints);
|
|
12
|
+
}
|
|
13
|
+
if (isBoolean(blocking)) {
|
|
14
|
+
features.set("blocking", blocking);
|
|
10
15
|
}
|
|
11
16
|
function init() {
|
|
12
|
-
app = new App(
|
|
17
|
+
app = new App(root).$mount();
|
|
13
18
|
return app;
|
|
14
19
|
}
|
|
15
20
|
let p;
|
|
16
|
-
if (features.get("
|
|
21
|
+
if (features.get("blocking")) {
|
|
17
22
|
p = Promise.resolve(init());
|
|
18
23
|
} else {
|
|
19
24
|
p = new Promise((resolve) => {
|
package/helpers/createApp.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/helpers/createApp.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Base, BaseConstructor, BaseProps } from '../Base/index.js';\nimport type { Features } from '../Base/features.js';\nimport { features } from '../Base/features.js';\nimport {
|
|
5
|
-
"mappings": "AAEA,SAAS,gBAAgB;AACzB,SAAS,
|
|
4
|
+
"sourcesContent": ["import type { Base, BaseConstructor, BaseProps } from '../Base/index.js';\nimport type { Features } from '../Base/features.js';\nimport { features } from '../Base/features.js';\nimport { isBoolean, isObject } from '../utils/index.js';\n\nexport type CreateAppOptions = Partial<Features> & {\n root?: HTMLElement;\n};\n\n/**\n * Instantiate and mount the given component on the given root element when the page has been loaded\n * and return a function to use the app instance when it is ready.\n */\nexport default function createApp<S extends BaseConstructor<Base>, T extends BaseProps = BaseProps>(\n App: S,\n options: HTMLElement | CreateAppOptions = {},\n): () => Promise<S & Base<T>> {\n let app: S & Base<T>;\n const {\n root = document.body,\n breakpoints = null,\n blocking = null,\n } = options instanceof HTMLElement ? { root: options } : options;\n\n if (isObject(breakpoints)) {\n features.set('breakpoints', breakpoints);\n }\n\n if (isBoolean(blocking)) {\n features.set('blocking', blocking);\n }\n\n function init() {\n app = (new App(root) as S & Base<T>).$mount();\n return app;\n }\n\n let p: Promise<S & Base<T>>;\n\n if (features.get('blocking')) {\n p = Promise.resolve(init());\n } else {\n p = new Promise<S & Base<T>>((resolve) => {\n if (document.readyState === 'complete') {\n resolve(init());\n } else {\n document.addEventListener('readystatechange', () => {\n if (document.readyState === 'complete') {\n resolve(init());\n }\n });\n }\n });\n }\n\n return () => p;\n}\n"],
|
|
5
|
+
"mappings": "AAEA,SAAS,gBAAgB;AACzB,SAAS,WAAW,gBAAgB;AAUrB,SAAR,UACL,KACA,UAA0C,CAAC,GACf;AAC5B,MAAI;AACJ,QAAM;AAAA,IACJ,OAAO,SAAS;AAAA,IAChB,cAAc;AAAA,IACd,WAAW;AAAA,EACb,IAAI,mBAAmB,cAAc,EAAE,MAAM,QAAQ,IAAI;AAEzD,MAAI,SAAS,WAAW,GAAG;AACzB,aAAS,IAAI,eAAe,WAAW;AAAA,EACzC;AAEA,MAAI,UAAU,QAAQ,GAAG;AACvB,aAAS,IAAI,YAAY,QAAQ;AAAA,EACnC;AAEA,WAAS,OAAO;AACd,UAAO,IAAI,IAAI,IAAI,EAAkB,OAAO;AAC5C,WAAO;AAAA,EACT;AAEA,MAAI;AAEJ,MAAI,SAAS,IAAI,UAAU,GAAG;AAC5B,QAAI,QAAQ,QAAQ,KAAK,CAAC;AAAA,EAC5B,OAAO;AACL,QAAI,IAAI,QAAqB,CAAC,YAAY;AACxC,UAAI,SAAS,eAAe,YAAY;AACtC,gBAAQ,KAAK,CAAC;AAAA,MAChB,OAAO;AACL,iBAAS,iBAAiB,oBAAoB,MAAM;AAClD,cAAI,SAAS,eAAe,YAAY;AACtC,oBAAQ,KAAK,CAAC;AAAA,UAChB;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO,MAAM;AACf;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
package/services/resize.js
CHANGED
|
@@ -2,7 +2,6 @@ import { useService } from "./service.js";
|
|
|
2
2
|
import { features } from "../Base/features.js";
|
|
3
3
|
import debounce from "../utils/debounce.js";
|
|
4
4
|
function createResizeService(breakpoints) {
|
|
5
|
-
const finalScreens = breakpoints ?? features.get("breakpoints");
|
|
6
5
|
function updateProps() {
|
|
7
6
|
props.width = window.innerWidth;
|
|
8
7
|
props.height = window.innerHeight;
|
|
@@ -26,14 +25,14 @@ function createResizeService(breakpoints) {
|
|
|
26
25
|
ratio: window.innerWidth / window.innerHeight,
|
|
27
26
|
orientation: "square",
|
|
28
27
|
get breakpoint() {
|
|
29
|
-
return Object.keys(this.activeBreakpoints).find((name) => this.activeBreakpoints[name]);
|
|
28
|
+
return Object.keys(this.activeBreakpoints).toReversed().find((name) => this.activeBreakpoints[name]);
|
|
30
29
|
},
|
|
31
30
|
get breakpoints() {
|
|
32
31
|
return Object.keys(this.activeBreakpoints);
|
|
33
32
|
},
|
|
34
33
|
get activeBreakpoints() {
|
|
35
34
|
return Object.fromEntries(
|
|
36
|
-
Object.entries(
|
|
35
|
+
Object.entries(breakpoints ?? features.get("breakpoints")).map(([name, breakpoint]) => [
|
|
37
36
|
name,
|
|
38
37
|
window.matchMedia(`(min-width: ${breakpoint})`).matches
|
|
39
38
|
])
|
package/services/resize.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../packages/js-toolkit/services/resize.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport { features } from '../Base/features.js';\nimport type { Features } from '../Base/features.js';\nimport debounce from '../utils/debounce.js';\nimport type { ResizeServiceInterface } from './index.js';\n\nexport interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {\n width: number;\n height: number;\n ratio: number;\n orientation: 'square' | 'landscape' | 'portrait';\n breakpoint: string;\n breakpoints: string[];\n activeBreakpoints: Record<keyof U, boolean>;\n}\n\n/**\n * Get resize service.\n */\nfunction createResizeService<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n
|
|
5
|
-
"mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAEzB,OAAO,cAAc;AAgBrB,SAAS,oBACP,aAC2B;
|
|
4
|
+
"sourcesContent": ["/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */\nimport { useService } from './service.js';\nimport { features } from '../Base/features.js';\nimport type { Features } from '../Base/features.js';\nimport debounce from '../utils/debounce.js';\nimport type { ResizeServiceInterface } from './index.js';\n\nexport interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {\n width: number;\n height: number;\n ratio: number;\n orientation: 'square' | 'landscape' | 'portrait';\n breakpoint: string;\n breakpoints: string[];\n activeBreakpoints: Record<keyof U, boolean>;\n}\n\n/**\n * Get resize service.\n */\nfunction createResizeService<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n /**\n * Update props.\n */\n function updateProps(): ResizeServiceProps<T> {\n props.width = window.innerWidth;\n props.height = window.innerHeight;\n props.ratio = window.innerWidth / window.innerHeight;\n props.orientation = 'square';\n\n if (props.ratio > 1) {\n props.orientation = 'landscape';\n }\n\n if (props.ratio < 1) {\n props.orientation = 'portrait';\n }\n\n return props;\n }\n\n const onResize = debounce(() => {\n trigger(updateProps());\n });\n\n const { add, remove, has, trigger, props } = useService({\n props: {\n width: window.innerWidth,\n height: window.innerHeight,\n ratio: window.innerWidth / window.innerHeight,\n orientation: 'square',\n get breakpoint() {\n return Object.keys(this.activeBreakpoints)\n .toReversed()\n .find((name) => this.activeBreakpoints[name]);\n },\n get breakpoints() {\n return Object.keys(this.activeBreakpoints);\n },\n get activeBreakpoints() {\n return Object.fromEntries(\n Object.entries(breakpoints ?? features.get('breakpoints')).map(([name, breakpoint]) => [\n name,\n window.matchMedia(`(min-width: ${breakpoint})`).matches,\n ]),\n ) as Record<keyof T, boolean>;\n },\n } as ResizeServiceProps<T>,\n init() {\n window.addEventListener('resize', onResize);\n },\n kill() {\n window.removeEventListener('resize', onResize);\n },\n });\n\n return {\n add,\n remove,\n has,\n props: () => props,\n };\n}\n\nconst instances: Map<string, ResizeServiceInterface | undefined> = new Map();\n\n/**\n * Use the resize service.\n */\nexport default function useResize<T extends Features['breakpoints'] = Features['breakpoints']>(\n breakpoints?: T,\n): ResizeServiceInterface<T> {\n const key = JSON.stringify(breakpoints);\n\n if (!instances.has(key)) {\n instances.set(key, createResizeService(breakpoints));\n }\n\n return (instances as Map<string, ResizeServiceInterface<T>>).get(key);\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAEzB,OAAO,cAAc;AAgBrB,SAAS,oBACP,aAC2B;AAI3B,WAAS,cAAqC;AAC5C,UAAM,QAAQ,OAAO;AACrB,UAAM,SAAS,OAAO;AACtB,UAAM,QAAQ,OAAO,aAAa,OAAO;AACzC,UAAM,cAAc;AAEpB,QAAI,MAAM,QAAQ,GAAG;AACnB,YAAM,cAAc;AAAA,IACtB;AAEA,QAAI,MAAM,QAAQ,GAAG;AACnB,YAAM,cAAc;AAAA,IACtB;AAEA,WAAO;AAAA,EACT;AAEA,QAAM,WAAW,SAAS,M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