-
Notifications
You must be signed in to change notification settings - Fork 95
/
tablefilter.js
3053 lines (2681 loc) · 90.2 KB
/
tablefilter.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
import {addEvt, cancelEvt, stopEvt, targetEvt, isKeyPressed} from './event';
import {
addClass, createElm, elm, getText, getFirstTextNode, removeClass, tag
} from './dom';
import {contains, matchCase, rgxEsc, trim, toCamelCase, uuid} from './string';
import {
isArray, isEmpty, isFn, isNumber, isObj, isString, isUndef, EMPTY_FN,
isBoolean
} from './types';
import {parse as parseNb} from './number';
import {
defaultsBool, defaultsStr, defaultsFn,
defaultsNb, defaultsArr
} from './settings';
import {root} from './root';
import {Emitter} from './emitter';
import {Dropdown} from './modules/dropdown';
import {CheckList} from './modules/checkList';
import {DateType} from './modules/dateType';
import {Help} from './modules/help';
import {State} from './modules/state';
import {GridLayout} from './modules/gridLayout';
import {Loader} from './modules/loader';
import {HighlightKeyword} from './modules/highlightKeywords';
import {PopupFilter} from './modules/popupFilter';
import {MarkActiveColumns} from './modules/markActiveColumns';
import {RowsCounter} from './modules/rowsCounter';
import {StatusBar} from './modules/statusBar';
import {ClearButton} from './modules/clearButton';
import {AlternateRows} from './modules/alternateRows';
import {NoResults} from './modules/noResults';
import {Paging} from './modules/paging';
import {Toolbar} from './modules/toolbar';
import {
INPUT, SELECT, MULTIPLE, CHECKLIST, NONE,
ENTER_KEY, TAB_KEY, ESC_KEY, UP_ARROW_KEY, DOWN_ARROW_KEY,
CELL_TAG, AUTO_FILTER_DELAY, NUMBER, DATE, FORMATTED_NUMBER
} from './const';
let doc = root.document;
const FEATURES = [
DateType, Help, State, MarkActiveColumns, GridLayout, Loader,
HighlightKeyword, PopupFilter, RowsCounter, StatusBar, ClearButton,
AlternateRows, NoResults, Paging, Toolbar
];
/**
* Makes HTML tables filterable and a bit more :)
*
* @export
* @class TableFilter
*/
export class TableFilter {
/**
* Creates an instance of TableFilter
* requires `table` or `id` arguments, `row` and `configuration` optional
* @param {HTMLTableElement} table Table DOM element
* @param {String} id Table id
* @param {Number} row index indicating the 1st row
* @param {Object} configuration object
*/
constructor(...args) {
/**
* ID of current instance
* @type {String}
* @private
*/
this.id = null;
/**
* Current version
* @type {String}
*/
this.version = '{VERSION}';
/**
* Current year
* @type {Number}
* @private
*/
this.year = new Date().getFullYear();
/**
* HTML Table DOM element
* @type {DOMElement}
* @private
*/
this.tbl = null;
/**
* Calculated row's index from which starts filtering once filters
* are generated
* @type {Number}
*/
this.refRow = null;
/**
* Index of the headers row
* @type {Number}
* @private
*/
this.headersRow = null;
/**
* Configuration object
* @type {Object}
* @private
*/
this.cfg = {};
/**
* Number of rows that can be filtered
* @type {Number}
* @private
*/
this.nbFilterableRows = 0;
/**
* Number of cells in the reference row
* @type {Number}
* @private
*/
this.nbCells = null;
/**
* Has a configuration object
* @type {Object}
* @private
*/
this.hasConfig = false;
/** @private */
this.initialized = false;
let startRow;
// TODO: use for-of
args.forEach((arg) => {
if (typeof arg === 'object' && arg.nodeName === 'TABLE') {
this.tbl = arg;
this.id = arg.id || `tf_${uuid()}`;
this.tbl.id = this.id;
} else if (isString(arg)) {
this.id = arg;
this.tbl = elm(arg);
} else if (isNumber(arg)) {
startRow = arg;
} else if (isObj(arg)) {
this.cfg = arg;
this.hasConfig = true;
}
});
if (!this.tbl || this.tbl.nodeName !== 'TABLE') {
throw new Error(`Could not instantiate TableFilter: HTML table
DOM element not found.`);
}
if (this.getRowsNb(true) === 0) {
throw new Error(`Could not instantiate TableFilter: HTML table
requires at least 1 row.`);
}
// configuration object
let f = this.cfg;
/**
* Event emitter instance
* @type {Emitter}
*/
this.emitter = new Emitter();
// start row
this.refRow = isUndef(startRow) ? 2 : (startRow + 1);
/**
* Collection of filter type by column
* @type {Array}
* @private
*/
this.filterTypes = [].map.call(
(this.dom().rows[this.refRow] || this.dom().rows[0]).cells,
(cell, idx) => {
let colType = this.cfg[`col_${idx}`];
return !colType ? INPUT : colType.toLowerCase();
});
/**
* Base path for static assets
* @type {String}
*/
this.basePath = defaultsStr(f.base_path, 'tablefilter/');
/*** filters' grid properties ***/
/**
* Enable/disable filters
* @type {Boolean}
*/
this.fltGrid = defaultsBool(f.grid, true);
/**
* Enable/disable grid layout (fixed headers)
* @type {Object|Boolean}
*/
this.gridLayout = isObj(f.grid_layout) || Boolean(f.grid_layout);
/**
* Filters row index
* @type {Number}
*/
this.filtersRowIndex = defaultsNb(f.filters_row_index, 0);
/**
* Headers row index
* @type {Number}
*/
this.headersRow = defaultsNb(f.headers_row_index,
(this.filtersRowIndex === 0 ? 1 : 0));
/**
* Define the type of cell containing a filter (td/th)
* @type {String}
*/
this.fltCellTag = defaultsStr(f.filters_cell_tag, CELL_TAG);
/**
* List of filters IDs
* @type {Array}
* @private
*/
this.fltIds = [];
/**
* List of valid rows indexes (rows visible upon filtering)
* @type {Array}
* @private
*/
this.validRowsIndex = [];
/*** filters' grid appearance ***/
/**
* Path for stylesheets
* @type {String}
*/
this.stylePath = this.getStylePath();
/**
* Main stylesheet path
* @type {String}
*/
this.stylesheet = this.getStylesheetPath();
/**
* Main stylesheet ID
* @type {String}
* @private
*/
this.stylesheetId = this.id + '_style';
/**
* Css class for the filters row
* @type {String}
*/
this.fltsRowCssClass = defaultsStr(f.flts_row_css_class, 'fltrow');
/**
* Enable/disable icons (paging, reset button)
* @type {Boolean}
*/
this.enableIcons = defaultsBool(f.enable_icons, true);
/**
* Enable/disable alternating rows
* @type {Boolean}
*/
this.alternateRows = Boolean(f.alternate_rows);
/**
* Columns widths array
* @type {Array}
*/
this.colWidths = defaultsArr(f.col_widths, []);
/**
* Default column width when column widths are defined
*/
this.defaultColWidth = defaultsNb(f.default_col_width, 100);
/**
* Css class for a filter element
* @type {String}
*/
this.fltCssClass = defaultsStr(f.flt_css_class, 'flt');
/**
* Css class for multiple select filters
* @type {String}
*/
this.fltMultiCssClass = defaultsStr(f.flt_multi_css_class, 'flt_multi');
/**
* Css class for small filter (when submit button is active)
* @type {String}
*/
this.fltSmallCssClass = defaultsStr(f.flt_small_css_class, 'flt_s');
/**
* Css class for single filter type
* @type {String}
*/
this.singleFltCssClass = defaultsStr((f.single_filter || {}).css_class,
'single_flt');
/*** filters' grid behaviours ***/
/**
* Enable/disable enter key for input type filters
* @type {Boolean}
*/
this.enterKey = defaultsBool(f.enter_key, true);
/**
* Callback fired before filtering process starts
* @type {Function}
*/
this.onBeforeFilter = defaultsFn(f.on_before_filter, EMPTY_FN);
/**
* Callback fired after filtering process is completed
* @type {Function}
*/
this.onAfterFilter = defaultsFn(f.on_after_filter, EMPTY_FN);
/**
* Enable/disable case sensitivity for filtering, default false
* @type {Boolean}
*/
this.caseSensitive = Boolean(f.case_sensitive);
/**
* Indicate whether exact match filtering is enabled on a per column
* basis
* @type {Boolean}
* @private
*/
this.hasExactMatchByCol = isArray(f.columns_exact_match);
/**
* Exact match filtering per column array
* @type {Array}
*/
this.exactMatchByCol = this.hasExactMatchByCol ?
f.columns_exact_match : [];
/**
* Globally enable/disable exact match filtering
* @type {Boolean}
*/
this.exactMatch = Boolean(f.exact_match);
/**
* Ignore diacritics globally or on a column basis
* @type {Boolean|Array}
*/
this.ignoreDiacritics = f.ignore_diacritics;
/**
* Enable/disable linked filters filtering mode
* @type {Boolean}
*/
this.linkedFilters = Boolean(f.linked_filters);
/**
* Enable/disable readonly state for excluded options when
* linked filters filtering mode is on
* @type {Boolean}
*/
this.disableExcludedOptions = Boolean(f.disable_excluded_options);
/**
* Active filter ID
* @type {String}
* @private
*/
this.activeFilterId = null;
/**
* Determine if there are excluded rows from filtering
* @type {Boolean}
* @private
*/
this.hasExcludedRows = Boolean(isArray(f.exclude_rows) &&
f.exclude_rows.length > 0);
/**
* List of row indexes to be excluded from filtering
* @type {Array}
*/
this.excludeRows = defaultsArr(f.exclude_rows, []);
/**
* List of containers IDs where external filters will be generated
* @type {Array}
*/
this.externalFltIds = defaultsArr(f.external_flt_ids, []);
/**
* Callback fired after filters are generated
* @type {Function}
*/
this.onFiltersLoaded = defaultsFn(f.on_filters_loaded, EMPTY_FN);
/**
* Enable/disable single filter mode
* @type {Boolean|Object}
*/
this.singleFlt = isObj(f.single_filter) || Boolean(f.single_filter);
/**
* Specify columns to be excluded from single filter search, by default
* searching in all columns:
* single_filter: {
* exclude_cols: [2, 7]
* }
*/
this.singleFltExcludeCols = isObj(f.single_filter) &&
isArray(f.single_filter.exclude_cols) ?
f.single_filter.exclude_cols : [];
/**
* Callback fired after a row is validated during filtering
* @type {Function}
*/
this.onRowValidated = defaultsFn(f.on_row_validated, EMPTY_FN);
/**
* Specify which column implements a custom cell parser to retrieve the
* cell value:
* cell_parser: {
* cols: [0, 2],
* parse: function(tf, cell, colIndex) {
* // custom cell parser logic here
* return cellValue;
* }
* }
* @type {Object}
*/
this.cellParser = isObj(f.cell_parser) && isFn(f.cell_parser.parse) &&
isArray(f.cell_parser.cols) ?
f.cell_parser : { cols: [], parse: EMPTY_FN };
/**
* Global watermark text for input filter type or watermark for each
* filter if an array is supplied
* @type {String|Array}
*/
this.watermark = f.watermark || '';
/**
* Indicate whether watermark is on a per column basis
* @type {Boolean}
* @private
*/
this.isWatermarkArray = isArray(this.watermark);
/**
* Indicate whether help UI component is disabled
* @type {Boolean}
*/
this.help = isUndef(f.help_instructions) ? undefined :
(isObj(f.help_instructions) || Boolean(f.help_instructions));
/**
* Indicate whether pop-up filters UI is enabled
* @type {Boolean|Object}
*/
this.popupFilters = isObj(f.popup_filters) || Boolean(f.popup_filters);
/**
* Indicate whether filtered (active) columns indicator is enabled
* @type {Boolean}
*/
this.markActiveColumns = isObj(f.mark_active_columns) ||
Boolean(f.mark_active_columns);
/*** select filter's customisation and behaviours ***/
/**
* Text for clear option in drop-down filter types (1st option)
* @type {String|Array}
*/
this.clearFilterText = isArray(f.clear_filter_text)
? f.clear_filter_text
: defaultsStr(f.clear_filter_text, 'Clear');
/**
* Indicate whether empty option is enabled in drop-down filter types
* @type {Boolean}
*/
this.enableEmptyOption = Boolean(f.enable_empty_option);
/**
* Text for empty option in drop-down filter types
* @type {String}
*/
this.emptyText = defaultsStr(f.empty_text, '(Empty)');
/**
* Indicate whether non-empty option is enabled in drop-down filter
* types
* @type {Boolean}
*/
this.enableNonEmptyOption = Boolean(f.enable_non_empty_option);
/**
* Text for non-empty option in drop-down filter types
* @type {String}
*/
this.nonEmptyText = defaultsStr(f.non_empty_text, '(Non empty)');
/**
* Indicate whether drop-down filter types filter the table by default
* on change event
* @type {Boolean}
*/
this.onSlcChange = defaultsBool(f.on_change, true);
/**
* Make drop-down filter types options sorted in alpha-numeric manner
* by default globally or on a column basis
* @type {Boolean|Array}
*/
this.sortSlc = isUndef(f.sort_select)
? true
: defaultsArr(f.sort_select, Boolean(f.sort_select));
/**
* List of columns implementing filter options sorting in ascending
* manner based on column data type
* @type {Array}
*/
this.sortFilterOptionsAsc = defaultsArr(f.sort_filter_options_asc, []);
/**
* List of columns implementing filter options sorting in descending
* manner based on column data type
* @type {Array}
*/
this.sortFilterOptionsDesc =
defaultsArr(f.sort_filter_options_desc, []);
/**
* Indicate whether drop-down filter types are populated on demand at
* first usage
* @type {Boolean}
*/
this.loadFltOnDemand = Boolean(f.load_filters_on_demand);
/**
* Indicate whether custom drop-down filter options are implemented
* @type {Boolean}
*/
this.hasCustomOptions = isObj(f.custom_options);
/**
* Custom options definition of a per column basis, ie:
* custom_options: {
* cols:[0, 1],
* texts: [
* ['a0', 'b0', 'c0'],
* ['a1', 'b1', 'c1']
* ],
* values: [
* ['a0', 'b0', 'c0'],
* ['a1', 'b1', 'c1']
* ],
* sorts: [false, true]
* }
*
* @type {Object}
*/
this.customOptions = f.custom_options;
/*** Filter operators ***/
/**
* Regular expression operator for input filter. Defaults to 'rgx:'
* @type {String}
*/
this.rgxOperator = defaultsStr(f.regexp_operator, 'rgx:');
/**
* Empty cells operator for input filter. Defaults to '[empty]'
* @type {String}
*/
this.emOperator = defaultsStr(f.empty_operator, '[empty]');
/**
* Non-empty cells operator for input filter. Defaults to '[nonempty]'
* @type {String}
*/
this.nmOperator = defaultsStr(f.nonempty_operator, '[nonempty]');
/**
* Logical OR operator for input filter. Defaults to '||'
* @type {String}
*/
this.orOperator = defaultsStr(f.or_operator, '||');
/**
* Logical AND operator for input filter. Defaults to '&&'
* @type {String}
*/
this.anOperator = defaultsStr(f.and_operator, '&&');
/**
* Greater than operator for input filter. Defaults to '>'
* @type {String}
*/
this.grOperator = defaultsStr(f.greater_operator, '>');
/**
* Lower than operator for input filter. Defaults to '<'
* @type {String}
*/
this.lwOperator = defaultsStr(f.lower_operator, '<');
/**
* Lower than or equal operator for input filter. Defaults to '<='
* @type {String}
*/
this.leOperator = defaultsStr(f.lower_equal_operator, '<=');
/**
* Greater than or equal operator for input filter. Defaults to '>='
* @type {String}
*/
this.geOperator = defaultsStr(f.greater_equal_operator, '>=');
/**
* Inequality operator for input filter. Defaults to '!'
* @type {String}
*/
this.dfOperator = defaultsStr(f.different_operator, '!');
/**
* Like operator for input filter. Defaults to '*'
* @type {String}
*/
this.lkOperator = defaultsStr(f.like_operator, '*');
/**
* Strict equality operator for input filter. Defaults to '='
* @type {String}
*/
this.eqOperator = defaultsStr(f.equal_operator, '=');
/**
* Starts with operator for input filter. Defaults to '='
* @type {String}
*/
this.stOperator = defaultsStr(f.start_with_operator, '{');
/**
* Ends with operator for input filter. Defaults to '='
* @type {String}
*/
this.enOperator = defaultsStr(f.end_with_operator, '}');
// this.curExp = f.cur_exp || '^[¥£€$]';
/**
* Stored values separator
* @type {String}
*/
this.separator = defaultsStr(f.separator, ',');
/**
* Enable rows counter UI component
* @type {Boolean|Object}
*/
this.rowsCounter = isObj(f.rows_counter) || Boolean(f.rows_counter);
/**
* Enable status bar UI component
* @type {Boolean|Object}
*/
this.statusBar = isObj(f.status_bar) || Boolean(f.status_bar);
/**
* Enable activity/spinner indicator UI component
* @type {Boolean|Object}
*/
this.loader = isObj(f.loader) || Boolean(f.loader);
/*** validation - reset buttons/links ***/
/**
* Enable filters submission button
* @type {Boolean}
*/
this.displayBtn = Boolean(f.btn);
/**
* Define filters submission button text
* @type {String}
*/
this.btnText = defaultsStr(f.btn_text, (!this.enableIcons ? 'Go' : ''));
/**
* Css class for filters submission button
* @type {String}
*/
this.btnCssClass = defaultsStr(f.btn_css_class,
(!this.enableIcons ? 'btnflt' : 'btnflt_icon'));
/**
* Enable clear button
* @type {Object|Boolean}
*/
this.btnReset = isObj(f.btn_reset) || Boolean(f.btn_reset);
/**
* Callback fired before filters are cleared
* @type {Function}
*/
this.onBeforeReset = defaultsFn(f.on_before_reset, EMPTY_FN);
/**
* Callback fired after filters are cleared
* @type {Function}
*/
this.onAfterReset = defaultsFn(f.on_after_reset, EMPTY_FN);
/**
* Enable paging component
* @type {Object|Boolean}
*/
this.paging = isObj(f.paging) || Boolean(f.paging);
/**
* Number of hidden rows
* @type {Number}
* @private
*/
this.nbHiddenRows = 0;
/**
* Enable auto-filter behaviour, table is filtered when a user
* stops typing
* @type {Object|Boolean}
*/
this.autoFilter = isObj(f.auto_filter) || Boolean(f.auto_filter);
/**
* Auto-filter delay in milliseconds
* @type {Number}
*/
this.autoFilterDelay = isObj(f.auto_filter) &&
isNumber(f.auto_filter.delay) ?
f.auto_filter.delay : AUTO_FILTER_DELAY;
/**
* Indicate whether user is typing
* @type {Boolean}
* @private
*/
this.isUserTyping = null;
/**
* Auto-filter interval ID
* @type {String}
* @private
*/
this.autoFilterTimer = null;
/**
* Enable keyword highlighting behaviour
* @type {Boolean}
*/
this.highlightKeywords = Boolean(f.highlight_keywords);
/**
* Enable no results message UI component
* @type {Object|Boolean}
*/
this.noResults = isObj(f.no_results_message) ||
Boolean(f.no_results_message);
/**
* Enable state persistence
* @type {Object|Boolean}
*/
this.state = isObj(f.state) || Boolean(f.state);
/*** data types ***/
/**
* Enable date type module
* @type {Boolean}
* @private
*/
this.dateType = true;
/**
* Define default locale, default to 'en' as per Sugar Date module:
* https://sugarjs.com/docs/#/DateLocales
* @type {String}
*/
this.locale = defaultsStr(f.locale, 'en');
/**
* Define thousands separator ',' or '.', defaults to ','
* @type {String}
*/
this.thousandsSeparator = defaultsStr(f.thousands_separator, ',');
/**
* Define decimal separator ',' or '.', defaults to '.'
* @type {String}
*/
this.decimalSeparator = defaultsStr(f.decimal_separator, '.');
/**
* Define data types on a column basis, possible values 'string',
* 'number', 'formatted-number', 'date', 'ipaddress' ie:
* col_types : [
* 'string', 'date', 'number',
* { type: 'formatted-number', decimal: ',', thousands: '.' },
* { type: 'date', locale: 'en-gb' },
* { type: 'date', format: ['{dd}-{months}-{yyyy|yy}'] }
* ]
*
* Refer to https://sugarjs.com/docs/#/DateParsing for exhaustive
* information on date parsing formats supported by Sugar Date
* @type {Array}
*/
this.colTypes = isArray(f.col_types) ? f.col_types : [];
/*** ids prefixes ***/
/**
* Main prefix
* @private
*/
this.prfxTf = 'TF';
/**
* Filter's ID prefix (inputs - selects)
* @private
*/
this.prfxFlt = 'flt';
/**
* Button's ID prefix
* @private
*/
this.prfxValButton = 'btn';
/**
* Responsive Css class
* @private
*/
this.prfxResponsive = 'resp';
/** @private */
this.stickyCssClass = 'sticky';
/*** extensions ***/
/**
* List of loaded extensions
* @type {Array}
*/
this.extensions = defaultsArr(f.extensions, []);
/*** themes ***/
/**
* Enable default theme
* @type {Boolean}
*/
this.enableDefaultTheme = Boolean(f.enable_default_theme);
/**
* Determine whether themes are enables
* @type {Boolean}
* @private
*/
this.hasThemes = (this.enableDefaultTheme || isArray(f.themes));
/**
* List of themes, ie:
* themes: [{ name: 'skyblue' }]
* @type {Array}
*/
this.themes = defaultsArr(f.themes, []);
/**
* Define path to themes assets, defaults to
* 'tablefilter/style/themes/'. Usage:
* themes: [{ name: 'skyblue' }]
* @type {Array}
*/
this.themesPath = this.getThemesPath();
/**
* Enable responsive layout
* @type {Boolean}
*/
this.responsive = Boolean(f.responsive);
/**
* Enable toolbar component
* @type {Object|Boolean}
*/
this.toolbar = isObj(f.toolbar) || Boolean(f.toolbar);
/**
* Enable sticky headers
* @type {Boolean}
*/
this.stickyHeaders = Boolean(f.sticky_headers);
/**
* Features registry
* @private
*/
this.Mod = {};
/**
* Extensions registry
* @private
*/
this.ExtRegistry = {};
// instantiate features if needed
this.instantiateFeatures(FEATURES);
}
/**
* Initialise features and layout
*/
init() {
if (this.initialized) {
return;
}
// import main stylesheet
this.import(this.stylesheetId, this.getStylesheetPath(), null, 'link');
let Mod = this.Mod;
let inpclass;
//loads theme
this.loadThemes();
//explicitly initialise features in given order
this.initFeatures([
DateType,
Help,
State,
MarkActiveColumns,
GridLayout,
Loader,
HighlightKeyword,
PopupFilter
]);
//filters grid is not generated
if (!this.fltGrid) {
this._initNoFilters();
} else {
let fltrow = this._insertFiltersRow();
this.nbCells = this.getCellsNb(this.refRow);
this.nbFilterableRows = this.getRowsNb();
let n = this.singleFlt ? 1 : this.nbCells;
//build filters
for (let i = 0; i < n; i++) {
this.emitter.emit('before-filter-init', this, i);
let fltCell = createElm(this.fltCellTag),
col = this.getFilterType(i);
if (this.singleFlt) {
fltCell.colSpan = this.nbCells;
}
if (!this.gridLayout) {
fltrow.appendChild(fltCell);
}
inpclass = (i === n - 1 && this.displayBtn) ?
this.fltSmallCssClass : this.fltCssClass;
//only 1 input for single search
if (this.singleFlt) {
col = INPUT;
inpclass = this.singleFltCssClass;
}