2016-01-06 08:12:16 +01:00
|
|
|
import {Feature} from './feature';
|
2015-05-30 14:23:33 +02:00
|
|
|
import Dom from '../dom';
|
|
|
|
import Arr from '../array';
|
|
|
|
import Str from '../string';
|
|
|
|
import Sort from '../sort';
|
2016-01-06 08:12:16 +01:00
|
|
|
import Event from '../event';
|
2015-02-14 09:59:12 +01:00
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
export class Dropdown extends Feature{
|
2015-02-14 09:59:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dropdown UI component
|
|
|
|
* @param {Object} tf TableFilter instance
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2015-06-04 15:04:38 +02:00
|
|
|
this.enableSlcResetFilter = f.enable_slc_reset_filter===false ?
|
|
|
|
false : true;
|
2015-02-14 09:59:12 +01:00
|
|
|
//defines empty option text
|
|
|
|
this.nonEmptyText = f.non_empty_text || '(Non empty)';
|
|
|
|
//sets select filling method: 'innerHTML' or 'createElement'
|
|
|
|
this.slcFillingMethod = f.slc_filling_method || 'createElement';
|
|
|
|
//IE only, tooltip text appearing on select before it is populated
|
|
|
|
this.activateSlcTooltip = f.activate_slc_tooltip ||
|
|
|
|
'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) {
|
|
|
|
let elm = Event.target(e);
|
|
|
|
let tf = this.tf;
|
|
|
|
tf.activeFilterId = elm.getAttribute('id');
|
|
|
|
tf.activeFlt = Dom.id(tf.activeFilterId);
|
|
|
|
// select is populated when element has focus
|
|
|
|
if(tf.loadFltOnDemand && elm.getAttribute('filled') === '0'){
|
|
|
|
let ct = elm.getAttribute('ct');
|
|
|
|
this.build(ct);
|
|
|
|
}
|
|
|
|
this.emitter.emit('filter-focus', tf, this);
|
|
|
|
}
|
|
|
|
|
2016-01-12 07:46:27 +01:00
|
|
|
onSlcChange() {
|
|
|
|
if(this.tf.onSlcChange){
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
init(colIndex, isExternal, container){
|
|
|
|
let tf = this.tf;
|
|
|
|
let col = tf.getFilterType(colIndex);
|
|
|
|
let externalFltTgtId = isExternal ?
|
|
|
|
tf.externalFltTgtIds[colIndex] : null;
|
|
|
|
|
|
|
|
let slc = Dom.create(tf.fltTypeSlc,
|
|
|
|
['id', tf.prfxFlt+colIndex+'_'+tf.id],
|
|
|
|
['ct', colIndex], ['filled', '0']
|
|
|
|
);
|
|
|
|
|
|
|
|
if(col === tf.fltTypeMulti){
|
|
|
|
slc.multiple = tf.fltTypeMulti;
|
|
|
|
slc.title = this.multipleSlcTooltip;
|
|
|
|
}
|
|
|
|
slc.className = Str.lower(col) === tf.fltTypeSlc ?
|
|
|
|
tf.fltCssClass : tf.fltMultiCssClass;
|
|
|
|
|
|
|
|
//filter is appended in container element
|
|
|
|
if(externalFltTgtId){
|
|
|
|
Dom.id(externalFltTgtId).appendChild(slc);
|
|
|
|
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
|
|
|
|
|
|
|
if(!tf.loadFltOnDemand){
|
|
|
|
this.build(colIndex);
|
|
|
|
} else {
|
|
|
|
//1st option is created here since build isn't invoked
|
|
|
|
let opt0 = Dom.createOpt(tf.displayAllText, '');
|
|
|
|
slc.appendChild(opt0);
|
|
|
|
}
|
|
|
|
|
2016-01-12 07:46:27 +01:00
|
|
|
Event.add(slc, 'change', ()=> this.onSlcChange());
|
2016-01-11 08:35:21 +01:00
|
|
|
Event.add(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)=>
|
|
|
|
this.build(colIndex, isLinked, isExternal)
|
|
|
|
);
|
|
|
|
|
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
|
|
|
* @param {Boolean} isExternal Render in external container
|
2015-02-15 04:25:22 +01:00
|
|
|
* @param {String} extSlcId External container id
|
2015-02-14 09:59:12 +01:00
|
|
|
*/
|
2016-01-02 15:33:31 +01:00
|
|
|
build(colIndex, isLinked=false, isExternal=false, extSlcId=null){
|
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];
|
2015-02-14 09:59:12 +01:00
|
|
|
if((!Dom.id(slcId) && !isExternal) ||
|
|
|
|
(!Dom.id(extSlcId) && isExternal)){
|
|
|
|
return;
|
|
|
|
}
|
2016-01-06 08:12:16 +01:00
|
|
|
let slc = !isExternal ? Dom.id(slcId) : Dom.id(extSlcId),
|
2015-02-14 09:59:12 +01:00
|
|
|
rows = tf.tbl.rows,
|
2015-05-15 12:37:27 +02:00
|
|
|
matchCase = tf.matchCase;
|
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-01-06 08:12:16 +01:00
|
|
|
let activeFlt;
|
2015-06-10 12:53:20 +02:00
|
|
|
if(isLinked && tf.activeFilterId){
|
2015-02-14 09:59:12 +01:00
|
|
|
activeFlt = tf.activeFilterId.split('_')[0];
|
|
|
|
activeFlt = activeFlt.split(tf.prfxFlt)[1];
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
let excludedOpts = null,
|
2015-02-14 09:59:12 +01:00
|
|
|
filteredDataCol = null;
|
2015-06-10 12:53:20 +02:00
|
|
|
if(isLinked && tf.disableExcludedOptions){
|
2015-02-14 09:59:12 +01:00
|
|
|
excludedOpts = [];
|
|
|
|
filteredDataCol = [];
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
for(let k=tf.refRow; k<tf.nbRows; k++){
|
2015-02-14 09:59:12 +01:00
|
|
|
// always visible rows don't need to appear on selects as always
|
|
|
|
// valid
|
2015-08-01 09:45:29 +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 #
|
|
|
|
if(nchilds !== tf.nbCells || this.isCustom){
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-15 04:25:22 +01:00
|
|
|
|
2015-02-14 09:59:12 +01:00
|
|
|
// this loop retrieves cell data
|
2016-01-06 08:12:16 +01:00
|
|
|
for(let j=0; j<nchilds; j++){
|
2015-06-14 04:23:27 +02:00
|
|
|
// WTF: cyclomatic complexity hell
|
2015-02-14 09:59:12 +01:00
|
|
|
if((colIndex===j &&
|
2015-06-10 12:53:20 +02:00
|
|
|
(!isLinked ||
|
|
|
|
(isLinked && tf.disableExcludedOptions))) ||
|
|
|
|
(colIndex==j && isLinked &&
|
2015-02-14 09:59:12 +01:00
|
|
|
((rows[k].style.display === '' && !tf.paging) ||
|
|
|
|
(tf.paging && (!tf.validRowsIndex ||
|
|
|
|
(tf.validRowsIndex &&
|
2015-10-19 14:41:09 +02:00
|
|
|
tf.validRowsIndex.indexOf(k) != -1)) &&
|
2015-02-14 09:59:12 +01:00
|
|
|
((activeFlt===undefined || activeFlt==colIndex) ||
|
|
|
|
(activeFlt!=colIndex &&
|
2015-10-19 14:41:09 +02:00
|
|
|
tf.validRowsIndex.indexOf(k) != -1 ))) ))){
|
2016-01-06 08:12:16 +01:00
|
|
|
let cell_data = tf.getCellData(cell[j]),
|
2015-02-14 09:59:12 +01:00
|
|
|
//Vary Peter's patch
|
|
|
|
cell_string = Str.matchCase(cell_data, matchCase);
|
|
|
|
|
|
|
|
// checks if celldata is already in array
|
2015-05-30 14:23:33 +02:00
|
|
|
if(!Arr.has(this.opts, cell_string, matchCase)){
|
2015-02-14 09:59:12 +01:00
|
|
|
this.opts.push(cell_data);
|
|
|
|
}
|
|
|
|
|
2015-06-10 12:53:20 +02:00
|
|
|
if(isLinked && tf.disableExcludedOptions){
|
2016-01-06 08:12:16 +01:00
|
|
|
let filteredCol = filteredDataCol[j];
|
2015-02-14 09:59:12 +01:00
|
|
|
if(!filteredCol){
|
2015-06-13 13:23:33 +02:00
|
|
|
filteredCol = tf.getFilteredDataCol(j);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
2015-05-30 14:23:33 +02:00
|
|
|
if(!Arr.has(filteredCol, cell_string, matchCase) &&
|
|
|
|
!Arr.has(
|
2016-01-14 11:13:19 +01:00
|
|
|
excludedOpts, cell_string, matchCase)){
|
2015-02-14 09:59:12 +01:00
|
|
|
excludedOpts.push(cell_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}//if colIndex==j
|
|
|
|
}//for j
|
|
|
|
}//for k
|
|
|
|
|
|
|
|
//Retrieves custom values
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tf.sortSlc && !this.isCustom){
|
|
|
|
if (!matchCase){
|
|
|
|
this.opts.sort(Sort.ignoreCase);
|
|
|
|
if(excludedOpts){
|
|
|
|
excludedOpts.sort(Sort.ignoreCase);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.opts.sort();
|
|
|
|
if(excludedOpts){ excludedOpts.sort(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//asc sort
|
2015-10-19 14:41:09 +02:00
|
|
|
if(tf.sortNumAsc && tf.sortNumAsc.indexOf(colIndex) != -1){
|
2015-02-14 09:59:12 +01:00
|
|
|
try{
|
|
|
|
this.opts.sort( numSortAsc );
|
|
|
|
if(excludedOpts){
|
|
|
|
excludedOpts.sort(numSortAsc);
|
|
|
|
}
|
|
|
|
if(this.isCustom){
|
|
|
|
this.optsTxt.sort(numSortAsc);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
this.opts.sort();
|
|
|
|
if(excludedOpts){
|
|
|
|
excludedOpts.sort();
|
|
|
|
}
|
|
|
|
if(this.isCustom){
|
|
|
|
this.optsTxt.sort();
|
|
|
|
}
|
|
|
|
}//in case there are alphanumeric values
|
|
|
|
}
|
|
|
|
//desc sort
|
2015-10-19 14:41:09 +02:00
|
|
|
if(tf.sortNumDesc && tf.sortNumDesc.indexOf(colIndex) != -1){
|
2015-02-14 09:59:12 +01:00
|
|
|
try{
|
|
|
|
this.opts.sort(numSortDesc);
|
|
|
|
if(excludedOpts){
|
|
|
|
excludedOpts.sort(numSortDesc);
|
|
|
|
}
|
|
|
|
if(this.isCustom){
|
|
|
|
this.optsTxt.sort(numSortDesc);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
this.opts.sort();
|
|
|
|
if(excludedOpts){
|
|
|
|
excludedOpts.sort();
|
|
|
|
}
|
|
|
|
if(this.isCustom){
|
|
|
|
this.optsTxt.sort();
|
|
|
|
}
|
|
|
|
}//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-01-17 08:27:12 +01: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
|
|
|
fillMethod = Str.lower(this.slcFillingMethod),
|
|
|
|
slcValue = slc.value;
|
|
|
|
|
|
|
|
slc.innerHTML = '';
|
|
|
|
slc = this.addFirstOption(slc);
|
|
|
|
|
2016-01-06 08:12:16 +01:00
|
|
|
for(let y=0; y<this.opts.length; y++){
|
2015-02-14 09:59:12 +01:00
|
|
|
if(this.opts[y]===''){
|
|
|
|
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;
|
2015-06-13 13:23:33 +02:00
|
|
|
if(isLinked && tf.disableExcludedOptions &&
|
2015-05-30 14:23:33 +02:00
|
|
|
Arr.has(
|
2015-02-14 09:59:12 +01:00
|
|
|
excludedOpts,
|
|
|
|
Str.matchCase(val, tf.matchCase),
|
|
|
|
tf.matchCase
|
|
|
|
)){
|
|
|
|
isDisabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fillMethod === 'innerhtml'){
|
2016-01-06 08:12:16 +01:00
|
|
|
let slcAttr = '';
|
2015-09-20 10:42:00 +02:00
|
|
|
if(tf.loadFltOnDemand && slcValue===this.opts[y]){
|
2015-02-14 09:59:12 +01:00
|
|
|
slcAttr = 'selected="selected"';
|
|
|
|
}
|
|
|
|
this.slcInnerHtml += '<option value="'+val+'" ' + slcAttr +
|
|
|
|
(isDisabled ? 'disabled="disabled"' : '')+ '>' +
|
|
|
|
lbl+'</option>';
|
|
|
|
} else {
|
2016-01-06 08:12:16 +01:00
|
|
|
let opt;
|
2015-02-14 09:59:12 +01:00
|
|
|
//fill select on demand
|
2015-09-20 10:42:00 +02:00
|
|
|
if(tf.loadFltOnDemand && slcValue===this.opts[y] &&
|
2015-10-13 08:41:14 +02:00
|
|
|
tf.getFilterType(colIndex) === tf.fltTypeSlc){
|
2015-02-14 09:59:12 +01:00
|
|
|
opt = Dom.createOpt(lbl, val, true);
|
|
|
|
} else {
|
2016-01-09 03:42:33 +01:00
|
|
|
opt = Dom.createOpt(lbl, val, false);
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
|
|
|
if(isDisabled){
|
|
|
|
opt.disabled = true;
|
|
|
|
}
|
|
|
|
slc.appendChild(opt);
|
|
|
|
}
|
|
|
|
}// for y
|
|
|
|
|
|
|
|
if(fillMethod === 'innerhtml'){
|
|
|
|
slc.innerHTML += this.slcInnerHtml;
|
|
|
|
}
|
|
|
|
slc.setAttribute('filled', '1');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add drop-down header option
|
|
|
|
* @param {Object} slc Select DOM element
|
|
|
|
*/
|
|
|
|
addFirstOption(slc){
|
2016-01-06 08:12:16 +01:00
|
|
|
let tf = this.tf,
|
2015-02-14 09:59:12 +01:00
|
|
|
fillMethod = Str.lower(this.slcFillingMethod);
|
|
|
|
|
|
|
|
if(fillMethod === 'innerhtml'){
|
|
|
|
this.slcInnerHtml += '<option value="">'+ tf.displayAllText +
|
|
|
|
'</option>';
|
|
|
|
}
|
|
|
|
else {
|
2016-01-06 08:12:16 +01:00
|
|
|
let opt0 = Dom.createOpt(
|
2015-02-14 09:59:12 +01:00
|
|
|
(!this.enableSlcResetFilter ? '' : tf.displayAllText),'');
|
|
|
|
if(!this.enableSlcResetFilter){
|
|
|
|
opt0.style.display = 'none';
|
|
|
|
}
|
|
|
|
slc.appendChild(opt0);
|
|
|
|
if(tf.enableEmptyOption){
|
2016-01-06 08:12:16 +01:00
|
|
|
let opt1 = Dom.createOpt(tf.emptyText, tf.emOperator);
|
2015-02-14 09:59:12 +01:00
|
|
|
slc.appendChild(opt1);
|
|
|
|
}
|
|
|
|
if(tf.enableNonEmptyOption){
|
2016-01-06 08:12:16 +01:00
|
|
|
let opt2 = Dom.createOpt(tf.nonEmptyText, tf.nmOperator);
|
2015-02-14 09:59:12 +01:00
|
|
|
slc.appendChild(opt2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slc;
|
|
|
|
}
|
|
|
|
|
2016-01-17 07:56:15 +01:00
|
|
|
destroy(){
|
|
|
|
this.emitter.off(
|
|
|
|
['build-select-filter'],
|
|
|
|
(colIndex, isLinked, isExternal)=>
|
|
|
|
this.build(colIndex, isLinked, isExternal)
|
|
|
|
);
|
|
|
|
}
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|