forked from Neovici/cosmoz-omnitable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmoz-omnitable.js
1223 lines (1037 loc) · 29.4 KB
/
cosmoz-omnitable.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*global Cosmoz, Polymer, window, saveAs, NullXlsx */
(function () {
'use strict';
const PROPERTY_HASH_PARAMS = ['sortOn', 'groupOn', 'descending', 'groupOnDescending'];
Polymer({
is: 'cosmoz-omnitable',
properties: {
/**
* Filename when saving as CSV
*/
csvFilename: {
type: String,
value: 'omnitable.csv'
},
/**
* Filename when saving as XLSX
*/
xlsxFilename: {
type: String,
value: 'omnitable.xlsx'
},
/**
* Sheet name when saving as XLSX
*/
xlsxSheetname: {
type: String,
value: 'Omnitable'
},
/**
* Array used to list items.
*/
data: {
type: Array
},
/**
* True if data is a valid and not empty array.
*/
_dataIsValid: {
type: Boolean,
value: false,
computed: '_computeDataValidity(data)'
},
/**
* If set to true, then group a row will be displayed for groups that contain no items.
*/
displayEmptyGroups: {
type: Boolean,
value: false
},
/**
* Specific columns to enable
*/
enabledColumns: {
type: Array,
observer: '_debounceUpdateColumns'
},
/**
* Whether bottom-bar has actions.
*/
hasActions: {
type: Boolean,
value: false
},
/**
* Shows a loading overlay to indicate data will be updated
*/
loading: {
type: Boolean,
value: false
},
/**
* Whether to show checkboxes to perform bottom-bar actions on
*/
_showCheckboxes: {
type: Boolean,
computed: '_computeShowCheckboxes(_dataIsValid, hasActions)'
},
/**
* List of selected rows/items in `data`.
*/
selectedItems: {
type: Array,
notify: true
},
highlightedItems: {
type: Array,
notify: true
},
descending: {
type: Boolean,
value: false
},
sortOn: {
type: String,
value: ''
},
sortOnColumn: {
type: Object,
computed: '_getColumn(sortOn, "name", columns)'
},
groupOnDescending: {
type: Boolean,
value: false,
observer: '_debounceGroupItems'
},
/**
* The column name to group on.
*/
groupOn: {
type: String,
notify: true,
value: ''
},
/**
* The column that matches the current `groupOn` value.
*/
groupOnColumn: {
type: Object,
notify: true,
observer: '_groupOnColumnChanged',
computed: '_getColumn(groupOn, "name", columns)'
},
/**
* Items matching current set filter(s)
*/
filteredItems: {
type: Array,
observer: '_debounceGroupItems'
},
/**
* Grouped items structure after filtering.
*/
filteredGroupedItems: {
type: Array
},
/**
* Sorted items structure after filtering and grouping.
*/
sortedFilteredGroupedItems: {
type: Array,
notify: true
},
/**
* Keep track of width-changes to identify if we go bigger or smaller
*/
_previousWidth: {
type: Number,
value: 0
},
_groupsCount: {
type: Number,
value: 0
},
/**
* List of columns definition for this table.
*/
columns: {
type: Array,
notify: true
},
visible: {
type: Boolean,
notify: true,
readOnly: true,
value: false,
observer: 'visibleChanged'
},
/**
* List of <b>visible</b> columns.
*/
visibleColumns: {
type: Array,
notify: true,
observer: '_visibleColumnsChanged'
},
disabledColumns: {
type: Array,
notify: true
},
_filterIsTooStrict: {
type: Boolean,
computed: '_computeFilterIsTooStrict(_dataIsValid, sortedFilteredGroupedItems.length)'
},
hashParam: {
type: String
},
_routeHash: {
type: Object
},
_routeHashKeyRule: {
type: RegExp,
computed: '_computeRouteHashKeyRule(hashParam)'
},
/**
* True when all items are selected.
*/
_allSelected: {
type: Boolean
}
},
observers: [
'_dataChanged(data.*)',
'_debounceSortItems(sortOn, descending, filteredGroupedItems)',
' _selectedItemsChanged(selectedItems.*)'
],
behaviors: [
Polymer.IronResizableBehavior,
Cosmoz.TranslatableBehavior
],
listeners: {
'iron-resize': '_onResize',
'update-item-size': '_onUpdateItemSize',
'cosmoz-column-title-changed': '_onColumnTitleChanged',
'cosmoz-column-filter-changed': '_filterChanged',
},
attached() {
/** WARNING: we do not support columns changes yet. */
// `isOmnitableColumn` is a property from cosmoz-omnitable-column-behavior
this._columnObserver = Polymer.dom(this).observeNodes(info => {
const colFilter = child => child.nodeType === Node.ELEMENT_NODE && child.isOmnitableColumn,
addedColumns = info.addedNodes.filter(colFilter),
removedColumns = info.removedNodes.filter(colFilter),
changedColumns = addedColumns.concat(removedColumns);
if (changedColumns.length === 0) {
return;
}
this._setColumnValues(addedColumns);
this._debounceUpdateColumns();
});
this.$.groupedList.scrollTarget = this.$.scroller;
this.listen(this, 'cosmoz-column-hidden-changed', '_debounceUpdateColumns');
},
detached: function () {
if (this._columnObserver) {
Polymer.dom(this).unobserveNodes(this._columnObserver);
}
this.unlisten(this, 'cosmoz-column-hidden-changed', '_debounceUpdateColumns');
// Just in case we get detached before a planned debouncer has not run yet.
this.cancelDebouncer('adjustColumns');
this.cancelDebouncer('updateColumns');
this.cancelDebouncer('filterItems');
this.cancelDebouncer('sortItems');
},
/** ELEMENT BEHAVIOR */
_disabledColumnsIndexes: null,
_computeDataValidity(data) {
return data && Array.isArray(data) && data.length > 0;
},
_computeFilterIsTooStrict(dataIsValid, visibleItemsLength) {
return dataIsValid && visibleItemsLength < 1;
},
_computeSortDirection(descending) {
var direction = descending ? this._('Descending') : this._('Ascending');
return `(${direction})`;
},
_computeShowCheckboxes(dataIsValid, hasActions) {
return dataIsValid && hasActions;
},
visibleChanged(turnedVisible) {
if (turnedVisible && !Array.isArray(this.columns)) {
this._debounceUpdateColumns();
}
},
_visibleColumnsChanged() {
this.disabledColumns = [];
this._disabledColumnsIndexes = [];
},
_onUpdateItemSize: function (event, detail) {
if (detail && detail.item) {
this.$.groupedList.updateSize(detail.item);
}
event.stopPropagation();
},
_onColumnTitleChanged: function (event) {
var column = event.target,
columnIndex;
event.stopPropagation();
if (!Array.isArray(this.columns)) {
return;
}
columnIndex = this.columns.indexOf(column);
// re-notify column change to make dom-repeat re-render menu item title
this.notifyPath(['columns', columnIndex, 'title']);
if (column === this.groupOnColumn) {
this.notifyPath(['groupOnColumn', 'title']);
}
},
// Handle selection/deselection of a group
_onGroupCheckboxChange: function (event) {
var
group = event.model.item,
selected = this.$.groupedList.isGroupSelected(group);
if (selected) {
this.$.groupedList.deselectGroup(group);
} else {
this.$.groupedList.selectGroup(group);
}
event.preventDefault();
event.stopPropagation();
},
// Handle selection/deselection of an item
_onItemCheckboxChange: function (event) {
var item = event.model.item;
if (this.isItemSelected(item)) {
this.deselectItem(item);
} else {
this.selectItem(item);
}
event.preventDefault();
event.stopPropagation();
},
_itemRowTapped(event) {
var item = event.model.item;
this.highlight(item, this.isItemHighlighted(item));
},
_onResize() {
this._setVisible(this.offsetParent != null);
this._debounceAdjustColumns();
},
_dataChanged() {
if (!Array.isArray(this.columns)) {
return;
}
this._setColumnValues();
this._debounceFilterItems();
},
_debounceUpdateColumns() {
this.debounce('updateColumns', this._updateColumns, 10);
},
_updateColumns() {
if (!this.isAttached) {
return;
}
this._setVisible(this.offsetParent != null);
if (!this.visible) {
return;
}
let columns = this.getEffectiveChildren().filter((child, index) => {
child.__index = index;
return child.nodeType === Node.ELEMENT_NODE && child.isOmnitableColumn && !child.hidden;
}),
valuePathNames;
const columnNames = columns.map(c => c.name);
if (Array.isArray(this.enabledColumns)) {
columns = columns.filter(column =>
this.enabledColumns.indexOf(column.name) !== -1
);
} else {
columns = columns.filter(column => !column.disabled);
}
if (!columns || columns.length === 0) {
return;
}
this._verifyColumnSetup(columns, columnNames);
columns.forEach((column, index) => {
if (!column.name) {
// No name set; Try to set name attribute via valuePath
if (!valuePathNames) {
valuePathNames = columns.map(c => c.valuePath);
}
const hasUniqueValuePath = valuePathNames.indexOf(column.valuePath) === valuePathNames.lastIndexOf(column.valuePath);
if (hasUniqueValuePath && columnNames.indexOf(column.valuePath) === -1) {
column.name = column.valuePath;
}
}
column.columnIndex = index;
});
if (!Array.isArray(this.columns) || this.columns.length === 0) {
this._setColumnValues(columns);
}
this.columns = columns;
this.visibleColumns = columns.slice();
this._updateParamsFromHash();
if (Array.isArray(this.data)) {
this._debounceFilterItems();
}
},
/**
* Checks if the column setup is valid and logs errors.
* As a separate functions to make testing easier.
* @param {any} columns The columns.
* @param {any} columnNames The column names.
* @returns {Boolean} True if setup is valid.
*/
_verifyColumnSetup(columns, columnNames = columns.map(c => c.name)) {
// Check if column names are set and unique
var columnsMissingNameAttribute = columns
.filter(column => {
var name = column.name;
if (!name) {
console.error('The name attribute needs to be set on all columns! Missing on column', column.title, column);
return;
}
return columnNames.indexOf(name) !== columnNames.lastIndexOf(name);
});
columnsMissingNameAttribute.forEach(column => {
console.error('The name attribute needs to be unique among all columns! Not unique on column', column.title, column);
});
return columnsMissingNameAttribute.length === 0;
},
// TODO: provides a mean to avoid setting the values for a column
// TODO: should process (distinct, sort, min, max) the values at the column level depending on the column type
_setColumnValues: function (columns = this.columns) {
if (!Array.isArray(this.data) || this.data.length < 1 || !Array.isArray(columns) || columns.length < 1) {
return;
}
columns.forEach(column => {
if (!column.bindValues || column.externalValues) {
return;
}
if (!column.valuePath) {
console.error('value path is not defined for column', column, 'with bindValues');
return;
}
column.set('values', this.data
.map(item => this.get(column.valuePath, item))
.filter((value, index, self) =>
value != null && self.indexOf(value) === index
)
);
});
},
/*
* Returns a column based on an attribute.
* @param {String} attributeValue The value of the column attribute.
* @param {String} attribute The attribute name of the column.
* @returns {Object} The found column.
*/
_getColumn(attributeValue, attribute = 'name', columns = this.columns) {
if (!attributeValue || !columns) {
return;
}
const column = columns.find(column => column[attribute] === attributeValue);
if (!column) {
console.warn(`Cannot find column with ${attribute} ${attributeValue}`);
}
return column;
},
_filterChanged: function (e, detail) {
if (!Array.isArray(this.columns) || this.columns.length < 1 || this.columns.indexOf(detail.column) < 0) {
return;
}
this._debounceFilterItems();
this._filterForRouteChanged(detail.column);
},
_debounceFilterItems: function () {
this.debounce('filterItems', this._filterItems);
},
_filterItems: function () {
if (Array.isArray(this.data) && this.data.length > 0 && Array.isArray(this.columns)) {
// Call filtering code only on columns that has a filter
const filterFunctions = this.columns
.map(col => col.getFilterFn())
.filter(fn => fn !== undefined);
if (filterFunctions.length) {
this.filteredItems = this.data.filter(item =>
filterFunctions.every(filterFn => filterFn(item))
);
} else {
this.filteredItems = this.data.slice();
}
} else {
this.filteredItems = [];
this.filteredGroupedItems = [];
this.sortedFilteredGroupedItems = [];
this._groupsCount = 0;
}
},
_groupOnColumnChanged: function (column) {
if (column && column.hasFilter()) {
column.resetFilter();
} else {
this.debounce('groupItems', this._groupItems);
}
},
_debounceGroupItems: function () {
if (!this.isAttached || !this.filteredItems) {
return;
}
this.debounce('groupItems', this._groupItems);
},
_groupItems: function () {
this._updateRouteParam('groupOn');
if (!this.filteredItems || this.filteredItems.length === 0) {
this.filteredGroupedItems = [];
this.sortedFilteredGroupedItems = [];
this._groupsCount = 0;
return;
}
var groupOnColumn = this.groupOnColumn,
groups;
if (!groupOnColumn || !groupOnColumn.groupOn) {
this.filteredGroupedItems = this.filteredItems;
this._groupsCount = 0;
return;
}
groups = this.filteredItems.reduce((array, item) => {
var gval = groupOnColumn.getComparableValue(item, groupOnColumn.groupOn),
group;
if (gval === undefined) {
return array;
}
group = array.find(g => g.id === gval);
if (!group) {
group = { id: gval, name: gval, items: [] };
array.push(group);
}
group.items.push(item);
return array;
}, []);
groups.sort(function (a, b) {
var
v1 = groupOnColumn.getComparableValue(a.items[0], groupOnColumn.groupOn),
v2 = groupOnColumn.getComparableValue(b.items[0], groupOnColumn.groupOn);
if (typeof v1 === 'object' && typeof v2 === 'object') {
// HACK(pasleq): worst case, compare using values converted to string
v1 = v1.toString();
v2 = v2.toString();
}
if (typeof v1 === 'number' && typeof v2 === 'number') {
return v1 - v2;
}
if (typeof v1 === 'string' && typeof v2 === 'string') {
return v1 < v2 ? -1 : 1;
}
if (typeof v1 === 'boolean' && typeof v2 === 'boolean') {
if (v1 === v2) {
return 0;
}
return v1 ? -1 : 1;
}
return 0;
});
if (this.groupOnDescending) {
groups.reverse();
}
this._groupsCount = groups.length;
this.filteredGroupedItems = groups;
},
_debounceSortItems: function () {
if (!Array.isArray(this.data) || this.data.length < 1 || !Array.isArray(this.columns)) {
return;
}
this.debounce('sortItems', this._sortFilteredGroupedItems);
},
/**
* Sorting method, can be overridden
* @param {*} a First compare value
* @param {*} b Second compare value
* @returns {void}
*/
sorter(a, b) {
const v1 = this.sortOnColumn.getComparableValue(a, this.sortOnColumn.sortOn),
v2 = this.sortOnColumn.getComparableValue(b, this.sortOnColumn.sortOn);
if (v1 === v2) {
return 0;
}
if (v1 === undefined) {
return -1;
}
if (v2 === undefined) {
return 1;
}
if (typeof v1 === 'number' && typeof v2 === 'number') {
return v1 - v2;
}
if (typeof v1 === 'string' && typeof v2 === 'string') {
return v1 < v2 ? -1 : 1;
}
if (typeof v1 === 'boolean' && typeof v2 === 'boolean') {
if (v1 === v2) {
return 0;
}
return v1 ? -1 : 1;
}
console.warn('unsupported sort', typeof v1, v1, typeof v2, v2);
return 0;
},
_sortFilteredGroupedItems: function () {
if (!this.filteredGroupedItems) {
return;
}
this._updateRouteParam('sortOn');
this._updateRouteParam('descending');
this._updateRouteParam('groupOnDescending');
if (!this.sortOn || !this.sortOnColumn) {
this.sortedFilteredGroupedItems = this.filteredGroupedItems;
this._debounceAdjustColumns();
return;
}
const sorter = this.sorter.bind(this);
if (this._groupsCount > 0) {
this.set('sortedFilteredGroupedItems', this.filteredGroupedItems
.filter(group => Array.isArray(group.items))
.map(group => {
group.items.sort(sorter);
if (this.descending) {
group.items.reverse();
}
return {
name: group.name,
id: group.id,
items: group.items
};
}));
this._debounceAdjustColumns();
return;
}
// No grouping
this.filteredGroupedItems.sort(sorter);
if (this.descending) {
this.filteredGroupedItems.reverse();
}
this.set('sortedFilteredGroupedItems', this.filteredGroupedItems.slice());
this._debounceAdjustColumns();
},
_debounceAdjustColumns: function () {
// 16ms 'magic' number copied from iron-list
// But this makes headers change width after the table has completed rendering,
// which might look strange.
this.debounce('adjustColumns', this._adjustColumns, 16);
},
/**
* Enable/disable columns to properly fit in the available space.
* Adjust headers width according to cells width
* @memberOf element/cz-omnitable
* @returns {Boolean} Return
*/
_adjustColumns() {
// Safety check, but should never happen
if (!this.isAttached || !this.visible) {
return;
}
const firstRow = this.$.groupedList.getFirstVisibleItemElement(),
visibleData = this.sortedFilteredGroupedItems,
hasVisibleData = Array.isArray(visibleData) && visibleData.length > 0;
if (!hasVisibleData || !firstRow && this.$.groupedList.hasRenderedData) {
// reset headers width
const headerRow = Polymer.dom(this.$.header).querySelector('cosmoz-omnitable-header-row');
Array
.from(Polymer.dom(headerRow).children)
.forEach(header => {
header.style.minWidth = 'auto';
header.style.maxWidth = 'none';
header.style.width = 'auto';
});
return;
}
if (!firstRow) {
// There is visible data, but nothing rendered in cosmoz-grouped-list yet.
// Retry later.
this._debounceAdjustColumns();
return;
}
var scroller = this.$.scroller,
currentWidth = this.$.tableContent.clientWidth,
itemRow = Polymer.dom(firstRow).querySelector('cosmoz-omnitable-item-row'),
cells = Array.from(Polymer.dom(itemRow).children);
let fits = scroller.scrollWidth <= scroller.clientWidth;
if (fits) {
fits = cells.every(cell =>
cell.__column.overflow || cell.scrollWidth <= cell.clientWidth
);
}
if (fits) {
if (this._canScaleUp(currentWidth)) {
this._enableColumn();
return;
}
} else {
this._overflowConfig = {
columns: this.visibleColumns.length,
width: currentWidth
};
this._disableColumn();
return;
}
this._adjustHeadersWidth(cells);
},
_adjustHeadersWidth(cells) {
const headerRow = Polymer.dom(this.$.header).querySelector('cosmoz-omnitable-header-row'),
headers = Array.from(Polymer.dom(headerRow).children);
cells.forEach((cell, index) => {
const header = headers[index];
// disabled column headers
if (header === undefined) {
return;
}
let width = getComputedStyle(cell).getPropertyValue('width');
header.style.minWidth = width;
header.style.maxWidth = width === 'auto' ? 'none' : width;
header.style.width = width;
});
},
_canScaleUp: function (width) {
if (!this.disabledColumns || this.disabledColumns.length === 0) {
return false;
}
if (!this._overflowConfig) {
return true;
}
if (width > this._overflowConfig.width) {
return true;
}
if (this.visibleColumns.length + 1 < this._overflowConfig.columns) {
return true;
}
return false;
},
_disableColumn: function () {
var disabledColumn,
disabledColumnIndex;
// disables/hides columns that for example does not fit in the current screen size.
this.visibleColumns.forEach(function (column, index) {
if (disabledColumn === undefined || disabledColumn.priority >= column.priority) {
disabledColumn = column;
disabledColumnIndex = index;
}
});
if (disabledColumn) {
this.push('disabledColumns', disabledColumn);
this._disabledColumnsIndexes.push(disabledColumnIndex);
this.splice('visibleColumns', disabledColumnIndex, 1);
this._debounceAdjustColumns();
}
},
_enableColumn: function () {
// Columns are disabled by priority, so we can re-enable them
var column = this.pop('disabledColumns'),
columnIndex = this._disabledColumnsIndexes.pop();
this.splice('visibleColumns', columnIndex, 0, column);
this._debounceAdjustColumns();
},
//TODO: Use cosmoz-behaviors
/**
* Helper method for Polymer 1.0+ templates - check if variable
* is undefined, null, empty Array list or empty String.
* @param {Object} obj variable
* @return {Boolean} true if "empty", false otherwise
* ^memberOf element/cz-omnitable
*/
_isEmpty: function (obj) {
if (obj === undefined || obj === null) {
return true;
}
if (obj instanceof Array && obj.length === 0) {
return true;
}
var objType = typeof obj;
if (objType === 'string' && obj.length === 0) {
return true;
}
if (objType === 'number' && obj === 0) {
return true;
}
return false;
},
_makeCsvField: function (str) {
var result = str.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0) {
return '"' + result + '"';
}
return str;
},
/**
* Triggers a download of selected rows as a CSV file.
* @returns {undefined}
*/
_saveAsCsvAction: function () {
var separator = ';',
lf = '\n',
header = this.columns.map(col => this._makeCsvField(col.title)).join(separator) + lf,
rows = this.selectedItems.map(item => {
return this.columns.map(column => {
const cell = column.getString(item);
if (cell === undefined || cell === null) {
return '';
}
return this._makeCsvField(String(cell));
}).join(separator) + lf;
});
rows.unshift(header);
saveAs(new File(rows, this.csvFilename, {
type: 'text/csv;charset=utf-8'
}));
},
/**
* Makes the data ready to be exported as XLSX.
* @returns {Array} data Array of prepared rows.
*/
_prepareXlsxData: function () {
var headers = this.columns.map(col => col.title),
data = this.selectedItems.map(item => {
return this.columns.map(column => {
var value = column.toXlsxValue(item);
if (value === undefined || value === null) {
return '';
}
return value;
});
});
data.unshift(headers);
return data;
},
/**
* Triggers a download of selected rows as a XLSX file.
* @param {Object} data The prepared rows to be saved as file with default value this._prepareXlsxData().
* @returns {undefined}
*/
_saveAsXlsxAction: function () {
var data = this._prepareXlsxData(),
xlsx = new NullXlsx(this.xlsxFilename).addSheetFromData(data, this.xlsxSheetname).generate();
saveAs(new File([xlsx], this.xlsxFilename,
{ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}
));
},
/** view functions */
_getGroupRowClasses: function (folded) {
return folded ? 'groupRow groupRow-folded' : 'groupRow';
},
_getFoldIcon: function (expanded) {
return expanded ? 'expand-less' : 'expand-more';
},
/**
* Called if an item from the sortOn dropdown gets tapped.
* Reverses the descending value if the sortOn value did not change.
* @param {Event} e The event with the column model.
* @returns {undefined}
*/
_applySortingDirection(e) {
const column = e.model.column,
data = Polymer.dom(e).localTarget.dataset,
isGroup = data.groupOn != null,
compareTo = isGroup ? this.groupOnColumn : this.sortOnColumn,
property = isGroup ? 'groupOnDescending' : 'descending';
if (column === compareTo) {
this.set(property, !this.get(property));
return;
}
this.set(property, false);
},
/**
* Toggle folding of a group
* @param {Event} event event
* @returns {undefined}