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.

79 lines
2.0 KiB

4 years ago
4 years ago
  1. export class ConfigItemList {
  2. constructor(public configItems: ConfigItem[]) {}
  3. static valueOf(items: ConfigItem[]): ConfigItemList {
  4. return new ConfigItemList(items)
  5. }
  6. toArray(): Object[] {
  7. return this.configItems.map(item => item.toObject())
  8. }
  9. }
  10. interface LabelMappingForUI {
  11. from: string,
  12. to: string
  13. }
  14. export interface ParametersForUI {
  15. name: string,
  16. value: string | object[],
  17. type?: string,
  18. items?: string[]
  19. }
  20. export interface Fields {
  21. modelName: string,
  22. modelAttrs: ParametersForUI[],
  23. template: string,
  24. labelMapping: LabelMappingForUI[]
  25. }
  26. export class ConfigItem {
  27. constructor(
  28. public id: number,
  29. public modelName: string,
  30. public modelAttrs: object,
  31. public template: string,
  32. public labelMapping: object
  33. ) {}
  34. static valueOf(
  35. { id, model_name, model_attrs, template, label_mapping }:
  36. { id: number, model_name: string, model_attrs: object, template: string, label_mapping: object }
  37. ): ConfigItem {
  38. return new ConfigItem(id, model_name, model_attrs, template, label_mapping)
  39. }
  40. static parseFromUI(
  41. { modelName, modelAttrs, template, labelMapping }: Fields): ConfigItem {
  42. const mapping = labelMapping.reduce((a, x) => ({...a, [x.from]: x.to}), {})
  43. const attributes: {[key: string]: any} = modelAttrs.reduce((a, x) => ({...a, [x.name]: x.value}), {})
  44. for (const [key, value] of Object.entries(attributes)) {
  45. if (Array.isArray(value)) {
  46. attributes[key] = value.reduce((a, x) => ({...a, [x.key]: x.value}), {})
  47. }
  48. }
  49. return new ConfigItem(99999, modelName, attributes, template, mapping)
  50. }
  51. toObject(): object {
  52. return {
  53. id: this.id,
  54. modelName: this.modelName,
  55. modelAttrs: this.modelAttrs,
  56. template: this.template,
  57. labelMapping: this.labelMapping
  58. }
  59. }
  60. toAPI(): object {
  61. return {
  62. id: this.id,
  63. model_name: this.modelName,
  64. model_attrs: this.modelAttrs,
  65. template: this.template,
  66. label_mapping: this.labelMapping
  67. }
  68. }
  69. }