@lordfokas/yrframe 0.4.7 → 0.5.1
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/README.md +1 -1
- package/dist/Component.d.ts +3 -4
- package/dist/Component.js +21 -5
- package/dist/ComponentEvents.d.ts +2 -2
- package/dist/ComponentEvents.js +48 -15
- package/dist/ComponentFactory.js +6 -1
- package/dist/ComponentLogic.d.ts +15 -0
- package/dist/ComponentLogic.js +1 -0
- package/dist/Facade.d.ts +22 -10
- package/dist/Facade.js +67 -8
- package/dist/yrframe.d.ts +1 -0
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
# yrframe
|
|
2
|
-
A Typescript class-based WebComponents
|
|
2
|
+
A Typescript class-based WebComponents framework
|
package/dist/Component.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ComponentEvents } from "./ComponentEvents.js";
|
|
1
|
+
import { ComponentEvents, symbol as evt } from "./ComponentEvents.js";
|
|
2
|
+
import { ComponentLogic } from "./ComponentLogic.js";
|
|
2
3
|
import { Attributes } from "./utils.js";
|
|
3
|
-
declare
|
|
4
|
-
export declare class Component<A extends Attributes> extends HTMLElement {
|
|
4
|
+
export declare class Component<A extends Attributes> extends HTMLElement implements ComponentLogic {
|
|
5
5
|
readonly [evt]: ComponentEvents;
|
|
6
6
|
protected initialChildren: (string | Node)[];
|
|
7
7
|
private wasConnected;
|
|
@@ -29,4 +29,3 @@ export declare class Component<A extends Attributes> extends HTMLElement {
|
|
|
29
29
|
/** Shortcut to register this component with a given tag name */
|
|
30
30
|
static register(tag: string): void;
|
|
31
31
|
}
|
|
32
|
-
export {};
|
package/dist/Component.js
CHANGED
|
@@ -1,16 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
var _a;
|
|
2
|
+
import { ComponentEvents, symbol as evt } from "./ComponentEvents.js";
|
|
2
3
|
import { ComponentFactory } from "./ComponentFactory.js";
|
|
3
4
|
import { specialAttributes } from "./utils.js";
|
|
4
|
-
const evt = Symbol('evt');
|
|
5
5
|
export class Component extends HTMLElement {
|
|
6
|
-
[evt];
|
|
7
|
-
initialChildren = [];
|
|
8
|
-
wasConnected = false;
|
|
9
6
|
events() {
|
|
10
7
|
return this[evt];
|
|
11
8
|
}
|
|
12
9
|
constructor(props, defaults) {
|
|
13
10
|
super();
|
|
11
|
+
Object.defineProperty(this, _a, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: void 0
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "initialChildren", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: []
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "wasConnected", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: false
|
|
28
|
+
});
|
|
14
29
|
this[evt] = new ComponentEvents(this);
|
|
15
30
|
const all = {};
|
|
16
31
|
// Assign all defaults to object and overwrite with existing values in provided props
|
|
@@ -83,3 +98,4 @@ export class Component extends HTMLElement {
|
|
|
83
98
|
window.customElements.define(tag, this);
|
|
84
99
|
}
|
|
85
100
|
}
|
|
101
|
+
_a = evt;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Event } from '@lordfokas/event-bus';
|
|
2
|
-
import { Component } from './Component.js';
|
|
3
2
|
import { EventTarget, Source } from './utils.js';
|
|
3
|
+
export declare const symbol: unique symbol;
|
|
4
4
|
type Value = object | string | number | boolean;
|
|
5
|
-
type Callback<T extends Event> = (this:
|
|
5
|
+
type Callback<T extends Event> = (this: HTMLElement, value: T | Value | Value[], evt: T) => void;
|
|
6
6
|
type Optional = "optional";
|
|
7
7
|
/**
|
|
8
8
|
* Event manager for component special functions.
|
package/dist/ComponentEvents.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { EventBus, EventListener } from '@lordfokas/event-bus';
|
|
2
2
|
import { Component } from './Component.js';
|
|
3
|
+
export const symbol = Symbol('evt');
|
|
3
4
|
class PersistentListener extends EventListener {
|
|
4
|
-
type;
|
|
5
5
|
constructor(type, callback, nice) {
|
|
6
6
|
super(callback, nice);
|
|
7
|
+
Object.defineProperty(this, "type", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: void 0
|
|
12
|
+
});
|
|
7
13
|
this.type = type;
|
|
8
14
|
}
|
|
9
15
|
}
|
|
@@ -13,14 +19,44 @@ class PersistentListener extends EventListener {
|
|
|
13
19
|
* except by calling Component.events()
|
|
14
20
|
*/
|
|
15
21
|
export class ComponentEvents {
|
|
16
|
-
listeners = [];
|
|
17
|
-
sources = {};
|
|
18
|
-
triggers = {};
|
|
19
|
-
component;
|
|
20
|
-
owner;
|
|
21
|
-
/** Used to keep track of internal mechanisms (triggers and sources) */
|
|
22
|
-
internals = 0;
|
|
23
22
|
constructor(component) {
|
|
23
|
+
Object.defineProperty(this, "listeners", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: []
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "sources", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: {}
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "triggers", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: {}
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "component", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: void 0
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "owner", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
/** Used to keep track of internal mechanisms (triggers and sources) */
|
|
54
|
+
Object.defineProperty(this, "internals", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: 0
|
|
59
|
+
});
|
|
24
60
|
this.component = component;
|
|
25
61
|
this.owner = component instanceof Component ? component.constructor.name : '<' + component.tagName.toLowerCase() + '>';
|
|
26
62
|
}
|
|
@@ -59,10 +95,10 @@ export class ComponentEvents {
|
|
|
59
95
|
this.createListener(target, name, handler);
|
|
60
96
|
}
|
|
61
97
|
attachGeneric(target, handler, name, optional) {
|
|
62
|
-
this.attachListener(target, handler, "generic listener", optional);
|
|
98
|
+
this.attachListener(target, handler, name, "generic listener", optional);
|
|
63
99
|
}
|
|
64
100
|
attachConsumer(target, handler, name, optional) {
|
|
65
|
-
this.attachListener(target, handler, "consumer", optional);
|
|
101
|
+
this.attachListener(target, handler, name, "consumer", optional);
|
|
66
102
|
}
|
|
67
103
|
/** Attach a listener that enriches the target event with data from this component. */
|
|
68
104
|
attachSupplier(target, source, name, optional) {
|
|
@@ -70,7 +106,6 @@ export class ComponentEvents {
|
|
|
70
106
|
return;
|
|
71
107
|
const [type, path, nice] = target;
|
|
72
108
|
this.listeners.push(new PersistentListener(type, event => {
|
|
73
|
-
const path = target[1];
|
|
74
109
|
const data = source.call(this);
|
|
75
110
|
if (path.length == 0)
|
|
76
111
|
event.with(data);
|
|
@@ -89,7 +124,6 @@ export class ComponentEvents {
|
|
|
89
124
|
return;
|
|
90
125
|
const [type, path, nice] = target;
|
|
91
126
|
this.listeners.push(new PersistentListener(type, event => {
|
|
92
|
-
const path = target[1];
|
|
93
127
|
if (!predicate.call(this))
|
|
94
128
|
return;
|
|
95
129
|
if (path.length == 0)
|
|
@@ -144,9 +178,8 @@ export class ComponentEvents {
|
|
|
144
178
|
/** Fire a source function and process the returned data. */
|
|
145
179
|
seek(name, callback) {
|
|
146
180
|
if (!this.sources[name])
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
this.sources[name].call(undefined, callback);
|
|
181
|
+
throw new Error(`Source '${name}' not found at '${this.owner}`);
|
|
182
|
+
this.sources[name].call(undefined, callback);
|
|
150
183
|
}
|
|
151
184
|
/** Create a function that will fire an event to export data. */
|
|
152
185
|
attachTrigger(target, name, optional) {
|
package/dist/ComponentFactory.js
CHANGED
|
@@ -68,5 +68,10 @@ export class ComponentFactory {
|
|
|
68
68
|
* `¯\_(ツ)_/¯`
|
|
69
69
|
*/
|
|
70
70
|
export class FakeReact {
|
|
71
|
-
static createElement = ComponentFactory.make;
|
|
72
71
|
}
|
|
72
|
+
Object.defineProperty(FakeReact, "createElement", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
configurable: true,
|
|
75
|
+
writable: true,
|
|
76
|
+
value: ComponentFactory.make
|
|
77
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ComponentLogic {
|
|
2
|
+
/** Remove all children nodes. */
|
|
3
|
+
clearChildren(): void;
|
|
4
|
+
isDisabled(): boolean;
|
|
5
|
+
/** HTML5 Custom Element lifecycle callback (remove from page) */
|
|
6
|
+
disconnectedCallback(): void;
|
|
7
|
+
/** HTML5 Custom Element lifecycle callback (add to page) */
|
|
8
|
+
connectedCallback(): void;
|
|
9
|
+
/** Draw this component's internals. Should return only children nodes. */
|
|
10
|
+
render(): Node | string | (Node | string)[] | null;
|
|
11
|
+
/** Redraw this component. Works by deleting all children, calling render() and appending the results. */
|
|
12
|
+
redraw(): void;
|
|
13
|
+
/** Called after redraw() to do special manipulation of children nodes. */
|
|
14
|
+
renderedCallback(): void;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/Facade.d.ts
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
1
|
-
import { ComponentEvents } from "./ComponentEvents.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
connectedCallback: Source<void>;
|
|
9
|
-
disconnectedCallback: Source<void>;
|
|
1
|
+
import { ComponentEvents, symbol as evt } from "./ComponentEvents.js";
|
|
2
|
+
import { ComponentLogic } from "./ComponentLogic.js";
|
|
3
|
+
import { Attributes } from "./utils.js";
|
|
4
|
+
declare const fcl: unique symbol;
|
|
5
|
+
type FacadeElement = HTMLElement & {
|
|
6
|
+
[evt]?: ComponentEvents;
|
|
7
|
+
[fcl]: Facade<any>;
|
|
10
8
|
};
|
|
11
9
|
/**
|
|
12
10
|
* Fake component that constructs and returns another component instead.
|
|
13
11
|
* Used for aliasing and adapting components from other libraries.
|
|
14
12
|
*/
|
|
15
|
-
export declare class Facade<A extends Attributes> {
|
|
13
|
+
export declare class Facade<A extends Attributes> implements ComponentLogic {
|
|
16
14
|
protected readonly node: FacadeElement;
|
|
17
15
|
protected readonly isCustom: boolean;
|
|
18
16
|
protected initialChildren: (string | Node)[];
|
|
17
|
+
protected wasConnected: boolean;
|
|
19
18
|
protected events(): ComponentEvents;
|
|
20
19
|
constructor(tag: string, props: Partial<A>, defaults?: Partial<A>);
|
|
20
|
+
/** Remove all children nodes. */
|
|
21
|
+
clearChildren(): void;
|
|
22
|
+
isDisabled(): boolean;
|
|
23
|
+
/** HTML5 Custom Element lifecycle callback (remove from page) */
|
|
24
|
+
disconnectedCallback(): void;
|
|
25
|
+
/** HTML5 Custom Element lifecycle callback (add to page) */
|
|
26
|
+
connectedCallback(): void;
|
|
27
|
+
/** Draw this component's internals. Should return only children nodes. */
|
|
28
|
+
render(): Node | string | (Node | string)[] | null;
|
|
29
|
+
/** Redraw this component. Works by deleting all children, calling render() and appending the results. */
|
|
30
|
+
redraw(): void;
|
|
31
|
+
/** Called after redraw() to do special manipulation of children nodes. */
|
|
32
|
+
renderedCallback(): void;
|
|
21
33
|
content(): FacadeElement;
|
|
22
34
|
setInitialChildren(children: (Node | string)[]): void;
|
|
23
35
|
}
|
package/dist/Facade.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import { ComponentEvents } from "./ComponentEvents.js";
|
|
1
|
+
import { ComponentEvents, symbol as evt } from "./ComponentEvents.js";
|
|
2
2
|
import { ComponentFactory } from "./ComponentFactory.js";
|
|
3
3
|
import { specialAttributes } from "./utils.js";
|
|
4
|
-
const
|
|
4
|
+
const fcl = Symbol('fcl');
|
|
5
5
|
/**
|
|
6
6
|
* Fake component that constructs and returns another component instead.
|
|
7
7
|
* Used for aliasing and adapting components from other libraries.
|
|
8
8
|
*/
|
|
9
9
|
export class Facade {
|
|
10
|
-
node;
|
|
11
|
-
isCustom;
|
|
12
|
-
initialChildren = [];
|
|
13
10
|
events() {
|
|
14
11
|
if (!this.node[evt]) { // Lazily instantiate event manager
|
|
15
12
|
this.node[evt] = new ComponentEvents(this.node);
|
|
@@ -17,7 +14,32 @@ export class Facade {
|
|
|
17
14
|
return this.node[evt];
|
|
18
15
|
}
|
|
19
16
|
constructor(tag, props, defaults) {
|
|
17
|
+
Object.defineProperty(this, "node", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: void 0
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "isCustom", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: void 0
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "initialChildren", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: []
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "wasConnected", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: false
|
|
40
|
+
});
|
|
20
41
|
const node = this.node = document.createElement(tag);
|
|
42
|
+
node[fcl] = this;
|
|
21
43
|
this.isCustom = tag.includes('-');
|
|
22
44
|
const all = {};
|
|
23
45
|
// Assign all defaults to object and overwrite with existing values in provided props
|
|
@@ -33,6 +55,43 @@ export class Facade {
|
|
|
33
55
|
}
|
|
34
56
|
ComponentFactory.setAttributesAndEvents(node, attrs);
|
|
35
57
|
}
|
|
58
|
+
/** Remove all children nodes. */
|
|
59
|
+
clearChildren() {
|
|
60
|
+
this.node.innerHTML = "";
|
|
61
|
+
}
|
|
62
|
+
isDisabled() {
|
|
63
|
+
return this.node.hasAttribute("disabled");
|
|
64
|
+
}
|
|
65
|
+
/** HTML5 Custom Element lifecycle callback (remove from page) */
|
|
66
|
+
disconnectedCallback() {
|
|
67
|
+
this.node[evt]?.disconnect();
|
|
68
|
+
}
|
|
69
|
+
/** HTML5 Custom Element lifecycle callback (add to page) */
|
|
70
|
+
connectedCallback() {
|
|
71
|
+
this.wasConnected = true;
|
|
72
|
+
this.node[evt]?.connect();
|
|
73
|
+
this.redraw();
|
|
74
|
+
}
|
|
75
|
+
/** Draw this component's internals. Should return only children nodes. */
|
|
76
|
+
render() {
|
|
77
|
+
return this.initialChildren;
|
|
78
|
+
}
|
|
79
|
+
/** Redraw this component. Works by deleting all children, calling render() and appending the results. */
|
|
80
|
+
redraw() {
|
|
81
|
+
const child = this.render();
|
|
82
|
+
this.clearChildren();
|
|
83
|
+
if (child) {
|
|
84
|
+
if (Array.isArray(child)) {
|
|
85
|
+
ComponentFactory.appendChildren(this.node, ...child);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
ComponentFactory.appendChildren(this.node, child);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this.renderedCallback();
|
|
92
|
+
}
|
|
93
|
+
/** Called after redraw() to do special manipulation of children nodes. */
|
|
94
|
+
renderedCallback() { }
|
|
36
95
|
content() {
|
|
37
96
|
// Dispose of unused event manager
|
|
38
97
|
if (this.node[evt]?.isEmpty()) {
|
|
@@ -56,11 +115,11 @@ function traverseHTML(node, fn) {
|
|
|
56
115
|
new MutationObserver((records) => {
|
|
57
116
|
for (const record of records) {
|
|
58
117
|
for (const added of record.addedNodes) {
|
|
59
|
-
traverseHTML(added, (node) => node[
|
|
118
|
+
traverseHTML(added, (node) => node[fcl]?.connectedCallback());
|
|
60
119
|
}
|
|
61
120
|
for (const removed of record.removedNodes) {
|
|
62
|
-
traverseHTML(removed, (node) => node[
|
|
121
|
+
traverseHTML(removed, (node) => node[fcl]?.disconnectedCallback());
|
|
63
122
|
}
|
|
64
123
|
}
|
|
65
|
-
}).observe(document.body, { subtree: true, childList: true });
|
|
124
|
+
}).observe(document.body, { subtree: true, attributes: false, childList: true });
|
|
66
125
|
})();
|
package/dist/yrframe.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { Component } from './Component.js';
|
|
2
2
|
export { ComponentEvents } from './ComponentEvents.js';
|
|
3
3
|
export { ComponentFactory, FakeReact } from './ComponentFactory.js';
|
|
4
|
+
export { ComponentLogic } from './ComponentLogic.js';
|
|
4
5
|
export { Facade } from './Facade.js';
|
|
5
6
|
export { Attributes, EventTarget } from './utils.js';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lordfokas/yrframe",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A Typescript class-based WebComponents
|
|
3
|
+
"version": "0.5.1",
|
|
4
|
+
"description": "A Typescript class-based WebComponents framework.",
|
|
5
5
|
"main": "dist/yrframe.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prebuild": "rm -rf ./dist",
|
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
"prepublishOnly": "npm run build"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
|
-
"
|
|
13
|
-
"design",
|
|
12
|
+
"webcomponents",
|
|
14
13
|
"components",
|
|
15
14
|
"typescript"
|
|
16
15
|
],
|