tablefilter 0.7.2 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (280) hide show
  1. package/.editorconfig +15 -15
  2. package/.esdoc.json +14 -14
  3. package/.eslintrc +37 -37
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
  5. package/.github/ISSUE_TEMPLATE/feature_request.md +36 -36
  6. package/.github/ISSUE_TEMPLATE/support.md +17 -17
  7. package/.github/dependabot.yml +10 -0
  8. package/.github/workflows/build-deploy.yml +113 -0
  9. package/AGENTS.md +36 -0
  10. package/CONTRIBUTING.md +128 -128
  11. package/Gruntfile.js +252 -252
  12. package/LICENSE +21 -21
  13. package/PULL_REQUEST_TEMPLATE.md +24 -24
  14. package/README.md +166 -174
  15. package/ROADMAP/README.md +29 -0
  16. package/ROADMAP/hybrid-migration-strategy.md +234 -0
  17. package/ROADMAP/key-decisions-review.md +213 -0
  18. package/ROADMAP/preliminary-and-milestone-0.8.0-plan.md +308 -0
  19. package/ROADMAP/v1-webcomponents-investigation.md +516 -0
  20. package/dist/starter.html +514 -514
  21. package/dist/tablefilter/tablefilter.js +1 -1
  22. package/dist/tablefilter/tf-1-d2360db09a424b1ed633.js +1 -0
  23. package/index.js +3 -3
  24. package/libs/sortabletable.js +438 -438
  25. package/package.json +79 -79
  26. package/src/array.js +22 -22
  27. package/src/const.js +118 -118
  28. package/src/cookie.js +57 -57
  29. package/src/dom.js +192 -192
  30. package/src/emitter.js +53 -53
  31. package/src/event.js +124 -124
  32. package/src/extensions/advancedGrid/adapterEzEditTable.js +509 -509
  33. package/src/extensions/advancedGrid/advancedGrid.js +2 -2
  34. package/src/extensions/colOps/colOps.js +474 -480
  35. package/src/extensions/colsVisibility/colsVisibility.js +725 -725
  36. package/src/extensions/filtersVisibility/filtersVisibility.js +316 -316
  37. package/src/extensions/sort/adapterSortabletable.js +537 -537
  38. package/src/extensions/sort/sort.js +8 -8
  39. package/src/feature.js +102 -102
  40. package/src/modules/alternateRows.js +145 -145
  41. package/src/modules/baseDropdown.js +160 -160
  42. package/src/modules/checkList.js +519 -519
  43. package/src/modules/clearButton.js +142 -142
  44. package/src/modules/dateType.js +157 -157
  45. package/src/modules/dropdown.js +376 -376
  46. package/src/modules/gridLayout.js +483 -483
  47. package/src/modules/hash.js +126 -126
  48. package/src/modules/help.js +264 -264
  49. package/src/modules/highlightKeywords.js +180 -180
  50. package/src/modules/loader.js +183 -183
  51. package/src/modules/markActiveColumns.js +146 -146
  52. package/src/modules/noResults.js +193 -193
  53. package/src/modules/paging.js +916 -916
  54. package/src/modules/popupFilter.js +461 -461
  55. package/src/modules/rowsCounter.js +207 -207
  56. package/src/modules/state.js +545 -545
  57. package/src/modules/statusBar.js +308 -308
  58. package/src/modules/storage.js +161 -161
  59. package/src/modules/toolbar.js +224 -224
  60. package/src/number.js +96 -32
  61. package/src/root.js +6 -6
  62. package/src/settings.js +53 -53
  63. package/src/sort.js +82 -82
  64. package/src/string.js +112 -112
  65. package/src/tablefilter.js +3053 -3053
  66. package/src/types.js +81 -81
  67. package/static/partials/books-table-with-images.html +327 -327
  68. package/static/partials/books.html +272 -272
  69. package/static/partials/countries-by-continent.html +2592 -2592
  70. package/static/partials/countries-table-tiny.html +436 -436
  71. package/static/partials/countries-table.html +12954 -12954
  72. package/static/partials/dummy-table-with-totals-footer.html +353 -353
  73. package/static/partials/dummy-table.html +502 -502
  74. package/static/partials/pre-inline-script.html +17 -17
  75. package/static/partials/pre.html +1 -1
  76. package/static/partials/style.html +22 -22
  77. package/static/partials/table-with-grouped-headers.html +9258 -9258
  78. package/static/style/active-header.styl +10 -10
  79. package/static/style/alternating-rows.styl +8 -8
  80. package/static/style/config.styl +44 -44
  81. package/static/style/extensions/colsVisibility.styl +71 -71
  82. package/static/style/extensions/filtersVisibility.styl +18 -18
  83. package/static/style/ezedittable.styl +93 -93
  84. package/static/style/filter-checklist.styl +52 -52
  85. package/static/style/filters.styl +111 -111
  86. package/static/style/grid-layout.styl +130 -130
  87. package/static/style/help.styl +57 -57
  88. package/static/style/keyword.styl +8 -8
  89. package/static/style/loader.styl +16 -16
  90. package/static/style/mixins/arrow.styl +63 -63
  91. package/static/style/mixins/box-shadow.styl +8 -8
  92. package/static/style/mixins/empty-content.styl +7 -7
  93. package/static/style/noresults.styl +14 -14
  94. package/static/style/paging.styl +73 -73
  95. package/static/style/reset-button.styl +21 -21
  96. package/static/style/rows-counter.styl +17 -17
  97. package/static/style/sort.styl +20 -20
  98. package/static/style/status-bar.styl +16 -16
  99. package/static/style/table.styl +44 -44
  100. package/static/style/tablefilter.styl +18 -18
  101. package/static/style/themes/default/default.styl +137 -137
  102. package/static/style/themes/mytheme/mytheme.styl +152 -152
  103. package/static/style/themes/skyblue/skyblue.styl +195 -195
  104. package/static/style/themes/transparent/transparent.styl +205 -205
  105. package/static/style/toolbar.styl +58 -58
  106. package/static/templates/advanced-grid-editable.html +140 -140
  107. package/static/templates/advanced-grid-selection.html +93 -93
  108. package/static/templates/auto-filter.html +46 -46
  109. package/static/templates/columns-visibility.html +67 -67
  110. package/static/templates/data-types.html +123 -123
  111. package/static/templates/demo.html +62 -62
  112. package/static/templates/external-filters-visibility-commad.html +49 -49
  113. package/static/templates/external-filters.html +125 -125
  114. package/static/templates/filter-images.html +91 -91
  115. package/static/templates/filters-visibility.html +45 -45
  116. package/static/templates/grid-layout.html +51 -51
  117. package/static/templates/grouped-headers.html +78 -78
  118. package/static/templates/linked-filters-disabled.html +56 -56
  119. package/static/templates/linked-filters.html +45 -45
  120. package/static/templates/load-filters-on-demand.html +42 -42
  121. package/static/templates/pagination.html +78 -78
  122. package/static/templates/requirejs.html +53 -53
  123. package/static/templates/starter.html +61 -61
  124. package/static/templates/theme-roller.html +106 -106
  125. package/test/libs/polyfill.js +359 -359
  126. package/test/libs/qunit/qunit.css +237 -237
  127. package/test/libs/qunit/qunit.js +2495 -2495
  128. package/test/test-active-columns.html +80 -80
  129. package/test/test-active-columns.js +318 -318
  130. package/test/test-alternate-rows.html +80 -80
  131. package/test/test-alternate-rows.js +335 -335
  132. package/test/test-api.html +141 -141
  133. package/test/test-api.js +795 -795
  134. package/test/test-auto-filter.html +80 -80
  135. package/test/test-auto-filter.js +43 -43
  136. package/test/test-callbacks.html +77 -77
  137. package/test/test-callbacks.js +58 -58
  138. package/test/test-case-sensitive-query.html +77 -77
  139. package/test/test-case-sensitive-query.js +39 -39
  140. package/test/test-cell-parser.html +109 -109
  141. package/test/test-cell-parser.js +80 -80
  142. package/test/test-checklist.html +77 -77
  143. package/test/test-checklist.js +267 -267
  144. package/test/test-clear-button.html +79 -79
  145. package/test/test-clear-button.js +182 -182
  146. package/test/test-col-ops-grid-layout.html +129 -129
  147. package/test/test-col-ops-grid-layout.js +134 -134
  148. package/test/test-col-ops-paging.html +129 -129
  149. package/test/test-col-ops-paging.js +154 -154
  150. package/test/test-col-ops.html +129 -129
  151. package/test/test-col-ops.js +337 -202
  152. package/test/test-col-widths.html +80 -80
  153. package/test/test-col-widths.js +40 -40
  154. package/test/test-cols-visibility-grid-layout.html +80 -80
  155. package/test/test-cols-visibility-grid-layout.js +165 -165
  156. package/test/test-cols-visibility.html +80 -80
  157. package/test/test-cols-visibility.js +121 -121
  158. package/test/test-custom-options.html +80 -80
  159. package/test/test-custom-options.js +139 -139
  160. package/test/test-data-types.html +377 -377
  161. package/test/test-data-types.js +392 -392
  162. package/test/test-date-type.html +356 -356
  163. package/test/test-date-type.js +159 -159
  164. package/test/test-disable-enter-key.html +77 -77
  165. package/test/test-disable-enter-key.js +29 -29
  166. package/test/test-disable-filter-onchange.html +77 -77
  167. package/test/test-disable-filter-onchange.js +40 -40
  168. package/test/test-dom-changes.html +77 -77
  169. package/test/test-dom-changes.js +166 -166
  170. package/test/test-dropdown.html +77 -77
  171. package/test/test-dropdown.js +246 -246
  172. package/test/test-emitter.html +77 -77
  173. package/test/test-emitter.js +57 -57
  174. package/test/test-empty-cells-filtering.html +8696 -8696
  175. package/test/test-empty-cells-filtering.js +45 -45
  176. package/test/test-exact-query-match.html +77 -77
  177. package/test/test-exact-query-match.js +56 -56
  178. package/test/test-excluded-rows.html +92 -92
  179. package/test/test-excluded-rows.js +129 -129
  180. package/test/test-external-filters.html +100 -100
  181. package/test/test-external-filters.js +219 -219
  182. package/test/test-filter-html-elements.html +88 -88
  183. package/test/test-filter-html-elements.js +63 -63
  184. package/test/test-filter-operators.html +80 -80
  185. package/test/test-filter-operators.js +264 -264
  186. package/test/test-filtering.html +53 -53
  187. package/test/test-filtering.js +293 -293
  188. package/test/test-filters-visibility-grid-layout.html +80 -80
  189. package/test/test-filters-visibility-grid-layout.js +50 -50
  190. package/test/test-filters-visibility-with-button.html +80 -80
  191. package/test/test-filters-visibility-with-button.js +48 -48
  192. package/test/test-filters-visibility.html +80 -80
  193. package/test/test-filters-visibility.js +48 -48
  194. package/test/test-grid-layout.html +7735 -7735
  195. package/test/test-grid-layout.js +39 -39
  196. package/test/test-grouped-headers.html +154 -154
  197. package/test/test-grouped-headers.js +107 -107
  198. package/test/test-hash.html +78 -78
  199. package/test/test-hash.js +88 -88
  200. package/test/test-help-responsive.html +78 -78
  201. package/test/test-help-responsive.js +70 -70
  202. package/test/test-help.html +77 -77
  203. package/test/test-help.js +217 -217
  204. package/test/test-highlight-keywords.html +77 -77
  205. package/test/test-highlight-keywords.js +94 -94
  206. package/test/test-ignore-diacritics.html +67 -57
  207. package/test/test-ignore-diacritics.js +117 -90
  208. package/test/test-integration-paging-sort.html +79 -79
  209. package/test/test-integration-paging-sort.js +60 -60
  210. package/test/test-linked-filters-disabled-options-grid-layout.html +80 -80
  211. package/test/test-linked-filters-disabled-options-grid-layout.js +73 -73
  212. package/test/test-linked-filters-grid-layout.html +80 -80
  213. package/test/test-linked-filters-grid-layout.js +72 -72
  214. package/test/test-linked-filters-load-filters-on-demand.html +80 -80
  215. package/test/test-linked-filters-load-filters-on-demand.js +99 -99
  216. package/test/test-linked-filters-paging.html +80 -80
  217. package/test/test-linked-filters-paging.js +99 -99
  218. package/test/test-linked-filters.html +80 -80
  219. package/test/test-linked-filters.js +138 -138
  220. package/test/test-load-filters-on-demand.html +77 -77
  221. package/test/test-load-filters-on-demand.js +124 -124
  222. package/test/test-loader.html +79 -79
  223. package/test/test-loader.js +159 -159
  224. package/test/test-no-config.html +80 -80
  225. package/test/test-no-config.js +63 -63
  226. package/test/test-no-filters.html +80 -80
  227. package/test/test-no-filters.js +172 -172
  228. package/test/test-no-headers.html +71 -71
  229. package/test/test-no-headers.js +99 -99
  230. package/test/test-no-results-message.html +262 -262
  231. package/test/test-no-results-message.js +222 -222
  232. package/test/test-no-rows.html +19 -19
  233. package/test/test-no-rows.js +10 -10
  234. package/test/test-one-row.html +29 -29
  235. package/test/test-one-row.js +18 -18
  236. package/test/test-paging.html +78 -78
  237. package/test/test-paging.js +273 -273
  238. package/test/test-popup-filter-one-row.html +38 -38
  239. package/test/test-popup-filter-one-row.js +138 -138
  240. package/test/test-popup-filter.html +78 -78
  241. package/test/test-popup-filter.js +384 -384
  242. package/test/test-popup-filters-grouped-headers.html +104 -104
  243. package/test/test-popup-filters-grouped-headers.js +206 -206
  244. package/test/test-rows-counter.html +77 -77
  245. package/test/test-rows-counter.js +112 -112
  246. package/test/test-single-filter.html +78 -78
  247. package/test/test-single-filter.js +138 -138
  248. package/test/test-sort-custom-sorter.html +243 -243
  249. package/test/test-sort-custom-sorter.js +74 -74
  250. package/test/test-sort-grid-layout.html +142 -142
  251. package/test/test-sort-grid-layout.js +86 -86
  252. package/test/test-sort-key.html +243 -243
  253. package/test/test-sort-key.js +67 -67
  254. package/test/test-sort.html +142 -142
  255. package/test/test-sort.js +126 -126
  256. package/test/test-state.html +78 -78
  257. package/test/test-state.js +369 -369
  258. package/test/test-status-bar.html +79 -79
  259. package/test/test-status-bar.js +146 -146
  260. package/test/test-storage-cookie.html +78 -78
  261. package/test/test-storage-cookie.js +82 -82
  262. package/test/test-storage-local.html +78 -78
  263. package/test/test-storage-local.js +82 -82
  264. package/test/test-tfoot.html +96 -96
  265. package/test/test-tfoot.js +123 -123
  266. package/test/test-thead.html +80 -80
  267. package/test/test-thead.js +69 -69
  268. package/test/test-toolbar.html +81 -81
  269. package/test/test-toolbar.js +218 -218
  270. package/test/test-uneven-rows.html +95 -95
  271. package/test/test-uneven-rows.js +61 -61
  272. package/test/test-watermark.html +80 -80
  273. package/test/test-watermark.js +103 -103
  274. package/test/test.html +141 -141
  275. package/test/test.js +162 -162
  276. package/webpack.config.js +87 -87
  277. package/webpack.dev.config.js +25 -25
  278. package/webpack.test.config.js +44 -44
  279. package/.travis.yml +0 -53
  280. package/dist/tablefilter/tf-1-2aa33b10e0e549020c12.js +0 -1
