/
decorate-properties.ts
112 lines (103 loc) · 3.59 KB
/
decorate-properties.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { ResourceDecorator } from '..'
import AdminBro from '../../../../admin-bro'
import { BaseProperty, BaseResource } from '../../../adapters'
import { PropertyDecorator } from '../../property'
import { getPropertyByKey } from './get-property-by-key'
import { pathToParts } from '../../../../utils/flat/path-to-parts'
export type DecoratedProperties = {[key: string]: PropertyDecorator}
const decorateDatabaseProperties = (
resource: BaseResource,
admin: AdminBro,
decorator: ResourceDecorator,
): DecoratedProperties => {
const { options } = decorator
return resource.properties().reduce((memo, property) => {
const decoratedProperty = new PropertyDecorator({
property,
admin,
options: options.properties && options.properties[property.name()],
resource: decorator,
})
return { ...memo, [property.name()]: decoratedProperty }
}, {} as DecoratedProperties)
}
const decorateVirtualProperties = (
dbProperties: DecoratedProperties,
admin: AdminBro,
decorator: ResourceDecorator,
): DecoratedProperties => {
const { options } = decorator
if (options.properties) {
return Object.keys(options.properties).reduce((memo, key) => {
const existingProperty = getPropertyByKey(key, dbProperties)
if (!existingProperty) {
const property = new BaseProperty({ path: key, isSortable: false })
return {
...memo,
[key]: new PropertyDecorator({
property,
admin,
options: options.properties && options.properties[key],
resource: decorator,
isVirtual: true,
}),
}
}
return memo
}, {} as DecoratedProperties)
}
return {}
}
/**
* This function moves nested properties to existing mixed properties if there are any.
* So that they could be printed as Section in the UI, and handled together as an Array if there
* is a need for that.
*
* @param {DecoratedProperties} dbProperties
* @param {DecoratedProperties} virtualProperties
*/
const organizeNestedProperties = (
dbProperties: DecoratedProperties,
virtualProperties: DecoratedProperties,
): DecoratedProperties => {
const properties = { ...dbProperties, ...virtualProperties }
const rootPropertyKeys = Object.keys(properties).filter((key) => {
const property = properties[key]
// reverse because we start by by finding from the longest path
// and removes itself.
// changes 'root.nested.nested1' to [root.nested', 'root']
const parts = pathToParts(property.path).reverse().splice(1)
if (parts.length) {
const mixedPropertyPath = parts.find(part => (
properties[part] && properties[part].type() === 'mixed'
))
if (mixedPropertyPath) {
const mixedProperty = properties[mixedPropertyPath]
mixedProperty.addSubProperty(property)
// remove from the root properties
return false
}
}
return true
})
return rootPropertyKeys.reduce((memo, key) => ({
...memo,
[key]: properties[key],
}), {} as DecoratedProperties)
}
/**
* Initializes PropertyDecorator for all properties within a resource. When
* user passes new property in the options - it will be created as well.
*
* @returns {Object<string,PropertyDecorator>}
* @private
*/
export function decorateProperties(
resource: BaseResource,
admin: AdminBro,
decorator: ResourceDecorator,
): DecoratedProperties {
const dbProperties = decorateDatabaseProperties(resource, admin, decorator)
const virtualProperties = decorateVirtualProperties(dbProperties, admin, decorator)
return organizeNestedProperties(dbProperties, virtualProperties)
}