mirror of
https://codeberg.org/vlw/elevent.git
synced 2025-09-14 08:33:42 +02:00
Compare commits
No commits in common. "master" and "1.0.0" have entirely different histories.
3 changed files with 12 additions and 15 deletions
|
@ -10,7 +10,7 @@ new Elevent("click", document.querySelector("#myButton"), () => console.log("But
|
||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
|
|
||||||
## Bind from a NodeList
|
## Bind from an HTMLCollection
|
||||||
```js
|
```js
|
||||||
new Elevent("click", document.querySelectorAll("button"), (event) => console.log(event)); // console: PointerEvent
|
new Elevent("click", document.querySelectorAll("button"), (event) => console.log(event)); // console: PointerEvent
|
||||||
```
|
```
|
||||||
|
@ -43,7 +43,7 @@ elevent.bind(document.querySelector("#bindMe"));
|
||||||
```ts
|
```ts
|
||||||
Elevent.constructor(
|
Elevent.constructor(
|
||||||
eventType: string | null
|
eventType: string | null
|
||||||
target: NodeList | HTMLElement | string | null,
|
target: HTMLCollection | HTMLElement | string | null,
|
||||||
callback: CallableFunction
|
callback: CallableFunction
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
{
|
{
|
||||||
"name": "elevent",
|
"name": "elevent",
|
||||||
"version": "1.0.2",
|
"version": "1.0.0",
|
||||||
"main": "src/Elevent.mjs",
|
"main": "src/Elevent.mjs",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "GPL-3.0-or-later",
|
|
||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"import": {
|
"import": {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
|
|
||||||
export class Elevent {
|
export class Elevent {
|
||||||
_type;
|
_type;
|
||||||
_callback;
|
_callback;
|
||||||
|
@ -11,7 +10,7 @@ export class Elevent {
|
||||||
/**
|
/**
|
||||||
* Create a new Elevent instance.
|
* Create a new Elevent instance.
|
||||||
* @param {string|null} eventType
|
* @param {string|null} eventType
|
||||||
* @param {NodeList|HTMLElement|string|null} target Strings will be treated as CSS selectors, null will create a template instance
|
* @param {HTMLCollection|HTMLElement|string|null} target Strings will be treated as CSS selectors, null will create a template instance
|
||||||
* @param {function} callback
|
* @param {function} callback
|
||||||
*/
|
*/
|
||||||
constructor(eventType = null, target = null, callback) {
|
constructor(eventType = null, target = null, callback) {
|
||||||
|
@ -20,13 +19,13 @@ export class Elevent {
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
switch (target.constructor) {
|
switch (target.constructor) {
|
||||||
case NodeList:
|
case HTMLCollection:
|
||||||
this.#bindNodeList(target);
|
this.#bindHTMLCollection(target);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case String:
|
case String:
|
||||||
this._selector = target;
|
this._selector = target;
|
||||||
this.#bindNodeList(document.querySelectorAll(target));
|
this.#bindHTMLCollection(document.querySelectorAll(target));
|
||||||
|
|
||||||
// Automatically bind new DOM elements that match the provided CSS selector string
|
// Automatically bind new DOM elements that match the provided CSS selector string
|
||||||
this._observer.observe(document.body, {
|
this._observer.observe(document.body, {
|
||||||
|
@ -58,11 +57,11 @@ export class Elevent {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bind all HTMLElements inside a NodeList
|
* Bind all HTMLElements inside an HTMLCollection
|
||||||
* @param {NodeList} nodeList
|
* @param {HTMLCollection} collection
|
||||||
*/
|
*/
|
||||||
#bindNodeList(nodeList) {
|
#bindHTMLCollection(collection) {
|
||||||
nodeList.forEach(element => this.bind(element));
|
[...collection].forEach(element => this.bind(element));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,5 +99,4 @@ export class Elevent {
|
||||||
|
|
||||||
this._boundElements = new WeakSet();
|
this._boundElements = new WeakSet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// @license-end
|
|
Loading…
Add table
Reference in a new issue