@lordfokas/yrframe 0.1.3 → 0.3.0

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.
@@ -5,8 +5,12 @@ export declare class Component<A extends Attributes> extends HTMLElement {
5
5
  /** Attribute Event Qualifier chars. Attributes starting with this are special. */
6
6
  static readonly AEQ = "yr:";
7
7
  readonly [evt]: ComponentEvents;
8
+ protected initialChildren: (string | Node)[];
9
+ private wasConnected;
8
10
  protected events(): ComponentEvents;
9
11
  constructor(props: Partial<A>, defaults?: Partial<A>);
12
+ /** Override HTMLElement */
13
+ append(...nodes: (string | Node)[]): void;
10
14
  /** Remove all children nodes. */
11
15
  clearChildren(): void;
12
16
  isDisabled(): boolean;
@@ -15,11 +19,11 @@ export declare class Component<A extends Attributes> extends HTMLElement {
15
19
  /** HTML5 Custom Element lifecycle callback (add to page) */
16
20
  connectedCallback(): void;
17
21
  /** Draw this component's internals. Should return only children nodes. */
18
- render(): HTMLElement | HTMLElement[] | string | null;
22
+ render(): Node | string | (Node | string)[] | null;
19
23
  /** Redraw this component. Works by deleting all children, calling render() and appending the results. */
20
24
  redraw(): void;
21
25
  /** Called after redraw() to do special manipulation of children nodes. */
22
- inject(): void;
26
+ onRedraw(): void;
23
27
  /** Get this component's real width in pixels. */
24
28
  width(): number;
25
29
  /** Get this component's real height in pixels. */
package/dist/Component.js CHANGED
@@ -5,6 +5,8 @@ export class Component extends HTMLElement {
5
5
  /** Attribute Event Qualifier chars. Attributes starting with this are special. */
6
6
  static AEQ = 'yr:';
7
7
  [evt];
8
+ initialChildren = [];
9
+ wasConnected = false;
8
10
  events() {
9
11
  return this[evt];
10
12
  }
@@ -25,6 +27,13 @@ export class Component extends HTMLElement {
25
27
  }
26
28
  ComponentFactory.setAttributesAndEvents(this, attrs);
27
29
  }
30
+ /** Override HTMLElement */
31
+ append(...nodes) {
32
+ if (!this.wasConnected) {
33
+ this.initialChildren.push(...nodes);
34
+ }
35
+ super.append(...nodes);
36
+ }
28
37
  /** Remove all children nodes. */
29
38
  clearChildren() {
30
39
  this.innerHTML = "";
@@ -38,27 +47,30 @@ export class Component extends HTMLElement {
38
47
  }
39
48
  /** HTML5 Custom Element lifecycle callback (add to page) */
40
49
  connectedCallback() {
50
+ this.wasConnected = true;
41
51
  this[evt].connect();
42
52
  this.redraw();
43
53
  }
44
54
  /** Draw this component's internals. Should return only children nodes. */
45
- render() { return null; }
55
+ render() {
56
+ return this.initialChildren;
57
+ }
46
58
  /** Redraw this component. Works by deleting all children, calling render() and appending the results. */
47
59
  redraw() {
48
60
  const child = this.render();
49
61
  this.clearChildren();
50
62
  if (child) {
51
63
  if (Array.isArray(child)) {
52
- ComponentFactory.appendChildren(this, child);
64
+ ComponentFactory.appendChildren(this, ...child);
53
65
  }
54
66
  else {
55
- ComponentFactory.appendChildren(this, [child]);
67
+ ComponentFactory.appendChildren(this, child);
56
68
  }
57
69
  }
58
- this.inject();
70
+ this.onRedraw();
59
71
  }
60
72
  /** Called after redraw() to do special manipulation of children nodes. */
61
- inject() { }
73
+ onRedraw() { }
62
74
  /** Get this component's real width in pixels. */
63
75
  width() {
64
76
  return parseFloat(getComputedStyle(this, null).width.replace("px", ""));
@@ -35,8 +35,8 @@ export declare class ComponentEvents {
35
35
  * If the target configuration has no path, the whole event is stopped instead.
36
36
  */
37
37
  attachRemover<T extends Event, K1 extends keyof T>(target: EventTarget<T, K1> | undefined, predicate: Source<boolean>, name: string, optional?: Optional): void;
38
- /** Attach a listener that disables a component based on a boolean event field. */
39
- attachDisabler<T extends Event, K1 extends keyof T>(target: EventTarget<T, K1> | undefined, source: Source<HTMLElement>, name: string, optional?: Optional): void;
38
+ /** Attach a listener that adds or removes a flag attribute from a component based on a boolean event field. */
39
+ attachFlagger<T extends Event, K1 extends keyof T>(target: EventTarget<T, K1> | undefined, source: Source<HTMLElement>, flag: string, ref: boolean, name: string, optional?: Optional): void;
40
40
  /** Create a function that will fire an event and process data from it. */
41
41
  attachSource<T extends Event, K1 extends keyof T>(target: EventTarget<T, K1> | undefined, name: string, optional?: Optional): void;
42
42
  /** Create a fake source function that supplies a static or local value. */
@@ -94,19 +94,19 @@ export class ComponentEvents {
94
94
  throw new Error("Unsupported path length > 1 for removers");
95
95
  }, nice).named(name, this.owner));
96
96
  }
97
- /** Attach a listener that disables a component based on a boolean event field. */
98
- attachDisabler(target, source, name, optional) {
99
- if (this.skip(target, name, "disabler", optional))
97
+ /** Attach a listener that adds or removes a flag attribute from a component based on a boolean event field. */
98
+ attachFlagger(target, source, flag, ref, name, optional) {
99
+ if (this.skip(target, name, "flagger", optional))
100
100
  return;
101
101
  this.createListener(target, name, (value, _) => {
102
102
  if (typeof value !== "boolean")
103
103
  return;
104
104
  const element = source.call(this);
105
- if (value) {
106
- element.setAttribute("disabled", "");
105
+ if (value === ref) {
106
+ element.setAttribute(flag, "");
107
107
  }
108
108
  else {
109
- element.removeAttribute("disabled");
109
+ element.removeAttribute(flag);
110
110
  }
111
111
  });
112
112
  }
@@ -10,7 +10,7 @@ export declare class ComponentFactory {
10
10
  /** Apply vanilla HTML attributes and event callback functions. There is no component level logic here. */
11
11
  static setAttributesAndEvents(element: HTMLElement, props: any): void;
12
12
  /** Logic for appending children to a parent element according to the possible returns of render() */
13
- static appendChildren(element: HTMLElement, children: HTMLElement[]): void;
13
+ static appendChildren(element: HTMLElement, ...children: (string | Node)[]): void;
14
14
  }
15
15
  /**
16
16
  * ## Here be Shenanigans
@@ -23,7 +23,7 @@ export class ComponentFactory {
23
23
  }
24
24
  // Append children if any
25
25
  if (children && children.length > 0) {
26
- this.appendChildren(element, children);
26
+ this.appendChildren(element, ...children);
27
27
  }
28
28
  return element;
29
29
  }
@@ -43,7 +43,7 @@ export class ComponentFactory {
43
43
  }
44
44
  }
45
45
  /** Logic for appending children to a parent element according to the possible returns of render() */
46
- static appendChildren(element, children) {
46
+ static appendChildren(element, ...children) {
47
47
  for (const child of children) {
48
48
  // Allow returning null from render() when there is nothing to do
49
49
  if (child === null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lordfokas/yrframe",
3
- "version": "0.1.3",
3
+ "version": "0.3.0",
4
4
  "description": "A Typescript class-based WebComponents library using Material Design.",
5
5
  "main": "dist/yrframe.js",
6
6
  "scripts": {
@@ -20,6 +20,6 @@
20
20
  "typescript": "^5.3.3"
21
21
  },
22
22
  "dependencies": {
23
- "@lordfokas/event-bus": "^1.0.4"
23
+ "@lordfokas/event-bus": "^1.0.22"
24
24
  }
25
25
  }