1 | import {Feature} from '../feature';
|
2 | import {createElm, createText, elm, removeElm} from '../dom';
|
3 | import {EMPTY_FN, isNull} from '../types';
|
4 | import {defaultsStr, defaultsFn} from '../settings';
|
5 | import {LEFT} from './toolbar';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | export class RowsCounter extends Feature {
|
14 |
|
15 | |
16 |
|
17 |
|
18 |
|
19 | constructor(tf) {
|
20 | super(tf, RowsCounter);
|
21 |
|
22 |
|
23 | let f = this.config.rows_counter || {};
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 | this.targetId = defaultsStr(f.target_id, null);
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 | this.container = null;
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 | this.label = null;
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 | this.text = defaultsStr(f.text, 'Rows: ');
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 | this.fromToTextSeparator = defaultsStr(f.separator, '-');
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | this.overText = defaultsStr(f.over_text, ' / ');
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 | this.cssClass = defaultsStr(f.css_class, 'tot');
|
71 |
|
72 | |
73 |
|
74 |
|
75 |
|
76 | this.toolbarPosition = defaultsStr(f.toolbar_position, LEFT);
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 | this.onBeforeRefreshCounter = defaultsFn(f.on_before_refresh_counter,
|
83 | EMPTY_FN);
|
84 |
|
85 | |
86 |
|
87 |
|
88 |
|
89 | this.onAfterRefreshCounter = defaultsFn(f.on_after_refresh_counter,
|
90 | EMPTY_FN);
|
91 | }
|
92 |
|
93 | |
94 |
|
95 |
|
96 | init() {
|
97 | if (this.initialized) {
|
98 | return;
|
99 | }
|
100 |
|
101 | this.emitter.emit('initializing-feature', this, !isNull(this.targetId));
|
102 |
|
103 | let tf = this.tf;
|
104 |
|
105 |
|
106 | let countDiv = createElm('div');
|
107 | countDiv.className = this.cssClass;
|
108 |
|
109 | let countSpan = createElm('span');
|
110 | let countText = createElm('span');
|
111 | countText.appendChild(createText(this.text));
|
112 |
|
113 |
|
114 | let targetEl = !this.targetId ?
|
115 | tf.feature('toolbar').container(this.toolbarPosition) :
|
116 | elm(this.targetId);
|
117 |
|
118 |
|
119 | if (!this.targetId) {
|
120 | countDiv.appendChild(countText);
|
121 | countDiv.appendChild(countSpan);
|
122 | targetEl.appendChild(countDiv);
|
123 | } else {
|
124 |
|
125 | targetEl.appendChild(countText);
|
126 | targetEl.appendChild(countSpan);
|
127 | }
|
128 | this.container = countDiv;
|
129 | this.label = countSpan;
|
130 |
|
131 |
|
132 | this.emitter.on(['after-filtering', 'grouped-by-page'],
|
133 | () => this.refresh(tf.getValidRowsNb()));
|
134 | this.emitter.on(['rows-changed'], () => this.refresh());
|
135 |
|
136 |
|
137 | this.initialized = true;
|
138 | this.refresh();
|
139 |
|
140 | this.emitter.emit('feature-initialized', this);
|
141 | }
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | refresh(p) {
|
148 | if (!this.initialized || !this.isEnabled()) {
|
149 | return;
|
150 | }
|
151 |
|
152 | let tf = this.tf;
|
153 |
|
154 | this.onBeforeRefreshCounter(tf, this.label);
|
155 |
|
156 | let totTxt;
|
157 | if (!tf.paging) {
|
158 | if (p && p !== '') {
|
159 | totTxt = p;
|
160 | } else {
|
161 | totTxt = tf.getFilterableRowsNb() - tf.nbHiddenRows;
|
162 | }
|
163 | } else {
|
164 | let paging = tf.feature('paging');
|
165 | if (paging) {
|
166 | let nbValidRows = tf.getValidRowsNb();
|
167 |
|
168 | let pagingStartRow = parseInt(paging.startPagingRow, 10) +
|
169 | ((nbValidRows > 0) ? 1 : 0);
|
170 | let pagingEndRow =
|
171 | (pagingStartRow + paging.pageLength) - 1 <=
|
172 | nbValidRows ?
|
173 | pagingStartRow + paging.pageLength - 1 :
|
174 | nbValidRows;
|
175 | totTxt = pagingStartRow + this.fromToTextSeparator +
|
176 | pagingEndRow + this.overText + nbValidRows;
|
177 | }
|
178 | }
|
179 |
|
180 | this.label.innerHTML = totTxt;
|
181 | this.onAfterRefreshCounter(tf, this.label, totTxt);
|
182 | }
|
183 |
|
184 | |
185 |
|
186 |
|
187 | destroy() {
|
188 | if (!this.initialized) {
|
189 | return;
|
190 | }
|
191 |
|
192 | if (!this.targetId && this.container) {
|
193 | removeElm(this.container);
|
194 | } else {
|
195 | elm(this.targetId).innerHTML = '';
|
196 | }
|
197 | this.label = null;
|
198 | this.container = null;
|
199 |
|
200 |
|
201 | this.emitter.off(['after-filtering', 'grouped-by-page'],
|
202 | () => this.refresh(tf.getValidRowsNb()));
|
203 | this.emitter.off(['rows-changed'], () => this.refresh());
|
204 |
|
205 | this.initialized = false;
|
206 | }
|
207 | }
|