terrier-engine 4.74.0 → 4.75.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -342,7 +342,7 @@ export class PollingSubscriber<TResult, TParams extends SubscriptionParams> exte
|
|
|
342
342
|
|
|
343
343
|
this.logger?.debug("polling in", intervalMs, "milliseconds")
|
|
344
344
|
|
|
345
|
-
this.timeoutHandle = setTimeout(this.makeRequest.bind(this), intervalMs)
|
|
345
|
+
this.timeoutHandle = window.setTimeout(this.makeRequest.bind(this), intervalMs)
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
348
|
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import Messages from "tuff-core/messages"
|
|
2
|
+
import { PartTag } from "tuff-core/parts"
|
|
3
|
+
import { SelectOption, SelectOptions } from "tuff-core/forms"
|
|
4
|
+
import TerrierPart from "./terrier-part"
|
|
5
|
+
|
|
6
|
+
export type MultiSelectFieldState = {
|
|
7
|
+
options: SelectOptions
|
|
8
|
+
selected_values: string[]
|
|
9
|
+
placeholder?: string
|
|
10
|
+
name?: string
|
|
11
|
+
disabled?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class MultiSelectFieldPart extends TerrierPart<MultiSelectFieldState> {
|
|
15
|
+
selectionChangedKey = Messages.typedKey<{ selected_values: string[] }>()
|
|
16
|
+
protected addOptionKey = Messages.untypedKey()
|
|
17
|
+
protected removeValueKey = Messages.typedKey<{ value: string }>()
|
|
18
|
+
|
|
19
|
+
async init() {
|
|
20
|
+
await super.init()
|
|
21
|
+
|
|
22
|
+
this.onChange(this.addOptionKey, m => {
|
|
23
|
+
if (m.value?.length) this.addValue(m.value)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
this.onClick(this.removeValueKey, m => {
|
|
27
|
+
this.removeValue(m.data.value)
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get parentClasses(): Array<string> {
|
|
32
|
+
const classes = ['tt-multi-select-field', ...super.parentClasses]
|
|
33
|
+
if (this.state.disabled) classes.push('disabled')
|
|
34
|
+
return classes
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
/// Selection
|
|
39
|
+
|
|
40
|
+
get selectedValues(): string[] {
|
|
41
|
+
return this.state.selected_values
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setSelectedValues(values: string[]) {
|
|
45
|
+
this.state.selected_values = [...values]
|
|
46
|
+
this.onSelectionChanged()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
addValue(value: string) {
|
|
50
|
+
if (this.state.selected_values.includes(value)) return
|
|
51
|
+
this.state.selected_values = [...this.state.selected_values, value]
|
|
52
|
+
this.onSelectionChanged()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
removeValue(value: string) {
|
|
56
|
+
if (!this.state.selected_values.includes(value)) return
|
|
57
|
+
this.state.selected_values = this.state.selected_values.filter(v => v != value)
|
|
58
|
+
this.onSelectionChanged()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected onSelectionChanged() {
|
|
62
|
+
this.dirty()
|
|
63
|
+
this.emitMessage(this.selectionChangedKey, { selected_values: this.state.selected_values })
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
/// Options
|
|
68
|
+
|
|
69
|
+
protected get flatOptions(): SelectOption[] {
|
|
70
|
+
return this.state.options.flatMap(opt => 'group' in opt ? opt.options : [opt])
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
titleForValue(value: string): string {
|
|
74
|
+
return this.flatOptions.find(opt => `${opt.value}` == value)?.title ?? value
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected isSelectable(opt: SelectOption): boolean {
|
|
78
|
+
return opt.value != null && `${opt.value}`.length > 0
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
protected isSelected(value: string | null): boolean {
|
|
82
|
+
return value != null && this.state.selected_values.includes(`${value}`)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
/// Rendering
|
|
87
|
+
|
|
88
|
+
render(parent: PartTag) {
|
|
89
|
+
parent.div('.tt-multi-select-field-container', container => {
|
|
90
|
+
this.renderAddSelect(container)
|
|
91
|
+
this.renderChips(container)
|
|
92
|
+
if (this.state.name) {
|
|
93
|
+
container.input('.tt-multi-select-values', {
|
|
94
|
+
type: 'hidden',
|
|
95
|
+
name: this.state.name,
|
|
96
|
+
value: this.state.selected_values.join(',')
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
protected renderAddSelect(parent: PartTag) {
|
|
102
|
+
const blankText = this.state.selected_values.length ? '' : (this.state.placeholder ?? 'Select…')
|
|
103
|
+
parent.select('.tt-multi-select-add', this.state.disabled ? { disabled: true } : {}, select => {
|
|
104
|
+
select.option({ value: '', text: blankText })
|
|
105
|
+
for (const opt of this.state.options) {
|
|
106
|
+
if ('group' in opt) {
|
|
107
|
+
const available = opt.options.filter(o => this.isSelectable(o) && !this.isSelected(o.value))
|
|
108
|
+
if (!available.length) continue
|
|
109
|
+
select.optgroup({ label: opt.group }, group => {
|
|
110
|
+
for (const o of available) this.renderAddOption(group, o)
|
|
111
|
+
})
|
|
112
|
+
} else if (this.isSelectable(opt) && !this.isSelected(opt.value)) {
|
|
113
|
+
this.renderAddOption(select, opt)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}).emitChange(this.addOptionKey)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected renderAddOption(parent: PartTag, opt: SelectOption) {
|
|
120
|
+
parent.option({ value: `${opt.value}`, text: opt.title })
|
|
121
|
+
}
|
|
122
|
+
protected renderChips(parent: PartTag) {
|
|
123
|
+
parent.div('.tt-multi-select-chips', container => {
|
|
124
|
+
for (const value of this.state.selected_values) {
|
|
125
|
+
container.div('.tt-multi-select-chip', chip => {
|
|
126
|
+
chip.div('.tt-multi-select-chip-label', { text: this.titleForValue(value) })
|
|
127
|
+
if (!this.state.disabled) {
|
|
128
|
+
chip.a('.tt-multi-select-chip-remove')
|
|
129
|
+
.emitClick(this.removeValueKey, { value })
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default MultiSelectFieldPart
|