-
Notifications
You must be signed in to change notification settings - Fork 9
/
LeftRightChooserModel.ts
293 lines (249 loc) · 9.1 KB
/
LeftRightChooserModel.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* This file belongs to Hoist, an application development toolkit
* developed by Extremely Heavy Industries (www.xh.io | [email protected])
*
* Copyright © 2025 Extremely Heavy Industries Inc.
*/
import {GridModel} from '@xh/hoist/cmp/grid';
import {div} from '@xh/hoist/cmp/layout';
import {HoistModel, HSide, managed, XH} from '@xh/hoist/core';
import '@xh/hoist/desktop/register';
import {Icon} from '@xh/hoist/icon';
import {bindable, computed} from '@xh/hoist/mobx';
import {FilterTestFn, StoreConfig, StoreRecord} from '@xh/hoist/data';
export interface LeftRightChooserConfig {
data?: LeftRightChooserItem[];
/** True to globally prevent the user from moving items between sides. */
readonly?: boolean;
/** Callback for when items change sides. */
onChange?: () => void;
/** Placeholder group value when an item has no group. */
ungroupedName?: string;
/** True to display the count of items on each side in the header. */
showCounts?: boolean;
leftTitle?: string;
leftSorted?: boolean;
leftGroupingEnabled?: boolean;
leftGroupingExpanded?: boolean;
leftEmptyText?: string;
rightTitle?: string;
rightSorted?: boolean;
rightGroupingEnabled?: boolean;
rightGroupingExpanded?: boolean;
rightEmptyText?: string;
xhImpl?: boolean;
}
/** Data record object for a LeftRightChooser value item. */
export interface LeftRightChooserItem {
/** Primary label for the item. */
text: string;
/** Value that the item represents. */
value: any;
/** User-friendly, longer description of the item. */
description?: string;
/** Grid group in which to show the item. */
group?: string;
/** Initial side of the item - default 'left'. */
side?: HSide;
/** True to prevent the user from moving the item between sides. */
locked?: boolean;
/* True to exclude the item from the chooser entirely. */
exclude?: boolean;
}
/**
* A Model for managing the state of a LeftRightChooser.
*/
export class LeftRightChooserModel extends HoistModel {
@managed leftModel: GridModel;
@managed rightModel: GridModel;
@bindable readonly = false;
onChange: () => void;
hasDescription: boolean;
leftGroupingEnabled: boolean;
rightGroupingEnabled: boolean;
leftGroupingExpanded: boolean;
rightGroupingExpanded: boolean;
private _hasGrouping: boolean;
private _ungroupedName: string;
private _data: LeftRightChooserItem[];
private _lastSelectedSide: HSide;
/**
* Filter for data rows to determine if they should be shown.
* Useful for helping users find values of interest in a large pool of rows.
*
* Note that this will *not* affect the actual 'value' property, which will continue
* to include unfiltered records.
*
* @see LeftRightChooserFilter - a component to easily control this field.
* @param fn - predicate function for filtering.
*/
setDisplayFilter(fn: FilterTestFn) {
const filter = fn ? {key: this.xhId, testFn: fn} : null;
this.leftModel.store.setFilter(filter);
this.rightModel.store.setFilter(filter);
}
/** Currently 'selected' values on the right hand side. */
@computed
get rightValues(): any[] {
return this.rightModel.store.allRecords.map(it => it.data.value);
}
/** Currently 'selected' values on the left hand side. */
@computed
get leftValues(): any[] {
return this.leftModel.store.allRecords.map(it => it.data.value);
}
constructor({
data = [],
onChange,
ungroupedName = 'Ungrouped',
leftTitle = 'Available',
leftSorted = false,
leftGroupingEnabled = true,
leftGroupingExpanded = true,
leftEmptyText = null,
readonly = false,
rightTitle = 'Selected',
rightSorted = false,
rightGroupingEnabled = true,
rightGroupingExpanded = true,
rightEmptyText = null,
showCounts = true,
xhImpl = false
}: LeftRightChooserConfig) {
super();
this.xhImpl = xhImpl;
this.onChange = onChange;
this._ungroupedName = ungroupedName;
this.leftGroupingEnabled = leftGroupingEnabled;
this.rightGroupingEnabled = rightGroupingEnabled;
this.leftGroupingExpanded = leftGroupingExpanded;
this.rightGroupingExpanded = rightGroupingExpanded;
this.readonly = readonly;
const store: StoreConfig = {
fields: [
{name: 'text', type: 'string'},
{name: 'value', type: 'string'},
{name: 'description', type: 'string'},
{name: 'group', type: 'string'},
{name: 'side', type: 'string'},
{name: 'locked', type: 'bool'},
{name: 'exclude', type: 'bool'}
]
};
const leftTextCol = {
field: 'text',
flex: true,
headerName: () =>
leftTitle + (showCounts ? ` (${this.leftModel.store.count})` : ''),
renderer: this.getTextColRenderer('left')
},
rightTextCol = {
field: 'text',
flex: true,
headerName: () =>
rightTitle + (showCounts ? ` (${this.rightModel.store.count})` : ''),
renderer: this.getTextColRenderer('right')
},
groupCol = {
field: 'group',
headerName: 'Group',
hidden: true
};
this.leftModel = new GridModel({
store,
selModel: 'multiple',
sortBy: leftSorted ? 'text' : null,
emptyText: leftEmptyText,
onRowDoubleClicked: e => this.onRowDoubleClicked(e),
columns: [leftTextCol, groupCol],
contextMenu: false,
xhImpl: true
});
this.rightModel = new GridModel({
store,
selModel: 'multiple',
sortBy: rightSorted ? 'text' : null,
emptyText: rightEmptyText,
onRowDoubleClicked: e => this.onRowDoubleClicked(e),
columns: [rightTextCol, groupCol],
contextMenu: false,
xhImpl: true
});
this.setData(data);
this.addReaction(this.syncSelectionReaction());
}
setData(data: LeftRightChooserItem[]) {
const hasGrouping = data.some(it => it.group),
lhGroupBy = this.leftGroupingEnabled && hasGrouping ? 'group' : null,
rhGroupBy = this.rightGroupingEnabled && hasGrouping ? 'group' : null;
this.hasDescription = data.some(it => it.description);
this.leftModel.setGroupBy(lhGroupBy);
this.rightModel.setGroupBy(rhGroupBy);
this._data = this.preprocessData(data);
this._hasGrouping = hasGrouping;
this.refreshStores();
}
moveRows(rows: StoreRecord[]) {
rows.forEach(rec => {
const {locked, side} = rec.data;
if (locked || this.readonly) return;
rec.raw.side = side === 'left' ? 'right' : 'left';
});
this.refreshStores();
this.onChange?.();
}
//------------------------
// Implementation
//------------------------
private getTextColRenderer(side: HSide) {
const groupingEnabled =
side === 'left' ? this.leftGroupingEnabled : this.rightGroupingEnabled,
lockSvg = Icon.lock({prefix: 'fal'});
return (v, {record}) => {
const groupClass =
groupingEnabled && this._hasGrouping ? 'xh-lr-chooser__group-row' : '';
return div({
className: `xh-lr-chooser__item-row ${groupClass}`,
items: [v, record.data.locked ? lockSvg : null]
});
};
}
private preprocessData(data) {
return data
.filter(r => !r.exclude)
.map(r => {
return {
id: XH.genId(),
group: this._ungroupedName,
side: 'left',
...r
};
});
}
private onRowDoubleClicked(e) {
if (e.data) this.moveRows([e.data]);
}
private syncSelectionReaction() {
const leftSel = this.leftModel.selModel,
rightSel = this.rightModel.selModel;
return {
track: () => [leftSel.selectedRecord, rightSel.selectedRecord],
run: () => {
const lastSelectedSide = this._lastSelectedSide;
if (leftSel.selectedRecord && lastSelectedSide !== 'left') {
this._lastSelectedSide = 'left';
rightSel.clear();
} else if (rightSel.selectedRecord && lastSelectedSide !== 'right') {
this._lastSelectedSide = 'right';
leftSel.clear();
}
}
};
}
private refreshStores() {
const data = this._data,
{leftModel, rightModel} = this;
leftModel.store.loadData(data.filter(it => it.side === 'left'));
rightModel.store.loadData(data.filter(it => it.side === 'right'));
}
}