From 53487095a39fdb9439754799a51abb37b51a5278 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 26 Feb 2021 21:35:07 +0900 Subject: [PATCH] Update label model --- frontend/models/label.ts | 47 ++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/frontend/models/label.ts b/frontend/models/label.ts index 112d41bb..3e9fb79b 100644 --- a/frontend/models/label.ts +++ b/frontend/models/label.ts @@ -5,10 +5,43 @@ export class LabelItemList { return new LabelItemList(items) } + add(item: LabelItem) { + this.labelItems.push(item) + } + + update(item: LabelItem) { + const index = this.labelItems.findIndex(label => label.id === item.id) + this.labelItems.splice(index, 1, item) + } + + delete(item: LabelItem) { + this.labelItems = this.labelItems.filter(label => label.id !== item.id) + } + + bulkDelete(items: LabelItemList) { + const ids = items.ids() + this.labelItems = this.labelItems.filter(label => !ids.includes(label.id)) + } + + count(): Number { + return this.labelItems.length + } + + ids(): Number[]{ + return this.labelItems.map(item => item.id) + } + get nameList(): string[] { return this.labelItems.map(item => item.name) } + get usedKeys(): string[] { + const items = this.labelItems + .filter(item => item.suffixKey !== null) + .map(item => item.suffixKey) as string[] + return items + } + toArray(): Object[] { return this.labelItems.map(item => item.toObject()) } @@ -18,10 +51,10 @@ export class LabelItem { constructor( public id: number, public text: string, - public prefixKey: string, - public suffixKey: string, + public prefixKey: string | null, + public suffixKey: string | null, public backgroundColor: string, - public textColor: string + public textColor: string = '#ffffff' ) {} static valueOf( @@ -39,10 +72,10 @@ export class LabelItem { return { id: this.id, text: this.text, - prefixKey: this.prefixKey, - suffixKey: this.suffixKey, - backgroundColor: this.backgroundColor, - textColor: this.textColor + prefix_key: this.prefixKey, + suffix_key: this.suffixKey, + background_color: this.backgroundColor, + text_color: this.textColor } } }