refactor: front end code to unified design

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-28 12:21:09 -04:00
parent 08c1de9723
commit 80a8dac2fd
21 changed files with 1087 additions and 1151 deletions
+54 -63
View File
@@ -1,47 +1,52 @@
/**
* Class model for Collection Interface
* Class models for Collection interfaces
*/
import type { CollectionContentTypes, CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
import type {
CollectionContentTypes,
CollectionInterface,
CollectionModelInterface,
CollectionPropertiesInterface,
CollectionPropertiesModelInterface
} from '@/types/collection';
import type {
CollectionIdentifier,
ServiceIdentifier
} from '@/types/common';
import { clonePlain } from './clone-plain';
export class CollectionObject implements CollectionInterface {
export class CollectionObject implements CollectionModelInterface {
_data!: CollectionInterface;
private _data: CollectionInterface<CollectionPropertiesInterface>;
private _properties: CollectionPropertiesObject | undefined = undefined;
constructor() {
this._data = {
'@type': 'people:collection',
version: 1,
provider: '',
service: '',
collection: null,
identifier: '',
signature: null,
created: null,
modified: null,
properties: new CollectionPropertiesObject(),
service: '' as ServiceIdentifier,
collection: null as CollectionIdentifier | null,
identifier: '' as CollectionIdentifier,
properties: new CollectionPropertiesObject().toJson(),
};
}
fromJson(data: CollectionInterface): CollectionObject {
this._data = data;
if (data.properties) {
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
}
this._data = clonePlain(data);
this._properties = undefined;
return this;
}
toJson(): CollectionInterface {
const json = { ...this._data };
if (this._data.properties instanceof CollectionPropertiesObject) {
json.properties = this._data.properties.toJson();
}
return json;
const json = this._properties
? { ...this._data, properties: this._properties.toJson() }
: this._data;
return clonePlain(json);
}
clone(): CollectionObject {
const cloned = new CollectionObject();
cloned._data = { ...this._data };
cloned._data.properties = this.properties.clone();
return cloned;
return new CollectionObject().fromJson(this.toJson());
}
/** Immutable Properties */
@@ -50,16 +55,16 @@ export class CollectionObject implements CollectionInterface {
return this._data.provider;
}
get service(): string | number {
return this._data.service;
get service(): ServiceIdentifier {
return this._data.service as ServiceIdentifier;
}
get collection(): string | number | null {
return this._data.collection;
get collection(): CollectionIdentifier | null {
return this._data.collection as CollectionIdentifier | null;
}
get identifier(): string | number {
return this._data.identifier;
get identifier(): CollectionIdentifier {
return this._data.identifier as CollectionIdentifier;
}
get signature(): string | null | undefined {
@@ -74,37 +79,33 @@ export class CollectionObject implements CollectionInterface {
return this._data.modified;
}
/** Mutable Properties */
get properties(): CollectionPropertiesObject {
if (this._data.properties instanceof CollectionPropertiesObject) {
return this._data.properties;
if (this._properties) {
return this._properties;
}
else if (this._data.properties) {
const properties = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
this._properties = properties;
return properties;
}
if (this._data.properties) {
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
this._data.properties = hydrated;
return hydrated;
}
return new CollectionPropertiesObject();
}
set properties(value: CollectionPropertiesObject) {
if (value instanceof CollectionPropertiesObject) {
this._data.properties = value as any;
} else {
this._data.properties = value;
}
this._properties = value;
}
}
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
_data!: CollectionPropertiesInterface;
export class CollectionPropertiesObject implements CollectionPropertiesModelInterface {
private _data: CollectionPropertiesInterface;
constructor() {
this._data = {
'@type': 'chrono:collection',
version: 1,
'@type': 'chrono:calendar',
content: [],
label: '',
description: null,
@@ -115,32 +116,22 @@ export class CollectionPropertiesObject implements CollectionPropertiesInterface
}
fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject {
this._data = data;
this._data = clonePlain(data);
return this;
}
toJson(): CollectionPropertiesInterface {
return this._data;
return clonePlain(this._data);
}
clone(): CollectionPropertiesObject {
const cloned = new CollectionPropertiesObject();
cloned._data = { ...this._data };
return cloned;
return new CollectionPropertiesObject().fromJson(this.toJson());
}
/** Immutable Properties */
get '@type'(): string {
return this._data['@type'];
}
get version(): number {
return this._data.version;
}
get content(): CollectionContentTypes[] {
return this._data.content || [];
return this._data.content ?? [];
}
/** Mutable Properties */
@@ -185,4 +176,4 @@ export class CollectionPropertiesObject implements CollectionPropertiesInterface
this._data.color = value;
}
}
}