1 | import {Feature} from '../feature';
|
2 | import {
|
3 | ignoreCase, numSortAsc, numSortDesc,
|
4 | dateSortAsc, dateSortDesc, sortNumberStr, sortDateStr
|
5 | } from '../sort';
|
6 | import {isArray, isObj, isEmpty} from '../types';
|
7 | import {NUMBER, FORMATTED_NUMBER, DATE} from '../const';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export class BaseDropdown extends Feature {
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 | constructor(tf, cls) {
|
22 | super(tf, cls);
|
23 |
|
24 | let f = this.config;
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 | this.customSorter = isObj(f.filter_options_sorter) &&
|
31 | isArray(f.filter_options_sorter.col) &&
|
32 | isArray(f.filter_options_sorter.comparer) ?
|
33 | f.filter_options_sorter :
|
34 | null;
|
35 |
|
36 |
|
37 |
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 | this.isCustom = false;
|
44 |
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 | this.opts = [];
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | this.optsTxt = [];
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 | this.excludedOpts = [];
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | sortOptions(colIndex, options = []) {
|
75 | let {tf} = this;
|
76 |
|
77 | if (tf.isCustomOptions(colIndex) || !tf.sortSlc ||
|
78 | (isArray(tf.sortSlc) && tf.sortSlc.indexOf(colIndex) === -1)) {
|
79 | return options;
|
80 | }
|
81 |
|
82 | let { caseSensitive, sortFilterOptionsDesc } = tf;
|
83 | let isSortDesc = sortFilterOptionsDesc.indexOf(colIndex) !== -1;
|
84 | let compareFn;
|
85 |
|
86 | if (this.customSorter &&
|
87 | this.customSorter.col.indexOf(colIndex) !== -1) {
|
88 | var idx = this.customSorter.col.indexOf(colIndex);
|
89 | compareFn = this.customSorter.comparer[idx];
|
90 | }
|
91 | else if (tf.hasType(colIndex, [NUMBER, FORMATTED_NUMBER])) {
|
92 | let decimal = tf.getDecimal(colIndex);
|
93 | let comparer = isSortDesc ? numSortDesc : numSortAsc;
|
94 | compareFn = sortNumberStr(comparer, decimal);
|
95 | }
|
96 | else if (tf.hasType(colIndex, [DATE])) {
|
97 | let locale = tf.feature('dateType').getLocale(colIndex);
|
98 | let comparer = isSortDesc ? dateSortDesc : dateSortAsc;
|
99 | compareFn = sortDateStr(comparer, locale);
|
100 | } else {
|
101 | compareFn = caseSensitive ? undefined : ignoreCase;
|
102 | if (isSortDesc) {
|
103 | return options.sort(compareFn).reverse();
|
104 | }
|
105 | }
|
106 |
|
107 | return options.sort(compareFn);
|
108 | }
|
109 |
|
110 | |
111 |
|
112 |
|
113 |
|
114 |
|
115 | refreshFilters(colIndexes) {
|
116 | colIndexes.forEach((colIdx) => {
|
117 | let values = this.getValues(colIdx);
|
118 | this.build(colIdx, this.tf.linkedFilters);
|
119 | this.selectOptions(colIdx, values);
|
120 | });
|
121 | }
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | isValidLinkedValue(rowIdx, activeFilterIdx) {
|
130 | let tf = this.tf;
|
131 |
|
132 | if (tf.disableExcludedOptions) {
|
133 | return true;
|
134 | }
|
135 |
|
136 | if (tf.paging) {
|
137 | if (!isEmpty(activeFilterIdx) && tf.isRowValid(rowIdx)) {
|
138 | return true;
|
139 | }
|
140 | } else {
|
141 | if (tf.isRowDisplayed(rowIdx)) {
|
142 | return true;
|
143 | }
|
144 | }
|
145 |
|
146 | return false;
|
147 | }
|
148 |
|
149 | |
150 |
|
151 |
|
152 | linkFilters() {
|
153 | let tf = this.tf;
|
154 | if (!tf.linkedFilters || !tf.activeFilterId) {
|
155 | return;
|
156 | }
|
157 |
|
158 | this.refreshAll();
|
159 | }
|
160 | }
|