You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. export class LabelItemList {
  2. constructor(public labelItems: LabelItem[]) {}
  3. static valueOf(items: LabelItem[]): LabelItemList {
  4. return new LabelItemList(items)
  5. }
  6. add(item: LabelItem) {
  7. this.labelItems.push(item)
  8. }
  9. update(item: LabelItem) {
  10. const index = this.labelItems.findIndex(label => label.id === item.id)
  11. this.labelItems.splice(index, 1, item)
  12. }
  13. delete(item: LabelItem) {
  14. this.labelItems = this.labelItems.filter(label => label.id !== item.id)
  15. }
  16. bulkDelete(items: LabelItemList) {
  17. const ids = items.ids()
  18. this.labelItems = this.labelItems.filter(label => !ids.includes(label.id))
  19. }
  20. count(): Number {
  21. return this.labelItems.length
  22. }
  23. ids(): Number[]{
  24. return this.labelItems.map(item => item.id)
  25. }
  26. get nameList(): string[] {
  27. return this.labelItems.map(item => item.name)
  28. }
  29. get usedKeys(): string[] {
  30. const items = this.labelItems
  31. .filter(item => item.suffixKey !== null)
  32. .map(item => item.suffixKey) as string[]
  33. return items
  34. }
  35. toArray(): Object[] {
  36. return this.labelItems.map(item => item.toObject())
  37. }
  38. }
  39. export class LabelItem {
  40. constructor(
  41. public id: number,
  42. public text: string,
  43. public prefixKey: string | null,
  44. public suffixKey: string | null,
  45. public backgroundColor: string,
  46. public textColor: string = '#ffffff'
  47. ) {}
  48. static valueOf(
  49. { id, text, prefix_key, suffix_key, background_color, text_color }:
  50. { id: number, text: string, prefix_key: string, suffix_key: string, background_color: string, text_color: string }
  51. ): LabelItem {
  52. return new LabelItem(id, text, prefix_key, suffix_key, background_color, text_color)
  53. }
  54. get name(): string {
  55. return this.text
  56. }
  57. toObject(): Object {
  58. return {
  59. id: this.id,
  60. text: this.text,
  61. prefix_key: this.prefixKey,
  62. suffix_key: this.suffixKey,
  63. background_color: this.backgroundColor,
  64. text_color: this.textColor
  65. }
  66. }
  67. }