-
Notifications
You must be signed in to change notification settings - Fork 90
/
filters.js
1047 lines (878 loc) · 35.7 KB
/
filters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _templateObject = _taggedTemplateLiteral(['The filter conditions have been applied properly, but couldn\u2019t be displayed visually. \n The overall amount of conditions exceed the capability of the dropdown menu. \n For more details see the documentation.'], ['The filter conditions have been applied properly, but couldn\u2019t be displayed visually. \n The overall amount of conditions exceed the capability of the dropdown menu. \n For more details see the documentation.']);
function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import BasePlugin from 'handsontable/plugins/_base';
import { arrayEach, arrayMap } from 'handsontable/helpers/array';
import { toSingleLine } from 'handsontable/helpers/templateLiteralTag';
import { rangeEach } from 'handsontable/helpers/number';
import EventManager from 'handsontable/eventManager';
import { addClass, removeClass, closest } from 'handsontable/helpers/dom/element';
import { registerPlugin } from 'handsontable/plugins';
import { SEPARATOR } from 'handsontable/plugins/contextMenu/predefinedItems';
import * as constants from 'handsontable/i18n/constants';
import ConditionComponent from './component/condition';
import OperatorsComponent from './component/operators';
import ValueComponent from './component/value';
import ActionBarComponent from './component/actionBar';
import ConditionCollection from './conditionCollection';
import DataFilter from './dataFilter';
import ConditionUpdateObserver from './conditionUpdateObserver';
import { createArrayAssertion, toEmptyString, unifyColumnValues } from './utils';
import { CONDITION_NONE, CONDITION_BY_VALUE, OPERATION_AND, OPERATION_OR, OPERATION_OR_THEN_VARIABLE } from './constants';
/**
* This plugin allows filtering the table data either by the built-in component or with the API.
*
* @plugin Filters
* @pro
* @dependencies DropdownMenu TrimRows BindRowsWithHeaders HiddenRows
*/
var Filters = function (_BasePlugin) {
_inherits(Filters, _BasePlugin);
function Filters(hotInstance) {
_classCallCheck(this, Filters);
/**
* Instance of {@link EventManager}.
*
* @type {EventManager}
*/
var _this = _possibleConstructorReturn(this, (Filters.__proto__ || Object.getPrototypeOf(Filters)).call(this, hotInstance));
_this.eventManager = new EventManager(_this);
/**
* Instance of {@link TrimRows}.
*
* @type {TrimRows}
*/
_this.trimRowsPlugin = null;
/**
* Instance of {@link DropdownMenu}.
*
* @type {DropdownMenu}
*/
_this.dropdownMenuPlugin = null;
/**
* Instance of {@link ConditionCollection}.
*
* @type {ConditionCollection}
*/
_this.conditionCollection = null;
/**
* Instance of {@link ConditionUpdateObserver}.
*
* @type {ConditionUpdateObserver}
*/
_this.conditionUpdateObserver = null;
/**
* Map, where key is component identifier and value represent `BaseComponent` element or it derivatives.
* @type {Map}
*/
_this.components = new Map([['filter_by_condition', null], ['filter_operators', null], ['filter_by_condition2', null], ['filter_by_value', null], ['filter_action_bar', null]]);
/**
* Object containing information about last selected column physical and visual index for added filter conditions.
*
* @type {Object}
* @default null
*/
_this.lastSelectedColumn = null;
/**
* Hidden menu rows indexed by physical column index
*
* @type {Map}
*/
_this.hiddenRowsCache = new Map();
// One listener for the enable/disable functionality
_this.hot.addHook('afterGetColHeader', function (col, TH) {
return _this.onAfterGetColHeader(col, TH);
});
return _this;
}
/**
* Check if the plugin is enabled in the Handsontable settings.
*
* @returns {Boolean}
*/
_createClass(Filters, [{
key: 'isEnabled',
value: function isEnabled() {
/* eslint-disable no-unneeded-ternary */
return this.hot.getSettings().filters ? true : false;
}
/**
* Enable plugin for this Handsontable instance.
*/
}, {
key: 'enablePlugin',
value: function enablePlugin() {
var _this2 = this;
if (this.enabled) {
return;
}
this.trimRowsPlugin = this.hot.getPlugin('trimRows');
this.dropdownMenuPlugin = this.hot.getPlugin('dropdownMenu');
var addConfirmationHooks = function addConfirmationHooks(component) {
component.addLocalHook('accept', function () {
return _this2.onActionBarSubmit('accept');
});
component.addLocalHook('cancel', function () {
return _this2.onActionBarSubmit('cancel');
});
component.addLocalHook('change', function (command) {
return _this2.onComponentChange(component, command);
});
return component;
};
var filterByConditionLabel = function filterByConditionLabel() {
return _this2.hot.getTranslatedPhrase(constants.FILTERS_DIVS_FILTER_BY_CONDITION) + ':';
};
var filterValueLabel = function filterValueLabel() {
return _this2.hot.getTranslatedPhrase(constants.FILTERS_DIVS_FILTER_BY_VALUE) + ':';
};
if (!this.components.get('filter_by_condition')) {
var conditionComponent = new ConditionComponent(this.hot, { id: 'filter_by_condition', name: filterByConditionLabel, addSeparator: false });
conditionComponent.addLocalHook('afterClose', function () {
return _this2.onSelectUIClosed();
});
this.components.set('filter_by_condition', addConfirmationHooks(conditionComponent));
}
if (!this.components.get('filter_operators')) {
this.components.set('filter_operators', new OperatorsComponent(this.hot, { id: 'filter_operators', name: 'Operators' }));
}
if (!this.components.get('filter_by_condition2')) {
var _conditionComponent = new ConditionComponent(this.hot, { id: 'filter_by_condition2', name: '', addSeparator: true });
_conditionComponent.addLocalHook('afterClose', function () {
return _this2.onSelectUIClosed();
});
this.components.set('filter_by_condition2', addConfirmationHooks(_conditionComponent));
}
if (!this.components.get('filter_by_value')) {
this.components.set('filter_by_value', addConfirmationHooks(new ValueComponent(this.hot, { id: 'filter_by_value', name: filterValueLabel })));
}
if (!this.components.get('filter_action_bar')) {
this.components.set('filter_action_bar', addConfirmationHooks(new ActionBarComponent(this.hot, { id: 'filter_action_bar', name: 'Action bar' })));
}
if (!this.conditionCollection) {
this.conditionCollection = new ConditionCollection();
}
if (!this.conditionUpdateObserver) {
this.conditionUpdateObserver = new ConditionUpdateObserver(this.conditionCollection, function (column) {
return _this2.getDataMapAtColumn(column);
});
this.conditionUpdateObserver.addLocalHook('update', function (conditionState) {
return _this2.updateComponents(conditionState);
});
}
this.components.forEach(function (component) {
component.show();
});
this.registerEvents();
this.addHook('beforeDropdownMenuSetItems', function (items) {
return _this2.onBeforeDropdownMenuSetItems(items);
});
this.addHook('afterDropdownMenuDefaultOptions', function (defaultOptions) {
return _this2.onAfterDropdownMenuDefaultOptions(defaultOptions);
});
this.addHook('afterDropdownMenuShow', function () {
return _this2.onAfterDropdownMenuShow();
});
this.addHook('afterDropdownMenuHide', function () {
return _this2.onAfterDropdownMenuHide();
});
this.addHook('afterChange', function (changes, source) {
return _this2.onAfterChange(changes);
});
// force to enable dependent plugins
this.hot.getSettings().trimRows = true;
this.trimRowsPlugin.enablePlugin();
// Temp. solution (extending menu items bug in contextMenu/dropdownMenu)
if (this.hot.getSettings().dropdownMenu) {
this.dropdownMenuPlugin.disablePlugin();
this.dropdownMenuPlugin.enablePlugin();
}
_get(Filters.prototype.__proto__ || Object.getPrototypeOf(Filters.prototype), 'enablePlugin', this).call(this);
}
/**
* Register the DOM listeners.
*
* @private
*/
}, {
key: 'registerEvents',
value: function registerEvents() {
var _this3 = this;
this.eventManager.addEventListener(this.hot.rootElement, 'click', function (event) {
return _this3.onTableClick(event);
});
}
/**
* Disable plugin for this Handsontable instance.
*/
}, {
key: 'disablePlugin',
value: function disablePlugin() {
if (this.enabled) {
if (this.dropdownMenuPlugin.enabled) {
this.dropdownMenuPlugin.menu.clearLocalHooks();
}
this.components.forEach(function (component) {
component.hide();
});
this.conditionCollection.clean();
this.trimRowsPlugin.untrimAll();
}
_get(Filters.prototype.__proto__ || Object.getPrototypeOf(Filters.prototype), 'disablePlugin', this).call(this);
}
/**
* @description
* Add condition to the conditions collection at specified column index.
*
* Possible predefined conditions:
* * `begins_with` - Begins with
* * `between` - Between
* * `by_value` - By value
* * `contains` - Contains
* * `empty` - Empty
* * `ends_with` - Ends with
* * `eq` - Equal
* * `gt` - Greater than
* * `gte` - Greater than or equal
* * `lt` - Less than
* * `lte` - Less than or equal
* * `none` - None (no filter)
* * `not_between` - Not between
* * `not_contains` - Not contains
* * `not_empty` - Not empty
* * `neq` - Not equal
*
* Possible operations:
* * `conjunction` - [**Conjunction**](https://en.wikipedia.org/wiki/Logical_conjunction) on conditions collection (by default)
* * `disjunction` - [**Disjunction**](https://en.wikipedia.org/wiki/Logical_disjunction) on conditions collection
*
* @example
* ```js
* // Add filter "Greater than" 95 to column at index 1
* hot.getPlugin('filters').addCondition(1, 'gt', [95]);
* hot.getPlugin('filters').filter();
*
* // Add filter "By value" to column at index 1
* // In this case all value's that don't match will be filtered.
* hot.getPlugin('filters').addCondition(1, 'by_value', [['ing', 'ed', 'as', 'on']]);
* hot.getPlugin('filters').filter();
*
* // Add filter "Begins with" with value "de" AND "Not contains" with value "ing"
* hot.getPlugin('filters').addCondition(1, 'begins_with', ['de'], 'conjunction');
* hot.getPlugin('filters').addCondition(1, 'not_contains', ['ing'], 'conjunction');
* hot.getPlugin('filters').filter();
*
* // Add filter "Begins with" with value "de" OR "Not contains" with value "ing"
* hot.getPlugin('filters').addCondition(1, 'begins_with', ['de'], 'disjunction');
* hot.getPlugin('filters').addCondition(1, 'not_contains', ['ing'], 'disjunction');
* hot.getPlugin('filters').filter();
* ```
* @param {Number} column Visual column index.
* @param {String} name Condition short name.
* @param {Array} args Condition arguments.
* @param {String} operationId `id` of operation which is performed on the column
*/
}, {
key: 'addCondition',
value: function addCondition(column, name, args) {
var operationId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : OPERATION_AND;
var physicalColumn = this.t.toPhysicalColumn(column);
this.conditionCollection.addCondition(physicalColumn, { command: { key: name }, args: args }, operationId);
}
/**
* Remove conditions at specified column index.
*
* @param {Number} column Visual column index.
*/
}, {
key: 'removeConditions',
value: function removeConditions(column) {
var physicalColumn = this.t.toPhysicalColumn(column);
this.conditionCollection.removeConditions(physicalColumn);
}
/**
* Clear all conditions previously added to the collection for the specified column index or, if the column index
* was not passed, clear the conditions for all columns.
*
* @param {Number} [column] Visual column index.
*/
}, {
key: 'clearConditions',
value: function clearConditions(column) {
if (column === void 0) {
this.conditionCollection.clean();
} else {
var physicalColumn = this.t.toPhysicalColumn(column);
this.conditionCollection.clearConditions(physicalColumn);
}
}
/**
* Filter data based on added filter conditions.
*/
}, {
key: 'filter',
value: function filter() {
var dataFilter = this._createDataFilter();
var needToFilter = !this.conditionCollection.isEmpty();
var visibleVisualRows = [];
var conditions = this.conditionCollection.exportAllConditions();
var allowFiltering = this.hot.runHooks('beforeFilter', conditions);
if (allowFiltering !== false) {
if (needToFilter) {
var trimmedRows = [];
this.trimRowsPlugin.trimmedRows.length = 0;
visibleVisualRows = arrayMap(dataFilter.filter(), function (rowData) {
return rowData.meta.visualRow;
});
var visibleVisualRowsAssertion = createArrayAssertion(visibleVisualRows);
rangeEach(this.hot.countSourceRows() - 1, function (row) {
if (!visibleVisualRowsAssertion(row)) {
trimmedRows.push(row);
}
});
this.trimRowsPlugin.trimRows(trimmedRows);
if (!visibleVisualRows.length) {
this.hot.deselectCell();
}
} else {
this.trimRowsPlugin.untrimAll();
}
}
this.hot.view.wt.wtOverlays.adjustElementsSize(true);
this.hot.render();
this.clearColumnSelection();
this.hot.runHooks('afterFilter', conditions);
}
/**
* Get last selected column index.
*
* @returns {Object|null} Return `null` when column isn't selected otherwise
* object containing information about selected column with keys `visualIndex` and `physicalIndex`
*/
}, {
key: 'getSelectedColumn',
value: function getSelectedColumn() {
return this.lastSelectedColumn;
}
/**
* Clear column selection.
*/
}, {
key: 'clearColumnSelection',
value: function clearColumnSelection() {
var _ref = this.hot.getSelectedLast() || [],
_ref2 = _slicedToArray(_ref, 2),
row = _ref2[0],
col = _ref2[1];
if (row !== void 0 && col !== void 0) {
this.hot.selectCell(row, col);
}
}
/**
* Get handsontable source data with cell meta based on current selection.
*
* @param {Number} [column] Column index. By default column index accept the value of the selected column.
* @returns {Array} Returns array of objects where keys as row index.
*/
}, {
key: 'getDataMapAtColumn',
value: function getDataMapAtColumn(column) {
var _this4 = this;
var visualIndex = this.t.toVisualColumn(column);
var data = [];
arrayEach(this.hot.getSourceDataAtCol(visualIndex), function (value, rowIndex) {
var _hot$getCellMeta = _this4.hot.getCellMeta(rowIndex, visualIndex),
row = _hot$getCellMeta.row,
col = _hot$getCellMeta.col,
visualCol = _hot$getCellMeta.visualCol,
visualRow = _hot$getCellMeta.visualRow,
type = _hot$getCellMeta.type,
instance = _hot$getCellMeta.instance,
dateFormat = _hot$getCellMeta.dateFormat;
data.push({
meta: { row: row, col: col, visualCol: visualCol, visualRow: visualRow, type: type, instance: instance, dateFormat: dateFormat },
value: toEmptyString(value)
});
});
return data;
}
/**
* `afterChange` listener.
*
* @private
* @param {Array} changes Array of changes.
*/
}, {
key: 'onAfterChange',
value: function onAfterChange(changes) {
var _this5 = this;
if (changes) {
arrayEach(changes, function (change) {
var _change = _slicedToArray(change, 2),
prop = _change[1];
var columnIndex = _this5.hot.propToCol(prop);
_this5.updateValueComponentCondition(columnIndex);
});
}
}
/**
* Update condition of ValueComponent basing on handled changes
*
* @private
* @param {Number} columnIndex Column index of handled ValueComponent condition
*/
}, {
key: 'updateValueComponentCondition',
value: function updateValueComponentCondition(columnIndex) {
var dataAtCol = this.hot.getDataAtCol(columnIndex);
var selectedValues = unifyColumnValues(dataAtCol);
this.conditionUpdateObserver.updateStatesAtColumn(columnIndex, selectedValues);
}
/**
* After dropdown menu show listener.
*
* @private
*/
}, {
key: 'onAfterDropdownMenuShow',
value: function onAfterDropdownMenuShow() {
var selectedColumn = this.getSelectedColumn();
var physicalIndex = selectedColumn && selectedColumn.physicalIndex;
this.components.forEach(function (component) {
if (!component.isHidden() && component.setState) {
component.restoreState(physicalIndex);
}
});
}
/**
* After dropdown menu hide listener.
*
* @private
*/
}, {
key: 'onAfterDropdownMenuHide',
value: function onAfterDropdownMenuHide() {
this.components.get('filter_by_condition').getSelectElement().closeOptions();
this.components.get('filter_by_condition2').getSelectElement().closeOptions();
}
/**
* Before dropdown menu set menu items listener.
*
* @private
* @param {Array} items DropdownMenu items created based on predefined items and settings provided by user.
*/
}, {
key: 'onBeforeDropdownMenuSetItems',
value: function onBeforeDropdownMenuSetItems(items) {
var menuKeys = arrayMap(items, function (item) {
return item.key;
});
this.components.forEach(function (component) {
component[menuKeys.indexOf(component.getMenuItemDescriptor().key) === -1 ? 'hide' : 'show']();
});
this.initHiddenRowsCache();
}
/**
* After dropdown menu default options listener.
*
* @private
* @param {Object} defaultOptions ContextMenu default item options.
*/
}, {
key: 'onAfterDropdownMenuDefaultOptions',
value: function onAfterDropdownMenuDefaultOptions(defaultOptions) {
defaultOptions.items.push({ name: SEPARATOR });
this.components.forEach(function (component) {
defaultOptions.items.push(component.getMenuItemDescriptor());
});
}
/**
* Get operation basing on number and type of arguments (where arguments are states of components)
*
* @param {String} suggestedOperation operation which was chosen by user from UI
* @param {Object} byConditionState1 state of first condition component
* @param {Object} byConditionState2 state of second condition component
* @param {Object} byValueState state of value component
* @private
* @returns {String}
*/
}, {
key: 'getOperationBasedOnArguments',
value: function getOperationBasedOnArguments(suggestedOperation, byConditionState1, byConditionState2, byValueState) {
var operation = suggestedOperation;
if (operation === OPERATION_OR && byConditionState1.command.key !== CONDITION_NONE && byConditionState2.command.key !== CONDITION_NONE && byValueState.command.key !== CONDITION_NONE) {
operation = OPERATION_OR_THEN_VARIABLE;
} else if (byValueState.command.key !== CONDITION_NONE) {
if (byConditionState1.command.key === CONDITION_NONE || byConditionState2.command.key === CONDITION_NONE) {
operation = OPERATION_AND;
}
}
return operation;
}
/**
* On action bar submit listener.
*
* @private
* @param {String} submitType
*/
}, {
key: 'onActionBarSubmit',
value: function onActionBarSubmit(submitType) {
if (submitType === 'accept') {
var selectedColumn = this.getSelectedColumn();
var physicalIndex = selectedColumn && selectedColumn.physicalIndex;
var byConditionState1 = this.components.get('filter_by_condition').getState();
var byConditionState2 = this.components.get('filter_by_condition2').getState();
var byValueState = this.components.get('filter_by_value').getState();
var operation = this.getOperationBasedOnArguments(this.components.get('filter_operators').getActiveOperationId(), byConditionState1, byConditionState2, byValueState);
this.conditionUpdateObserver.groupChanges();
this.conditionCollection.clearConditions(physicalIndex);
if (byConditionState1.command.key === CONDITION_NONE && byConditionState2.command.key === CONDITION_NONE && byValueState.command.key === CONDITION_NONE) {
this.conditionCollection.removeConditions(physicalIndex);
} else {
if (byConditionState1.command.key !== CONDITION_NONE) {
this.conditionCollection.addCondition(physicalIndex, byConditionState1, operation);
if (byConditionState2.command.key !== CONDITION_NONE) {
this.conditionCollection.addCondition(physicalIndex, byConditionState2, operation);
}
}
if (byValueState.command.key !== CONDITION_NONE) {
this.conditionCollection.addCondition(physicalIndex, byValueState, operation);
}
}
this.conditionUpdateObserver.flush();
this.components.get('filter_operators').saveState(physicalIndex);
this.components.get('filter_by_value').saveState(physicalIndex);
this.saveHiddenRowsCache(physicalIndex);
this.trimRowsPlugin.trimmedRows.length = 0;
this.filter();
}
this.dropdownMenuPlugin.close();
}
/**
* On component change listener.
*
* @private
* @param {BaseComponent} component Component inheriting BaseComponent
* @param {Object} command Menu item object (command).
*/
}, {
key: 'onComponentChange',
value: function onComponentChange(component, command) {
if (component === this.components.get('filter_by_condition')) {
if (command.showOperators) {
this.showComponents(this.components.get('filter_by_condition2'), this.components.get('filter_operators'));
} else {
this.hideComponents(this.components.get('filter_by_condition2'), this.components.get('filter_operators'));
}
}
if (component.constructor === ConditionComponent && !command.inputsCount) {
this.setListeningDropdownMenu();
}
}
/**
* On component SelectUI closed listener.
*
* @private
*/
}, {
key: 'onSelectUIClosed',
value: function onSelectUIClosed() {
this.setListeningDropdownMenu();
}
/**
* Listen to the keyboard input on document body and forward events to instance of Handsontable
* created by DropdownMenu plugin
*
* @private
*/
}, {
key: 'setListeningDropdownMenu',
value: function setListeningDropdownMenu() {
this.dropdownMenuPlugin.setListening();
}
/**
* On after get column header listener.
*
* @private
* @param {Number} col
* @param {HTMLTableCellElement} TH
*/
}, {
key: 'onAfterGetColHeader',
value: function onAfterGetColHeader(col, TH) {
var physicalColumn = this.t.toPhysicalColumn(col);
if (this.enabled && this.conditionCollection.hasConditions(physicalColumn)) {
addClass(TH, 'htFiltersActive');
} else {
removeClass(TH, 'htFiltersActive');
}
}
/**
* On table click listener.
*
* @private
* @param {Event} event DOM Event.
*/
}, {
key: 'onTableClick',
value: function onTableClick(event) {
var th = closest(event.target, 'TH');
if (th) {
var visualIndex = this.hot.getCoords(th).col;
var physicalIndex = this.t.toPhysicalColumn(visualIndex);
this.lastSelectedColumn = {
visualIndex: visualIndex,
physicalIndex: physicalIndex
};
}
}
/**
* Destroy plugin.
*/
}, {
key: 'destroy',
value: function destroy() {
if (this.enabled) {
this.components.forEach(function (component) {
component.destroy();
});
this.conditionCollection.destroy();
this.conditionUpdateObserver.destroy();
this.hiddenRowsCache.clear();
this.trimRowsPlugin.disablePlugin();
}
_get(Filters.prototype.__proto__ || Object.getPrototypeOf(Filters.prototype), 'destroy', this).call(this);
}
/**
* Create DataFilter instance based on condition collection.
*
* @private
* @param {ConditionCollection} conditionCollection Condition collection object.
* @returns {DataFilter}
*/
}, {
key: '_createDataFilter',
value: function _createDataFilter() {
var _this6 = this;
var conditionCollection = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.conditionCollection;
return new DataFilter(conditionCollection, function (column) {
return _this6.getDataMapAtColumn(column);
});
}
/**
* Update components basing on conditions state.
*
* @private
* @param {Object} conditionsState
*/
}, {
key: 'updateComponents',
value: function updateComponents(conditionsState) {
if (!this.dropdownMenuPlugin.enabled) {
return;
}
var conditions = conditionsState.editedConditionStack.conditions;
var column = conditionsState.editedConditionStack.column;
var conditionsByValue = conditions.filter(function (condition) {
return condition.name === CONDITION_BY_VALUE;
});
var conditionsWithoutByValue = conditions.filter(function (condition) {
return condition.name !== CONDITION_BY_VALUE;
});
var operationType = this.conditionCollection.columnTypes[column];
if (conditionsByValue.length === 2 || conditionsWithoutByValue.length === 3) {
console.warn(toSingleLine(_templateObject));
} else {
if (conditionsWithoutByValue.length > 0) {
this.showComponentForParticularColumn(this.components.get('filter_operators'), column);
}
this.components.get('filter_by_condition').updateState(conditionsWithoutByValue[0], column);
this.components.get('filter_by_condition2').updateState(conditionsWithoutByValue[1], column);
this.components.get('filter_by_value').updateState(conditionsState);
this.components.get('filter_operators').updateState(operationType, column);
}
}
/**
* Show component for particular column.
*
* @private
* @param {BaseComponent} component `BaseComponent` element or it derivatives.
* @param {Number} column Physical column index.
*/
}, {
key: 'showComponentForParticularColumn',
value: function showComponentForParticularColumn(component, column) {
if (!this.hiddenRowsCache.has(column)) {
this.hiddenRowsCache.set(column, []);
} else {
var indexes = this.getIndexesOfComponents(component);
this.removeIndexesFromHiddenRowsCache(column, indexes);
}
}
/**
* Remove specific rows from `hiddenRows` cache for particular column.
*
* @private
* @param {Number} column Physical column index.
* @param {Array} indexes Physical indexes of rows which will be removed from `hiddenRows` cache
*/
}, {
key: 'removeIndexesFromHiddenRowsCache',
value: function removeIndexesFromHiddenRowsCache(column, indexes) {
var hiddenRowsForColumn = this.hiddenRowsCache.get(column);
arrayEach(indexes, function (index) {
if (hiddenRowsForColumn.includes(index)) {
hiddenRowsForColumn.splice(hiddenRowsForColumn.indexOf(index), 1);
}
});
}
/**
* Hide component for particular column.
*
* @private
* @param {BaseComponent} component `BaseComponent` element or it derivatives.
* @param {Number} column Physical column index.
*/
}, {
key: 'hideComponentForParticularColumn',
value: function hideComponentForParticularColumn(component, column) {
if (!this.hiddenRowsCache.has(column)) {
this.hiddenRowsCache.set(column, this.getIndexesOfComponents(component));
} else {
var indexes = this.getIndexesOfComponents(component);
this.addIndexesToHiddenRowsCache(column, indexes);
}
}
/**
* Add specific rows to `hiddenRows` cache for particular column.
*
* @private
* @param column Physical column index.
* @param indexes Physical indexes of rows which will be added to `hiddenRows` cache
*/
}, {
key: 'addIndexesToHiddenRowsCache',
value: function addIndexesToHiddenRowsCache(column, indexes) {
var hiddenRowsForColumn = this.hiddenRowsCache.get(column);
arrayEach(indexes, function (index) {
if (hiddenRowsForColumn.indexOf(index) === -1) {
hiddenRowsForColumn.push(index);
}
});
}
/**
* Get indexes of passed components inside list of `dropdownMenu` items.
*
* @private
* @param {...BaseComponent} components List of components.
* @returns {Array}
*/
}, {
key: 'getIndexesOfComponents',
value: function getIndexesOfComponents() {
var menu = this.dropdownMenuPlugin.menu;
var indexes = [];
for (var _len = arguments.length, components = Array(_len), _key = 0; _key < _len; _key++) {
components[_key] = arguments[_key];
}
arrayEach(components, function (component) {
arrayEach(menu.menuItems, function (item, index) {
if (item.key === component.getMenuItemDescriptor().key) {
indexes.push(index);
}
});
});
return indexes;
}
/**
* Change visibility of component.
*
* @private
* @param {Boolean} visible Determine if components should be visible.
* @param {...BaseComponent} components List of components.
*/
}, {
key: 'changeComponentsVisibility',
value: function changeComponentsVisibility() {
var visible = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
var menu = this.dropdownMenuPlugin.menu;
var hotMenu = menu.hotMenu;
var hiddenRows = hotMenu.getPlugin('hiddenRows');
for (var _len2 = arguments.length, components = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
components[_key2 - 1] = arguments[_key2];
}
var indexes = this.getIndexesOfComponents.apply(this, components);
if (visible) {
hiddenRows.showRows(indexes);
} else {
hiddenRows.hideRows(indexes);
}
hotMenu.render();
}
/**
* Init `hiddenRows` cache.
*
* @private
*/
}, {
key: 'initHiddenRowsCache',
value: function initHiddenRowsCache() {
var _this7 = this;
this.dropdownMenuPlugin.menu.addLocalHook('afterOpen', function () {
var index = _this7.lastSelectedColumn.physicalIndex;
if (!_this7.hiddenRowsCache.has(index)) {
_this7.hiddenRowsCache.set(index, _this7.getIndexesOfComponents(_this7.components.get('filter_operators'), _this7.components.get('filter_by_condition2')));
}
_this7.dropdownMenuPlugin.menu.hotMenu.updateSettings({ hiddenRows: { rows: _this7.hiddenRowsCache.get(index) } });
});
}
/**
* Save `hiddenRows` cache for particular row.
*
* @private
* @param rowIndex Physical row index
*/