-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmoz-omnitable-column-boolean.js
159 lines (131 loc) · 4.27 KB
/
cosmoz-omnitable-column-boolean.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
import { PolymerElement } from '@polymer/polymer/polymer-element';
import { columnMixin } from './cosmoz-omnitable-column-mixin';
import '@polymer/paper-spinner/paper-spinner-lite';
import '@neovici/cosmoz-autocomplete';
import {
html, nothing
} from 'lit-html';
import { get } from '@polymer/polymer/lib/utils/path';
import { memooize } from '@neovici/cosmoz-utils/memoize';
const
computeValue = (value, source) =>
source.find(({ value: valueProp }) => value === valueProp),
computeTooltip = (title, value, source) => {
const val = computeValue(value, source);
return val ? val.text : title;
},
computeItemTooltip = (title, item, valuePath, source) => computeTooltip(
title,
get(item, valuePath),
source
),
computeItemValue = ({ valuePath }, item, source) => computeValue(
get(item, valuePath),
source
),
onChange = setState => selection => {
setState(state => ({ ...state, filter: selection?.[0]?.value ?? null }));
},
onFocus = setState => focused => {
setState(state => ({ ...state, headerFocused: focused }));
},
onText = setState => text => {
setState(state => ({ ...state, query: text }));
},
onEditableChange = onItemChange => selection => onItemChange(selection?.[0]?.value),
getString = ({ valuePath, trueLabel, falseLabel }, item) => {
const value = get(item, valuePath);
return value ? trueLabel : falseLabel;
},
applySingleFilter = ({ valuePath }, filter) => item => get(item, valuePath) === filter,
computeSource = memooize((trueLabel, falseLabel) => [
{ text: trueLabel, value: true },
{ text: falseLabel, value: false }
]),
toXlsxValue = ({ valuePath, trueLabel, falseLabel }, item) => {
if (!valuePath) {
return '';
}
return get(item, valuePath) ? trueLabel : falseLabel;
},
deserializeFilter = (column, filter) => {
try {
return JSON.parse(filter);
} catch (e) {
return null;
}
};
/**
* @polymer
* @customElement
* @appliesMixin columnMixin
*/
class OmnitableColumnBoolean extends columnMixin(PolymerElement) {
static get properties() {
return {
trueLabel: { type: String, value: 'True' },
falseLabel: { type: String, value: 'False' },
flex: { type: String, value: '0' },
cellClass: { type: String, value: 'boolean-cell' }
};
}
getString(column, item) {
return getString(column, item);
}
renderCell(column, { item }) {
return getString(column, item);
}
cellTitleFn(column, item) {
return getString(column, item);
}
renderEditCell(column, { item }, onItemChange) {
const
{ trueLabel, falseLabel } = column,
spinner = column.loading
? html`<paper-spinner-lite style="width: 20px; height: 20px;" suffix slot="suffix" active></paper-spinner-lite>`
: nothing;
return html`<cosmoz-autocomplete
no-label-float
.title=${ computeItemTooltip(column.title, item, column.valuePath, computeSource(trueLabel, falseLabel)) }
.source=${ computeSource(trueLabel, falseLabel) }
.textProperty=${ 'text' }
.value=${ computeItemValue(column, item, computeSource(trueLabel, falseLabel)) }
.onChange=${ onEditableChange(onItemChange) }
.limit=${ 1 }
>${ spinner }</cosmoz-autocomplete>`;
}
renderHeader(column, { filter, query }, setState, source) {
const spinner = column.loading
? html`<paper-spinner-lite style="width: 20px; height: 20px;" suffix slot="suffix" active></paper-spinner-lite>`
: nothing;
return html`<cosmoz-autocomplete-ui
.label=${ column.title }
.title=${ computeItemTooltip(column.title, filter, column.valuePath, source) }
.source=${ source }
.textProperty=${ 'text' }
.value=${ computeValue(filter, source) }
.text=${ query }
.onChange=${ onChange(setState) }
.onFocus=${ onFocus(setState) }
.onText=${ onText(setState) }
.limit=${ 1 }
>${ spinner }</cosmoz-autocomplete-ui>`;
}
computeSource({ trueLabel, falseLabel }) {
return computeSource(trueLabel, falseLabel);
}
getFilterFn(column, filter) {
if (filter == null) {
return;
}
return applySingleFilter(column, filter);
}
toXlsxValue(column, item) {
return toXlsxValue(column, item);
}
deserializeFilter(column, filter) {
return deserializeFilter(column, filter);
}
}
customElements.define('cosmoz-omnitable-column-boolean', OmnitableColumnBoolean);
export { getString, computeItemValue, computeSource, toXlsxValue, onChange, deserializeFilter };