1 | import {Feature} from '../feature';
|
2 | import {addClass, removeClass} from '../dom';
|
3 | import {defaultsStr} from '../settings';
|
4 | import {bound} from '../event';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export class AlternateRows extends Feature {
|
10 |
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | constructor(tf) {
|
17 | super(tf, AlternateRows);
|
18 |
|
19 | let config = this.config;
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 | this.evenCss = defaultsStr(config.even_row_css_class, 'even');
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 | this.oddCss = defaultsStr(config.odd_row_css_class, 'odd');
|
32 | }
|
33 |
|
34 | |
35 |
|
36 |
|
37 | init() {
|
38 | if (this.initialized) {
|
39 | return;
|
40 | }
|
41 |
|
42 | this.processAll();
|
43 |
|
44 |
|
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 |
|
51 | this.initialized = true;
|
52 | }
|
53 |
|
54 | |
55 |
|
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 |
|
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 |
|
76 |
|
77 |
|
78 |
|
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 |
|
90 |
|
91 |
|
92 |
|
93 |
|
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 |
|
108 |
|
109 |
|
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 |
|
121 | processRowHandler(tf, rowIndex, arrIndex, isValid) {
|
122 | this.processRow(rowIndex, arrIndex, isValid);
|
123 | }
|
124 |
|
125 | |
126 |
|
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 |
|
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 | }
|