-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmoz-omnitable-column-time.js
141 lines (122 loc) · 3.38 KB
/
cosmoz-omnitable-column-time.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
/* eslint-disable no-return-assign */
import '@polymer/paper-dropdown-menu/paper-dropdown-menu';
import '@neovici/cosmoz-input';
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 {
getComparableValue,
getString,
toXlsxValue,
applySingleFilter,
toDate,
toHashString,
fromHashString,
} from './lib/utils-time';
import './lib/cosmoz-omnitable-time-range-input';
import { defaultComputeSource } from './lib/utils-data';
/**
* @polymer
* @customElement
* @appliesMixin columnMixin
*/
class OmnitableColumnTime extends columnMixin(PolymerElement) {
static get properties() {
return {
min: { type: Number, value: null, notify: true },
max: { type: Number, value: null, notify: true },
locale: { type: String, value: null, notify: true },
headerCellClass: { type: String, value: 'time-header-cell' },
minWidth: { type: String, value: '63px' },
width: { type: String, value: '210px' },
flex: { type: String, value: '0' },
/*
* Specifies the value granularity of the time input's value in the filter.
* 1 => full seconds
* 0.1 => milliseconds
*/
filterStep: { type: String, value: '1' },
};
}
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(event.target.value);
return html`<cosmoz-input
no-label-float
type="text"
@change=${onChange}
.value=${getString(column, item)}
></cosmoz-input>`;
}
renderHeader(
{ title, min, max, locale, filterStep },
{ filter },
setState,
source,
) {
return html`<cosmoz-omnitable-time-range-input
.title=${title}
.filter=${filter}
.values=${source}
.min=${min}
.max=${max}
.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-time-range-input>`;
}
computeSource(column, data) {
return defaultComputeSource(column, data);
}
}
customElements.define('cosmoz-omnitable-column-time', OmnitableColumnTime);