Skip to content

Commit 800fef1

Browse files
committed
Some code cleanup + Fixed minor issue if empty data set
1 parent b0cc2f4 commit 800fef1

File tree

9 files changed

+52
-84
lines changed

9 files changed

+52
-84
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "exmg-datatable",
3-
"version": "2.0.4",
3+
"version": "2.0.8",
44
"description": "Material design data table (Polymer 2.0)",
55
"authors": [
66
"Mark Smits"

demo/collapse.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ <h3>[[item.shortText]]</h3>
161161
return []
162162
}
163163
_computedClass(isSelected) {
164-
var classes = 'tr item';
164+
let classes = 'tr item';
165165
if (isSelected) {
166166
classes += ' selected';
167167
}

demo/simple.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ <h2>Simple Table</h2>
116116
};
117117
}
118118
_computedClass(isSelected) {
119-
var classes = 'tr ';
119+
let classes = 'tr ';
120120
if (isSelected) {
121121
classes += ' selected';
122122
}

exmg-data-filter.html

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
-->
3636

3737
<dom-module id="exmg-data-filter">
38-
<template></template>
39-
4038
<script>
41-
4239
class ExmgDataFilter extends Polymer.Element {
4340
static get is() { return 'exmg-data-filter'; }
4441
static get properties() {
@@ -81,31 +78,24 @@
8178

8279
_updateItems() {
8380

84-
console.log('_updateItems, ');
85-
8681
/* Skip if raw items or filterfields are not set */
87-
if(this.rawItems === undefined || this.filterFields === undefined) {
82+
if(!this.rawItems || !this.filterFields) {
8883
return;
8984
}
9085

9186
/* return all items when filter is empty */
92-
if(this.filterValue === undefined && this.filterValue === '') {
87+
if(!this.filterValue || this.filterValue === '') {
9388
this.set('items', []);
9489
this.set('items', this.rawItems);
9590
return;
9691
}
9792

9893
/* Look for occurances of the filter value in the item fields */
99-
var items = [];
100-
var filterFields = this.filterFields.split(',');
101-
for(var i = 0; i < this.rawItems.length; i++) {
102-
for(var j = 0; j < filterFields.length; j++) {
103-
if(this.rawItems[i][filterFields[j].trim()].indexOf(this.filterValue) !== -1){
104-
items.push(this.rawItems[i]);
105-
break;
106-
}
107-
}
108-
}
94+
let items = [];
95+
const filterFields = this.filterFields.split(',');
96+
this.rawItems.forEach(o => { filterFields.some(p => o[p].indexOf(this.filterValue) !== -1) && items.push(o)});
97+
98+
/* Reset result array before adding new results */
10999
this.set('items', []);
110100
this.set('items', items);
111101

exmg-data-helper.html

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
<link rel="import" href="../polymer/lib/mixins/gesture-event-listeners.html">
33

44
<dom-module id="exmg-data-helper">
5-
<template>
6-
<style>
7-
:host {
8-
display: block;
9-
}
10-
</style>
11-
<slot></slot>
12-
</template>
13-
145
<script>
156
/**
167
* `exmg-data-helper`
@@ -20,6 +11,12 @@
2011
* @polymer
2112
* @demo demo/index.html
2213
*/
14+
15+
(function() {
16+
const isArray = o => Array.isArray(o) || Object.prototype.toString.call(o) === '[object Array]';
17+
const properArray = o => isArray(o) ? o : [];
18+
const lookupValueByPath = (o, path) => path.split('.').reduce((r, p) => r[p], o);
19+
2320
class ExmgDataHelper extends Polymer.GestureEventListeners(Polymer.Element) {
2421
static get is() { return 'exmg-data-helper'; }
2522
static get properties() {
@@ -91,9 +88,8 @@
9188
this._updatePage();
9289
}
9390

94-
_computeLength(rawItems) {
95-
const rawItems = this.rawItems || [];
96-
return rawItems.length;
91+
_computeLength() {
92+
return properArray(this.rawItems).length;
9793
}
9894

9995
_ramItemsChanged() {
@@ -106,32 +102,19 @@
106102
}
107103

108104
_updatePage() {
109-
if(this.rawItems === undefined) {
110-
return;
111-
}
112105
/* Make sure to always start with same array to ensure consistency in sorting results */
113-
const workArray = this.rawItems.slice();
114-
const start = (this.pageIndex * this.pageSize);
115-
116-
console.log('start, this._doSort', start, this._doSort);
106+
var workArray = [...properArray(this.rawItems)];
107+
var start = (this.pageIndex * this.pageSize);
117108

118109
if(this._doSort && this.sorted) {
119-
const lookupItemByPath = (item, path) => {
120-
const parts = path.split(".");
121-
if (parts.length === 1){
122-
return item[parts[0]];
123-
}
124-
return lookupItemByPath(item[parts[0]], parts.slice(1).join("."));
125-
};
126-
127110
workArray.sort((a, b) => {
128-
const fieldA = lookupItemByPath(a, this.sorted);
129-
const fieldB = lookupItemByPath(b, this.sorted);
111+
let fieldA = lookupValueByPath(a, this.sorted);
112+
let fieldB = lookupValueByPath(b, this.sorted);
130113
if(typeof fieldA === 'number' || typeof fieldA === 'boolean') {
131114
return this.sortDirection === 'ASC' ? fieldA - fieldB : fieldB - fieldA;
132115
} else {
133-
fieldA = fieldA.toLowerCase();
134-
fieldB = fieldB.toLowerCase();
116+
fieldA = fieldA ? fieldA.toLowerCase() : '';
117+
fieldB = fieldB ? fieldB.toLowerCase() : '';
135118
if (fieldA < fieldB) {
136119
return this.sortDirection === 'ASC' ? -1 : 1;
137120
}
@@ -152,5 +135,7 @@
152135
}
153136

154137
window.customElements.define(ExmgDataHelper.is, ExmgDataHelper);
138+
139+
})();
155140
</script>
156141
</dom-module>

exmg-paging.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,10 @@
119119
}
120120

121121
_observePageSizeList(ps) {
122-
console.log('_observePageSizeList', ps);
123122
this.pageSize = Number(ps);
124123
}
125124

126125
_observePageSize(ps) {
127-
console.log('_oservePageSize', ps);
128126
this._pageSize = Number(ps);
129127
}
130128

@@ -133,7 +131,7 @@
133131
}
134132

135133
_pageInfoEnd(pageIndex, pageSize, totalItems) {
136-
var end = (pageIndex + 1) * pageSize;
134+
const end = (pageIndex + 1) * pageSize;
137135
return end < totalItems ? end : totalItems;
138136
}
139137

exmg-tbody.html

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<style>
88
:host {
99
display: table-row-group;
10-
border-bottom: 1px solid #dbdbdb;
10+
1111
}
1212
.tr {
1313
display:table-row;
@@ -198,17 +198,17 @@
198198
* Select an item from an event object.
199199
*/
200200
_selectionHandler(e) {
201-
var m = e.path.find(entry => {
201+
let m = e.path.find(entry => {
202202
return entry.classList && entry.classList.contains('tr');
203203
});
204-
var model = this.modelForElement(m);
204+
let model = this.modelForElement(m);
205205
if (!model) {
206206
return;
207207
}
208-
var modelTabIndex, activeElTabIndex;
209-
var target = Polymer.dom(e).path[0];
210-
var activeEl = Polymer.dom(this.domHost ? this.domHost.root : document).activeElement;
211-
var physicalItem = this._physicalItems[model[this.indexAs]];
208+
let modelTabIndex, activeElTabIndex;
209+
let target = Polymer.dom(e).path[0];
210+
let activeEl = Polymer.dom(this.domHost ? this.domHost.root : document).activeElement;
211+
let physicalItem = this._physicalItems[model[this.indexAs]];
212212
// Safari does not focus certain form controls via mouse
213213
// https://bugs.webkit.org/show_bug.cgi?id=118043
214214
if (target.localName === 'input' ||
@@ -296,15 +296,15 @@
296296

297297
_forwardItemPath(path, value) {
298298
path = path.slice(6); // 'items.'.length == 6
299-
var dot = path.indexOf('.') + 1;
299+
let dot = path.indexOf('.') + 1;
300300
if (dot === 0) {
301301
dot = path.length;
302302
}
303-
var idx = parseInt(path.substring(0, dot), 10);
304-
var offscreenItem = this._offscreenFocusedItem;
303+
let idx = parseInt(path.substring(0, dot), 10);
304+
let offscreenItem = this._offscreenFocusedItem;
305+
306+
let inst = this.modelForElement(this._physicalItems[idx]);
305307

306-
var inst = this.modelForElement(this._physicalItems[idx]);
307-
308308
if (!inst || inst[this.indexAs] !== idx) {
309309
return;
310310
}
@@ -320,10 +320,10 @@
320320
* @param {number} size Size of the pool
321321
*/
322322
_createPool(size) {
323-
var physicalItems = new Array(size);
323+
let physicalItems = new Array(size);
324324
this._ensureTemplatized();
325-
for (var i = 0; i < size; i++) {
326-
var inst = this.stamp(null);
325+
for (let i = 0; i < size; i++) {
326+
let inst = this.stamp(null);
327327
// First element child is item; Safari doesn't support children[0]
328328
// on a doc fragment.
329329
physicalItems[i] = inst.root.querySelector('*');
@@ -336,12 +336,10 @@
336336
* Increases the pool size.
337337
*/
338338
_increasePool(poolSize) {
339-
var ts = window.performance.now();
340339
// Concat arrays in place.
341340
[].push.apply(this._physicalItems, this._createPool(poolSize));
342341
this._physicalCount = this._physicalItems.length;
343342
this._update();
344-
this._templateCost = (window.performance.now() - ts);
345343
}
346344

347345
/**
@@ -351,7 +349,7 @@
351349
// Check if already templatized
352350
if (!this.ctor) {
353351
// Template instance props that should be excluded from forwarding
354-
var props = {};
352+
let props = {};
355353
props.__key__ = true;
356354
props[this.as] = true;
357355
props[this.indexAs] = true;
@@ -383,10 +381,10 @@
383381
* @param {!Array<number>=} itemSet
384382
*/
385383
_assignModels(itemSet) {
386-
var count = 0;
384+
let count = 0;
387385
this._physicalItems.forEach((el) => {
388-
var inst = this.modelForElement(el);
389-
var item = this.items[count];
386+
let inst = this.modelForElement(el);
387+
let item = this.items[count];
390388
if (item != null && item !== undefined) {
391389
//inst.__key__ = this._collection ? this._collection.getKey(item) : null;
392390
this._forwardProperty(inst, this.as, item);
@@ -419,7 +417,7 @@
419417

420418
_notifyInstancePropV2(inst, prop, value) {
421419
if (Polymer.Path.matches(this.as, prop)) {
422-
var idx = inst[this.indexAs];
420+
let idx = inst[this.indexAs];
423421
if (prop == this.as) {
424422
this.items[idx] = value;
425423
}
@@ -451,7 +449,7 @@
451449
this.clearSelection();
452450
}
453451

454-
var model = this.modelForElement(this._physicalItems[index]);
452+
let model = this.modelForElement(this._physicalItems[index]);
455453
if (model) {
456454
model[this.selectedAs] = true;
457455
}
@@ -494,7 +492,7 @@
494492
return;
495493
}
496494

497-
var model = this.modelForElement(this._physicalItems[index]);
495+
let model = this.modelForElement(this._physicalItems[index]);
498496
model[this.selectedAs] = false;
499497

500498
this.$.selector.deselectIndex(index);
@@ -519,7 +517,7 @@
519517
* @param {Object} index The index of the item in the items array.
520518
*/
521519
toggleSelectionForIndex(index) {
522-
var isSelected = this.$.selector.isIndexSelected
520+
let isSelected = this.$.selector.isIndexSelected
523521
? this.$.selector.isIndexSelected(index) : this.$.selector.isSelected(this.items[index]);
524522
isSelected ? this.deselectIndex(index) : this.selectIndex(index);
525523
}

exmg-thead.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,20 @@
141141
*/
142142
_sortedByChanged(sorted) {
143143
let headerColumns = this.shadowRoot.querySelectorAll('.th');
144-
145144
this._resetOldSorting();
146-
147145
headerColumns.forEach((el) => {
148146
if(el.getAttribute('sortable') === sorted) {
149147
el.setAttribute('sorted','');
150148
this.set('sortDirection', 'ASC');
151149
}
152150
});
153151
}
152+
154153
/*
155154
* Handle column header tap
156155
*/
157156
_handleTap(e) {
158157
let sortField = e.path[0].getAttribute("sortable");
159-
160158
if(e.path[0].classList.contains('th') && sortField){
161159
if(this.sorted === sortField){
162160
this.set('sortDirection', this.sortDirection === 'ASC' ? 'DESC' : 'ASC');
@@ -166,12 +164,12 @@
166164
}
167165
}
168166
}
167+
169168
/*
170169
* Reset old column sorting attributes
171170
*/
172171
_resetOldSorting(tr) {
173172
let rowChildren = this.shadowRoot.querySelectorAll('.th[sorted]');
174-
// let rowChildren = this.querySelectorAll('.th[sorted]');
175173
rowChildren.forEach(el => el.removeAttribute('sorted'));
176174
}
177175
}

exmg-toolbar.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
]
9393
}
9494
_selectedItemsChanged(changes) {
95-
console.log('_selectedItemsChanged', changes);
9695
if(changes.path === 'selectedItems') {
9796
this.set('_selectedRows', changes.value === null ? 0 : Array.isArray(changes.value) ? changes.value.length : 1);
9897
}

0 commit comments

Comments
 (0)