-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexm-data-helper.html
More file actions
161 lines (143 loc) · 3.96 KB
/
exm-data-helper.html
File metadata and controls
161 lines (143 loc) · 3.96 KB
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
<link rel="import" href="../polymer/polymer.html">
<!--
`exm-data-helper` is helper element that facilitates sorting and paging on local data sets.
```html
<iron-ajax url="data/contacts.json" last-response="{{rawItems}}" auto></iron-ajax>
<exm-data-helper
raw-items="[[rawItems]]"
items="{{items}}"
page-index="[[pageIndex]]"
page-size="[[pageSize]]"
sorted="[[sorted]]"
sort-direction="[[sortDirection]]"
total-items="{{totalItems}}">
</exm-data-helper>
<table is="exm-datatable">
<thead is="exm-thead" sorted="{{sorted}}" sort-direction="{{sortDirection}}">
<tr>
<th sortable="name">Name</th>
<th sortable="email">Email</th>
<th sortable="phone">Phone</th>
</tr>
</thead>
<tbody is="exm-tbody" items='[[items]]'>
<template id="tbody">
<tr>
<td>[[item.name]]</td>
<td>[[item.email]]</td>
<td>[[item.phone]]</td>
</tr>
</template>
</tbody>
</table>
<exm-paging
total-items="[[totalItems]]"
page-index="{{pageIndex}}"
page-size="{{pageSize}}">
</exm-paging>
```
@element exm-data-helper
@demo demo/index.html
-->
<dom-module id="exm-data-helper">
<template></template>
<script>
Polymer({
is: 'exm-data-helper',
properties: {
/**
* Raw item list
*/
rawItems: {
type: Array
},
/**
* Returnes a page of the sorted items
*/
items: {
type: Array,
notify: true
},
/**
* Returns the total length of the raw list that will be used in the exm-paging
* as indication on how much pages there are available.
*/
totalItems: {
notify: true,
type: Number,
computed: '_computeLength(rawItems.*)'
},
/**
* This property can be used to change the current visible items page
*/
pageIndex: {
type: Number,
value: 0
},
/**
* Set the page size of the exposed item list
*/
pageSize: {
type: Number,
value: 10
},
/**
* An string containing the column sort key
*/
sorted: String,
/**
* An string containing 'ASC' or 'DESC' to indicate the sorting direction
*/
sortDirection: String
},
observers: [
'_sortChanged(sorted,sortDirection)',
'_ramItemsChanged(rawItems.*)',
'_pageChanged(pageIndex, pageSize)'
],
_sortChanged: function(sorted, sortDirection) {
this._updatePage();
},
_computeLength: function(rawItems) {
return this.rawItems.length;
},
_ramItemsChanged: function() {
this._updatePage();
},
_pageChanged: function(pageIndex, pageSize) {
this._updatePage();
},
_updatePage: function() {
const lookupValueByPath = (o, path) => path.split('.').reduce((r, p) => r[p], o);
const isArray = (o) => Array.isArray(o) || Object.prototype.toString.call(o) === '[object Array]';
const properArray = (o) => {
return isArray(o) ? o : [];
};
const workArray = [...properArray(this.rawItems)];
const start = (this.pageIndex * this.pageSize);
const sortArray = (a, b) => {
let fieldA = lookupValueByPath(a, this.sorted);
let fieldB = lookupValueByPath(b, this.sorted);
if (typeof fieldA === 'number' || typeof fieldA === 'boolean') {
return this.sortDirection === 'ASC' ? fieldA - fieldB : fieldB - fieldA;
} else {
fieldA = fieldA ? fieldA.toLowerCase() : '';
fieldB = fieldB ? fieldB.toLowerCase() : '';
if (fieldA < fieldB) {
return this.sortDirection === 'ASC' ? -1 : 1;
}
if (fieldA > fieldB) {
return this.sortDirection === 'ASC' ? 1 : -1;
}
}
return 0;
}
if (this.sorted) {
workArray.sort((a, b) => sortArray(a, b));
}
this.set('items', []);
this.set('items', workArray.slice(start, start + this.pageSize));
}
});
</script>
</dom-module>