ERP-node/lib/registry/WebTypeRegistry.ts

266 lines
6.0 KiB
TypeScript
Raw Permalink Normal View History

2025-09-09 14:29:04 +09:00
import {
WebTypeDefinition,
ButtonActionDefinition,
RegistryEvent,
RegistryEventListener,
} from "./types";
/**
*
* /
*/
export class WebTypeRegistry {
private static webTypes = new Map<string, WebTypeDefinition>();
private static buttonActions = new Map<string, ButtonActionDefinition>();
private static listeners: RegistryEventListener[] = [];
// ===== 웹타입 관리 =====
/**
*
*/
static register(definition: WebTypeDefinition): void {
this.webTypes.set(definition.webType, definition);
this.notifyListeners({
type: "webtype_registered",
data: definition,
});
}
/**
*
*/
static get(webType: string): WebTypeDefinition | undefined {
return this.webTypes.get(webType);
}
/**
* ( , )
*/
static getAll(): WebTypeDefinition[] {
return Array.from(this.webTypes.values())
.filter((type) => type.isActive !== false)
.sort((a, b) => (a.sortOrder || 0) - (b.sortOrder || 0));
}
/**
*
*/
static getByCategory(category: string): WebTypeDefinition[] {
return this.getAll().filter((type) => type.category === category);
}
/**
*
*/
static unregister(webType: string): boolean {
const definition = this.webTypes.get(webType);
if (definition) {
this.webTypes.delete(webType);
this.notifyListeners({
type: "webtype_unregistered",
data: definition,
});
return true;
}
return false;
}
/**
*
*/
static has(webType: string): boolean {
return this.webTypes.has(webType);
}
/**
*
*/
static getCategories(): string[] {
const categories = new Set<string>();
this.getAll().forEach((type) => categories.add(type.category));
return Array.from(categories).sort();
}
// ===== 버튼 액션 관리 =====
/**
*
*/
static registerAction(definition: ButtonActionDefinition): void {
this.buttonActions.set(definition.actionType, definition);
this.notifyListeners({
type: "action_registered",
data: definition,
});
}
/**
*
*/
static getAction(actionType: string): ButtonActionDefinition | undefined {
return this.buttonActions.get(actionType);
}
/**
* ( , )
*/
static getAllActions(): ButtonActionDefinition[] {
return Array.from(this.buttonActions.values())
.filter((action) => action.isActive !== false)
.sort((a, b) => (a.sortOrder || 0) - (b.sortOrder || 0));
}
/**
*
*/
static getActionsByCategory(category: string): ButtonActionDefinition[] {
return this.getAllActions().filter(
(action) => action.category === category
);
}
/**
*
*/
static unregisterAction(actionType: string): boolean {
const definition = this.buttonActions.get(actionType);
if (definition) {
this.buttonActions.delete(actionType);
this.notifyListeners({
type: "action_unregistered",
data: definition,
});
return true;
}
return false;
}
/**
*
*/
static hasAction(actionType: string): boolean {
return this.buttonActions.has(actionType);
}
/**
*
*/
static getActionCategories(): string[] {
const categories = new Set<string>();
this.getAllActions().forEach((action) => categories.add(action.category));
return Array.from(categories).sort();
}
// ===== 이벤트 관리 =====
/**
*
*/
static addEventListener(listener: RegistryEventListener): void {
this.listeners.push(listener);
}
/**
*
*/
static removeEventListener(listener: RegistryEventListener): void {
const index = this.listeners.indexOf(listener);
if (index > -1) {
this.listeners.splice(index, 1);
}
}
/**
*
*/
private static notifyListeners(event: RegistryEvent): void {
this.listeners.forEach((listener) => {
try {
listener(event);
} catch (error) {
console.error("Registry event listener error:", error);
}
});
}
// ===== 유틸리티 메서드 =====
/**
*
*/
static getStats() {
return {
webTypes: {
total: this.webTypes.size,
active: this.getAll().length,
categories: this.getCategories().length,
},
buttonActions: {
total: this.buttonActions.size,
active: this.getAllActions().length,
categories: this.getActionCategories().length,
},
};
}
/**
* ()
*/
static clear(): void {
this.webTypes.clear();
this.buttonActions.clear();
this.listeners.length = 0;
}
/**
*
*/
static validateWebType(definition: Partial<WebTypeDefinition>): string[] {
const errors: string[] = [];
if (!definition.webType) {
errors.push("webType is required");
}
if (!definition.name) {
errors.push("name is required");
}
if (!definition.category) {
errors.push("category is required");
}
if (!definition.component) {
errors.push("component is required");
}
return errors;
}
/**
*
*/
static validateButtonAction(
definition: Partial<ButtonActionDefinition>
): string[] {
const errors: string[] = [];
if (!definition.actionType) {
errors.push("actionType is required");
}
if (!definition.name) {
errors.push("name is required");
}
if (!definition.category) {
errors.push("category is required");
}
return errors;
}
}