-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmoz-omnitable-column-list-mixin.js
198 lines (183 loc) · 4.68 KB
/
cosmoz-omnitable-column-list-mixin.js
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
import { prop } from '@neovici/cosmoz-utils/object';
import { array } from '@neovici/cosmoz-utils/array';
import { invoke } from '@neovici/cosmoz-utils/function';
import { get } from '@polymer/polymer/lib/utils/path';
import { valuesFrom } from './lib/utils-data';
const unique = (values, valueProperty) => {
if (!Array.isArray(values)) {
return;
}
const used = [];
return values
.reduce((acc, cur) => {
if (Array.isArray(cur)) {
cur.forEach((subcur) => {
acc.push(subcur);
});
return acc;
}
acc.push(cur);
return acc;
}, [])
.filter((item, index, array) => {
if (array.indexOf(item) !== index) {
return false;
}
if (valueProperty) {
const value = get(item, valueProperty);
if (used.indexOf(value) !== -1) {
return false;
}
used.push(value);
}
return true;
});
},
toAutocompleteSource = (values, valueProperty, textProperty) => {
if (values == null) {
return [];
}
if (Array.isArray(values)) {
return unique(values, valueProperty);
}
if (typeof values === 'object') {
const valProp = valueProperty ?? 'id',
textProp = textProperty ?? 'label';
return Object.entries(values)
.map(([id, label]) => ({
[valProp]: id,
[textProp]: label,
}))
.sort((a, b) => {
if (a[textProp] < b[textProp]) {
return -1;
}
if (a[textProp] > b[textProp]) {
return 1;
}
return 0;
});
}
return [];
},
getTexts = (item, valuePath, textProperty) =>
array(valuePath && get(item, valuePath)).map(prop(textProperty)),
getString = ({ valuePath, textProperty }, item) => {
return getTexts(item, valuePath, textProperty)
.filter((i) => i != null)
.join(', ');
},
toXlsxValue = getString,
applyMultiFilter =
({ valueProperty, valuePath, emptyValue, emptyProperty }, filters) =>
(item) => {
const val = prop(valueProperty),
values = array(get(item, valuePath));
return filters.some(
(filter) =>
(values.length === 0 &&
prop(emptyProperty || valueProperty)(filter) === emptyValue) ||
values.some((value) => val(value) === val(filter)),
);
},
onChange = (setState) => (value) =>
setState((state) => ({ ...state, filter: value })),
onFocus = (setState) => (focused) =>
setState((state) => ({ ...state, headerFocused: focused })),
onText = (setState) => (text) =>
setState((state) => ({ ...state, query: text })),
computeValues = (
{ emptyValue, emptyLabel, emptyProperty, textProperty, valueProperty },
rawSource,
) => {
const source = toAutocompleteSource(rawSource, valueProperty, textProperty);
if (
!emptyLabel ||
emptyValue === undefined ||
!textProperty ||
!(emptyProperty || valueProperty) ||
!source
) {
return source;
}
return [
{
[textProperty]: emptyLabel,
[emptyProperty || valueProperty]: emptyValue,
},
...source,
];
},
computeSource = (column, data) =>
computeValues(column, valuesFrom(data, column.valuePath)),
listColumnMixin = (base) =>
class extends base {
static get properties() {
return {
textProperty: { type: String },
valueProperty: { type: String },
emptyLabel: { type: String },
emptyValue: { type: Object },
emptyProperty: { type: String },
};
}
getConfig(column) {
return {
emptyProperty: column.emptyProperty,
};
}
getString(column, item) {
return getString(column, item);
}
toXlsxValue(column, item) {
return toXlsxValue(column, item);
}
cellTitleFn(column, item) {
return getString(column, item);
}
getComparableValue({ valuePath, valueProperty }, item) {
const value = get(item, valuePath);
if (valueProperty == null) {
return value;
}
const subValues = array(value).reduce((acc, subItem) => {
acc.push(get(subItem, valueProperty));
return acc;
}, []);
return subValues.sort().join(' ');
}
getFilterFn(column, filters) {
if (!filters || !Array.isArray(filters) || filters.length === 0) {
return;
}
return applyMultiFilter(column, filters);
}
serializeFilter(column, filter) {
return filter.length === 0 ? null : JSON.stringify(filter);
}
deserializeFilter(column, filter) {
return JSON.parse(decodeURIComponent(filter));
}
computeSource(column, data) {
return column.externalValues || typeof column.values === 'function'
? async (...args) =>
computeValues(
column,
await Promise.resolve(invoke(column.values, ...args)),
)
: computeSource(column, data);
}
};
export {
unique,
getTexts,
getString,
toXlsxValue,
applyMultiFilter,
onChange,
onFocus,
onText,
computeSource,
toAutocompleteSource,
listColumnMixin,
};