tablefilter 0.7.3 → 0.7.5

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 (319) hide show
  1. package/.editorconfig +15 -15
  2. package/.esdoc.json +14 -14
  3. package/.eslintrc +37 -37
  4. package/.gitattributes +16 -0
  5. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +36 -36
  7. package/.github/ISSUE_TEMPLATE/support.md +17 -17
  8. package/.github/dependabot.yml +10 -0
  9. package/.github/workflows/build-deploy.yml +141 -0
  10. package/.github/workflows/opencode-review.yml +104 -0
  11. package/.github/workflows/opencode.yml +34 -0
  12. package/AGENTS.md +36 -0
  13. package/CONTRIBUTING.md +128 -128
  14. package/Gruntfile.js +252 -252
  15. package/LICENSE +21 -21
  16. package/PULL_REQUEST_TEMPLATE.md +24 -24
  17. package/README.md +166 -174
  18. package/ROADMAP/README.md +34 -0
  19. package/ROADMAP/content-projection-parity.md +162 -0
  20. package/ROADMAP/hybrid-migration-strategy.md +275 -0
  21. package/ROADMAP/key-decisions-review.md +215 -0
  22. package/ROADMAP/preliminary-and-milestone-0.8.0-plan.md +307 -0
  23. package/ROADMAP/v1-webcomponents-investigation.md +521 -0
  24. package/dist/starter.html +514 -514
  25. package/dist/tablefilter/style/colsVisibility.css +1 -1
  26. package/dist/tablefilter/style/filtersVisibility.css +1 -1
  27. package/dist/tablefilter/style/tablefilter.css +1 -1
  28. package/dist/tablefilter/style/themes/default/default.css +1 -1
  29. package/dist/tablefilter/style/themes/mytheme/mytheme.css +1 -1
  30. package/dist/tablefilter/style/themes/skyblue/skyblue.css +1 -1
  31. package/dist/tablefilter/style/themes/transparent/transparent.css +1 -1
  32. package/dist/tablefilter/tablefilter.js +1 -1
  33. package/dist/tablefilter/tablefilter.vite.js +4 -0
  34. package/dist/tablefilter/tf-1-a5c3a73bd559e47dd5a2.js +1 -0
  35. package/dist/tablefilter/tf-2-3a936d22b849361acff5.js +6 -0
  36. package/dist/tablefilter.cjs.js +4 -0
  37. package/dist/tablefilter.esm.js +7278 -0
  38. package/dist/tablefilter.umd.js +4 -0
  39. package/index.js +3 -3
  40. package/libs/sortabletable.js +438 -438
  41. package/package.json +12 -5
  42. package/playwright.config.js +25 -0
  43. package/scripts/check-css-tokens.js +65 -0
  44. package/src/array.js +22 -22
  45. package/src/components/table-filter.js +243 -0
  46. package/src/components/tf-clear-button.js +95 -0
  47. package/src/components/tf-rows-counter.js +82 -0
  48. package/src/components/tf-status.js +106 -0
  49. package/src/const.js +118 -118
  50. package/src/cookie.js +57 -57
  51. package/src/dom.js +192 -192
  52. package/src/emitter.js +53 -53
  53. package/src/event.js +124 -124
  54. package/src/extensions/advancedGrid/adapterEzEditTable.js +509 -509
  55. package/src/extensions/advancedGrid/advancedGrid.js +2 -2
  56. package/src/extensions/colOps/colOps.js +474 -480
  57. package/src/extensions/colsVisibility/colsVisibility.js +725 -725
  58. package/src/extensions/filtersVisibility/filtersVisibility.js +316 -316
  59. package/src/extensions/sort/adapterSortabletable.js +537 -537
  60. package/src/extensions/sort/sort.js +8 -8
  61. package/src/feature.js +102 -102
  62. package/src/modules/alternateRows.js +145 -145
  63. package/src/modules/baseDropdown.js +160 -160
  64. package/src/modules/checkList.js +519 -519
  65. package/src/modules/clearButton.js +196 -142
  66. package/src/modules/dateType.js +157 -157
  67. package/src/modules/dropdown.js +376 -376
  68. package/src/modules/gridLayout.js +483 -483
  69. package/src/modules/hash.js +126 -126
  70. package/src/modules/help.js +264 -264
  71. package/src/modules/highlightKeywords.js +180 -180
  72. package/src/modules/loader.js +183 -183
  73. package/src/modules/markActiveColumns.js +146 -146
  74. package/src/modules/noResults.js +193 -193
  75. package/src/modules/paging.js +916 -916
  76. package/src/modules/popupFilter.js +461 -461
  77. package/src/modules/rowsCounter.js +230 -207
  78. package/src/modules/state.js +545 -545
  79. package/src/modules/statusBar.js +340 -308
  80. package/src/modules/storage.js +161 -161
  81. package/src/modules/toolbar.js +224 -224
  82. package/src/number.js +96 -32
  83. package/src/root.js +6 -6
  84. package/src/sanitize.js +25 -0
  85. package/src/settings.js +53 -53
  86. package/src/sort.js +82 -82
  87. package/src/string.js +112 -112
  88. package/src/tablefilter.js +3053 -3053
  89. package/src/types.js +81 -81
  90. package/static/partials/books-table-with-images.html +327 -327
  91. package/static/partials/books.html +272 -272
  92. package/static/partials/countries-by-continent.html +2592 -2592
  93. package/static/partials/countries-table-tiny.html +436 -436
  94. package/static/partials/countries-table.html +12954 -12954
  95. package/static/partials/dummy-table-with-totals-footer.html +353 -353
  96. package/static/partials/dummy-table.html +502 -502
  97. package/static/partials/pre-inline-script.html +17 -17
  98. package/static/partials/pre.html +1 -1
  99. package/static/partials/style.html +22 -22
  100. package/static/partials/table-with-grouped-headers.html +9258 -9258
  101. package/static/style/active-header.styl +10 -10
  102. package/static/style/alternating-rows.styl +8 -8
  103. package/static/style/config.styl +44 -44
  104. package/static/style/custom-properties.styl +43 -0
  105. package/static/style/extensions/colsVisibility.styl +71 -71
  106. package/static/style/extensions/filtersVisibility.styl +18 -18
  107. package/static/style/ezedittable.styl +93 -93
  108. package/static/style/filter-checklist.styl +52 -52
  109. package/static/style/filters.styl +111 -111
  110. package/static/style/grid-layout.styl +130 -130
  111. package/static/style/help.styl +57 -57
  112. package/static/style/keyword.styl +8 -8
  113. package/static/style/loader.styl +16 -16
  114. package/static/style/mixins/arrow.styl +63 -63
  115. package/static/style/mixins/box-shadow.styl +8 -8
  116. package/static/style/mixins/empty-content.styl +7 -7
  117. package/static/style/noresults.styl +14 -14
  118. package/static/style/paging.styl +73 -73
  119. package/static/style/reset-button.styl +21 -21
  120. package/static/style/rows-counter.styl +17 -17
  121. package/static/style/sort.styl +20 -20
  122. package/static/style/status-bar.styl +13 -16
  123. package/static/style/table.styl +44 -44
  124. package/static/style/tablefilter.styl +19 -18
  125. package/static/style/themes/default/default.styl +137 -137
  126. package/static/style/themes/mytheme/mytheme.styl +152 -152
  127. package/static/style/themes/skyblue/skyblue.styl +195 -195
  128. package/static/style/themes/transparent/transparent.styl +205 -205
  129. package/static/style/toolbar.styl +58 -58
  130. package/static/templates/advanced-grid-editable.html +140 -140
  131. package/static/templates/advanced-grid-selection.html +93 -93
  132. package/static/templates/auto-filter.html +46 -46
  133. package/static/templates/columns-visibility.html +67 -67
  134. package/static/templates/data-types.html +123 -123
  135. package/static/templates/demo.html +62 -62
  136. package/static/templates/external-filters-visibility-commad.html +49 -49
  137. package/static/templates/external-filters.html +125 -125
  138. package/static/templates/filter-images.html +91 -91
  139. package/static/templates/filters-visibility.html +45 -45
  140. package/static/templates/grid-layout.html +51 -51
  141. package/static/templates/grouped-headers.html +78 -78
  142. package/static/templates/linked-filters-disabled.html +56 -56
  143. package/static/templates/linked-filters.html +45 -45
  144. package/static/templates/load-filters-on-demand.html +42 -42
  145. package/static/templates/pagination.html +78 -78
  146. package/static/templates/requirejs.html +53 -53
  147. package/static/templates/starter.html +61 -61
  148. package/static/templates/table-filter.html +40 -0
  149. package/static/templates/theme-roller.html +106 -106
  150. package/test/libs/polyfill.js +359 -359
  151. package/test/libs/qunit/qunit.css +237 -237
  152. package/test/libs/qunit/qunit.js +2495 -2495
  153. package/test/playwright/axe.baseline.spec.js +34 -0
  154. package/test/playwright/axe.statusbar.spec.js +35 -0
  155. package/test/playwright/axeScan.js +42 -0
  156. package/test/playwright/ce-bridge.spec.js +167 -0
  157. package/test/playwright/qunit.all.spec.js +10 -0
  158. package/test/playwright/qunit.single.spec.js +14 -0
  159. package/test/playwright/qunitSuite.js +56 -0
  160. package/test/playwright/table-filter.config.spec.js +265 -0
  161. package/test/playwright/table-filter.demo.spec.js +60 -0
  162. package/test/playwright/table-filter.element.spec.js +477 -0
  163. package/test/playwright/tf-clear-button.spec.js +135 -0
  164. package/test/playwright/tf-rows-counter.spec.js +172 -0
  165. package/test/playwright/tf-status.spec.js +258 -0
  166. package/test/test-active-columns.html +80 -80
  167. package/test/test-active-columns.js +318 -318
  168. package/test/test-alternate-rows.html +80 -80
  169. package/test/test-alternate-rows.js +335 -335
  170. package/test/test-api.html +141 -141
  171. package/test/test-api.js +795 -795
  172. package/test/test-auto-filter.html +80 -80
  173. package/test/test-auto-filter.js +43 -43
  174. package/test/test-callbacks.html +77 -77
  175. package/test/test-callbacks.js +58 -58
  176. package/test/test-case-sensitive-query.html +77 -77
  177. package/test/test-case-sensitive-query.js +39 -39
  178. package/test/test-cell-parser.html +109 -109
  179. package/test/test-cell-parser.js +80 -80
  180. package/test/test-checklist.html +77 -77
  181. package/test/test-checklist.js +267 -267
  182. package/test/test-clear-button.html +79 -79
  183. package/test/test-clear-button.js +183 -182
  184. package/test/test-col-ops-grid-layout.html +129 -129
  185. package/test/test-col-ops-grid-layout.js +134 -134
  186. package/test/test-col-ops-paging.html +129 -129
  187. package/test/test-col-ops-paging.js +154 -154
  188. package/test/test-col-ops.html +129 -129
  189. package/test/test-col-ops.js +337 -202
  190. package/test/test-col-widths.html +80 -80
  191. package/test/test-col-widths.js +40 -40
  192. package/test/test-cols-visibility-grid-layout.html +80 -80
  193. package/test/test-cols-visibility-grid-layout.js +165 -165
  194. package/test/test-cols-visibility.html +80 -80
  195. package/test/test-cols-visibility.js +121 -121
  196. package/test/test-custom-options.html +80 -80
  197. package/test/test-custom-options.js +139 -139
  198. package/test/test-data-types.html +377 -377
  199. package/test/test-data-types.js +392 -392
  200. package/test/test-date-type.html +356 -356
  201. package/test/test-date-type.js +159 -159
  202. package/test/test-disable-enter-key.html +77 -77
  203. package/test/test-disable-enter-key.js +29 -29
  204. package/test/test-disable-filter-onchange.html +77 -77
  205. package/test/test-disable-filter-onchange.js +40 -40
  206. package/test/test-dom-changes.html +77 -77
  207. package/test/test-dom-changes.js +166 -166
  208. package/test/test-dropdown.html +77 -77
  209. package/test/test-dropdown.js +246 -246
  210. package/test/test-emitter.html +77 -77
  211. package/test/test-emitter.js +57 -57
  212. package/test/test-empty-cells-filtering.html +8696 -8696
  213. package/test/test-empty-cells-filtering.js +45 -45
  214. package/test/test-exact-query-match.html +77 -77
  215. package/test/test-exact-query-match.js +56 -56
  216. package/test/test-excluded-rows.html +92 -92
  217. package/test/test-excluded-rows.js +129 -129
  218. package/test/test-external-filters.html +100 -100
  219. package/test/test-external-filters.js +219 -219
  220. package/test/test-filter-html-elements.html +88 -88
  221. package/test/test-filter-html-elements.js +63 -63
  222. package/test/test-filter-operators.html +80 -80
  223. package/test/test-filter-operators.js +264 -264
  224. package/test/test-filtering.html +53 -53
  225. package/test/test-filtering.js +293 -293
  226. package/test/test-filters-visibility-grid-layout.html +80 -80
  227. package/test/test-filters-visibility-grid-layout.js +50 -50
  228. package/test/test-filters-visibility-with-button.html +80 -80
  229. package/test/test-filters-visibility-with-button.js +48 -48
  230. package/test/test-filters-visibility.html +80 -80
  231. package/test/test-filters-visibility.js +48 -48
  232. package/test/test-grid-layout.html +7735 -7735
  233. package/test/test-grid-layout.js +39 -39
  234. package/test/test-grouped-headers.html +154 -154
  235. package/test/test-grouped-headers.js +107 -107
  236. package/test/test-hash.html +78 -78
  237. package/test/test-hash.js +88 -88
  238. package/test/test-help-responsive.html +78 -78
  239. package/test/test-help-responsive.js +70 -70
  240. package/test/test-help.html +77 -77
  241. package/test/test-help.js +217 -217
  242. package/test/test-highlight-keywords.html +77 -77
  243. package/test/test-highlight-keywords.js +94 -94
  244. package/test/test-ignore-diacritics.html +67 -57
  245. package/test/test-ignore-diacritics.js +117 -90
  246. package/test/test-integration-paging-sort.html +79 -79
  247. package/test/test-integration-paging-sort.js +60 -60
  248. package/test/test-linked-filters-disabled-options-grid-layout.html +80 -80
  249. package/test/test-linked-filters-disabled-options-grid-layout.js +73 -73
  250. package/test/test-linked-filters-grid-layout.html +80 -80
  251. package/test/test-linked-filters-grid-layout.js +72 -72
  252. package/test/test-linked-filters-load-filters-on-demand.html +80 -80
  253. package/test/test-linked-filters-load-filters-on-demand.js +99 -99
  254. package/test/test-linked-filters-paging.html +80 -80
  255. package/test/test-linked-filters-paging.js +99 -99
  256. package/test/test-linked-filters.html +80 -80
  257. package/test/test-linked-filters.js +138 -138
  258. package/test/test-load-filters-on-demand.html +77 -77
  259. package/test/test-load-filters-on-demand.js +124 -124
  260. package/test/test-loader.html +79 -79
  261. package/test/test-loader.js +159 -159
  262. package/test/test-no-config.html +80 -80
  263. package/test/test-no-config.js +63 -63
  264. package/test/test-no-filters.html +80 -80
  265. package/test/test-no-filters.js +172 -172
  266. package/test/test-no-headers.html +71 -71
  267. package/test/test-no-headers.js +99 -99
  268. package/test/test-no-results-message.html +262 -262
  269. package/test/test-no-results-message.js +222 -222
  270. package/test/test-no-rows.html +19 -19
  271. package/test/test-no-rows.js +10 -10
  272. package/test/test-one-row.html +29 -29
  273. package/test/test-one-row.js +18 -18
  274. package/test/test-paging.html +78 -78
  275. package/test/test-paging.js +273 -273
  276. package/test/test-popup-filter-one-row.html +38 -38
  277. package/test/test-popup-filter-one-row.js +138 -138
  278. package/test/test-popup-filter.html +78 -78
  279. package/test/test-popup-filter.js +384 -384
  280. package/test/test-popup-filters-grouped-headers.html +104 -104
  281. package/test/test-popup-filters-grouped-headers.js +206 -206
  282. package/test/test-rows-counter.html +77 -77
  283. package/test/test-rows-counter.js +114 -112
  284. package/test/test-single-filter.html +78 -78
  285. package/test/test-single-filter.js +138 -138
  286. package/test/test-sort-custom-sorter.html +243 -243
  287. package/test/test-sort-custom-sorter.js +74 -74
  288. package/test/test-sort-grid-layout.html +142 -142
  289. package/test/test-sort-grid-layout.js +86 -86
  290. package/test/test-sort-key.html +243 -243
  291. package/test/test-sort-key.js +67 -67
  292. package/test/test-sort.html +142 -142
  293. package/test/test-sort.js +126 -126
  294. package/test/test-state.html +78 -78
  295. package/test/test-state.js +369 -369
  296. package/test/test-status-bar.html +79 -79
  297. package/test/test-status-bar.js +149 -146
  298. package/test/test-storage-cookie.html +78 -78
  299. package/test/test-storage-cookie.js +82 -82
  300. package/test/test-storage-local.html +78 -78
  301. package/test/test-storage-local.js +82 -82
  302. package/test/test-tfoot.html +96 -96
  303. package/test/test-tfoot.js +123 -123
  304. package/test/test-thead.html +80 -80
  305. package/test/test-thead.js +69 -69
  306. package/test/test-toolbar.html +81 -81
  307. package/test/test-toolbar.js +218 -218
  308. package/test/test-uneven-rows.html +95 -95
  309. package/test/test-uneven-rows.js +61 -61
  310. package/test/test-watermark.html +80 -80
  311. package/test/test-watermark.js +103 -103
  312. package/test/test.html +141 -141
  313. package/test/test.js +162 -162
  314. package/vite.config.js +71 -0
  315. package/webpack.config.js +99 -87
  316. package/webpack.dev.config.js +25 -25
  317. package/webpack.test.config.js +44 -44
  318. package/.travis.yml +0 -53
  319. 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
+ }