@neovici/cosmoz-omnitable 7.2.1 → 8.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +23 -0
  2. package/cosmoz-omnitable-column-amount.js +89 -320
  3. package/cosmoz-omnitable-column-autocomplete.js +36 -47
  4. package/cosmoz-omnitable-column-boolean.js +107 -209
  5. package/cosmoz-omnitable-column-date.js +89 -102
  6. package/cosmoz-omnitable-column-datetime.js +86 -119
  7. package/cosmoz-omnitable-column-list-data.js +4 -1
  8. package/cosmoz-omnitable-column-list-horizontal.js +20 -38
  9. package/cosmoz-omnitable-column-list-mixin.js +133 -140
  10. package/cosmoz-omnitable-column-list.js +19 -28
  11. package/cosmoz-omnitable-column-mixin.js +69 -447
  12. package/cosmoz-omnitable-column-number.js +91 -183
  13. package/cosmoz-omnitable-column-time.js +77 -162
  14. package/cosmoz-omnitable-column.js +49 -93
  15. package/cosmoz-omnitable-group-row.js +1 -5
  16. package/cosmoz-omnitable-header-row.js +9 -6
  17. package/cosmoz-omnitable-item-expand.js +0 -3
  18. package/cosmoz-omnitable-item-row.js +5 -8
  19. package/cosmoz-omnitable-styles.js +1 -5
  20. package/cosmoz-omnitable.js +72 -763
  21. package/lib/cosmoz-omnitable-amount-range-input.js +295 -0
  22. package/{cosmoz-omnitable-column-date-mixin.js → lib/cosmoz-omnitable-date-input-mixin.js} +4 -26
  23. package/lib/cosmoz-omnitable-date-range-input.js +81 -0
  24. package/lib/cosmoz-omnitable-datetime-range-input.js +75 -0
  25. package/lib/cosmoz-omnitable-number-range-input.js +159 -0
  26. package/{cosmoz-omnitable-column-range-mixin.js → lib/cosmoz-omnitable-range-input-mixin.js} +45 -123
  27. package/lib/cosmoz-omnitable-settings.js +7 -4
  28. package/lib/cosmoz-omnitable-time-range-input.js +130 -0
  29. package/lib/generic-sorter.js +2 -2
  30. package/lib/invoke.js +1 -0
  31. package/lib/memoize.js +54 -0
  32. package/lib/polymer-haunted-render-mixin.js +19 -0
  33. package/lib/save-as-csv-action.js +32 -0
  34. package/lib/save-as-xlsx-action.js +25 -0
  35. package/lib/use-canvas-width.js +1 -1
  36. package/lib/use-dom-columns.js +133 -0
  37. package/lib/use-hash-state.js +59 -0
  38. package/lib/use-layout.js +1 -1
  39. package/lib/use-omnitable.js +26 -14
  40. package/lib/use-processed-items.js +132 -0
  41. package/lib/use-sort-and-group-options.js +30 -0
  42. package/lib/utils-amount.js +147 -0
  43. package/lib/utils-data.js +36 -0
  44. package/lib/utils-date.js +204 -0
  45. package/lib/utils-datetime.js +71 -0
  46. package/lib/utils-number.js +112 -0
  47. package/lib/utils-time.js +115 -0
  48. package/package.json +1 -1
  49. package/lib/use-force-render.js +0 -8
  50. package/lib/use-render-on-column-updates.js +0 -18
