rebase
This commit is contained in:
commit
e2f472d802
83 changed files with 6338 additions and 0 deletions
76
src/components/UI.ts
Normal file
76
src/components/UI.ts
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import { FormatProperties } from "../compilers/FormatProperties.js"
|
||||
import { Memory } from "../compilers/Memory.js"
|
||||
import { Type } from "../types/enums/Type.js"
|
||||
import { Properties } from "../types/properties/components.js"
|
||||
import { Class } from "./Class.js"
|
||||
import { RandomString } from "./Utils.js"
|
||||
|
||||
import util from "node:util"
|
||||
|
||||
export class UI<T extends Type> extends Class {
|
||||
private path: string
|
||||
|
||||
name: string
|
||||
namespace: string
|
||||
extend?: UI<Type>
|
||||
|
||||
controls = new Map<string, [UI<Type>, Properties<Type>]>()
|
||||
properties: Properties<T> = <any>{}
|
||||
|
||||
constructor(public type?: T, name?: string, namespace?: string, path?: string) {
|
||||
super()
|
||||
|
||||
this.name = name?.match(/^\w+/)?.[0] || RandomString(16)
|
||||
this.namespace = namespace || RandomString(16)
|
||||
|
||||
if (!path) this.path = `@/${this.namespace}`
|
||||
else this.path = path
|
||||
|
||||
Memory.register_ui(this.path, this)
|
||||
}
|
||||
|
||||
setProperties(properties: Properties<T>) {
|
||||
this.properties = properties
|
||||
return this
|
||||
}
|
||||
|
||||
addChild<T extends Type>(child: UI<T>, properties?: Properties<T>, name?: string) {
|
||||
if (this === <any>child) {
|
||||
throw new Error("Cannot add a child to itself")
|
||||
}
|
||||
|
||||
this.controls.set(name || child.name, [child, properties || {}])
|
||||
return this
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `@${this.namespace}.${this.name}`
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
const obj: any = {
|
||||
type: this.type,
|
||||
...FormatProperties(this.properties),
|
||||
}
|
||||
|
||||
if (this.controls.size) {
|
||||
obj.controls = []
|
||||
this.controls.forEach((e, key) => obj.controls.push({ [key + e[0]]: e[1] }))
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
[util.inspect.custom]($: any, opts: any) {
|
||||
const obj: any = this.properties
|
||||
|
||||
if (this.controls.size) {
|
||||
obj.controls = []
|
||||
this.controls.forEach((e, key) => obj.controls.push({ [key + e[0]]: e[1] }))
|
||||
}
|
||||
|
||||
return `\x1b[33mUI\x1b[0m<\x1b[92m${
|
||||
this.type || `${this.extend}`
|
||||
}\x1b[0m> \x1b[92m"${this}\x1b[92m"\x1b[0m ${util.inspect(obj, opts)}\n`
|
||||
}
|
||||
}
|
||||
Reference in a new issue