2016-05-14 12:04:00 +02:00
|
|
|
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';
|
2016-05-07 13:59:42 +02:00
|
|
|
import {SELECT, MULTIPLE, NONE} from '../const';
|
2015-02-14 09:59:12 +01:00
|
|
|
|
2016-02-02 08:24:16 +01:00
|
|
|
const SORT_ERROR = 'Filter options for column {0} cannot be sorted in ' +
|
|
|
|
'{1} manner.';
|
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
export class Dropdown extends Feature {
|
2015-02-14 09:59:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dropdown UI component
|
|
|
|
* @param {Object} tf TableFilter instance
|
|
|
|
*/
|
2016-05-04 14:02:22 +02:00
|
|
|
constructor(tf) {
|
2016-01-06 08:12:16 +01:00
|
|
|
super(tf, 'dropdown');
|
|
|
|
|
2015-02-14 09:59:12 +01:00
|
|
|
// Configuration object
|
2016-01-06 08:12:16 +01:00
|
|
|
let f = tf.config();
|
2015-02-14 09:59:12 +01:00
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
this.enableSlcResetFilter = f.enable_slc_reset_filter === false ?
|
2015-06-04 15:04:38 +02:00
|
|
|
false : true;
|
2015-02-14 09:59:12 +01:00
|
|
|
//defines empty option text
|
|
|
|
this.nonEmptyText = f.non_empty_text || '(Non empty)';
|
|
|
|
//IE only, tooltip text appearing on select before it is populated
|
2016-02-16 08:41:47 +01:00
|
|
|
this.activateSlcTooltip = f.activate_slc_tooltip ||
|
2015-02-14 09:59:12 +01:00
|
|
|
'Click to activate';
|
|
|
|
//tooltip text appearing on multiple select
|
|
|
|
this.multipleSlcTooltip = f.multiple_slc_tooltip ||
|
|
|
|
'Use Ctrl key for multiple selections';
|
|
|
|
|
|
|
|
this.isCustom = null;
|
2015-02-15 04:25:22 +01:00
|
|
|
this.opts = null;
|
|
|
|
this.optsTxt = null;
|
|
|
|
this.slcInnerHtml = null;
|
2016-01-06 08:12:16 +01:00
|
|
|
}
|
|
|
|
|
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
|
2016-05-04 14:02:22 +02:00
|
|
|
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-01-12 07:46:27 +01:00
|
|
|
onSlcChange() {
|
2016-05-04 14:02:22 +02:00
|
|
|
if (this.tf.onSlcChange) {
|
2016-01-12 07:46:27 +01:00
|
|
|
this.tf.filter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
/**
|
|
|
|
* Initialize drop-down filter
|
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
* @param {Boolean} isExternal External filter flag
|
|
|
|
* @param {DOMElement} container Dom element containing the filter
|
|
|
|
*/
|
2016-05-04 14:02:22 +02:00
|
|
|
init(colIndex, isExternal, container) {
|
2016-01-06 08:12:16 +01:00
|
|
|
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,
|
2016-05-04 14:02:22 +02:00
|
|
|
['id', tf.prfxFlt + colIndex + '_' + tf.id],
|
2016-01-06 08:12:16 +01:00
|
|
|
['ct', colIndex], ['filled', '0']
|
|
|
|
);
|
|
|
|
|
2016-05-07 13:59:42 +02:00
|
|
|
if (col === MULTIPLE) {
|
|
|
|
slc.multiple = MULTIPLE;
|
2016-01-06 08:12:16 +01:00
|
|
|
slc.title = this.multipleSlcTooltip;
|
|
|
|
}
|
2016-05-20 10:08:39 +02:00
|
|
|
slc.className = col.toLowerCase() === SELECT ?
|
2016-01-06 08:12:16 +01:00
|
|
|
tf.fltCssClass : tf.fltMultiCssClass;
|
|
|
|
|
|
|
|
//filter is appended in container element
|
2016-05-04 14:02:22 +02:00
|
|
|
if (externalFltTgtId) {
|
2016-05-25 09:31:53 +02:00
|
|
|
elm(externalFltTgtId).appendChild(slc);
|
2016-01-06 08:12:16 +01:00
|
|
|
tf.externalFltEls.push(slc);
|
|
|
|
} else {
|
|
|
|
container.appendChild(slc);
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:41:25 +01:00
|
|
|
tf.fltIds.push(slc.id);
|
2016-01-06 08:12:16 +01:00
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
if (!tf.loadFltOnDemand) {
|
2016-01-06 08:12:16 +01:00
|
|
|
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, '');
|
2016-01-06 08:12:16 +01:00
|
|
|
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'],
|
2016-05-04 14:02:22 +02:00
|
|
|
(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'],
|
2016-05-04 14:02:22 +02:00
|
|
|
(tf, colIndex, values) => this.selectOptions(colIndex, values)
|
2016-01-20 07:14:32 +01:00
|
|
|
);
|
2016-01-17 07:56:15 +01:00
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
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
|
|
|
*/
|
2016-05-04 14:02:22 +02:00
|
|
|
build(colIndex, isLinked = false) {
|
2016-01-06 08:12:16 +01:00
|
|
|
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 = [];
|
|
|
|
this.slcInnerHtml = '';
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
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
|
2015-06-02 09:33:16 +02:00
|
|
|
this.isCustom = tf.isCustomOptions(colIndex);
|
2015-02-14 09:59:12 +01:00
|
|
|
|
|
|
|
//custom selects text
|
2016-04-05 18:09:21 +02:00
|
|
|
let activeIdx;
|
|
|
|
let activeFilterId = tf.getActiveFilterId();
|
2016-05-04 14:02:22 +02:00
|
|
|
if (isLinked && activeFilterId) {
|
2016-04-05 18:09:21 +02:00
|
|
|
activeIdx = tf.getColumnIndexFromFilterId(activeFilterId);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
let excludedOpts = null,
|
2015-02-14 09:59:12 +01:00
|
|
|
filteredDataCol = null;
|
2016-05-04 14:02:22 +02:00
|
|
|
if (isLinked && tf.disableExcludedOptions) {
|
2015-02-14 09:59:12 +01:00
|
|
|
excludedOpts = [];
|
|
|
|
filteredDataCol = [];
|
|
|
|
}
|
|
|
|
|
2016-05-19 09:38:18 +02:00
|
|
|
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
|
2016-05-04 14:02:22 +02:00
|
|
|
if (tf.hasVisibleRows && tf.visibleRows.indexOf(k) !== -1) {
|
2015-02-14 09:59:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
let cell = rows[k].cells,
|
2015-02-14 09:59:12 +01:00
|
|
|
nchilds = cell.length;
|
|
|
|
|
|
|
|
// checks if row has exact cell #
|
2016-05-04 14:02:22 +02:00
|
|
|
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
|
2016-05-04 14:02:22 +02:00
|
|
|
for (let j = 0; j < nchilds; j++) {
|
2015-06-14 04:23:27 +02:00
|
|
|
// WTF: cyclomatic complexity hell
|
2016-05-20 08:58:54 +02:00
|
|
|
// TODO: simplify hell below
|
2016-05-04 14:02:22 +02:00
|
|
|
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) ||
|
2016-05-04 14:02:22 +02:00
|
|
|
(tf.paging && (!tf.validRowsIndex ||
|
|
|
|
(tf.validRowsIndex &&
|
2016-05-15 05:33:16 +02:00
|
|
|
tf.validRowsIndex.indexOf(k) !== -1)) &&
|
2016-05-04 14:02:22 +02:00
|
|
|
((activeIdx === undefined ||
|
2016-05-08 10:03:39 +02:00
|
|
|
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
|
2016-05-26 04:21:27 +02:00
|
|
|
cellString = matchCase(cellData, tf.caseSensitive);
|
2015-02-14 09:59:12 +01:00
|
|
|
|
|
|
|
// checks if celldata is already in array
|
2016-05-26 04:21:27 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
if (isLinked && tf.disableExcludedOptions) {
|
2016-01-06 08:12:16 +01:00
|
|
|
let filteredCol = filteredDataCol[j];
|
2016-05-04 14:02:22 +02:00
|
|
|
if (!filteredCol) {
|
2015-06-13 13:23:33 +02:00
|
|
|
filteredCol = tf.getFilteredDataCol(j);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2016-05-26 04:21:27 +02: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
|
2016-05-04 14:02:22 +02:00
|
|
|
if (this.isCustom) {
|
2016-01-06 08:12:16 +01:00
|
|
|
let customValues = tf.getCustomOptions(colIndex);
|
2015-02-14 09:59:12 +01:00
|
|
|
this.opts = customValues[0];
|
|
|
|
this.optsTxt = customValues[1];
|
|
|
|
}
|
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
if (tf.sortSlc && !this.isCustom) {
|
2016-05-26 04:21:27 +02:00
|
|
|
if (!tf.caseSensitive) {
|
2016-05-20 09:14:05 +02:00
|
|
|
this.opts.sort(ignoreCase);
|
2016-05-04 14:02:22 +02:00
|
|
|
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();
|
2016-05-04 14:02:22 +02:00
|
|
|
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) {
|
2016-05-04 14:02:22 +02:00
|
|
|
try {
|
2016-05-20 09:14:05 +02:00
|
|
|
this.opts.sort(numSortAsc);
|
2016-05-04 14:02:22 +02:00
|
|
|
if (excludedOpts) {
|
2016-05-20 09:14:05 +02:00
|
|
|
excludedOpts.sort(numSortAsc);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2016-05-04 14:02:22 +02:00
|
|
|
if (this.isCustom) {
|
2016-05-20 09:14:05 +02:00
|
|
|
this.optsTxt.sort(numSortAsc);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2016-05-04 14:02:22 +02:00
|
|
|
} catch (e) {
|
2016-02-02 08:24:16 +01:00
|
|
|
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) {
|
2016-05-04 14:02:22 +02:00
|
|
|
try {
|
2016-05-20 09:14:05 +02:00
|
|
|
this.opts.sort(numSortDesc);
|
2016-05-04 14:02:22 +02:00
|
|
|
if (excludedOpts) {
|
2016-05-20 09:14:05 +02:00
|
|
|
excludedOpts.sort(numSortDesc);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2016-05-04 14:02:22 +02:00
|
|
|
if (this.isCustom) {
|
2016-05-20 09:14:05 +02:00
|
|
|
this.optsTxt.sort(numSortDesc);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2016-05-04 14:02:22 +02:00
|
|
|
} catch (e) {
|
2016-02-02 08:24:16 +01:00
|
|
|
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
|
2016-01-11 02:41:25 +01:00
|
|
|
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
|
|
|
*/
|
2016-05-04 14:02:22 +02:00
|
|
|
addOptions(colIndex, slc, isLinked, excludedOpts) {
|
2016-01-06 08:12:16 +01:00
|
|
|
let tf = this.tf,
|
2015-02-14 09:59:12 +01:00
|
|
|
slcValue = slc.value;
|
|
|
|
|
|
|
|
slc.innerHTML = '';
|
|
|
|
slc = this.addFirstOption(slc);
|
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
for (let y = 0; y < this.opts.length; y++) {
|
|
|
|
if (this.opts[y] === '') {
|
2015-02-14 09:59:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-06 08:12:16 +01:00
|
|
|
let val = this.opts[y]; //option value
|
|
|
|
let lbl = this.isCustom ? this.optsTxt[y] : val; //option text
|
|
|
|
let isDisabled = false;
|
2016-05-04 14:02:22 +02:00
|
|
|
if (isLinked && tf.disableExcludedOptions &&
|
2016-05-26 04:21:27 +02:00
|
|
|
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
|
2016-05-04 14:02:22 +02:00
|
|
|
if (tf.loadFltOnDemand && slcValue === this.opts[y] &&
|
2016-05-07 13:59:42 +02:00
|
|
|
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
|
|
|
}
|
2016-05-04 14:02:22 +02: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
|
|
|
|
*/
|
2016-05-04 14:02:22 +02:00
|
|
|
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(
|
2016-05-04 14:02:22 +02:00
|
|
|
(!this.enableSlcResetFilter ? '' : tf.displayAllText), '');
|
|
|
|
if (!this.enableSlcResetFilter) {
|
2016-05-07 13:59:42 +02:00
|
|
|
opt0.style.display = NONE;
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2016-02-03 08:34:46 +01:00
|
|
|
slc.appendChild(opt0);
|
2016-05-04 14:02:22 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-05-04 14:02:22 +02:00
|
|
|
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
|
|
|
|
*/
|
2016-05-04 14:02:22 +02:00
|
|
|
selectOptions(colIndex, values = []) {
|
2016-01-20 07:14:32 +01:00
|
|
|
let tf = this.tf;
|
2016-05-07 13:59:42 +02:00
|
|
|
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);
|
2016-05-04 14:02:22 +02:00
|
|
|
[].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
|
2016-05-04 14:02:22 +02:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-04 14:02:22 +02:00
|
|
|
/**
|
|
|
|
* 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 = [];
|
2016-05-08 10:03:39 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-05-04 14:02:22 +02:00
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2016-01-17 07:56:15 +01:00
|
|
|
this.emitter.off(
|
|
|
|
['build-select-filter'],
|
2016-05-04 14:02:22 +02:00
|
|
|
(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'],
|
2016-05-04 14:02:22 +02:00
|
|
|
(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
|
|
|
}
|