@@ -0,0 +1,204 @@
1
+ import { toLocalISOString } from '@neovici/cosmoz-utils/lib/date';
2
+ import { get } from '@polymer/polymer/lib/utils/path';
3
+ import { toNumber } from './utils-number';
4
+
5
+ export const
6
+
7
+
8
+ /**
9
+ * Calculates the local timezone offset and formats it to ISO Timezone string.
10
+ * @param {String} localISOString an ISO date string
11
+ * @return {String} the ISO timezone
12
+ */
13
+ getTimezoneString = localISOString => {
14
+ const off = -new Date(localISOString).getTimezoneOffset() / 60;
15
+ return (off < 0 ? '-' : '+') + ['0', Math.abs(off)].join('').substr(-2) + ':00';
16
+ },
17
+
18
+ /**
19
+ * Computes the local timezone and adds it to a local ISO string
20
+ * @param {String} localISOString an ISO date string, without timezone info
21
+ * @return {String} an ISO date string, with timezone info
22
+ */
23
+ getAbsoluteISOString = localISOString => {
24
+ // Most browsers use local timezone when no timezone is specified
25
+ // but Safari uses UTC, so we set it implicitly
26
+ // TODO: Consider removing this when/if Safari handles local timezone correctly
27
+ if (localISOString.length === 19) {
28
+ return localISOString + getTimezoneString(localISOString);
29
+ }
30
+ return localISOString;
31
+ },
32
+
33
+ parseDate = value => {
34
+ if (value == null || value === '') {
35
+ return;
36
+ }
37
+
38
+ // convert value to Date
39
+ let date = value;
40
+ if (!(date instanceof Date)) {
41
+ // if the value is an ISO string, make sure that it has an explicit timezone
42
+ if (typeof value === 'string') {
43
+ date = getAbsoluteISOString(date);
44
+ }
45
+
46
+ date = new Date(date);
47
+ }
48
+
49
+ if (Number.isNaN(date.getTime())) {
50
+ return null;
51
+ }
52
+
53
+ return date;
54
+ },
55
+
56
+ /**
57
+ * Get comparable number from date
58
+ *
59
+ * @param {Object} item Item to be processed
60
+ * @param {String} valuePath Property path
61
+ * @returns {Number|void} Valid value or void
62
+ */
63
+ getComparableValue = (item, valuePath) => {
64
+ if (item == null) {
65
+ return;
66
+ }
67
+ let value = item;
68
+ if (valuePath != null) {
69
+ value = get(item, valuePath);
70
+ }
71
+ value = parseDate(value);
72
+
73
+ if (value == null) {
74
+ return;
75
+ }
76
+ return toNumber(value.getTime());
77
+ },
78
+
79
+ /**
80
+ * Converts an value to date optionaly limiting it.
81
+ *
82
+ * @param {Date|String} value Value to convert to date
83
+ * @param {Date|String} limit Value used to limit the date
84
+ * @param {Function} limitFunc Function used to limit the date (Math.min|Math.max)
85
+ * @returns {Date|void} Value converted to date optionaly limitated
86
+ */
87
+ toDate = (value, limit, limitFunc) => { // eslint-disable-line max-statements
88
+ const date = parseDate(value);
89
+
90
+ if (date == null) {
91
+ return null;
92
+ }
93
+
94
+ if (limitFunc == null || limit == null) {
95
+ return date;
96
+ }
97
+
98
+ const lDate = toDate(limit);
99
+ if (lDate == null) {
100
+ return date;
101
+ }
102
+
103
+ const comparableDate = getComparableValue(date),
104
+ comparableLDate = getComparableValue(lDate),
105
+ limitedValue = limitFunc(comparableDate, comparableLDate);
106
+ return limitedValue === comparableDate ? date : lDate;
107
+ },
108
+
109
+ renderValue = (value, formatter) => {
110
+ if (formatter == null) {
111
+ return;
112
+ }
113
+ const date = toDate(value);
114
+ if (date == null) {
115
+ return;
116
+ }
117
+ return formatter.format(date);
118
+ },
119
+
120
+ formatters = {},
121
+ getFormatter = locale => {
122
+ const key = locale || '';
123
+
124
+ if (formatters[key]) {
125
+ return formatters[key];
126
+ }
127
+
128
+ formatters[key] = new Intl.DateTimeFormat(locale || undefined);
129
+
130
+ return formatters[key];
131
+ },
132
+
133
+ getString = ({ valuePath, locale }, item) => {
134
+ let value = get(item, valuePath);
135
+ if (value === undefined) {
136
+ return '';
137
+ }
138
+ value = toDate(value);
139
+ if (value === null) {
140
+ return 'Invalid Date';
141
+ }
142
+ return renderValue(value, getFormatter(locale));
143
+ },
144
+
145
+ toInputString = value => {
146
+ const date = toDate(value);
147
+ if (date == null) {
148
+ return null;
149
+ }
150
+ return toLocalISOString(date).slice(0, 10);
151
+ },
152
+
153
+ getInputString = ({ valuePath }, item) => toInputString(get(item, valuePath)),
154
+
155
+ fromInputString = (value, property) => {
156
+ const date = toDate(value);
157
+ if (date == null) {
158
+ return;
159
+ }
160
+ if (property === 'min') {
161
+ date.setHours(0, 0, 0, 0);
162
+ }
163
+ if (property === 'max') {
164
+ date.setHours(23, 59, 59);
165
+ }
166
+ return date;
167
+ },
168
+
169
+ toHashString = value => {
170
+ const string = toInputString(value);
171
+ if (string == null) {
172
+ return '';
173
+ }
174
+ return string;
175
+ },
176
+
177
+ toXlsxValue = ({ valuePath }, item) => {
178
+ if (!valuePath) {
179
+ return '';
180
+ }
181
+ const date = toDate(get(item, valuePath));
182
+ if (!date) {
183
+ return '';
184
+ }
185
+ const localDate = toDate(toLocalISOString(date));
186
+ localDate.setHours(0, 0, 0, 0);
187
+ return localDate;
188
+ },
189
+
190
+ applySingleFilter = (column, filter) => item => {
191
+ const value = getComparableValue(item, column.valuePath, column);
192
+
193
+ if (value == null) {
194
+ return false;
195
+ }
196
+
197
+ const
198
+ min = getComparableValue(filter, 'min', column),
199
+ max = getComparableValue(filter, 'max', column);
200
+
201
+ return !(value < min || value > max);
202
+ };
203
+
204
+
@@ -0,0 +1,71 @@
1
+ import { toLocalISOString } from '@neovici/cosmoz-utils/lib/date';
2
+ import { get } from '@polymer/polymer/lib/utils/path';
3
+ import { renderValue, toDate } from './utils-date';
4
+
5
+ export const
6
+
7
+ formatters = {},
8
+ getFormatter = locale => {
9
+ const key = locale || '';
10
+
11
+ if (formatters[key]) {
12
+ return formatters[key];
13
+ }
14
+
15
+ const timeFormatOption = {
16
+ year: 'numeric',
17
+ month: 'numeric',
18
+ day: 'numeric',
19
+ hour: 'numeric',
20
+ minute: 'numeric'
21
+ };
22
+
23
+ formatters[key] = new Intl.DateTimeFormat(locale || undefined, timeFormatOption);
24
+
25
+ return formatters[key];
26
+ },
27
+
28
+ getString = ({ valuePath, locale }, item) => {
29
+ const value = toDate(get(item, valuePath));
30
+ if (value === undefined) {
31
+ return '';
32
+ }
33
+ if (value === null) {
34
+ return 'Invalid Date';
35
+ }
36
+ return renderValue(value, getFormatter(locale));
37
+ },
38
+
39
+ toXlsxValue = ({ valuePath }, item) => {
40
+ if (!valuePath) {
41
+ return '';
42
+ }
43
+ return get(item, valuePath);
44
+ },
45
+
46
+ toHashString = value => {
47
+ const date = toDate(value);
48
+ if (date == null) {
49
+ return '';
50
+ }
51
+ //Use utc in hash
52
+ return date.toISOString().slice(0, 19).replace(/:/gu, '.');
53
+ },
54
+
55
+ fromHashString = value => {
56
+ if (value == null || value === '') {
57
+ return;
58
+ }
59
+ //Parse utc from hash string
60
+ return toDate(value.replace(/\./gu, ':') + 'Z');
61
+ },
62
+
63
+ toInputString = value => {
64
+ const date = toDate(value);
65
+ if (date == null) {
66
+ return null;
67
+ }
68
+ return toLocalISOString(date).slice(0, 19);
69
+ },
70
+
71
+ getInputString = ({ valuePath }, item) => toInputString(get(item, valuePath));
@@ -0,0 +1,112 @@
1
+
2
+ import { get } from '@polymer/polymer/lib/utils/path';
3
+ import { memoooize } from './memoize';
4
+
5
+ export const
6
+ /**
7
+ * Converts a value to number optionaly limiting it.
8
+ *
9
+ * @param {Number|*} value The value to convert to number
10
+ * @param {Number|*} limit The value used to limit the number
11
+ * @param {Function} limitFunc The function used to limit the number (Math.min|Math.max)
12
+ * @returns {Number|void} Value converted to Number or void
13
+ */
14
+ toNumber = (value, limit, limitFunc) => {
15
+ if (value == null || value === '') {
16
+ return;
17
+ }
18
+ const number = typeof value === 'number' ? value : Number(value);
19
+ if (Number.isNaN(number)) {
20
+ return;
21
+ }
22
+ if (limitFunc == null || limit == null) {
23
+ return number;
24
+ }
25
+ const lNumber = toNumber(limit);
26
+ if (lNumber == null) {
27
+ return number;
28
+ }
29
+ return limitFunc(number, lNumber);
30
+ },
31
+
32
+ toInputString = value => {
33
+ const val = toNumber(value);
34
+ if (val == null) {
35
+ return null;
36
+ }
37
+ return val.toString();
38
+ },
39
+
40
+ getInputString = ({ valuePath }, item) => {
41
+ const value = toNumber(get(item, valuePath));
42
+ return toInputString(value);
43
+ },
44
+
45
+ toHashString = value => {
46
+ const string = toInputString(value);
47
+ if (string == null) {
48
+ return '';
49
+ }
50
+ return string;
51
+ },
52
+
53
+ getComparableValue = (item, valuePath, { maximumFractionDigits } = {}) => {
54
+ if (item == null) {
55
+ return;
56
+ }
57
+ let value = item;
58
+ if (valuePath != null) {
59
+ value = get(item, valuePath);
60
+ }
61
+ value = toNumber(value);
62
+ if (value == null) {
63
+ return;
64
+ }
65
+
66
+ if (maximumFractionDigits !== null) {
67
+ return toNumber(value.toFixed(maximumFractionDigits));
68
+ }
69
+ return value;
70
+ },
71
+
72
+ makeFormatter = memoooize((locale, minimumFractionDigits, maximumFractionDigits) => {
73
+ const options = {
74
+ localeMatcher: 'best fit' // chrome expects this when using custom options
75
+ };
76
+ if (minimumFractionDigits !== null) {
77
+ options.minimumFractionDigits = minimumFractionDigits;
78
+ }
79
+ if (maximumFractionDigits !== null) {
80
+ options.maximumFractionDigits = maximumFractionDigits;
81
+ }
82
+ return new Intl.NumberFormat(locale || undefined, options);
83
+ }),
84
+
85
+ getString = ({ valuePath, locale, minimumFractionDigits, maximumFractionDigits }, item) => {
86
+ const value = get(item, valuePath);
87
+
88
+ if (value == null) {
89
+ return '';
90
+ }
91
+
92
+ const number = toNumber(value);
93
+ if (number == null) {
94
+ return;
95
+ }
96
+ const formatter = makeFormatter(locale, minimumFractionDigits, maximumFractionDigits);
97
+ return formatter.format(number);
98
+ },
99
+
100
+ applySingleFilter = (column, filter) => item => {
101
+ const value = getComparableValue(item, column.valuePath, column);
102
+
103
+ if (value == null) {
104
+ return false;
105
+ }
106
+
107
+ const
108
+ min = getComparableValue(filter, 'min', column),
109
+ max = getComparableValue(filter, 'max', column);
110
+
111
+ return !(value < min || value > max);
112
+ };
@@ -0,0 +1,115 @@
1
+ import { toLocalISOString } from '@neovici/cosmoz-utils/lib/date';
2
+ import { get } from '@polymer/polymer/lib/utils/path';
3
+ import { getAbsoluteISOString, renderValue, toDate as superToDate } from './utils-date';
4
+ import { toNumber } from './utils-number';
5
+
6
+ export const
7
+
8
+ _fixedDate = '1970-01-01',
9
+
10
+ /**
11
+ * Converts time to date optionaly limiting it.
12
+ *
13
+ * @param {Date|Number} value Date or Timestamp ( miliseconds since property _fixedDate ) to be converted
14
+ * @param {Date|Number} limit Optional value to limit the date.
15
+ * @param {Function} limitFunc Function used to limit the date (Math.min|Math.max)
16
+ * @returns {Date|void} Value converted to date optionaly limitated
17
+ */
18
+ toDate = (value, limit, limitFunc) => {
19
+ // Most browsers use local timezone when no timezone is specified
20
+ // but Safari uses UTC, so we set it implicitly
21
+ // TODO: Consider removing this when/if Safari handles local timezone correctly
22
+ const date = typeof value === 'string' && value.length > 3 && value.length <= 9
23
+ ? getAbsoluteISOString(_fixedDate + 'T' + value)
24
+ : value;
25
+ return superToDate(date, limit, limitFunc);
26
+ },
27
+
28
+ formatters = {},
29
+ getFormatter = locale => {
30
+ const key = locale || '';
31
+
32
+ if (formatters[key]) {
33
+ return formatters[key];
34
+ }
35
+
36
+ const timeFormatOption = {
37
+ hour: 'numeric',
38
+ minute: 'numeric',
39
+ second: 'numeric'
40
+ };
41
+ formatters[key] = new Intl.DateTimeFormat(locale || undefined, timeFormatOption);
42
+
43
+ return formatters[key];
44
+ },
45
+ getString = ({ valuePath, locale }, item) => {
46
+ const value = toDate(get(item, valuePath));
47
+ if (value === undefined) {
48
+ return '';
49
+ }
50
+ if (value === null) {
51
+ return 'Invalid Date';
52
+ }
53
+ return renderValue(value, getFormatter(locale));
54
+ },
55
+
56
+ toXlsxValue = (column, item) => {
57
+ if (!column.valuePath) {
58
+ return '';
59
+ }
60
+ return getString(column, item);
61
+ },
62
+ toInputString = value => {
63
+ const date = toDate(value);
64
+ if (date == null) {
65
+ return null;
66
+ }
67
+ return toLocalISOString(date).slice(11, 19);
68
+ },
69
+
70
+ getComparableValue = (item, valuePath) => {
71
+ if (item == null) {
72
+ return;
73
+ }
74
+ let value = toInputString(valuePath == null ? item : get(item, valuePath));
75
+ if (value == null) {
76
+ return;
77
+ }
78
+ value = toDate(getAbsoluteISOString(_fixedDate + 'T' + value));
79
+ if (value == null) {
80
+ return;
81
+ }
82
+ return toNumber(value.getTime());
83
+ },
84
+
85
+ applySingleFilter = (column, filter) => item => {
86
+ const value = getComparableValue(item, column.valuePath, column);
87
+
88
+ if (value == null) {
89
+ return false;
90
+ }
91
+
92
+ const
93
+ min = getComparableValue(filter, 'min', column),
94
+ max = getComparableValue(filter, 'max', column);
95
+
96
+ return !(value < min || value > max);
97
+ },
98
+
99
+ toHashString = value => {
100
+ const date = toDate(value);
101
+ if (date == null) {
102
+ return '';
103
+ }
104
+ //Use utc in hash
105
+ return date.toISOString().slice(11, 19).replace(/:/gu, '.');
106
+ },
107
+
108
+ fromHashString = value => {
109
+ if (value == null || value === '') {
110
+ return;
111
+ }
112
+ //Parse utc from hash string
113
+ return toDate(value.replace(/\./gu, ':') + 'Z');
114
+ };
115
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-omnitable",
3
- "version": "7.2.1",
3
+ "version": "8.0.0-beta.1",
4
4
  "description": "[![Build Status](https://travis-ci.org/Neovici/cosmoz-omnitable.svg?branch=master)](https://travis-ci.org/Neovici/cosmoz-omnitable)",
5
5
  "keywords": [
6
6
  "web-components"
@@ -1,8 +0,0 @@
1
- import {
2
- useState, useCallback
3
- } from 'haunted';
4
-
5
- export const useForceRender = () => {
6
- const [, setState] = useState(0);
7
- return useCallback(() => setState(state => state + 1), [setState]);
8
- };
@@ -1,18 +0,0 @@
1
- import { useEffect } from 'haunted';
2
- import { useForceRender } from './use-force-render';
3
-
4
- export const useRenderOnColumnUpdates = columns => {
5
- const render = useForceRender();
6
-
7
- useEffect(() => {
8
- if (columns == null) {
9
- return;
10
- }
11
-
12
- // NOTE: this re-renders *all* of the cells when any column prop changes
13
- // re-rendering costs around 2ms, so we can ignore this for now
14
- // TODO: revisit this after haunted migration
15
- columns.forEach(column => column.addEventListener('cosmoz-column-prop-changed', render));
16
- return () => columns.forEach(column => column.removeEventListener('cosmoz-column-prop-changed', render));
17
- }, [columns, render]);
18
- };