1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-24 17:42:39 +02:00
TableFilter/src/modules/dropdown.js

433 lines
13 KiB
JavaScript
Raw Normal View History

import {Feature} from '../feature';
2016-05-25 09:31:53 +02:00
import {createElm, createOpt, elm} from '../dom';
2016-05-20 08:58:54 +02:00
import {has} from '../array';
2016-05-20 10:08:39 +02:00
import {matchCase} from '../string';
2016-05-20 09:14:05 +02:00
import {ignoreCase, numSortAsc, numSortDesc} from '../sort';
2016-06-02 06:13:56 +02:00
import {addEvt, targetEvt} from '../event';
import {SELECT, MULTIPLE, NONE} from '../const';
2015-02-14 09:59:12 +01:00
const SORT_ERROR = 'Filter options for column {0} cannot be sorted in ' +
'{1} manner.';
2016-07-09 13:51:32 +02:00
/**
* Dropdown filter UI component
*/
export class Dropdown extends Feature {
2015-02-14 09:59:12 +01:00
/**
2016-07-09 13:51:32 +02:00
* Creates an instance of Dropdown
* @param {TableFilter} tf TableFilter instance
2015-02-14 09:59:12 +01:00
*/
constructor(tf) {
super(tf, 'dropdown');
2015-02-14 09:59:12 +01:00
// Configuration object
2016-07-09 13:51:32 +02:00
let f = this.config;
2015-02-14 09:59:12 +01:00
2016-07-09 13:51:32 +02:00
/**
* Enable the reset filter option as first item
* @type {Boolean}
*/
this.enableSlcResetFilter = f.enable_slc_reset_filter === false ?
2015-06-04 15:04:38 +02:00
false : true;
2016-07-09 13:51:32 +02:00
/**
* Non empty option text
* @type {String}
*/
2015-02-14 09:59:12 +01:00
this.nonEmptyText = f.non_empty_text || '(Non empty)';
2016-07-09 13:51:32 +02:00
/**
* Tooltip text appearing on multiple select
* @type {String}
*/
2015-02-14 09:59:12 +01:00
this.multipleSlcTooltip = f.multiple_slc_tooltip ||
2016-07-09 13:51:32 +02:00
'Use Ctrl/Cmd key for multiple selections';
2015-02-14 09:59:12 +01:00
2016-07-09 13:51:32 +02:00
/**
* Indicates drop-down has custom options
* @private
*/
2015-02-14 09:59:12 +01:00
this.isCustom = null;
2016-07-09 13:51:32 +02:00
/**
* List of options values
* @type {Array}
* @private
*/
2015-02-15 04:25:22 +01:00
this.opts = null;
2016-07-09 13:51:32 +02:00
/**
* List of options texts for custom values
* @type {Array}
* @private
*/
2015-02-15 04:25:22 +01:00
this.optsTxt = null;
}
2016-07-09 13:51:32 +02:00
/**
* Drop-down filter focus event handler
* @param {Event} e DOM Event
* @private
*/
2016-01-11 08:35:21 +01:00
onSlcFocus(e) {
2016-06-02 06:13:56 +02:00
let elm = targetEvt(e);
2016-01-11 08:35:21 +01:00
let tf = this.tf;
// select is populated when element has focus
if (tf.loadFltOnDemand && elm.getAttribute('filled') === '0') {
2016-01-11 08:35:21 +01:00
let ct = elm.getAttribute('ct');
this.build(ct);
}
2016-04-05 10:51:56 +02:00
this.emitter.emit('filter-focus', tf, elm);
2016-01-11 08:35:21 +01:00
}
2016-07-09 13:51:32 +02:00
/**
* Drop-down filter change event handler
* @private
*/
2016-01-12 07:46:27 +01:00
onSlcChange() {
if (this.tf.onSlcChange) {
2016-01-12 07:46:27 +01:00
this.tf.filter();
}
}
/**
* Initialize drop-down filter
* @param {Number} colIndex Column index
* @param {Boolean} isExternal External filter flag
* @param {DOMElement} container Dom element containing the filter
*/
init(colIndex, isExternal, container) {
let tf = this.tf;
let col = tf.getFilterType(colIndex);
let externalFltTgtId = isExternal ?
tf.externalFltTgtIds[colIndex] : null;
2016-05-24 10:42:11 +02:00
let slc = createElm(SELECT,
['id', tf.prfxFlt + colIndex + '_' + tf.id],
['ct', colIndex], ['filled', '0']
);
if (col === MULTIPLE) {
slc.multiple = MULTIPLE;
slc.title = this.multipleSlcTooltip;
}
2016-05-20 10:08:39 +02:00
slc.className = col.toLowerCase() === SELECT ?
tf.fltCssClass : tf.fltMultiCssClass;
//filter is appended in container element
if (externalFltTgtId) {
2016-05-25 09:31:53 +02:00
elm(externalFltTgtId).appendChild(slc);
} else {
container.appendChild(slc);
}
tf.fltIds.push(slc.id);
if (!tf.loadFltOnDemand) {
this.build(colIndex);
} else {
//1st option is created here since build isn't invoked
2016-05-24 10:42:11 +02:00
let opt0 = createOpt(tf.displayAllText, '');
slc.appendChild(opt0);
}
2016-06-02 06:13:56 +02:00
addEvt(slc, 'change', () => this.onSlcChange());
addEvt(slc, 'focus', (e) => this.onSlcFocus(e));
2015-02-14 09:59:12 +01:00
2016-01-17 07:56:15 +01:00
this.emitter.on(
['build-select-filter'],
(tf, colIndex, isLinked, isExternal) =>
2016-01-17 07:56:15 +01:00
this.build(colIndex, isLinked, isExternal)
);
2016-01-20 07:14:32 +01:00
this.emitter.on(
['select-options'],
(tf, colIndex, values) => this.selectOptions(colIndex, values)
2016-01-20 07:14:32 +01:00
);
2016-01-17 07:56:15 +01:00
/**
* @inherited
*/
this.initialized = true;
2015-02-14 09:59:12 +01:00
}
2015-02-15 04:25:22 +01:00
/**
* Build drop-down filter UI
2015-02-14 09:59:12 +01:00
* @param {Number} colIndex Column index
2016-01-17 07:56:15 +01:00
* @param {Boolean} isLinked Enable linked refresh behaviour
2015-02-14 09:59:12 +01:00
*/
build(colIndex, isLinked = false) {
let tf = this.tf;
2015-02-14 09:59:12 +01:00
colIndex = parseInt(colIndex, 10);
2016-01-02 15:33:31 +01:00
this.emitter.emit('before-populating-filter', tf, colIndex);
2015-02-15 04:25:22 +01:00
this.opts = [];
this.optsTxt = [];
let slcId = tf.fltIds[colIndex];
2016-05-25 09:31:53 +02:00
let slc = elm(slcId);
2016-05-24 10:42:11 +02:00
let rows = tf.tbl.rows;
let nbRows = tf.getRowsNb(true);
2015-02-14 09:59:12 +01:00
//custom select test
this.isCustom = tf.isCustomOptions(colIndex);
2015-02-14 09:59:12 +01:00
//custom selects text
let activeIdx;
let activeFilterId = tf.getActiveFilterId();
if (isLinked && activeFilterId) {
activeIdx = tf.getColumnIndexFromFilterId(activeFilterId);
2015-02-14 09:59:12 +01:00
}
let excludedOpts = null,
2015-02-14 09:59:12 +01:00
filteredDataCol = null;
if (isLinked && tf.disableExcludedOptions) {
2015-02-14 09:59:12 +01:00
excludedOpts = [];
filteredDataCol = [];
}
for (let k = tf.refRow; k < nbRows; k++) {
2015-02-14 09:59:12 +01:00
// always visible rows don't need to appear on selects as always
// valid
if (tf.hasVisibleRows && tf.visibleRows.indexOf(k) !== -1) {
2015-02-14 09:59:12 +01:00
continue;
}
let cell = rows[k].cells,
2015-02-14 09:59:12 +01:00
nchilds = cell.length;
// checks if row has exact cell #
if (nchilds !== tf.nbCells || this.isCustom) {
2015-02-14 09:59:12 +01:00
continue;
}
2015-02-15 04:25:22 +01:00
2015-02-14 09:59:12 +01:00
// this loop retrieves cell data
for (let j = 0; j < nchilds; j++) {
// WTF: cyclomatic complexity hell
2016-05-20 08:58:54 +02:00
// TODO: simplify hell below
if ((colIndex === j &&
2015-06-10 12:53:20 +02:00
(!isLinked ||
(isLinked && tf.disableExcludedOptions))) ||
2016-03-11 06:55:42 +01:00
(colIndex === j && isLinked &&
2015-02-14 09:59:12 +01:00
((rows[k].style.display === '' && !tf.paging) ||
(tf.paging && (!tf.validRowsIndex ||
(tf.validRowsIndex &&
2016-05-15 05:33:16 +02:00
tf.validRowsIndex.indexOf(k) !== -1)) &&
((activeIdx === undefined ||
activeIdx === colIndex) ||
2016-05-15 05:33:16 +02:00
(activeIdx !== colIndex &&
tf.validRowsIndex.indexOf(k) !== -1)))))) {
2016-03-11 06:55:42 +01:00
let cellData = tf.getCellData(cell[j]),
2015-02-14 09:59:12 +01:00
//Vary Peter's patch
cellString = matchCase(cellData, tf.caseSensitive);
2015-02-14 09:59:12 +01:00
// checks if celldata is already in array
if (!has(this.opts, cellString, tf.caseSensitive)) {
2016-03-11 06:55:42 +01:00
this.opts.push(cellData);
2015-02-14 09:59:12 +01:00
}
if (isLinked && tf.disableExcludedOptions) {
let filteredCol = filteredDataCol[j];
if (!filteredCol) {
filteredCol = tf.getFilteredDataCol(j);
2015-02-14 09:59:12 +01:00
}
if (!has(filteredCol, cellString, tf.caseSensitive) &&
!has(excludedOpts, cellString, tf.caseSensitive)) {
2016-03-11 06:55:42 +01:00
excludedOpts.push(cellData);
2015-02-14 09:59:12 +01:00
}
}
}//if colIndex==j
}//for j
}//for k
//Retrieves custom values
if (this.isCustom) {
let customValues = tf.getCustomOptions(colIndex);
2015-02-14 09:59:12 +01:00
this.opts = customValues[0];
this.optsTxt = customValues[1];
}
if (tf.sortSlc && !this.isCustom) {
if (!tf.caseSensitive) {
2016-05-20 09:14:05 +02:00
this.opts.sort(ignoreCase);
if (excludedOpts) {
2016-05-20 09:14:05 +02:00
excludedOpts.sort(ignoreCase);
2015-02-14 09:59:12 +01:00
}
} else {
this.opts.sort();
if (excludedOpts) { excludedOpts.sort(); }
2015-02-14 09:59:12 +01:00
}
}
//asc sort
2016-05-15 05:33:16 +02:00
if (tf.sortNumAsc.indexOf(colIndex) !== -1) {
try {
2016-05-20 09:14:05 +02:00
this.opts.sort(numSortAsc);
if (excludedOpts) {
2016-05-20 09:14:05 +02:00
excludedOpts.sort(numSortAsc);
2015-02-14 09:59:12 +01:00
}
if (this.isCustom) {
2016-05-20 09:14:05 +02:00
this.optsTxt.sort(numSortAsc);
2015-02-14 09:59:12 +01:00
}
} catch (e) {
throw new Error(SORT_ERROR.replace('{0}', colIndex)
.replace('{1}', 'ascending'));
2015-02-14 09:59:12 +01:00
}//in case there are alphanumeric values
}
//desc sort
2016-05-15 05:33:16 +02:00
if (tf.sortNumDesc.indexOf(colIndex) !== -1) {
try {
2016-05-20 09:14:05 +02:00
this.opts.sort(numSortDesc);
if (excludedOpts) {
2016-05-20 09:14:05 +02:00
excludedOpts.sort(numSortDesc);
2015-02-14 09:59:12 +01:00
}
if (this.isCustom) {
2016-05-20 09:14:05 +02:00
this.optsTxt.sort(numSortDesc);
2015-02-14 09:59:12 +01:00
}
} catch (e) {
throw new Error(SORT_ERROR.replace('{0}', colIndex)
.replace('{1}', 'ascending'));
2015-02-14 09:59:12 +01:00
}//in case there are alphanumeric values
}
//populates drop-down
this.addOptions(colIndex, slc, isLinked, excludedOpts);
2016-01-02 15:33:31 +01:00
2016-01-08 07:44:22 +01:00
this.emitter.emit('after-populating-filter', tf, colIndex, slc);
2015-02-14 09:59:12 +01:00
}
/**
* Add drop-down options
2015-02-15 04:25:22 +01:00
* @param {Number} colIndex Column index
* @param {Object} slc Select Dom element
2015-06-10 12:53:20 +02:00
* @param {Boolean} isLinked Enable linked refresh behaviour
2015-02-15 04:25:22 +01:00
* @param {Array} excludedOpts Array of excluded options
2015-02-14 09:59:12 +01:00
*/
addOptions(colIndex, slc, isLinked, excludedOpts) {
let tf = this.tf,
2015-02-14 09:59:12 +01:00
slcValue = slc.value;
slc.innerHTML = '';
slc = this.addFirstOption(slc);
for (let y = 0; y < this.opts.length; y++) {
if (this.opts[y] === '') {
2015-02-14 09:59:12 +01:00
continue;
}
let val = this.opts[y]; //option value
let lbl = this.isCustom ? this.optsTxt[y] : val; //option text
let isDisabled = false;
if (isLinked && tf.disableExcludedOptions &&
has(excludedOpts, matchCase(val, tf.caseSensitive),
tf.caseSensitive)) {
2015-02-14 09:59:12 +01:00
isDisabled = true;
}
2016-02-03 08:34:46 +01:00
let opt;
//fill select on demand
if (tf.loadFltOnDemand && slcValue === this.opts[y] &&
tf.getFilterType(colIndex) === SELECT) {
2016-05-24 10:42:11 +02:00
opt = createOpt(lbl, val, true);
2015-02-14 09:59:12 +01:00
} else {
2016-05-24 10:42:11 +02:00
opt = createOpt(lbl, val, false);
2015-02-14 09:59:12 +01:00
}
if (isDisabled) {
2016-02-03 08:34:46 +01:00
opt.disabled = true;
}
slc.appendChild(opt);
2015-02-14 09:59:12 +01:00
}// for y
slc.setAttribute('filled', '1');
}
/**
* Add drop-down header option
* @param {Object} slc Select DOM element
*/
addFirstOption(slc) {
2016-02-03 08:34:46 +01:00
let tf = this.tf;
2015-02-14 09:59:12 +01:00
2016-05-24 10:42:11 +02:00
let opt0 = createOpt(
(!this.enableSlcResetFilter ? '' : tf.displayAllText), '');
if (!this.enableSlcResetFilter) {
opt0.style.display = NONE;
2015-02-14 09:59:12 +01:00
}
2016-02-03 08:34:46 +01:00
slc.appendChild(opt0);
if (tf.enableEmptyOption) {
2016-05-24 10:42:11 +02:00
let opt1 = createOpt(tf.emptyText, tf.emOperator);
2016-02-03 08:34:46 +01:00
slc.appendChild(opt1);
}
if (tf.enableNonEmptyOption) {
2016-05-24 10:42:11 +02:00
let opt2 = createOpt(tf.nonEmptyText, tf.nmOperator);
2016-02-03 08:34:46 +01:00
slc.appendChild(opt2);
2015-02-14 09:59:12 +01:00
}
return slc;
}
2016-01-21 08:29:09 +01:00
/**
* Select filter options programmatically
* @param {Number} colIndex Column index
* @param {Array} values Array of option values to select
*/
selectOptions(colIndex, values = []) {
2016-01-20 07:14:32 +01:00
let tf = this.tf;
if (tf.getFilterType(colIndex) !== MULTIPLE || values.length === 0) {
2016-01-21 08:29:09 +01:00
return;
2016-01-20 07:14:32 +01:00
}
let slc = tf.getFilterElement(colIndex);
[].forEach.call(slc.options, (option) => {
2016-01-20 07:14:32 +01:00
// Empty value means clear all selections and first option is the
// clear all option
if (values[0] === '' || option.value === '') {
2016-01-20 07:14:32 +01:00
option.selected = false;
}
2016-05-20 08:58:54 +02:00
if (option.value !== '' && has(values, option.value, true)) {
2016-01-20 07:14:32 +01:00
option.selected = true;
}//if
});
}
/**
* Get filter values for a given column index
* @param {Number} colIndex Column index
* @returns {Array} values Array of selected values
*/
getValues(colIndex) {
let tf = this.tf;
let slc = tf.getFilterElement(colIndex);
let values = [];
// IE >= 9 does not support the selectedOptions property :(
if (slc.selectedOptions) {
[].forEach.call(slc.selectedOptions,
option => values.push(option.value));
} else {
[].forEach.call(slc.options, (option) => {
if (option.selected) {
values.push(option.value);
}
});
}
return values;
}
2016-07-09 13:51:32 +02:00
/**
* Destroy Dropdown instance
*/
destroy() {
2016-01-17 07:56:15 +01:00
this.emitter.off(
['build-select-filter'],
(colIndex, isLinked, isExternal) =>
2016-01-17 07:56:15 +01:00
this.build(colIndex, isLinked, isExternal)
);
2016-01-20 07:14:32 +01:00
this.emitter.off(
['select-options'],
(tf, colIndex, values) => this.selectOptions(colIndex, values)
2016-01-20 07:14:32 +01:00
);
2016-01-17 07:56:15 +01:00
}
2015-02-14 09:59:12 +01:00
}