amateras 0.5.0 → 0.6.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.
Files changed (81) hide show
  1. package/README.md +23 -26
  2. package/ext/html/node/$Anchor.ts +2 -2
  3. package/ext/html/node/$Canvas.ts +2 -2
  4. package/ext/html/node/$Dialog.ts +2 -2
  5. package/ext/html/node/$Form.ts +2 -2
  6. package/ext/html/node/$Image.ts +2 -2
  7. package/ext/html/node/$Input.ts +2 -2
  8. package/ext/html/node/$Label.ts +2 -2
  9. package/ext/html/node/$Media.ts +2 -2
  10. package/ext/html/node/$OptGroup.ts +2 -2
  11. package/ext/html/node/$Option.ts +2 -2
  12. package/ext/html/node/$Select.ts +2 -2
  13. package/ext/html/node/$TextArea.ts +2 -2
  14. package/ext/i18n/README.md +20 -0
  15. package/ext/i18n/src/index.ts +106 -12
  16. package/ext/i18n/src/structure/I18n.ts +12 -8
  17. package/ext/i18n/src/structure/I18nTranslation.ts +35 -0
  18. package/ext/idb/src/structure/builder/$IDBBuilder.ts +8 -8
  19. package/ext/markdown/README.md +53 -0
  20. package/ext/markdown/package.json +7 -0
  21. package/ext/markdown/src/index.ts +3 -0
  22. package/ext/markdown/src/lib/type.ts +26 -0
  23. package/ext/markdown/src/lib/util.ts +21 -0
  24. package/ext/markdown/src/structure/Markdown.ts +54 -0
  25. package/ext/markdown/src/structure/MarkdownLexer.ts +111 -0
  26. package/ext/markdown/src/structure/MarkdownParser.ts +33 -0
  27. package/ext/markdown/src/syntax/alert.ts +46 -0
  28. package/ext/markdown/src/syntax/blockquote.ts +35 -0
  29. package/ext/markdown/src/syntax/bold.ts +11 -0
  30. package/ext/markdown/src/syntax/code.ts +11 -0
  31. package/ext/markdown/src/syntax/codeblock.ts +44 -0
  32. package/ext/markdown/src/syntax/heading.ts +14 -0
  33. package/ext/markdown/src/syntax/horizontalRule.ts +11 -0
  34. package/ext/markdown/src/syntax/image.ts +23 -0
  35. package/ext/markdown/src/syntax/italic.ts +11 -0
  36. package/ext/markdown/src/syntax/link.ts +46 -0
  37. package/ext/markdown/src/syntax/list.ts +121 -0
  38. package/ext/markdown/src/syntax/table.ts +67 -0
  39. package/ext/markdown/src/syntax/text.ts +19 -0
  40. package/ext/router/README.md +111 -17
  41. package/ext/router/package.json +10 -0
  42. package/ext/router/src/index.ts +69 -0
  43. package/ext/router/src/node/Page.ts +34 -0
  44. package/ext/router/src/node/Router.ts +191 -0
  45. package/ext/router/{node → src/node}/RouterAnchor.ts +13 -2
  46. package/ext/router/src/structure/PageBuilder.ts +24 -0
  47. package/ext/router/src/structure/Route.ts +105 -0
  48. package/ext/signal/README.md +93 -0
  49. package/ext/signal/package.json +9 -0
  50. package/ext/signal/src/index.ts +128 -0
  51. package/{src → ext/signal/src}/structure/Signal.ts +6 -10
  52. package/ext/ssr/index.ts +4 -4
  53. package/ext/ui/lib/VirtualScroll.ts +25 -0
  54. package/ext/ui/node/Accordian.ts +97 -0
  55. package/ext/ui/node/Form.ts +53 -0
  56. package/ext/ui/node/Grid.ts +0 -0
  57. package/ext/ui/node/Table.ts +43 -0
  58. package/ext/ui/node/Tabs.ts +114 -0
  59. package/ext/ui/node/Toast.ts +16 -0
  60. package/ext/ui/node/Waterfall.ts +72 -0
  61. package/ext/ui/package.json +11 -0
  62. package/package.json +6 -3
  63. package/src/core.ts +30 -60
  64. package/src/global.ts +9 -2
  65. package/src/index.ts +1 -2
  66. package/src/lib/assignProperties.ts +57 -0
  67. package/src/lib/native.ts +25 -8
  68. package/src/lib/uppercase.ts +3 -0
  69. package/src/node/$Element.ts +7 -41
  70. package/src/node/$EventTarget.ts +45 -0
  71. package/src/node/$Node.ts +60 -65
  72. package/src/node/$Virtual.ts +65 -0
  73. package/src/node.ts +7 -6
  74. package/ext/i18n/src/node/I18nText.ts +0 -35
  75. package/ext/markdown/index.ts +0 -121
  76. package/ext/router/index.ts +0 -73
  77. package/ext/router/node/Page.ts +0 -27
  78. package/ext/router/node/Route.ts +0 -54
  79. package/ext/router/node/Router.ts +0 -149
  80. package/src/lib/assign.ts +0 -38
  81. package/src/lib/assignHelper.ts +0 -18
