import {Feature} from './feature'; import Dom from '../dom'; import Arr from '../array'; import Str from '../string'; import Sort from '../sort'; import Event from '../event'; export class Dropdown extends Feature{ /** * Dropdown UI component * @param {Object} tf TableFilter instance */ constructor(tf){ super(tf, 'dropdown'); // Configuration object let f = tf.config(); this.enableSlcResetFilter = f.enable_slc_reset_filter===false ? false : true; //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; this.opts = null; this.optsTxt = null; this.slcInnerHtml = null; } /** * 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); } tf.fltIds.push(/*this.prfxFlt+i+'_'+this.id*/slc.id); 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); } Event.add(slc, 'keypress', tf.Evt.detectKey.bind(tf)); Event.add(slc, 'change', tf.Evt.onSlcChange.bind(tf)); Event.add(slc, 'focus', tf.Evt.onSlcFocus.bind(tf)); this.initialized = true; } /** * Build drop-down filter UI * @param {Number} colIndex Column index * @param {Boolean} isLinked Enable linked refresh behaviour * @param {Boolean} isExternal Render in external container * @param {String} extSlcId External container id */ build(colIndex, isLinked=false, isExternal=false, extSlcId=null){ let tf = this.tf; colIndex = parseInt(colIndex, 10); this.emitter.emit('before-populating-filter', tf, colIndex); this.opts = []; this.optsTxt = []; this.slcInnerHtml = ''; let slcId = tf.fltIds[colIndex]; if((!Dom.id(slcId) && !isExternal) || (!Dom.id(extSlcId) && isExternal)){ return; } let slc = !isExternal ? Dom.id(slcId) : Dom.id(extSlcId), rows = tf.tbl.rows, matchCase = tf.matchCase; //custom select test this.isCustom = tf.isCustomOptions(colIndex); //custom selects text let activeFlt; if(isLinked && tf.activeFilterId){ activeFlt = tf.activeFilterId.split('_')[0]; activeFlt = activeFlt.split(tf.prfxFlt)[1]; } /*** remember grid values ***/ let fltsValues = [], fltArr = []; if(tf.rememberGridValues){ fltsValues = tf.feature('store').getFilterValues(tf.fltsValuesCookie); if(fltsValues && !Str.isEmpty(fltsValues.toString())){ if(this.isCustom){ fltArr.push(fltsValues[colIndex]); } else { fltArr = fltsValues[colIndex].split(' '+tf.orOperator+' '); } } } let excludedOpts = null, filteredDataCol = null; if(isLinked && tf.disableExcludedOptions){ excludedOpts = []; filteredDataCol = []; } for(let k=tf.refRow; k' + lbl+''; } else { let opt; //fill select on demand if(tf.loadFltOnDemand && slcValue===this.opts[y] && tf.getFilterType(colIndex) === tf.fltTypeSlc){ opt = Dom.createOpt(lbl, val, true); } else { if(tf.getFilterType(colIndex) !== tf.fltTypeMulti){ opt = Dom.createOpt( lbl, val, (fltsValues[colIndex]!==' ' && val===fltsValues[colIndex]) ? true : false ); } else { opt = Dom.createOpt( lbl, val, (Arr.has(fltArr, Str.matchCase(this.opts[y], tf.matchCase), tf.matchCase) || fltArr.toString().indexOf(val)!== -1) ? true : false ); } } 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){ let tf = this.tf, fillMethod = Str.lower(this.slcFillingMethod); if(fillMethod === 'innerhtml'){ this.slcInnerHtml += ''; } else { let opt0 = Dom.createOpt( (!this.enableSlcResetFilter ? '' : tf.displayAllText),''); if(!this.enableSlcResetFilter){ opt0.style.display = 'none'; } slc.appendChild(opt0); if(tf.enableEmptyOption){ let opt1 = Dom.createOpt(tf.emptyText, tf.emOperator); slc.appendChild(opt1); } if(tf.enableNonEmptyOption){ let opt2 = Dom.createOpt(tf.nonEmptyText, tf.nmOperator); slc.appendChild(opt2); } } return slc; } destroy(){} }