-
Notifications
You must be signed in to change notification settings - Fork 31
/
field-locale.ts
110 lines (95 loc) · 3.12 KB
/
field-locale.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
import { Channel } from './channel'
import { MemoizedSignal } from './signal'
import { FieldInfo, FieldType, FieldLinkType, Items, SerializedJSONValue } from './types'
import { ExhaustiveFieldAPI } from './types/field-locale.types'
import { ValidationError } from './types/validation-error'
export default class FieldLocale implements ExhaustiveFieldAPI {
id: string
name: string
locale: string
type: FieldType
required: boolean
validations: any[]
items?: Items
linkType?: FieldLinkType
private _value: any
private _valueSignal: MemoizedSignal<[any]>
private _isDisabledSignal: MemoizedSignal<[boolean]>
private _schemaErrorsChangedSignal: MemoizedSignal<[ValidationError[]]>
private _channel: Channel
constructor(channel: Channel, info: FieldInfo) {
this.id = info.id
this.name = info.name
this.locale = info.locale
this.type = info.type
this.required = info.required
this.validations = info.validations
if (info.type === 'Array') {
this.items = info.items
}
if (info.type === 'Link') {
this.linkType = info.linkType
}
this._value = info.value
this._valueSignal = new MemoizedSignal(this._value)
this._isDisabledSignal = new MemoizedSignal<[boolean]>(info.isDisabled)
this._schemaErrorsChangedSignal = new MemoizedSignal<[ValidationError[]]>(info.schemaErrors)
this._channel = channel
channel.addHandler('valueChanged', (id: string, locale: string, value: any) => {
if (id === this.id && (!locale || locale === this.locale)) {
this._value = value
this._valueSignal.dispatch(value)
}
})
channel.addHandler(
'isDisabledChangedForFieldLocale',
(id: string, locale: string, isDisabled: boolean) => {
if (id === this.id && locale === this.locale) {
this._isDisabledSignal.dispatch(isDisabled)
}
},
)
channel.addHandler(
'schemaErrorsChangedForFieldLocale',
(id: string, locale: string, errors: ValidationError[]) => {
if (id === this.id && locale === this.locale) {
this._schemaErrorsChangedSignal.dispatch(errors)
}
},
)
}
getValue() {
return this._value
}
async setValue(value: any) {
this._value = value
return await this._channel.call<SerializedJSONValue | undefined>(
'setValue',
this.id,
this.locale,
value,
)
}
async removeValue() {
this._value = undefined
await this._channel.call('removeValue', this.id, this.locale)
}
setInvalid(isInvalid: boolean) {
return this._channel.call('setInvalid', isInvalid, this.locale)
}
onValueChanged(handler: (value: any) => any) {
return this._valueSignal.attach(handler)
}
getIsDisabled(): boolean {
return this._isDisabledSignal.getMemoizedArgs()[0]
}
onIsDisabledChanged(handler: (isDisabled: boolean) => any) {
return this._isDisabledSignal.attach(handler)
}
getSchemaErrors(): ValidationError[] {
return this._schemaErrorsChangedSignal.getMemoizedArgs()[0]
}
onSchemaErrorsChanged(handler: (errors: ValidationError[]) => void) {
return this._schemaErrorsChangedSignal.attach(handler)
}
}