@@ -0,0 +1,105 @@
1
+ import type { Page } from "#node/Page";
2
+ import { _instanceof, _null, isUndefined } from "../../../../src/lib/native";
3
+ import { PageBuilder, type PageBuilderFunction } from "./PageBuilder";
4
+
5
+ export class Route<Path extends RoutePath = RoutePath, Params extends RouteParams = []> {
6
+ readonly routes = new Map<RoutePath, Route>();
7
+ readonly path: Path;
8
+ readonly builder: PageBuilder<any> | undefined;
9
+ readonly paths = new Map<string, RouteAliasParams<RoutePath, Params> | null>()
10
+ readonly pages = new Map<string, Page>();
11
+ redirectURL: string | null = null
12
+ constructor(path: Path, builder?: RouteBuilder<Params>) {
13
+ this.path = path;
14
+ this.paths.set(path, _null);
15
+ this.builder = _instanceof(builder, PageBuilder<any>) || isUndefined(builder) ? builder : new PageBuilder(builder as any);
16
+ }
17
+
18
+
19
+ alias<K extends string, P extends RouteAliasParams<K, Params>>(path: K, ...params: RequireKeys<P> extends never ? [] : [Prettify<P>]): this
20
+ alias(path: string, params: RouteAliasParams<RoutePath, Params> | null = _null) {
21
+ this.paths.set(path, params);
22
+ return this;
23
+ }
24
+ }
25
+
26
+ export interface Route<Path extends RoutePath = RoutePath, Params extends RouteParams = []> {
27
+ route<
28
+ P extends RoutePath,
29
+ B extends PageBuilder
30
+ >(path: P, builder: B, handle?: (route: Route<`${Path}${P}`, B['params']>) => Route<`${Path}${P}`, B['params']>): this
31
+ route<
32
+ K extends RoutePath,
33
+ P extends [...Params, ...RouteParamsStrings<K>],
34
+ F extends PageBuilderFunction<P>
35
+ >(path: K, builder: F, handle?: (route: Route<`${Path}${K}`, P>) => Route<`${Path}${K}`, P>): this
36
+ route<
37
+ K extends RoutePath,
38
+ P extends [...Params, ...RouteParamsStrings<K>],
39
+ F extends AsyncPageBuilder<P>
40
+ >(path: K, builder: F, handle?: (route: Route<`${Path}${K}`, P>) => Route<`${Path}${K}`, P>): this
41
+ route<P extends RoutePath>(path: P, builder: RouteBuilder<RouteParamsResolver<`${Path}${P}`>>, handle?: <R extends Route<`${Path}${P}`>>(route: R) => R): this;
42
+ group<P extends RoutePath>(path: P, handle: <R extends Route<`${Path}${P}`>>(route: R) => R): this;
43
+ notFound(builder: RouteBuilder<RouteParamsResolver<`${Path}`>>): this;
44
+ }
45
+
46
+ export type RouteBuilder<Params extends RouteParams = []> = PageBuilder<Params | `${string}?`[]> | AsyncPageBuilder<Params | `${string}?`[]> | PageBuilderFunction<Params>;
47
+ export type RoutePath = string;
48
+ export type RouteParams = string[]
49
+ export type AsyncPageBuilder<Params extends RouteParams = any> = () => Promise<{default: PageBuilder<Params>}>
50
+
51
+ export type RouteParamsResolver<Path extends RoutePath> = RouteParamsOptional<RouteParamsStrings<Path>>
52
+
53
+ /** Convert route path to literals array */
54
+ export type RouteParamsStrings<Path extends string> =
55
+ Path extends `${infer Segment}/${infer Rest}`
56
+ ? Segment extends `${string}:${infer Param}`
57
+ ? [Param, ...RouteParamsStrings<Rest>]
58
+ : [...RouteParamsStrings<Rest>]
59
+ : Path extends `${string}:${infer Param}?${infer Query}`
60
+ ? [Param]
61
+ : Path extends `${string}:${infer Param}`
62
+ ? [Param]
63
+ : []
64
+ /** Convert route path to object structure */
65
+ export type RouteParamsConfig<Path extends string> =
66
+ Path extends `${infer Segment}/${infer Rest}`
67
+ ? Segment extends `${string}:${infer Param}`
68
+ ? { [key in Param]: string } & RouteParamsConfig<Rest>
69
+ : RouteParamsConfig<Rest>
70
+ : Path extends `${string}:${infer Param}?${infer Query}`
71
+ ? { [key in Param]: string }
72
+ : Path extends `${string}:${infer Param}`
73
+ ? { [key in Param]: string }
74
+ : {}
75
+ /** Convert literals array to optional literals arrays */
76
+ export type RouteParamsOptional<Params extends string[]> =
77
+ Params extends [infer A, ...infer Rest]
78
+ ? Rest extends string[]
79
+ ? A extends string
80
+ ? [A | `${A}?`, ...RouteParamsOptional<Rest>] | RouteParamsOptional<Rest>
81
+ : never
82
+ : never
83
+ : []
84
+
85
+ export type RouteAliasPath<Path extends string, RoutePath extends string> = RouteParamsStrings<RoutePath> extends RouteParamsStrings<Path> ? Route<RoutePath> : never
86
+
87
+ type RouteParamsConfigByArrayString<RouteParams extends string[]> =
88
+ RouteParams extends [`${infer A}`, ...infer Rest]
89
+ ? Rest extends string[]
90
+ ? A extends `${infer P}?`
91
+ ? { [key in P]?: string } & RouteParamsConfigByArrayString<Rest>
92
+ : { [key in A]: string } & RouteParamsConfigByArrayString<Rest>
93
+ : never
94
+ : {}
95
+
96
+
97
+ type RouteAliasParams<Path extends string, Params extends RouteParams> = Omit<RouteParamsConfigByArrayString<Params>, keyof RouteParamsConfig<Path>>
98
+
99
+ type a<T> = T[keyof T] extends (string | undefined) ? true : false
100
+
101
+ type b = RequireKeys<{t: string, a?: string, b?: string}> & {}
102
+
103
+ type RequireKeys<T> = {
104
+ [K in keyof T]: {} extends Pick<T, K> ? never : K
105
+ }[keyof T];
@@ -0,0 +1,93 @@
1
+ # amateras/signal
2
+
3
+ ## Usage
4
+
5
+ ```ts
6
+ import 'amateras';
7
+ import 'amateras/signal';
8
+
9
+ // define a signal with value 0
10
+ const count$ = $.signal(0);
11
+
12
+ // this variable will be auto recalculate when count$ changes
13
+ const doubleCount$ = $.compute(() => count$() * 2);
14
+
15
+ // the console message will fired when count$ changes
16
+ $.effect(() => console.log( count$() ))
17
+
18
+ $(document.body).content([
19
+ // Display Counts
20
+ $('p').content( $`Counts: ${count$}` ),
21
+
22
+ // Display Double Counts
23
+ $('p').content( $`Double Counts: ${doubleCount$}` ),
24
+
25
+ // Create a button that make counts plus 1 on click
26
+ $('button').content('Add Count').on('click', () => count$.set(value => value + 1))
27
+ ])
28
+ ```
29
+
30
+ ## Read and Write
31
+
32
+ ```ts
33
+ const number$ = $.signal(0);
34
+ const string$ = $.singal('');
35
+ const boolean$ = $.signal(false);
36
+ const object$ = $.signal({ number: 1 });
37
+
38
+ // write value
39
+ number$.set(42);
40
+ string$.set('New Content');
41
+ boolean$.set(true);
42
+ object$.set({ number: 42 });
43
+
44
+ // read value
45
+ number$(); // 42
46
+ string$(); // 'New Content'
47
+ boolean$(); // true
48
+ object$(); // { number: 42 }
49
+ ```
50
+
51
+ ## Use in attribute methods
52
+
53
+ ```ts
54
+ const src$ = $.signal('/image-1.png');
55
+
56
+ $(document.body).content([
57
+ // you can set signal variable in attribute
58
+ $('img').src( src$ ),
59
+
60
+ $('button').content('Change Image').on('click', () => src$.set('/image-2.png'))
61
+ ])
62
+ ```
63
+
64
+ ## Reactive object
65
+
66
+ ```ts
67
+ const user$ = $.signal({
68
+ name: 'Amateras',
69
+ age: 16,
70
+ avatar: {
71
+ url: '/amateras/avatar.png',
72
+ size: '350x350'
73
+ }
74
+ })
75
+
76
+ $(document.body).content([
77
+ // Display name and age
78
+ $('h1').content( $`${user$.name$} (${user$.age$})` ),
79
+ // Display avatar image
80
+ $('img').src( user$.avatar$.url$ ),
81
+ // Change the user$ when button is clicked
82
+ $('button')
83
+ .content('Change User')
84
+ .on('click', () => user$.set({
85
+ name: 'Tsukimi',
86
+ age: 10,
87
+ avatar: {
88
+ url: '/tsukimi/avatar.png',
89
+ size: '350x350'
90
+ }
91
+ }))
92
+ ])
93
+ ```
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@amateras/signal",
3
+ "peerDependencies": {
4
+ "amateras": "../../"
5
+ },
6
+ "imports": {
7
+ "#structure/*": "./src/structure/*.ts"
8
+ }
9
+ }
@@ -0,0 +1,128 @@
1
+ import { Signal } from "#structure/Signal";
2
+ import { _document } from "amateras/lib/env";
3
+ import { _instanceof, isObject, _JSON_stringify, _Object_assign, forEach, _null, _Object_defineProperty, _Object_entries, isNull, isFunction } from "amateras/lib/native";
4
+ import { $Node, $Text } from "amateras/node/$Node";
5
+
6
+ // handle $Node content process
7
+ $Node.processors.add((_, content) => {
8
+ const signal = (content as SignalFunction<any>)?.signal;
9
+ if (_instanceof(signal, Signal)) {
10
+ const resolver = (content as SignalFunction<any>)();
11
+ if (_instanceof(resolver, $Node)) {
12
+ // handler signal $Node result
13
+ let node = resolver;
14
+ const set = (value: any) => {
15
+ node.replace(value);
16
+ node = value;
17
+ }
18
+ signal.subscribe(set);
19
+ return [resolver];
20
+ } else {
21
+ // handler signal other type result
22
+ const $text = new $Text()
23
+ const set = (value: any) => $text.textContent(isObject(value) ? _JSON_stringify(value) : value);
24
+ signal.subscribe(set);
25
+ set(resolver);
26
+ return [$text];
27
+ }
28
+ }
29
+ })
30
+
31
+ // handle $Node native method setter
32
+ $Node.setters.add((value, set) => {
33
+ const signal = value?.signal
34
+ if (isFunction(value) && _instanceof(signal, Signal)) {
35
+ signal.subscribe(set);
36
+ return value();
37
+ }
38
+ })
39
+
40
+ declare module 'amateras/core' {
41
+ export namespace $ {
42
+ export function signal<T>(value: T): SignalFunction<T>;
43
+ export function compute<T>(process: () => T): ComputeFunction<T>;
44
+ export function effect(process: () => void): void;
45
+ export interface $NodeContentMap {
46
+ signalFn: SignalFunction<any>;
47
+ computeFn: ComputeFunction<any>;
48
+ }
49
+ export interface $NodeParameterMap<T> {
50
+ signalFn: SignalFunction<T>
51
+ }
52
+ }
53
+ }
54
+
55
+ type SignalObject<T> = T extends Array<any> ? {} : T extends object ? { [key in keyof T as `${string & key}$`]: SignalFunction<T[key]> } : {};
56
+ export type SignalFunction<T> = {
57
+ signal: Signal<T>,
58
+ set: (newValue: T | ((oldValue: T) => T)) => SignalFunction<T>,
59
+ value: () => T;
60
+ } & (() => T) & SignalObject<T>;
61
+ export type ComputeFunction<T> = ({(): T}) & { signal: Signal<T> };
62
+
63
+ const signalComputeListeners = new Set<(signal: Signal<any>) => void>();
64
+ const signalEffectListeners = new Set<(signal: Signal<any>) => void>();
65
+ const signalFnMap = new Map<any, SignalFunction<any> | ComputeFunction<any>>();
66
+
67
+ // experiment feature
68
+ const nestedComputeFn = (value: any, parentSignalFn: SignalFunction<any> | ComputeFunction<any>) => {
69
+ if (isObject(value) && !isNull(value)) {
70
+ forEach(_Object_entries(value), ([key, val]) => {
71
+ const cachedFn = signalFnMap.get(val);
72
+ const val$ = cachedFn ?? $.compute(() => parentSignalFn()[key]);
73
+ if (!cachedFn && isObject(val)) {
74
+ signalFnMap.set(val, val$);
75
+ nestedComputeFn(val, val$)
76
+ }
77
+ _Object_defineProperty(parentSignalFn, `${key}$`, {value: val$});
78
+ })
79
+ }
80
+ }
81
+
82
+ _Object_assign($, {
83
+ // signal function
84
+ signal<T>(value: T): SignalFunction<T> {
85
+ const signal = new Signal<T>(value);
86
+ const signalFn = function () {
87
+ forEach([...signalComputeListeners, ...signalEffectListeners], fn => fn(signal));
88
+ return signal.value();
89
+ } as SignalFunction<T>
90
+ nestedComputeFn(value, signalFn);
91
+ _Object_assign(signalFn, {
92
+ signal,
93
+ set: (newValue: T) => (signal.value(newValue), signalFn),
94
+ value: () => signal.value()
95
+ })
96
+ return signalFn
97
+ },
98
+
99
+ // compute function
100
+ compute<T>(process: () => T): ComputeFunction<T> {
101
+ let subscribed = false;
102
+ const signalFn: SignalFunction<any> = $.signal(_null);
103
+ const computeFn = () => {
104
+ if (!subscribed) return signalFn.set(subscribe()).value();
105
+ else return signalFn.set(process()).value();
106
+ }
107
+ const subscribe = () => {
108
+ const signalHandler = (signal: Signal<any>) =>
109
+ signal.subscribe(() => signalFn.set(process()))
110
+ signalComputeListeners.add(signalHandler);
111
+ const result = process();
112
+ signalComputeListeners.delete(signalHandler);
113
+ subscribed = true;
114
+ return result;
115
+ }
116
+ _Object_assign(computeFn, { signal: signalFn.signal });
117
+ return computeFn as ComputeFunction<T>
118
+ },
119
+
120
+ // effect
121
+ effect(process: () => void) {
122
+ const signalHandler = (signal: Signal<any>) =>
123
+ signal.subscribe(process);
124
+ signalEffectListeners.add(signalHandler);
125
+ process();
126
+ signalEffectListeners.delete(signalHandler);
127
+ }
128
+ })
@@ -1,9 +1,8 @@
1
- import { _instanceof, forEach, isFunction, isUndefined } from "#lib/native";
1
+ import { _instanceof, forEach, isFunction, isUndefined } from "amateras/lib/native";
2
2
 
