forked from Neovici/cosmoz-omnitable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmoz-omnitable-column-date-behavior.html
162 lines (146 loc) · 4.02 KB
/
cosmoz-omnitable-column-date-behavior.html
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
<link rel="import" href="cosmoz-omnitable-column-range-behavior.html">
<script>
window.Cosmoz = window.Cosmoz || {};
/** @polymerBehavior */
Cosmoz.DateColumnBehaviorImpl = {
properties: {
max: {
type: Date,
value: null
},
min: {
type: Date,
value: null
},
_filterText: {
type: String,
computed: '_computeFilterText(filter.*, formatter)'
},
width: {
type: String,
value: '100px'
},
editWidth: {
type: String,
value: '150px'
},
/**
* No need to grow, as the values in a date column should have known fixed width
* @returns {String} Default flex
*/
flex: {
type: String,
value: '0'
},
formatter: {
type: Object,
computed: '_computeFormatter(locale)'
}
},
/**
* Converts an value to date optionaly limiting it.
*
* @param {Date|String} value Value to convert to date
* @param {Date|String} limit Value used to limit the date
* @param {Function} limitFunc Function used to limit the date (Math.min|Math.max)
* @returns {Date|void} Value converted to date optionaly limitated
*/
toDate(value, limit, limitFunc) {
if (value == null || value === '') {
return;
}
const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) {
return null;
}
if (limitFunc == null || limit == null) {
return date;
}
let lDate = this.toDate(limit);
if (lDate == null) {
return date;
}
var comparableDate = this.getComparableValue(date),
comparableLDate = this.getComparableValue(lDate),
limitedValue = limitFunc(comparableDate, comparableLDate);
return limitedValue === comparableDate ? date : lDate;
},
toValue() {
return this.toDate.apply(this, arguments);
},
/**
* Get comparable number from date
*
* @returns {Number|void} Valid value or void
*/
getComparableValue() {
const value = Cosmoz.RangeColumnBehavior.getComparableValue.apply(this, arguments);
if (value == null) {
return;
}
return this.toNumber(value.getTime());
},
/**
* Get date of item as string
*
* @param {Object} item Item to be processed
* @param {String} valuePath = this.valuePath Property path
* @param {Object} formatter = this.formatter Property formatter
* @returns {String} Date rendered as string or 'Invalid Date'
*/
getString(item, valuePath = this.valuePath, formatter = this.formatter) {
const value = this.toValue(this.get(valuePath, item));
if (value === undefined) {
return '';
}
if (value === null) {
return 'Invalid Date';
}
return this.renderValue(value, formatter);
},
renderValue(value, formatter = this.formatter) {
const date = this.toValue(value);
if (date == null) {
return;
}
return formatter.format(date);
},
_computeFormatter(locale) {
return new Intl.DateTimeFormat(locale || undefined);
},
_toInputString(value) {
const date = this.toValue(value);
if (date == null) {
return null;
}
return this._toLocalISOString(date).slice(0, 10);
},
_dateValueChanged(event) {
const input = event.currentTarget,
value = input.value,
item = event.model.item,
oldValue = this.get(this.valuePath, item),
date = this._fromInputString(value);
if (date == null) {
return;
}
this.set(this.valuePath, date, item);
this._fireItemChangeEvent(item, this.valuePath, oldValue, this.renderValue.bind(this));
},
_toLocalISOString: function (date) {
if (!(date instanceof Date)) {
return null;
}
const pad = number => number < 10 ? '0' + number : number;
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds()) +
'.' + (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5);
}
};
/** @polymerBehavior */
Cosmoz.DateColumnBehavior = [Cosmoz.RangeColumnBehavior, Cosmoz.DateColumnBehaviorImpl];
</script>