-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmoz-omnitable-column-amount.js
166 lines (145 loc) · 3.91 KB
/
cosmoz-omnitable-column-amount.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
/* eslint-disable no-return-assign */
import '@neovici/cosmoz-input';
import '@polymer/paper-dropdown-menu/paper-dropdown-menu';
import './ui-helpers/cosmoz-clear-button';
import { PolymerElement } from '@polymer/polymer/polymer-element';
import { html } from 'lit-html';
import { columnMixin } from './cosmoz-omnitable-column-mixin';
import { defaultComputeSource } from './lib/utils-data';
import './lib/cosmoz-omnitable-amount-range-input';
import {
getComparableValue,
getCurrency,
applySingleFilter,
getString,
getInputString,
toAmount,
toHashString,
fromHashString,
} from './lib/utils-amount';
import { get } from '@polymer/polymer/lib/utils/path';
/**
* @polymer
* @customElement
* @appliesMixin columnMixin
*/
class OmnitableColumnAmount extends columnMixin(PolymerElement) {
static get properties() {
return {
min: { type: Number, value: null, notify: true },
max: { type: Number, value: null, notify: true },
limits: { type: Function },
locale: { type: String, value: null, notify: true },
autoupdate: { type: Boolean, value: false, notify: true },
currency: { type: String, notify: true },
autodetect: { type: Boolean, value: false, notify: true },
rates: { type: Object, notify: true },
width: { type: String, value: '70px' },
cellClass: { type: String, value: 'amount-cell align-right' },
headerCellClass: { type: String, value: 'amount-header-cell' },
};
}
getConfig(column) {
return { limits: column.limits };
}
getFilterFn(column, filter) {
const min = getComparableValue({ ...column, valuePath: 'min' }, filter),
max = getComparableValue({ ...column, valuePath: 'max' }, filter);
if (min == null && max == null) {
return;
}
return applySingleFilter(column, filter);
}
getString(column, item) {
return getString(column, item);
}
toXlsxValue(column, item) {
return getString(column, item);
}
cellTitleFn(column, item) {
return getString(column, item);
}
getComparableValue(column, item) {
return getComparableValue(column, item);
}
serializeFilter({ rates }, filter) {
if (filter == null) {
return;
}
const min = toAmount(rates, filter.min),
max = toAmount(rates, filter.max);
if (min == null && max == null) {
return;
}
return toHashString(min) + '~' + toHashString(max);
}
deserializeFilter(column, filter) {
if (filter == null || filter === '') {
return null;
}
const matches = filter.match(/^([^~]+)?~([^~]+)?/iu);
if (!Array.isArray(matches)) {
return null;
}
return {
min: fromHashString(matches[1]),
max: fromHashString(matches[2]),
};
}
renderCell(column, { item }) {
return html`<span>${column.getString(column, item)}</span>`;
}
renderEditCell(column, { item }, onItemChange) {
const onChange = (event) =>
onItemChange({
amount: event.target.value,
currency: get(item, column.valuePath)?.currency,
});
return html`<cosmoz-input
no-label-float
type="number"
@change=${onChange}
.value=${getInputString(column, item)}
>
<div slot="suffix">${getCurrency(column, item)}</div>
</cosmoz-input>`;
}
renderHeader(
{
title,
min,
max,
limits,
locale,
rates,
currency,
autoupdate,
autodetect,
},
{ filter },
setState,
source,
) {
return html`<cosmoz-omnitable-amount-range-input
.title=${title}
.filter=${filter}
.values=${source}
.rates=${rates}
.min=${min}
.max=${max}
.limits=${limits}
.locale=${locale}
.currency=${currency}
.autoupdate=${autoupdate}
.autodetect=${autodetect}
@filter-changed=${({ detail: { value } }) =>
setState((state) => ({ ...state, filter: value }))}
@header-focused-changed=${({ detail: { value } }) =>
setState((state) => ({ ...state, headerFocused: value }))}
></cosmoz-omnitable-amount-range-input>`;
}
computeSource(column, data) {
return defaultComputeSource(column, data);
}
}
customElements.define('cosmoz-omnitable-column-amount', OmnitableColumnAmount);