-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcosmoz-omnitable-column-datetime.js
150 lines (130 loc) · 3.58 KB
/
cosmoz-omnitable-column-datetime.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
/* eslint-disable no-return-assign */
import '@polymer/paper-dropdown-menu/paper-dropdown-menu';
import '@neovici/cosmoz-datetime-input';
import './ui-helpers/cosmoz-clear-button';
import { columnMixin } from './cosmoz-omnitable-column-mixin';
import { PolymerElement } from '@polymer/polymer/polymer-element';
import { html } from 'lit-html';
import {
fromHashString,
getString,
toHashString,
toXlsxValue,
} from './lib/utils-datetime';
import {
applySingleFilter,
fromInputString,
getComparableValue,
toDate,
} from './lib/utils-date';
import { defaultComputeSource } from './lib/utils-data';
import './lib/cosmoz-omnitable-datetime-range-input';
/**
* @polymer
* @customElement
* @appliesMixin columnMixin
*/
class OmnitableColumnDatetime extends columnMixin(PolymerElement) {
static get is() {
return 'cosmoz-omnitable-column-datetime';
}
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 },
headerCellClass: { type: String, value: 'datetime-header-cell' },
width: { type: String, value: '210px' },
minWidth: { type: String, value: '128px' },
flex: { type: String, value: '0' },
filterStep: { type: Number, value: 1 },
};
}
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 toXlsxValue(column, item);
}
cellTitleFn(column, item) {
return getString(column, item);
}
getComparableValue(column, item) {
return getComparableValue(column, item);
}
serializeFilter(column, filter) {
if (filter == null) {
return;
}
const min = toDate(filter.min),
max = toDate(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 getString(column, item);
}
renderEditCell(column, { item }, onItemChange) {
const onChange = (event) =>
onItemChange(fromInputString(event.target.value));
return html`<cosmoz-input
no-label-float
type="text"
@change=${onChange}
.value=${getString(column, item)}
></cosmoz-input>`;
}
// eslint-disable-next-line max-lines-per-function
renderHeader(
{ title, min, max, limits, locale, filterStep },
{ filter },
setState,
source,
) {
return html`<cosmoz-omnitable-datetime-range-input
.title=${title}
.filter=${filter}
.values=${source}
.min=${min}
.max=${max}
.limits=${limits}
.locale=${locale}
.filterStep=${filterStep}
@filter-changed=${({ detail: { value } }) =>
setState((state) => ({ ...state, filter: value }))}
@header-focused-changed=${({ detail: { value } }) =>
setState((state) => ({ ...state, headerFocused: value }))}
></cosmoz-omnitable-datetime-range-input>`;
}
computeSource(column, data) {
return defaultComputeSource(column, data);
}
}
customElements.define(OmnitableColumnDatetime.is, OmnitableColumnDatetime);