@@ -1,480 +1,474 @@
1
- import {Feature} from '../../feature';
2
- import {createText, elm} from '../../dom';
3
- import {isArray, isEmpty, EMPTY_FN} from '../../types';
4
- import {numSortAsc} from '../../sort';
5
- import {FORMATTED_NUMBER} from '../../const';
6
- import formatNumber from 'format-number';
7
- import {defaultsFn, defaultsArr} from '../../settings';
8
- import {bound} from '../../event';
9
-
10
- const EVENTS = [
11
- 'after-filtering',
12
- 'after-page-change',
13
- 'after-page-length-change'
14
- ];
15
-
16
- const SUM = 'sum';
17
- const MEAN = 'mean';
18
- const MIN = 'min';
19
- const MAX = 'max';
20
- const MEDIAN = 'median';
21
- const Q1 = 'q1';
22
- const Q3 = 'q3';
23
-
24
- /**
25
- * Column calculations extension
26
- */
27
- export default class ColOps extends Feature {
28
-
29
- /**
30
- * Creates an instance of ColOps
31
- *
32
- * @param {TableFilter} tf TableFilter instance
33
- * @param {Object} opts Configuration object
34
- */
35
- constructor(tf, opts) {
36
- super(tf, ColOps);
37
-
38
- /**
39
- * Callback fired before columns operations start
40
- * @type {Function}
41
- */
42
- this.onBeforeOperation = defaultsFn(opts.on_before_operation, EMPTY_FN);
43
-
44
- /**
45
- * Callback fired after columns operations are completed
46
- * @type {Function}
47
- */
48
- this.onAfterOperation = defaultsFn(opts.on_after_operation, EMPTY_FN);
49
-
50
- /**
51
- * Configuration options
52
- * @type {Object}
53
- */
54
- this.opts = opts;
55
-
56
- /**
57
- * List of DOM element IDs containing column's calculation result
58
- * @type {Array}
59
- */
60
- this.labelIds = defaultsArr(opts.id, []);
61
-
62
- /**
63
- * List of columns' indexes for calculations
64
- * @type {Array}
65
- */
66
- this.colIndexes = defaultsArr(opts.col, []);
67
-
68
- /**
69
- * List of operations - possible values: 'sum', 'mean', 'min', 'max',
70
- * 'median', 'q1', 'q3'
71
- * @type {Array}
72
- */
73
- this.operations = defaultsArr(opts.operation, []);
74
-
75
- /**
76
- * List of write methods used to write the result - possible values:
77
- * 'innerHTML', 'setValue', 'createTextNode'
78
- * @type {Array}
79
- */
80
- this.outputTypes = defaultsArr(opts.write_method, []);
81
-
82
- /**
83
- * List of format objects used for formatting the result -
84
- * refer to https://github.com/componitable/format-number to check
85
- * configuration options
86
- * @type {Array}
87
- */
88
- this.formatResults = defaultsArr(opts.format_result, []);
89
-
90
- /**
91
- * List of row indexes displaying the results
92
- * @type {Array}
93
- */
94
- this.totRowIndexes = defaultsArr(opts.tot_row_index, []);
95
-
96
- /**
97
- * List of row indexes excluded from calculations
98
- * @type {Array}
99
- */
100
- this.excludeRows = defaultsArr(opts.exclude_row, []);
101
-
102
- /**
103
- * List of decimal precision for calculation results
104
- * @type {Array}
105
- */
106
- this.decimalPrecisions = defaultsArr(opts.decimal_precision, 2);
107
-
108
- this.enable();
109
- }
110
-
111
- /**
112
- * Initializes ColOps instance
113
- */
114
- init() {
115
- if (this.initialized) {
116
- return;
117
- }
118
- // subscribe to events
119
- this.emitter.on(EVENTS, bound(this.calcAll, this));
120
-
121
- this.calcAll();
122
-
123
- /** @inherited */
124
- this.initialized = true;
125
- }
126
-
127
- /**
128
- * Calculates columns' values
129
- * Configuration options are stored in 'opts' property
130
- * - 'id' contains ids of elements showing result (array)
131
- * - 'col' contains the columns' indexes (array)
132
- * - 'operation' contains operation type (array, values: 'sum', 'mean',
133
- * 'min', 'max', 'median', 'q1', 'q3')
134
- * - 'write_method' array defines which method to use for displaying the
135
- * result (innerHTML, setValue, createTextNode) - default: 'innerHTML'
136
- * - 'tot_row_index' defines in which row results are displayed
137
- * (integers array)
138
- *
139
- * - changes made by Nuovella:
140
- * (1) optimized the routine (now it will only process each column once),
141
- * (2) added calculations for the median, lower and upper quartile.
142
- */
143
- calcAll() {
144
- let tf = this.tf;
145
- if (!tf.isInitialized()) {
146
- return;
147
- }
148
-
149
- this.onBeforeOperation(tf, this);
150
- this.emitter.emit('before-column-operation', tf, this);
151
-
152
- let { colIndexes, operations: colOperations, outputTypes,
153
- totRowIndexes, excludeRows, formatResults,
154
- decimalPrecisions } = this;
155
-
156
- //nuovella: determine unique list of columns to operate on
157
- let uIndexes = [];
158
- colIndexes.forEach((val) => {
159
- if (uIndexes.indexOf(val) === -1) {
160
- uIndexes.push(val);
161
- }
162
- });
163
-
164
- let nbCols = uIndexes.length,
165
- rows = tf.dom().rows,
166
- colValues = [];
167
-
168
- for (let u = 0; u < nbCols; u++) {
169
- //this retrieves col values
170
- //use uIndexes because we only want to pass through this loop
171
- //once for each column get the values in this unique column
172
- colValues.push(
173
- tf.getVisibleColumnData(uIndexes[u], false, excludeRows)
174
- );
175
-
176
- let curValues = colValues[u];
177
-
178
- //next: calculate all operations for this column
179
- let result = 0,
180
- operations = [],
181
- precisions = [],
182
- labels = [],
183
- writeType,
184
- formatResult = [],
185
- idx = 0;
186
-
187
- for (let k = 0; k < colIndexes.length; k++) {
188
- if (colIndexes[k] !== uIndexes[u]) {
189
- continue;
190
- }
191
- operations[idx] = (colOperations[k] || 'sum').toLowerCase();
192
- precisions[idx] = decimalPrecisions[k];
193
- labels[idx] = this.labelIds[k];
194
- writeType = isArray(outputTypes) ? outputTypes[k] : null;
195
- formatResult[idx] =
196
- this.configureFormat(uIndexes[u], formatResults[k]);
197
- idx++;
198
- }
199
-
200
- for (let i = 0; i < idx; i++) {
201
- // emit values before column calculation
202
- this.emitter.emit(
203
- 'before-column-calc',
204
- tf,
205
- this,
206
- uIndexes[u],
207
- curValues,
208
- operations[i],
209
- precisions[i]
210
- );
211
-
212
- result = Number(this.calc(curValues, operations[i], null));
213
-
214
- // emit column calculation result
215
- this.emitter.emit(
216
- 'column-calc',
217
- tf,
218
- this,
219
- uIndexes[u],
220
- result,
221
- operations[i],
222
- precisions[i]
223
- );
224
-
225
- // write result in expected DOM element
226
- this.writeResult(
227
- result,
228
- labels[i],
229
- writeType,
230
- precisions[i],
231
- formatResult[i]
232
- );
233
-
234
- }//for i
235
-
236
- // row(s) with result are always visible
237
- let totRow = totRowIndexes && totRowIndexes[u] ?
238
- rows[totRowIndexes[u]] : null;
239
- if (totRow) {
240
- totRow.style.display = '';
241
- }
242
- }//for u
243
-
244
- this.onAfterOperation(tf, this);
245
- this.emitter.emit('after-column-operation', tf, this);
246
- }
247
-
248
- /**
249
- * Make desired calculation on specified column.
250
- * @param {Number} colIndex Column index
251
- * @param {String} [operation=SUM] Operation type
252
- * @param {Number} precision Decimal precision
253
- * @returns {Number}
254
- */
255
- columnCalc(colIndex, operation = SUM, precision) {
256
- let excludeRows = this.excludeRows || [];
257
- let colValues = tf.getVisibleColumnData(colIndex, false, excludeRows);
258
-
259
- return Number(this.calc(colValues, operation, precision));
260
- }
261
-
262
- /**
263
- * Make calculation on passed values.
264
- * @param {Array} values List of values
265
- * @param {String} [operation=SUM] Optional operation type
266
- * @param {Number} precision Optional result precision
267
- * @returns {Number}
268
- * @private
269
- */
270
- calc(colValues, operation = SUM, precision) {
271
- let result = 0;
272
-
273
- if (operation === Q1 || operation === Q3 || operation === MEDIAN) {
274
- colValues = this.sortColumnValues(colValues, numSortAsc);
275
- }
276
-
277
- switch (operation) {
278
- case MEAN:
279
- result = this.calcMean(colValues);
280
- break;
281
- case SUM:
282
- result = this.calcSum(colValues);
283
- break;
284
- case MIN:
285
- result = this.calcMin(colValues);
286
- break;
287
- case MAX:
288
- result = this.calcMax(colValues);
289
- break;
290
- case MEDIAN:
291
- result = this.calcMedian(colValues);
292
- break;
293
- case Q1:
294
- result = this.calcQ1(colValues);
295
- break;
296
- case Q3:
297
- result = this.calcQ3(colValues);
298
- break;
299
- }
300
-
301
- return isEmpty(precision) ? result : result.toFixed(precision);
302
- }
303
-
304
- /**
305
- * Calculate the sum of passed values.
306
- * @param {Array} [values=[]] List of values
307
- * @returns {Number}
308
- */
309
- calcSum(values = []) {
310
- if (isEmpty(values)) {
311
- return 0;
312
- }
313
- let result = values.reduce((x, y) => Number(x) + Number(y));
314
- return result;
315
- }
316
-
317
- /**
318
- * Calculate the mean of passed values.
319
- * @param {Array} [values=[]] List of values
320
- * @returns {Number}
321
- */
322
- calcMean(values = []) {
323
- let result = this.calcSum(values) / values.length;
324
- return Number(result);
325
- }
326
-
327
- /**
328
- * Calculate the max value of passed values.
329
- * @param {Array} [values=[]] List of values
330
- * @returns {Number}
331
- */
332
- calcMax(values = []) {
333
- return Math.max.apply(null, values);
334
- }
335
-
336
- /**
337
- * Calculate the min value of passed values.
338
- * @param {Array} [values=[]] List of values
339
- * @returns {Number}
340
- */
341
- calcMin(values = []) {
342
- return Math.min.apply(null, values);
343
- }
344
-
345
- /**
346
- * Calculate the median of passed values.
347
- * @param {Array} [values=[]] List of values
348
- * @returns {Number}
349
- */
350
- calcMedian(values = []) {
351
- let nbValues = values.length;
352
- let aux = 0;
353
- if (nbValues % 2 === 1) {
354
- aux = Math.floor(nbValues / 2);
355
- return Number(values[aux]);
356
- }
357
- return (Number(values[nbValues / 2]) +
358
- Number(values[((nbValues / 2) - 1)])) / 2;
359
- }
360
-
361
- /**
362
- * Calculate the lower quartile of passed values.
363
- * @param {Array} [values=[]] List of values
364
- * @returns {Number}
365
- */
366
- calcQ1(values = []) {
367
- let nbValues = values.length;
368
- let posa = 0.0;
369
- posa = Math.floor(nbValues / 4);
370
- if (4 * posa === nbValues) {
371
- return (Number(values[posa - 1]) +
372
- Number(values[posa])) / 2;
373
- }
374
- return Number(values[posa]);
375
- }
376
-
377
- /**
378
- * Calculate the upper quartile of passed values.
379
- * @param {Array} [values=[]] List of values
380
- * @returns {Number}
381
- */
382
- calcQ3(values = []) {
383
- let nbValues = values.length;
384
- let posa = 0.0;
385
- let posb = 0.0;
386
- posa = Math.floor(nbValues / 4);
387
- if (4 * posa === nbValues) {
388
- posb = 3 * posa;
389
- return (Number(values[posb]) +
390
- Number(values[posb - 1])) / 2;
391
- }
392
- return Number(values[nbValues - posa - 1]);
393
- }
394
-
395
- /**
396
- * Sort passed values with supplied sorter function.
397
- * @param {Array} [values=[]] List of values to be sorted
398
- * @param {Function} sorter Sorter function
399
- * @returns {Array}
400
- */
401
- sortColumnValues(values = [], sorter) {
402
- return values.sort(sorter);
403
- }
404
-
405
- /**
406
- * Write calculation result in passed DOM element with supplied write method
407
- * and decimal precision.
408
- * @param {Number} [result=0] Calculation result
409
- * @param {DOMElement} label DOM element
410
- * @param {String} [writeType='innerhtml'] Write method
411
- * @param {Number} [precision=2] Applied decimal precision
412
- * @private
413
- */
414
- writeResult(result = 0, label, writeType = 'innerhtml',
415
- precision = 2, format = {}) {
416
- let labelElm = elm(label);
417
-
418
- if (!labelElm) {
419
- return;
420
- }
421
-
422
- result = result.toFixed(precision);
423
- if (isNaN(result) || !isFinite(result)) {
424
- result = '';
425
- } else {
426
- result = formatNumber(format)(result);
427
- }
428
-
429
- switch (writeType.toLowerCase()) {
430
- case 'innerhtml':
431
- labelElm.innerHTML = result;
432
- break;
433
- case 'setvalue':
434
- labelElm.value = result;
435
- break;
436
- case 'createtextnode':
437
- let oldNode = labelElm.firstChild;
438
- let txtNode = createText(result);
439
- labelElm.replaceChild(txtNode, oldNode);
440
- break;
441
- }
442
- }
443
-
444
- /**
445
- * Configure the format options used to format the operation result based
446
- * on column type.
447
- * @param {Number} colIndex Column index
448
- * @param {Object} [format={}] Format object
449
- * @returns {Object}
450
- * @private
451
- */
452
- configureFormat(colIndex, format = {}) {
453
- let tf = this.tf;
454
- if (tf.hasType(colIndex, [FORMATTED_NUMBER])) {
455
- let colType = tf.colTypes[colIndex];
456
- if (colType.decimal && !format.decimal) {
457
- format.decimal = colType.decimal;
458
- }
459
- if (colType.thousands && !format.integerSeparator) {
460
- format.integerSeparator = colType.thousands;
461
- }
462
- } else {
463
- format.decimal = format.decimal || '';
464
- format.integerSeparator = format.integerSeparator || '';
465
- }
466
- return format;
467
- }
468
-
469
- /** Remove extension */
470
- destroy() {
471
- if (!this.initialized) {
472
- return;
473
- }
474
- // unsubscribe to events
475
- this.emitter.off(EVENTS, bound(this.calcAll, this));
476
-
477
- this.initialized = false;
478
- }
479
-
480
- }
1
+ import {Feature} from '../../feature';
2
+ import {createText, elm} from '../../dom';
3
+ import {isArray, isEmpty, EMPTY_FN} from '../../types';
4
+ import {numSortAsc} from '../../sort';
5
+ import {FORMATTED_NUMBER} from '../../const';
6
+ import {formatNumberValue} from '../../number';
7
+ import {defaultsFn, defaultsArr} from '../../settings';
8
+ import {bound} from '../../event';
9
+
10
+ const EVENTS = [
11
+ 'after-filtering',
12
+ 'after-page-change',
13
+ 'after-page-length-change'
14
+ ];
15
+
16
+ const SUM = 'sum';
17
+ const MEAN = 'mean';
18
+ const MIN = 'min';
19
+ const MAX = 'max';
20
+ const MEDIAN = 'median';
21
+ const Q1 = 'q1';
22
+ const Q3 = 'q3';
23
+
24
+ /**
25
+ * Column calculations extension
26
+ */
27
+ export default class ColOps extends Feature {
28
+
29
+ /**
30
+ * Creates an instance of ColOps
31
+ *
32
+ * @param {TableFilter} tf TableFilter instance
33
+ * @param {Object} opts Configuration object
34
+ */
35
+ constructor(tf, opts) {
36
+ super(tf, ColOps);
37
+
38
+ /**
39
+ * Callback fired before columns operations start
40
+ * @type {Function}
41
+ */
42
+ this.onBeforeOperation = defaultsFn(opts.on_before_operation, EMPTY_FN);
43
+
44
+ /**
45
+ * Callback fired after columns operations are completed
46
+ * @type {Function}
47
+ */
48
+ this.onAfterOperation = defaultsFn(opts.on_after_operation, EMPTY_FN);
49
+
50
+ /**
51
+ * Configuration options
52
+ * @type {Object}
53
+ */
54
+ this.opts = opts;
55
+
56
+ /**
57
+ * List of DOM element IDs containing column's calculation result
58
+ * @type {Array}
59
+ */
60
+ this.labelIds = defaultsArr(opts.id, []);
61
+
62
+ /**
63
+ * List of columns' indexes for calculations
64
+ * @type {Array}
65
+ */
66
+ this.colIndexes = defaultsArr(opts.col, []);
67
+
68
+ /**
69
+ * List of operations - possible values: 'sum', 'mean', 'min', 'max',
70
+ * 'median', 'q1', 'q3'
71
+ * @type {Array}
72
+ */
73
+ this.operations = defaultsArr(opts.operation, []);
74
+
75
+ /**
76
+ * List of write methods used to write the result - possible values:
77
+ * 'innerHTML', 'setValue', 'createTextNode'
78
+ * @type {Array}
79
+ */
80
+ this.outputTypes = defaultsArr(opts.write_method, []);
81
+
82
+ /**
83
+ * List of format objects used for formatting the result - see
84
+ * formatNumberValue in number.js for the supported options
85
+ * @type {Array}
86
+ */
87
+ this.formatResults = defaultsArr(opts.format_result, []);
88
+
89
+ /**
90
+ * List of row indexes displaying the results
91
+ * @type {Array}
92
+ */
93
+ this.totRowIndexes = defaultsArr(opts.tot_row_index, []);
94
+
95
+ /**
96
+ * List of row indexes excluded from calculations
97
+ * @type {Array}
98
+ */
99
+ this.excludeRows = defaultsArr(opts.exclude_row, []);
100
+
101
+ /**
102
+ * List of decimal precision for calculation results
103
+ * @type {Array}
104
+ */
105
+ this.decimalPrecisions = defaultsArr(opts.decimal_precision, 2);
106
+
107
+ this.enable();
108
+ }
109
+
110
+ /**
111
+ * Initializes ColOps instance
112
+ */
113
+ init() {
114
+ if (this.initialized) {
115
+ return;
116
+ }
117
+ // subscribe to events
118
+ this.emitter.on(EVENTS, bound(this.calcAll, this));
119
+
120
+ this.calcAll();
121
+
122
+ /** @inherited */
123
+ this.initialized = true;
124
+ }
125
+
126
+ /**
127
+ * Calculates columns' values
128
+ * Configuration options are stored in 'opts' property
129
+ * - 'id' contains ids of elements showing result (array)
130
+ * - 'col' contains the columns' indexes (array)
131
+ * - 'operation' contains operation type (array, values: 'sum', 'mean',
132
+ * 'min', 'max', 'median', 'q1', 'q3')
133
+ * - 'write_method' array defines which method to use for displaying the
134
+ * result (innerHTML, setValue, createTextNode) - default: 'innerHTML'
135
+ * - 'tot_row_index' defines in which row results are displayed
136
+ * (integers array)
137
+ *
138
+ * - changes made by Nuovella:
139
+ * (1) optimized the routine (now it will only process each column once),
140
+ * (2) added calculations for the median, lower and upper quartile.
141
+ */
142
+ calcAll() {
143
+ let tf = this.tf;
144
+ if (!tf.isInitialized()) {
145
+ return;
146
+ }
147
+
148
+ this.onBeforeOperation(tf, this);
149
+ this.emitter.emit('before-column-operation', tf, this);
150
+
151
+ let { colIndexes, operations: colOperations, outputTypes,
152
+ totRowIndexes, excludeRows, formatResults,
153
+ decimalPrecisions } = this;
154
+
155
+ //nuovella: determine unique list of columns to operate on
156
+ let uIndexes = [];
157
+ colIndexes.forEach((val) => {
158
+ if (uIndexes.indexOf(val) === -1) {
159
+ uIndexes.push(val);
160
+ }
161
+ });
162
+
163
+ let nbCols = uIndexes.length,
164
+ rows = tf.dom().rows,
165
+ colValues = [];
166
+
167
+ for (let u = 0; u < nbCols; u++) {
168
+ //this retrieves col values
169
+ //use uIndexes because we only want to pass through this loop
170
+ //once for each column get the values in this unique column
171
+ colValues.push(
172
+ tf.getVisibleColumnData(uIndexes[u], false, excludeRows)
173
+ );
174
+
175
+ let curValues = colValues[u];
176
+
177
+ //next: calculate all operations for this column
178
+ let result = 0,
179
+ operations = [],
180
+ precisions = [],
181
+ labels = [],
182
+ writeType,
183
+ formatResult = [],
184
+ idx = 0;
185
+
186
+ for (let k = 0; k < colIndexes.length; k++) {
187
+ if (colIndexes[k] !== uIndexes[u]) {
188
+ continue;
189
+ }
190
+ operations[idx] = (colOperations[k] || 'sum').toLowerCase();
191
+ precisions[idx] = decimalPrecisions[k];
192
+ labels[idx] = this.labelIds[k];
193
+ writeType = isArray(outputTypes) ? outputTypes[k] : null;
194
+ formatResult[idx] =
195
+ this.configureFormat(uIndexes[u], formatResults[k]);
196
+ idx++;
197
+ }
198
+
199
+ for (let i = 0; i < idx; i++) {
200
+ // emit values before column calculation
201
+ this.emitter.emit(
202
+ 'before-column-calc',
203
+ tf,
204
+ this,
205
+ uIndexes[u],
206
+ curValues,
207
+ operations[i],
208
+ precisions[i]
209
+ );
210
+
211
+ result = Number(this.calc(curValues, operations[i], null));
212
+
213
+ // emit column calculation result
214
+ this.emitter.emit(
215
+ 'column-calc',
216
+ tf,
217
+ this,
218
+ uIndexes[u],
219
+ result,
220
+ operations[i],
221
+ precisions[i]
222
+ );
223
+
224
+ // write result in expected DOM element
225
+ this.writeResult(
226
+ result,
227
+ labels[i],
228
+ writeType,
229
+ precisions[i],
230
+ formatResult[i]
231
+ );
232
+
233
+ }//for i
234
+
235
+ // row(s) with result are always visible
236
+ let totRow = totRowIndexes && totRowIndexes[u] ?
237
+ rows[totRowIndexes[u]] : null;
238
+ if (totRow) {
239
+ totRow.style.display = '';
240
+ }
241
+ }//for u
242
+
243
+ this.onAfterOperation(tf, this);
244
+ this.emitter.emit('after-column-operation', tf, this);
245
+ }
246
+
247
+ /**
248
+ * Make desired calculation on specified column.
249
+ * @param {Number} colIndex Column index
250
+ * @param {String} [operation=SUM] Operation type
251
+ * @param {Number} precision Decimal precision
252
+ * @returns {Number}
253
+ */
254
+ columnCalc(colIndex, operation = SUM, precision) {
255
+ let excludeRows = this.excludeRows || [];
256
+ let colValues = tf.getVisibleColumnData(colIndex, false, excludeRows);
257
+
258
+ return Number(this.calc(colValues, operation, precision));
259
+ }
260
+
261
+ /**
262
+ * Make calculation on passed values.
263
+ * @param {Array} values List of values
264
+ * @param {String} [operation=SUM] Optional operation type
265
+ * @param {Number} precision Optional result precision
266
+ * @returns {Number}
267
+ * @private
268
+ */
269
+ calc(colValues, operation = SUM, precision) {
270
+ let result = 0;
271
+
272
+ if (operation === Q1 || operation === Q3 || operation === MEDIAN) {
273
+ colValues = this.sortColumnValues(colValues, numSortAsc);
274
+ }
275
+
276
+ switch (operation) {
277
+ case MEAN:
278
+ result = this.calcMean(colValues);
279
+ break;
280
+ case SUM:
281
+ result = this.calcSum(colValues);
282
+ break;
283
+ case MIN:
284
+ result = this.calcMin(colValues);
285
+ break;
286
+ case MAX:
287
+ result = this.calcMax(colValues);
288
+ break;
289
+ case MEDIAN:
290
+ result = this.calcMedian(colValues);
291
+ break;
292
+ case Q1:
293
+ result = this.calcQ1(colValues);
294
+ break;
295
+ case Q3:
296
+ result = this.calcQ3(colValues);
297
+ break;
298
+ }
299
+
300
+ return isEmpty(precision) ? result : result.toFixed(precision);
301
+ }
302
+
303
+ /**
304
+ * Calculate the sum of passed values.
305
+ * @param {Array} [values=[]] List of values
306
+ * @returns {Number}
307
+ */
308
+ calcSum(values = []) {
309
+ if (isEmpty(values)) {
310
+ return 0;
311
+ }
312
+ let result = values.reduce((x, y) => Number(x) + Number(y));
313
+ return result;
314
+ }
315
+
316
+ /**
317
+ * Calculate the mean of passed values.
318
+ * @param {Array} [values=[]] List of values
319
+ * @returns {Number}
320
+ */
321
+ calcMean(values = []) {
322
+ let result = this.calcSum(values) / values.length;
323
+ return Number(result);
324
+ }
325
+
326
+ /**
327
+ * Calculate the max value of passed values.
328
+ * @param {Array} [values=[]] List of values
329
+ * @returns {Number}
330
+ */
331
+ calcMax(values = []) {
332
+ return Math.max.apply(null, values);
333
+ }
334
+
335
+ /**
336
+ * Calculate the min value of passed values.
337
+ * @param {Array} [values=[]] List of values
338
+ * @returns {Number}
339
+ */
340
+ calcMin(values = []) {
341
+ return Math.min.apply(null, values);
342
+ }
343
+
344
+ /**
345
+ * Calculate the median of passed values.
346
+ * @param {Array} [values=[]] List of values
347
+ * @returns {Number}
348
+ */
349
+ calcMedian(values = []) {
350
+ let nbValues = values.length;
351
+ let aux = 0;
352
+ if (nbValues % 2 === 1) {
353
+ aux = Math.floor(nbValues / 2);
354
+ return Number(values[aux]);
355
+ }
356
+ return (Number(values[nbValues / 2]) +
357
+ Number(values[((nbValues / 2) - 1)])) / 2;
358
+ }
359
+
360
+ /**
361
+ * Calculate the lower quartile of passed values.
362
+ * @param {Array} [values=[]] List of values
363
+ * @returns {Number}
364
+ */
365
+ calcQ1(values = []) {
366
+ let nbValues = values.length;
367
+ let posa = 0.0;
368
+ posa = Math.floor(nbValues / 4);
369
+ if (4 * posa === nbValues) {
370
+ return (Number(values[posa - 1]) +
371
+ Number(values[posa])) / 2;
372
+ }
373
+ return Number(values[posa]);
374
+ }
375
+
376
+ /**
377
+ * Calculate the upper quartile of passed values.
378
+ * @param {Array} [values=[]] List of values
379
+ * @returns {Number}
380
+ */
381
+ calcQ3(values = []) {
382
+ let nbValues = values.length;
383
+ let posa = 0.0;
384
+ let posb = 0.0;
385
+ posa = Math.floor(nbValues / 4);
386
+ if (4 * posa === nbValues) {
387
+ posb = 3 * posa;
388
+ return (Number(values[posb]) +
389
+ Number(values[posb - 1])) / 2;
390
+ }
391
+ return Number(values[nbValues - posa - 1]);
392
+ }
393
+
394
+ /**
395
+ * Sort passed values with supplied sorter function.
396
+ * @param {Array} [values=[]] List of values to be sorted
397
+ * @param {Function} sorter Sorter function
398
+ * @returns {Array}
399
+ */
400
+ sortColumnValues(values = [], sorter) {
401
+ return values.sort(sorter);
402
+ }
403
+
404
+ /**
405
+ * Write calculation result in passed DOM element with supplied write method
406
+ * and decimal precision.
407
+ * @param {Number} [result=0] Calculation result
408
+ * @param {DOMElement} label DOM element
409
+ * @param {String} [writeType='innerhtml'] Write method
410
+ * @param {Number} [precision=2] Applied decimal precision
411
+ * @private
412
+ */
413
+ writeResult(result = 0, label, writeType = 'innerhtml',
414
+ precision = 2, format = {}) {
415
+ let labelElm = elm(label);
416
+
417
+ if (!labelElm) {
418
+ return;
419
+ }
420
+
421
+ result = formatNumberValue(result.toFixed(precision), format);
422
+
423
+ switch (writeType.toLowerCase()) {
424
+ case 'innerhtml':
425
+ labelElm.innerHTML = result;
426
+ break;
427
+ case 'setvalue':
428
+ labelElm.value = result;
429
+ break;
430
+ case 'createtextnode':
431
+ let oldNode = labelElm.firstChild;
432
+ let txtNode = createText(result);
433
+ labelElm.replaceChild(txtNode, oldNode);
434
+ break;
435
+ }
436
+ }
437
+
438
+ /**
439
+ * Configure the format options used to format the operation result based
440
+ * on column type.
441
+ * @param {Number} colIndex Column index
442
+ * @param {Object} [format={}] Format object
443
+ * @returns {Object}
444
+ * @private
445
+ */
446
+ configureFormat(colIndex, format = {}) {
447
+ let tf = this.tf;
448
+ if (tf.hasType(colIndex, [FORMATTED_NUMBER])) {
449
+ let colType = tf.colTypes[colIndex];
450
+ if (colType.decimal && !format.decimal) {
451
+ format.decimal = colType.decimal;
452
+ }
453
+ if (colType.thousands && !format.integerSeparator) {
454
+ format.integerSeparator = colType.thousands;
455
+ }
456
+ } else {
457
+ format.decimal = format.decimal || '';
458
+ format.integerSeparator = format.integerSeparator || '';
459
+ }
460
+ return format;
461
+ }
462
+
463
+ /** Remove extension */
464
+ destroy() {
465
+ if (!this.initialized) {
466
+ return;
467
+ }
468
+ // unsubscribe to events
469
+ this.emitter.off(EVENTS, bound(this.calcAll, this));
470
+
471
+ this.initialized = false;
472
+ }
473
+
474
+ }