UNPKG

4.67 kBJavaScriptView Raw
1import {Feature} from '../feature';
2import {
3 ignoreCase, numSortAsc, numSortDesc,
4 dateSortAsc, dateSortDesc, sortNumberStr, sortDateStr
5} from '../sort';
6import {isArray, isObj, isEmpty} from '../types';
7import {NUMBER, FORMATTED_NUMBER, DATE} from '../const';
8
9/**
10 * Base class for Dropdown and CheckList UI components
11 * @export
12 * @class BaseDropdown
13 * @extends {Feature}
14 */
15export class BaseDropdown extends Feature {
16
17 /**
18 * Creates an instance of BaseDropdown
19 * @param {TableFilter} tf
20 */
21 constructor(tf, cls) {
22 super(tf, cls);
23
24 let f = this.config;
25
26 /**
27 * Filter options custom sorter on a column basis
28 * @type {Object}
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 // TODO: move here all properties shared by Dropdown and CheckList
37
38 /**
39 * Has custom options
40 * @type {Boolean}
41 * @private
42 */
43 this.isCustom = false;
44
45 /**
46 * List of options values
47 * @type {Array}
48 * @private
49 */
50 this.opts = [];
51
52 /**
53 * List of options texts for custom values
54 * @type {Array}
55 * @private
56 */
57 this.optsTxt = [];
58
59 /**
60 * List of options to be excluded from the checklist filter
61 * @type {Array}
62 * @private
63 */
64 this.excludedOpts = [];
65 }
66
67 /**
68 * Sort passed options based on the type of the specified column
69 * @param {Number} colIndex Column index
70 * @param {Array} [options=[]] Collection of values
71 * @return {Array} Sorted values
72 * @private
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 { // string
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 * Regenerate filters of specified columns and maintain selection if any
112 * @param {Array} colIndexes Collection of column indexes
113 * @private
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 * Check passed row contains a valid linked value
125 * @param {Number} rowIdx Row index
126 * @param {Number} activeFilterIdx Current active filter index
127 * @returns {Boolean}
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 * Refresh linked filters to offer only selected options
151 */
152 linkFilters() {
153 let tf = this.tf;
154 if (!tf.linkedFilters || !tf.activeFilterId) {
155 return;
156 }
157
158 this.refreshAll();
159 }
160}