3
3
  export class Signal<T> {
4
4
  #value: T;
5
- subscribers = new Set<(value: T) => void>();
6
- static listeners = new Set<(signal: Signal<any>) => void>();
5
+ #subscribers = new Set<(value: T) => void>();
7
6
  constructor(value: T) {
8
7
  this.#value = value;
9
8
  }
@@ -12,10 +11,7 @@ export class Signal<T> {
12
11
  value(newValue: T): this;
13
12
  value(callback: (oldValue: T) => T): this;
14
13
  value(resolver?: T | ((oldValue: T) => T)) {
15
- if (!arguments.length) {
16
- forEach(Signal.listeners, fn => fn(this));
17
- return this.#value;
18
- }
14
+ if (!arguments.length) return this.#value;
19
15
  if (isFunction(resolver)) this.value(resolver(this.#value));
20
16
  else if (!isUndefined(resolver)) {
21
17
  this.#value = resolver;
@@ -25,17 +21,17 @@ export class Signal<T> {
25
21
  }
26
22
 
27
23
  emit() {
28
- forEach(this.subscribers, subs => subs(this.#value))
24
+ forEach(this.#subscribers, subs => subs(this.#value))
29
25
  return this;
30
26
  }
31
27
 
32
28
  subscribe(callback: (value: T) => void) {
33
- this.subscribers.add(callback);
29
+ this.#subscribers.add(callback);
34
30
  return this;
35
31
  }
36
32
 
37
33
  unsubscribe(callback: (value: T) => void) {
38
- this.subscribers.delete(callback);
34
+ this.#subscribers.delete(callback);
39
35
  return this;
40
36
  }
41
37
 
package/ext/ssr/index.ts CHANGED
@@ -32,10 +32,10 @@ _Object_assign($, {
32
32
  node.replaceWith($node.node);
33
33
 
34
34
  function getData(node: Node, $node: $Node) {
35
- if (node.nodeName === 'SIGNAL' && _instanceof(node, Element) && _instanceof($node, $Text)) {
36
- const type = $(node).attr()['type'];
37
- return forEach($node.signals, signal => signal.value(type === 'number' ? Number(node.textContent) : type === 'boolean' ? node.textContent == 'true' ? true : false : node.textContent));
38
- }
35
+ // if (node.nodeName === 'SIGNAL' && _instanceof(node, Element) && _instanceof($node, $Text)) {
36
+ // const type = $(node).attr()['type'];
37
+ // return forEach($node.signals, signal => signal.value(type === 'number' ? Number(node.textContent) : type === 'boolean' ? node.textContent == 'true' ? true : false : node.textContent));
38
+ // }
39
39
  if (_instanceof(node, Text)) return $node.textContent(node.textContent);
40
40
  if (_instanceof(node, Element) && _instanceof($node, $Element)) $node.attr($(node).attr());
41
41
  const arr = _Array_from($node.childNodes);
@@ -0,0 +1,25 @@
1
+ import { _Array_from, _instanceof, forEach } from "amateras/lib/native";
2
+ import type { $Virtual } from "amateras/node/$Virtual";
3
+ import { $HTMLElement } from "amateras/node/$HTMLElement";
4
+ import { _document } from "amateras/lib/env";
5
+ import type { $Node } from "amateras/node/$Node";
6
+
7
+ export const VirtualScroll = ($parent: $Virtual, scroller: $Node = $(_document)) => {
8
+ scroller.on('scroll', () => render($parent), true);
9
+ $parent.on('layout', () => render($parent));
10
+ }
11
+
12
+ const render = ($parent: $Virtual) => {
13
+ const number = parseInt;
14
+ const parentRect = $parent.getBoundingClientRect();
15
+ const children = _Array_from($parent.nodes);
16
+ forEach(children, $child => {
17
+ if (!_instanceof($child, $HTMLElement)) return;
18
+ const { top, height } = $child.style();
19
+ const topPos = parentRect.top + number(top);
20
+ const bottomPos = topPos + number(height);
21
+ if (bottomPos < 0 || topPos > outerHeight + 200) $parent.hide($child);
22
+ else $parent.show($child);
23
+ })
24
+ $parent.render();
25
+ }
@@ -0,0 +1,97 @@
1
+ import { _Array_from, _instanceof, forEach, isNull } from "amateras/lib/native";
2
+ import { $HTMLElement } from "amateras/node/$HTMLElement";
3
+ import type { $Node, $NodeContentResolver } from "amateras/node/$Node";
4
+ import { chain } from "../../../src/lib/chain";
5
+
6
+ const [ACCORDIAN, ACCORDIAN_ITEM, ACCORDIAN_TRIGGER, ACCORDIAN_CONTENT, ACCORDIAN_CONTAINER] = ['accordian', 'accordian-item', 'accordian-trigger', 'accordian-content', 'accordian-container'] as const;
7
+ forEach([
8
+ `${ACCORDIAN},${ACCORDIAN_ITEM},${ACCORDIAN_TRIGGER}{display:block}`,
9
+ `${ACCORDIAN_CONTENT}{display:grid;grid-template-rows:0fr}`,
10
+ `${ACCORDIAN_CONTENT}[opened]{grid-template-rows:1fr}`,
11
+ `${ACCORDIAN_CONTAINER}{overflow:hidden}`,
12
+ ], $.style)
13
+
14
+ export class Accordian extends $HTMLElement {
15
+ #autoclose = false;
16
+ constructor() {
17
+ super(ACCORDIAN);
18
+ }
19
+
20
+ autoclose(): boolean;
21
+ autoclose(autoclose: boolean): this;
22
+ autoclose(autoclose?: boolean) {
23
+ return chain(this, arguments, () => this.#autoclose, autoclose, autoclose => this.#autoclose = autoclose);
24
+ }
25
+
26
+ get items() {
27
+ return _Array_from($(this.childNodes)).filter($child => _instanceof($child, AccordianItem))
28
+ }
29
+ }
30
+
31
+ export class AccordianItem extends $HTMLElement {
32
+ $content: null | AccordianContent = null;
33
+ $trigger: null | AccordianTrigger = null;
34
+ $root: null | Accordian = null;
35
+ constructor() {
36
+ super(ACCORDIAN_ITEM);
37
+ }
38
+
39
+ mounted($parent: $Node): this {
40
+ if (_instanceof($parent, Accordian)) this.$root = $parent;
41
+ forEach($(this.childNodes), $c => {
42
+ if (_instanceof($c, AccordianTrigger)) this.$trigger = $c;
43
+ if (_instanceof($c, AccordianContent)) this.$content = $c;
44
+ })
45
+ return this;
46
+ }
47
+ }
48
+
49
+ export class AccordianTrigger extends $HTMLElement {
50
+ $item: null | AccordianItem = null;
51
+ constructor() {
52
+ super(ACCORDIAN_TRIGGER);
53
+ this.on('click', _ => {
54
+ const $item = this.$item;
55
+ const $root = $item?.$root;
56
+ this.$item?.$content?.use($content => isNull($content.attr('opened')) ? $content.open() : $content.close());
57
+ $root?.autoclose() && $root.items.forEach($i => $i !== $item && $i.$content?.close())
58
+ })
59
+ }
60
+
61
+ mounted($parent: $Node): this {
62
+ if (_instanceof($parent, AccordianItem)) this.$item = $parent;
63
+ return this;
64
+ }
65
+ }
66
+
67
+ export class AccordianContent extends $HTMLElement {
68
+ $container = $(AccordianContainer);
69
+ constructor() {
70
+ super(ACCORDIAN_CONTENT);
71
+ super.insert(this.$container);
72
+ }
73
+
74
+ content(children: $NodeContentResolver<AccordianContainer>): this {
75
+ this.$container.content(children);
76
+ return this;
77
+ }
78
+
79
+ insert(resolver: $NodeContentResolver<AccordianContainer>, position?: number): this {
80
+ this.$container.insert(resolver, position);
81
+ return this;
82
+ }
83
+
84
+ open() {
85
+ return this.attr({opened: ''})
86
+ }
87
+
88
+ close() {
89
+ return this.attr({opened: null});
90
+ }
91
+ }
92
+
93
+ export class AccordianContainer extends $HTMLElement {
94
+ constructor() {
95
+ super(ACCORDIAN_CONTAINER);
96
+ }
97
+ }
@@ -0,0 +1,53 @@
1
+ import { $Form } from 'amateras/html/$Form';
2
+ import { $HTMLElement } from 'amateras/node/$HTMLElement';
3
+ import { $Label } from 'amateras/html/$Label';
4
+ import { $Input } from 'amateras/html/$Input';
5
+ import type { $Node } from '../../../src/node/$Node';
6
+ import { _instanceof, _undefined } from '../../../src/lib/native';
7
+
8
+ export class Form extends $Form {
9
+ constructor() {
10
+ super();
11
+ }
12
+
13
+ get data() {
14
+ return Object.fromEntries(new FormData(this.node).entries());
15
+ }
16
+ }
17
+
18
+ export class FormField extends $HTMLElement {
19
+ constructor() {
20
+ super('form-field');
21
+ }
22
+ }
23
+
24
+ export class FormItem extends $HTMLElement {
25
+ constructor(name?: string) {
26
+ super('form-item');
27
+ this.attr({ name });
28
+ }
29
+ }
30
+
31
+ export class Label extends $Label {
32
+ constructor() {
33
+ super();
34
+ }
35
+
36
+ mounted($parent: $Node): void {
37
+ if (_instanceof($parent, FormItem))
38
+ this.htmlFor($parent.attr('name') ?? _undefined);
39
+ }
40
+ }
41
+
42
+ export class Input extends $Input {
43
+ constructor() {
44
+ super();
45
+ }
46
+
47
+ mounted($parent: $Node): void {
48
+ if (_instanceof($parent, FormItem)) {
49
+ const name = $parent.attr('name') ?? _undefined;
50
+ this.id(name).name(name);
51
+ }
52
+ }
53
+ }
File without changes
@@ -0,0 +1,43 @@
1
+ import { $HTMLElement } from "../../../src/node/$HTMLElement";
2
+
3
+ export class Table extends $HTMLElement {
4
+ constructor() {
5
+ super('table');
6
+ }
7
+ }
8
+
9
+ export class TableHeader extends $HTMLElement {
10
+ constructor() {
11
+ super('thead')
12
+ }
13
+ }
14
+
15
+ export class TableBody extends $HTMLElement {
16
+ constructor() {
17
+ super('tbody')
18
+ }
19
+ }
20
+
21
+ export class TableRow extends $HTMLElement {
22
+ constructor() {
23
+ super('tr')
24
+ }
25
+ }
26
+
27
+ export class TableHead extends $HTMLElement {
28
+ constructor() {
29
+ super('th')
30
+ }
31
+ }
32
+
33
+ export class TableCell extends $HTMLElement {
34
+ constructor() {
35
+ super('td')
36
+ }
37
+ }
38
+
39
+ export class TableFooter extends $HTMLElement {
40
+ constructor() {
41
+ super('tfoot')
42
+ }
43
+ }