|
| 1 | +import { Component, OnInit } from "@angular/core"; |
| 2 | +import { EntityRegistry } from "../../entity/database-entity.decorator"; |
| 3 | +import { |
| 4 | + MatCell, |
| 5 | + MatCellDef, |
| 6 | + MatColumnDef, |
| 7 | + MatHeaderCell, |
| 8 | + MatHeaderCellDef, |
| 9 | + MatHeaderRow, |
| 10 | + MatHeaderRowDef, |
| 11 | + MatRow, |
| 12 | + MatRowDef, |
| 13 | + MatTable, |
| 14 | +} from "@angular/material/table"; |
| 15 | +import { MatButton } from "@angular/material/button"; |
| 16 | +import { FaIconComponent } from "@fortawesome/angular-fontawesome"; |
| 17 | +import { NgIf } from "@angular/common"; |
| 18 | +import { EntityConstructor } from "../../entity/model/entity"; |
| 19 | +import { RouterLink } from "@angular/router"; |
| 20 | +import { generateIdFromLabel } from "../../../utils/generate-id-from-label/generate-id-from-label"; |
| 21 | +import { EntityMapperService } from "../../entity/entity-mapper/entity-mapper.service"; |
| 22 | +import { EntityConfig } from "../../entity/entity-config"; |
| 23 | +import { EntityDetailsConfig } from "../../entity-details/EntityDetailsConfig"; |
| 24 | +import { EntityListConfig } from "../../entity-list/EntityListConfig"; |
| 25 | +import { Config } from "../../config/config"; |
| 26 | +import { EntityConfigService } from "../../entity/entity-config.service"; |
| 27 | +import { DynamicComponentConfig } from "../../config/dynamic-components/dynamic-component-config.interface"; |
| 28 | + |
| 29 | +/** |
| 30 | + * Manage the configuration of all entity types. |
| 31 | + * Currently, this only serves as a utility for internal use, speeding up setup processes. |
| 32 | + * |
| 33 | + * TODO: This component is only a raw prototype! Needs further concept and implementation. |
| 34 | + */ |
| 35 | +@Component({ |
| 36 | + selector: "app-admin-entity-types", |
| 37 | + standalone: true, |
| 38 | + imports: [ |
| 39 | + MatHeaderRow, |
| 40 | + MatHeaderRowDef, |
| 41 | + MatRow, |
| 42 | + MatRowDef, |
| 43 | + MatCell, |
| 44 | + MatHeaderCell, |
| 45 | + MatColumnDef, |
| 46 | + MatTable, |
| 47 | + MatButton, |
| 48 | + MatCellDef, |
| 49 | + MatHeaderCellDef, |
| 50 | + FaIconComponent, |
| 51 | + NgIf, |
| 52 | + RouterLink, |
| 53 | + ], |
| 54 | + templateUrl: "./admin-entity-types.component.html", |
| 55 | + styleUrl: "./admin-entity-types.component.scss", |
| 56 | +}) |
| 57 | +export class AdminEntityTypesComponent implements OnInit { |
| 58 | + entityTypes: EntityConstructor[] = []; |
| 59 | + columnsToDisplay: string[] = ["label", "icon"]; |
| 60 | + |
| 61 | + constructor( |
| 62 | + private entities: EntityRegistry, |
| 63 | + private entityMapper: EntityMapperService, |
| 64 | + ) {} |
| 65 | + |
| 66 | + ngOnInit() { |
| 67 | + this.loadEntityTypes(); |
| 68 | + } |
| 69 | + |
| 70 | + protected loadEntityTypes(onlyUserFacing = true) { |
| 71 | + this.entityTypes = this.entities |
| 72 | + .getEntityTypes(onlyUserFacing) |
| 73 | + .map((e) => e.value); |
| 74 | + } |
| 75 | + |
| 76 | + async create() { |
| 77 | + const name = prompt("Please enter entity type name:"); |
| 78 | + if (!name) { |
| 79 | + return; |
| 80 | + } |
| 81 | + const id = generateIdFromLabel(name); |
| 82 | + if (this.entityTypeExists(id)) { |
| 83 | + alert("Entity type already exists."); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // save default entity schema |
| 88 | + await this.saveDefaultEntityConfig( |
| 89 | + id, |
| 90 | + this.getDefaultEntityConfig(id, name), |
| 91 | + this.getDefaultDetailsViewConfig(id), |
| 92 | + this.getDefaultListViewConfig(id), |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private entityTypeExists(id: string) { |
| 97 | + return Array.from(this.entities.keys()).some( |
| 98 | + (key) => key.toLowerCase() === id.toLowerCase(), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + private async saveDefaultEntityConfig( |
| 103 | + id: string, |
| 104 | + entityConfig: EntityConfig, |
| 105 | + detailsViewConfig: DynamicComponentConfig, |
| 106 | + listViewConfig: DynamicComponentConfig, |
| 107 | + ) { |
| 108 | + const originalConfig = await this.entityMapper.load( |
| 109 | + Config, |
| 110 | + Config.CONFIG_KEY, |
| 111 | + ); |
| 112 | + const newConfig = originalConfig.copy(); |
| 113 | + |
| 114 | + newConfig.data[EntityConfigService.PREFIX_ENTITY_CONFIG + id] = |
| 115 | + entityConfig; |
| 116 | + newConfig.data[EntityConfigService.getDetailsViewId(entityConfig)] = |
| 117 | + detailsViewConfig; |
| 118 | + newConfig.data[EntityConfigService.getListViewId(entityConfig)] = |
| 119 | + listViewConfig; |
| 120 | + |
| 121 | + await this.entityMapper.save(newConfig); |
| 122 | + } |
| 123 | + |
| 124 | + private getDefaultEntityConfig( |
| 125 | + entityTypeId: string, |
| 126 | + name: string, |
| 127 | + ): EntityConfig { |
| 128 | + return { |
| 129 | + label: name, |
| 130 | + route: entityTypeId, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + private getDefaultDetailsViewConfig( |
| 135 | + entityType: string, |
| 136 | + ): DynamicComponentConfig<EntityDetailsConfig> { |
| 137 | + return { |
| 138 | + component: "EntityDetails", |
| 139 | + config: { |
| 140 | + entityType: entityType, |
| 141 | + panels: [ |
| 142 | + { |
| 143 | + title: "Basic Information", |
| 144 | + components: [ |
| 145 | + { |
| 146 | + title: "", |
| 147 | + component: "Form", |
| 148 | + config: { |
| 149 | + fieldGroups: [{ fields: [] }], |
| 150 | + }, |
| 151 | + }, |
| 152 | + ], |
| 153 | + }, |
| 154 | + ], |
| 155 | + }, |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + private getDefaultListViewConfig( |
| 160 | + entityType: string, |
| 161 | + ): DynamicComponentConfig<EntityListConfig> { |
| 162 | + return { |
| 163 | + component: "EntityList", |
| 164 | + config: { |
| 165 | + entityType: entityType, |
| 166 | + columnGroups: { |
| 167 | + default: "Overview", |
| 168 | + mobile: "Overview", |
| 169 | + groups: [ |
| 170 | + { |
| 171 | + name: "Overview", |
| 172 | + columns: [], |
| 173 | + }, |
| 174 | + ], |
| 175 | + }, |
| 176 | + }, |
| 177 | + }; |
| 178 | + } |
| 179 | +} |
0 commit comments