UNPKG

3.79 kBJavaScriptView Raw
1import {Feature} from '../feature';
2import {addClass, removeClass} from '../dom';
3import {defaultsStr} from '../settings';
4import {bound} from '../event';
5
6/**
7 * Rows with alternating background color for improved readability
8 */
9export class AlternateRows extends Feature {
10
11 /**
12 * Creates an instance of AlternateRows.
13 *
14 * @param {Object} tf TableFilter instance
15 */
16 constructor(tf) {
17 super(tf, AlternateRows);
18
19 let config = this.config;
20
21 /**
22 * Css class for even rows (default: 'even')
23 * @type {String}
24 */
25 this.evenCss = defaultsStr(config.even_row_css_class, 'even');
26
27 /**
28 * Css class for odd rows (default: 'odd')
29 * @type {String}
30 */
31 this.oddCss = defaultsStr(config.odd_row_css_class, 'odd');
32 }
33
34 /**
35 * Sets alternating rows color
36 */
37 init() {
38 if (this.initialized) {
39 return;
40 }
41
42 this.processAll();
43
44 // Subscribe to events
45 this.emitter.on(['row-processed', 'row-paged'],
46 bound(this.processRowHandler, this));
47 this.emitter.on(['column-sorted', 'rows-changed'],
48 bound(this.processAll, this));
49
50 /** @inherited */
51 this.initialized = true;
52 }
53
54 /**
55 * Apply background to all valid rows
56 */
57 processAll() {
58 if (!this.isEnabled()) {
59 return;
60 }
61 let tf = this.tf;
62 let validRowsIndex = tf.getValidRows(true);
63 let indexLen = validRowsIndex.length;
64 let idx = 0;
65
66 //alternates bg color
67 for (let j = 0; j < indexLen; j++) {
68 let rowIdx = validRowsIndex[j];
69 this.setRowBg(rowIdx, idx);
70 idx++;
71 }
72 }
73
74 /**
75 * Set/remove row background based on row validation
76 * @param {Number} rowIdx Row index
77 * @param {Number} arrIdx Array index
78 * @param {Boolean} isValid Valid row flag
79 */
80 processRow(rowIdx, arrIdx, isValid) {
81 if (isValid) {
82 this.setRowBg(rowIdx, arrIdx);
83 } else {
84 this.removeRowBg(rowIdx);
85 }
86 }
87
88 /**
89 * Sets row background color
90 * @param {Number} rowIdx Row index
91 * @param {Number} idx Valid rows collection index needed to calculate bg
92 * color
93 * @private
94 */
95 setRowBg(rowIdx, idx) {
96 if (!this.isEnabled() || isNaN(rowIdx)) {
97 return;
98 }
99 let rows = this.tf.dom().rows;
100 let i = isNaN(idx) ? rowIdx : idx;
101 this.removeRowBg(rowIdx);
102
103 addClass(rows[rowIdx], (i % 2) ? this.evenCss : this.oddCss);
104 }
105
106 /**
107 * Removes row background color
108 * @param {Number} idx Row index
109 * @private
110 */
111 removeRowBg(idx) {
112 if (isNaN(idx)) {
113 return;
114 }
115 let rows = this.tf.dom().rows;
116 removeClass(rows[idx], this.oddCss);
117 removeClass(rows[idx], this.evenCss);
118 }
119
120 /** @private */
121 processRowHandler(tf, rowIndex, arrIndex, isValid) {
122 this.processRow(rowIndex, arrIndex, isValid);
123 }
124
125 /**
126 * Removes all alternating backgrounds
127 */
128 destroy() {
129 if (!this.initialized) {
130 return;
131 }
132
133 let eachRow = this.tf.eachRow(0);
134 eachRow((row, i) => this.removeRowBg(i));
135
136 // Unsubscribe to events
137 this.emitter.off(['row-processed', 'row-paged'],
138 bound(this.processRowHandler, this));
139 this.emitter.off(['column-sorted', 'rows-changed'],
140 bound(this.processAll, this));
141
142 this.initialized = false;
143 }
144
145}