forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-grid-array-data-provider-mixin.html
146 lines (121 loc) · 3.85 KB
/
vaadin-grid-array-data-provider-mixin.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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<script>
window.Vaadin = window.Vaadin || {};
window.Vaadin.Grid = window.Vaadin.Grid || {};
/**
* @polymerMixin
*/
Vaadin.Grid.ArrayDataProviderMixin = superClass => class ArrayDataProviderMixin extends superClass {
static get properties() {
return {
/**
* An array containing the items which will be stamped to the column template
* instances.
*/
items: Array
};
}
static get observers() {
return [
'_itemsChanged(items, items.*)'
];
}
_itemsChanged(items, splices) {
if (!Array.isArray(items)) {
if (items === undefined || items === null) {
this.size = 0;
}
if (this.dataProvider === this._arrayDataProvider) {
this.dataProvider = undefined;
}
return;
}
this.size = items.length;
this.dataProvider = this.dataProvider || this._arrayDataProvider;
this.clearCache();
}
_arrayDataProvider(opts, cb) {
let items = (Array.isArray(this.items) ? this.items : []).slice(0);
if (this._filters && this._checkPaths(this._filters, 'filtering', items)) {
items = this._filter(items);
}
this.size = items.length;
if (opts.sortOrders.length && this._checkPaths(this._sorters, 'sorting', items)) {
items = items.sort(this._multiSort.bind(this));
}
const start = opts.page * opts.pageSize;
const end = start + opts.pageSize;
const slice = items.slice(start, end);
cb(slice, items.length);
}
/**
* Check array of filters/sorters for paths validity, console.warn invalid items
* @param {Array} arrayToCheck The array of filters/sorters to check
* @param {string} action The name of action to include in warning (filtering, sorting)
* @param {Array} items
*/
_checkPaths(arrayToCheck, action, items) {
if (!items.length) {
return false;
}
let result = true;
for (var i in arrayToCheck) {
const path = arrayToCheck[i].path;
// skip simple paths
if (!path || path.indexOf('.') === -1) {
continue;
}
const parentProperty = path.replace(/\.[^\.]*$/, ''); // a.b.c -> a.b
if (Polymer.Base.get(parentProperty, items[0]) === undefined) {
console.warn(`Path "${path}" used for ${action} does not exist in all of the items, ${action} is disabled.`);
result = false;
}
}
return result;
}
_multiSort(a, b) {
return this._sorters.map(sort => {
if (sort.direction === 'asc') {
return this._compare(Polymer.Base.get(sort.path, a), Polymer.Base.get(sort.path, b));
} else if (sort.direction === 'desc') {
return this._compare(Polymer.Base.get(sort.path, b), Polymer.Base.get(sort.path, a));
}
return 0;
}).reduce((p, n) => {
return p ? p : n;
}, 0);
}
_normalizeEmptyValue(value) {
if ([undefined, null].indexOf(value) >= 0) {
return '';
} else if (isNaN(value)) {
return value.toString();
} else {
return value;
}
}
_compare(a, b) {
a = this._normalizeEmptyValue(a);
b = this._normalizeEmptyValue(b);
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
_filter(items) {
return items.filter((item, index) => {
return this._filters.filter(filter => {
const value = this._normalizeEmptyValue(Polymer.Base.get(filter.path, item));
return value.toString().toLowerCase().indexOf(filter.value.toString().toLowerCase()) === -1;
}).length === 0;
});
}
};
</script>