@lordfokas/yrframe 0.4.7 → 0.5.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.
package/README.md CHANGED
@@ -1,2 +1,2 @@
1
1
  # yrframe
2
- A Typescript class-based WebComponents library using Material Design
2
+ A Typescript class-based WebComponents framework
@@ -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 const evt: unique symbol;
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,17 +1,19 @@
1
- import { ComponentEvents } from "./ComponentEvents.js";
2
- import { ComponentFactory } from "./ComponentFactory.js";
3
- import { specialAttributes } from "./utils.js";
4
- const evt = Symbol('evt');
5
- export class Component extends HTMLElement {
6
- [evt];
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Component = void 0;
4
+ const ComponentEvents_js_1 = require("./ComponentEvents.js");
5
+ const ComponentFactory_js_1 = require("./ComponentFactory.js");
6
+ const utils_js_1 = require("./utils.js");
7
+ class Component extends HTMLElement {
8
+ [ComponentEvents_js_1.symbol];
7
9
  initialChildren = [];
8
10
  wasConnected = false;
9
11
  events() {
10
- return this[evt];
12
+ return this[ComponentEvents_js_1.symbol];
11
13
  }
12
14
  constructor(props, defaults) {
13
15
  super();
14
- this[evt] = new ComponentEvents(this);
16
+ this[ComponentEvents_js_1.symbol] = new ComponentEvents_js_1.ComponentEvents(this);
15
17
  const all = {};
16
18
  // Assign all defaults to object and overwrite with existing values in provided props
17
19
  if (defaults)
@@ -20,11 +22,11 @@ export class Component extends HTMLElement {
20
22
  Object.assign(all, props);
21
23
  const attrs = {};
22
24
  for (const [k, v] of Object.entries(all)) {
23
- if (!specialAttributes.test(k)) {
25
+ if (!utils_js_1.specialAttributes.test(k)) {
24
26
  attrs[k] = v;
25
27
  }
26
28
  }
27
- ComponentFactory.setAttributesAndEvents(this, attrs);
29
+ ComponentFactory_js_1.ComponentFactory.setAttributesAndEvents(this, attrs);
28
30
  }
29
31
  /** Override HTMLElement */
30
32
  append(...nodes) {
@@ -42,12 +44,12 @@ export class Component extends HTMLElement {
42
44
  }
43
45
  /** HTML5 Custom Element lifecycle callback (remove from page) */
44
46
  disconnectedCallback() {
45
- this[evt].disconnect();
47
+ this[ComponentEvents_js_1.symbol].disconnect();
46
48
  }
47
49
  /** HTML5 Custom Element lifecycle callback (add to page) */
48
50
  connectedCallback() {
49
51
  this.wasConnected = true;
50
- this[evt].connect();
52
+ this[ComponentEvents_js_1.symbol].connect();
51
53
  this.redraw();
52
54
  }
53
55
  /** Draw this component's internals. Should return only children nodes. */
@@ -60,10 +62,10 @@ export class Component extends HTMLElement {
60
62
  this.clearChildren();
61
63
  if (child) {
62
64
  if (Array.isArray(child)) {
63
- ComponentFactory.appendChildren(this, ...child);
65
+ ComponentFactory_js_1.ComponentFactory.appendChildren(this, ...child);
64
66
  }
65
67
  else {
66
- ComponentFactory.appendChildren(this, child);
68
+ ComponentFactory_js_1.ComponentFactory.appendChildren(this, child);
67
69
  }
68
70
  }
69
71
  this.renderedCallback();
@@ -83,3 +85,4 @@ export class Component extends HTMLElement {
83
85
  window.customElements.define(tag, this);
84
86
  }
85
87
  }
88
+ exports.Component = Component;
@@ -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: Component<any>, value: T | Value | Value[], evt: T) => void;
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.
@@ -1,6 +1,10 @@
1
- import { EventBus, EventListener } from '@lordfokas/event-bus';
2
- import { Component } from './Component.js';
3
- class PersistentListener extends EventListener {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ComponentEvents = exports.symbol = void 0;
4
+ const event_bus_1 = require("@lordfokas/event-bus");
5
+ const Component_js_1 = require("./Component.js");
6
+ exports.symbol = Symbol('evt');
7
+ class PersistentListener extends event_bus_1.EventListener {
4
8
  type;
5
9
  constructor(type, callback, nice) {
6
10
  super(callback, nice);
@@ -12,7 +16,7 @@ class PersistentListener extends EventListener {
12
16
  * A developer isn't meant to directly interface with this class,
13
17
  * except by calling Component.events()
14
18
  */
15
- export class ComponentEvents {
19
+ class ComponentEvents {
16
20
  listeners = [];
17
21
  sources = {};
18
22
  triggers = {};
@@ -22,18 +26,18 @@ export class ComponentEvents {
22
26
  internals = 0;
23
27
  constructor(component) {
24
28
  this.component = component;
25
- this.owner = component instanceof Component ? component.constructor.name : '<' + component.tagName.toLowerCase() + '>';
29
+ this.owner = component instanceof Component_js_1.Component ? component.constructor.name : '<' + component.tagName.toLowerCase() + '>';
26
30
  }
27
31
  isEmpty() {
28
32
  return (this.listeners.length + this.internals) === 0;
29
33
  }
30
34
  /** Called by the component when entering DOM */
31
35
  connect() {
32
- this.listeners.forEach(l => EventBus.GLOBAL.subscribe(l.type, l));
36
+ this.listeners.forEach(l => event_bus_1.EventBus.GLOBAL.subscribe(l.type, l));
33
37
  }
34
38
  /** Called by the component when leaving DOM */
35
39
  disconnect() {
36
- this.listeners.forEach(l => EventBus.GLOBAL.unsubscribe(l));
40
+ this.listeners.forEach(l => event_bus_1.EventBus.GLOBAL.unsubscribe(l));
37
41
  }
38
42
  /** Set up and attach a listener, given a configuration */
39
43
  createListener(target, name, handler) {
@@ -59,10 +63,10 @@ export class ComponentEvents {
59
63
  this.createListener(target, name, handler);
60
64
  }
61
65
  attachGeneric(target, handler, name, optional) {
62
- this.attachListener(target, handler, "generic listener", optional);
66
+ this.attachListener(target, handler, name, "generic listener", optional);
63
67
  }
64
68
  attachConsumer(target, handler, name, optional) {
65
- this.attachListener(target, handler, "consumer", optional);
69
+ this.attachListener(target, handler, name, "consumer", optional);
66
70
  }
67
71
  /** Attach a listener that enriches the target event with data from this component. */
68
72
  attachSupplier(target, source, name, optional) {
@@ -70,7 +74,6 @@ export class ComponentEvents {
70
74
  return;
71
75
  const [type, path, nice] = target;
72
76
  this.listeners.push(new PersistentListener(type, event => {
73
- const path = target[1];
74
77
  const data = source.call(this);
75
78
  if (path.length == 0)
76
79
  event.with(data);
@@ -89,7 +92,6 @@ export class ComponentEvents {
89
92
  return;
90
93
  const [type, path, nice] = target;
91
94
  this.listeners.push(new PersistentListener(type, event => {
92
- const path = target[1];
93
95
  if (!predicate.call(this))
94
96
  return;
95
97
  if (path.length == 0)
@@ -144,9 +146,8 @@ export class ComponentEvents {
144
146
  /** Fire a source function and process the returned data. */
145
147
  seek(name, callback) {
146
148
  if (!this.sources[name])
147
- callback.call(this.component, undefined, undefined);
148
- else
149
- this.sources[name].call(undefined, callback);
149
+ throw new Error(`Source '${name}' not found at '${this.owner}`);
150
+ this.sources[name].call(undefined, callback);
150
151
  }
151
152
  /** Create a function that will fire an event to export data. */
152
153
  attachTrigger(target, name, optional) {
@@ -172,3 +173,4 @@ export class ComponentEvents {
172
173
  this.triggers[name].call(this.component, data, parent);
173
174
  }
174
175
  }
176
+ exports.ComponentEvents = ComponentEvents;
@@ -1,9 +1,12 @@
1
- import { Facade } from "./Facade.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FakeReact = exports.ComponentFactory = void 0;
4
+ const Facade_js_1 = require("./Facade.js");
2
5
  /**
3
6
  * JSX / TSX component factory.
4
7
  * Should not be invoked manually, is used by TSC when building the project.
5
8
  */
6
- export class ComponentFactory {
9
+ class ComponentFactory {
7
10
  static make(tag, props, ...children) {
8
11
  if (tag === Array)
9
12
  return children; // Allow returning multiple elements from render()
@@ -11,7 +14,7 @@ export class ComponentFactory {
11
14
  let element;
12
15
  let facade = undefined;
13
16
  if (typeof tag === 'function') { // Construct class based components
14
- if (tag.prototype instanceof Facade) {
17
+ if (tag.prototype instanceof Facade_js_1.Facade) {
15
18
  facade = new tag(props ?? {});
16
19
  element = facade.content();
17
20
  }
@@ -57,6 +60,7 @@ export class ComponentFactory {
57
60
  })));
58
61
  }
59
62
  }
63
+ exports.ComponentFactory = ComponentFactory;
60
64
  /**
61
65
  * ## Here be Shenanigans
62
66
  * In the event your TSC refuses to find ComponentFactory.make
@@ -67,6 +71,7 @@ export class ComponentFactory {
67
71
  * At least until you can be arsed to figure it out...
68
72
  * `¯\_(ツ)_/¯`
69
73
  */
70
- export class FakeReact {
74
+ class FakeReact {
71
75
  static createElement = ComponentFactory.make;
72
76
  }
77
+ exports.FakeReact = FakeReact;
@@ -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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/Facade.d.ts CHANGED
@@ -1,23 +1,35 @@
1
- import { ComponentEvents } from "./ComponentEvents.js";
2
- import { Attributes, Source } from "./utils.js";
3
- declare const evt: unique symbol;
4
- interface EventHost {
5
- [evt]: ComponentEvents;
6
- }
7
- type FacadeElement = HTMLElement & EventHost & {
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,23 +1,28 @@
1
- import { ComponentEvents } from "./ComponentEvents.js";
2
- import { ComponentFactory } from "./ComponentFactory.js";
3
- import { specialAttributes } from "./utils.js";
4
- const evt = Symbol('evt');
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Facade = void 0;
4
+ const ComponentEvents_js_1 = require("./ComponentEvents.js");
5
+ const ComponentFactory_js_1 = require("./ComponentFactory.js");
6
+ const utils_js_1 = require("./utils.js");
7
+ const fcl = Symbol('fcl');
5
8
  /**
6
9
  * Fake component that constructs and returns another component instead.
7
10
  * Used for aliasing and adapting components from other libraries.
8
11
  */
9
- export class Facade {
12
+ class Facade {
10
13
  node;
11
14
  isCustom;
12
15
  initialChildren = [];
16
+ wasConnected = false;
13
17
  events() {
14
- if (!this.node[evt]) { // Lazily instantiate event manager
15
- this.node[evt] = new ComponentEvents(this.node);
18
+ if (!this.node[ComponentEvents_js_1.symbol]) { // Lazily instantiate event manager
19
+ this.node[ComponentEvents_js_1.symbol] = new ComponentEvents_js_1.ComponentEvents(this.node);
16
20
  }
17
- return this.node[evt];
21
+ return this.node[ComponentEvents_js_1.symbol];
18
22
  }
19
23
  constructor(tag, props, defaults) {
20
24
  const node = this.node = document.createElement(tag);
25
+ node[fcl] = this;
21
26
  this.isCustom = tag.includes('-');
22
27
  const all = {};
23
28
  // Assign all defaults to object and overwrite with existing values in provided props
@@ -27,16 +32,53 @@ export class Facade {
27
32
  Object.assign(all, props);
28
33
  const attrs = {};
29
34
  for (const [k, v] of Object.entries(all)) {
30
- if (!specialAttributes.test(k)) {
35
+ if (!utils_js_1.specialAttributes.test(k)) {
31
36
  attrs[k] = v;
32
37
  }
33
38
  }
34
- ComponentFactory.setAttributesAndEvents(node, attrs);
39
+ ComponentFactory_js_1.ComponentFactory.setAttributesAndEvents(node, attrs);
35
40
  }
41
+ /** Remove all children nodes. */
42
+ clearChildren() {
43
+ this.node.innerHTML = "";
44
+ }
45
+ isDisabled() {
46
+ return this.node.hasAttribute("disabled");
47
+ }
48
+ /** HTML5 Custom Element lifecycle callback (remove from page) */
49
+ disconnectedCallback() {
50
+ this.node[ComponentEvents_js_1.symbol]?.disconnect();
51
+ }
52
+ /** HTML5 Custom Element lifecycle callback (add to page) */
53
+ connectedCallback() {
54
+ this.wasConnected = true;
55
+ this.node[ComponentEvents_js_1.symbol]?.connect();
56
+ this.redraw();
57
+ }
58
+ /** Draw this component's internals. Should return only children nodes. */
59
+ render() {
60
+ return this.initialChildren;
61
+ }
62
+ /** Redraw this component. Works by deleting all children, calling render() and appending the results. */
63
+ redraw() {
64
+ const child = this.render();
65
+ this.clearChildren();
66
+ if (child) {
67
+ if (Array.isArray(child)) {
68
+ ComponentFactory_js_1.ComponentFactory.appendChildren(this.node, ...child);
69
+ }
70
+ else {
71
+ ComponentFactory_js_1.ComponentFactory.appendChildren(this.node, child);
72
+ }
73
+ }
74
+ this.renderedCallback();
75
+ }
76
+ /** Called after redraw() to do special manipulation of children nodes. */
77
+ renderedCallback() { }
36
78
  content() {
37
79
  // Dispose of unused event manager
38
- if (this.node[evt]?.isEmpty()) {
39
- delete this.node[evt];
80
+ if (this.node[ComponentEvents_js_1.symbol]?.isEmpty()) {
81
+ delete this.node[ComponentEvents_js_1.symbol];
40
82
  }
41
83
  return this.node;
42
84
  }
@@ -44,6 +86,7 @@ export class Facade {
44
86
  this.initialChildren = children;
45
87
  }
46
88
  }
89
+ exports.Facade = Facade;
47
90
  function traverseHTML(node, fn) {
48
91
  if (node instanceof HTMLElement) {
49
92
  fn(node);
@@ -56,11 +99,11 @@ function traverseHTML(node, fn) {
56
99
  new MutationObserver((records) => {
57
100
  for (const record of records) {
58
101
  for (const added of record.addedNodes) {
59
- traverseHTML(added, (node) => node[evt]?.connect());
102
+ traverseHTML(added, (node) => node[fcl]?.connectedCallback());
60
103
  }
61
104
  for (const removed of record.removedNodes) {
62
- traverseHTML(removed, (node) => node[evt]?.disconnect());
105
+ traverseHTML(removed, (node) => node[fcl]?.disconnectedCallback());
63
106
  }
64
107
  }
65
- }).observe(document.body, { subtree: true, childList: true });
108
+ }).observe(document.body, { subtree: true, attributes: false, childList: true });
66
109
  })();
package/dist/utils.js CHANGED
@@ -1,4 +1,8 @@
1
- export function has($) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.specialAttributes = exports.has = void 0;
4
+ function has($) {
2
5
  return $ !== undefined;
3
6
  }
4
- export const specialAttributes = /^[a-z]+\:/;
7
+ exports.has = has;
8
+ exports.specialAttributes = /^[a-z]+\:/;
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/dist/yrframe.js CHANGED
@@ -1,4 +1,12 @@
1
- export { Component } from './Component.js';
2
- export { ComponentEvents } from './ComponentEvents.js';
3
- export { ComponentFactory, FakeReact } from './ComponentFactory.js';
4
- export { Facade } from './Facade.js';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Facade = exports.FakeReact = exports.ComponentFactory = exports.ComponentEvents = exports.Component = void 0;
4
+ var Component_js_1 = require("./Component.js");
5
+ Object.defineProperty(exports, "Component", { enumerable: true, get: function () { return Component_js_1.Component; } });
6
+ var ComponentEvents_js_1 = require("./ComponentEvents.js");
7
+ Object.defineProperty(exports, "ComponentEvents", { enumerable: true, get: function () { return ComponentEvents_js_1.ComponentEvents; } });
8
+ var ComponentFactory_js_1 = require("./ComponentFactory.js");
9
+ Object.defineProperty(exports, "ComponentFactory", { enumerable: true, get: function () { return ComponentFactory_js_1.ComponentFactory; } });
10
+ Object.defineProperty(exports, "FakeReact", { enumerable: true, get: function () { return ComponentFactory_js_1.FakeReact; } });
11
+ var Facade_js_1 = require("./Facade.js");
12
+ Object.defineProperty(exports, "Facade", { enumerable: true, get: function () { return Facade_js_1.Facade; } });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lordfokas/yrframe",
3
- "version": "0.4.7",
4
- "description": "A Typescript class-based WebComponents library using Material Design.",
3
+ "version": "0.5.0",
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
- "material",
13
- "design",
12
+ "webcomponents",
14
13
  "components",
15
14
  "typescript"
16
15
  ],