2015-05-30 14:23:33 +02:00
|
|
|
import Dom from '../dom';
|
|
|
|
import Arr from '../array';
|
|
|
|
import Str from '../string';
|
|
|
|
import Sort from '../sort';
|
2015-02-14 09:59:12 +01:00
|
|
|
|
|
|
|
export class Dropdown{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dropdown UI component
|
|
|
|
* @param {Object} tf TableFilter instance
|
|
|
|
*/
|
|
|
|
constructor(tf){
|
|
|
|
// Configuration object
|
2015-02-21 04:39:09 +01:00
|
|
|
var 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;
|
2015-02-14 09:59:12 +01:00
|
|
|
|
|
|
|
this.tf = tf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-15 04:25:22 +01:00
|
|
|
* Build drop-down filter UI asynchronously
|
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
* @param {Boolean} isRefreshed Enable linked refresh behaviour
|
|
|
|
* @param {Boolean} isExternal Render in external container
|
|
|
|
* @param {String} extSlcId External container id
|
|
|
|
*/
|
|
|
|
build(colIndex, isRefreshed, isExternal, extSlcId){
|
|
|
|
var tf = this.tf;
|
|
|
|
tf.EvtManager(
|
|
|
|
tf.Evt.name.dropdown,
|
|
|
|
{
|
|
|
|
slcIndex: colIndex,
|
|
|
|
slcRefreshed: isRefreshed,
|
|
|
|
slcExternal: isExternal,
|
|
|
|
slcId: extSlcId
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build drop-down filter UI
|
2015-02-14 09:59:12 +01:00
|
|
|
* @param {Number} colIndex Column index
|
|
|
|
* @param {Boolean} isRefreshed Enable linked refresh behaviour
|
|
|
|
* @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
|
|
|
*/
|
2015-02-15 04:25:22 +01:00
|
|
|
_build(colIndex, isRefreshed=false, isExternal=false, extSlcId=null){
|
2015-02-14 09:59:12 +01:00
|
|
|
var tf = this.tf;
|
|
|
|
colIndex = parseInt(colIndex, 10);
|
|
|
|
|
2015-02-15 04:25:22 +01:00
|
|
|
this.opts = [];
|
|
|
|
this.optsTxt = [];
|
|
|
|
this.slcInnerHtml = '';
|
|
|
|
|
2015-02-14 09:59:12 +01:00
|
|
|
var slcId = tf.fltIds[colIndex];
|
|
|
|
if((!Dom.id(slcId) && !isExternal) ||
|
|
|
|
(!Dom.id(extSlcId) && isExternal)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var slc = !isExternal ? Dom.id(slcId) : Dom.id(extSlcId),
|
|
|
|
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
|
|
|
|
var activeFlt;
|
|
|
|
if(isRefreshed && tf.activeFilterId){
|
|
|
|
activeFlt = tf.activeFilterId.split('_')[0];
|
|
|
|
activeFlt = activeFlt.split(tf.prfxFlt)[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** remember grid values ***/
|
2015-02-15 04:25:22 +01:00
|
|
|
var fltsValues = [], fltArr = [];
|
2015-02-14 09:59:12 +01:00
|
|
|
if(tf.rememberGridValues){
|
2015-02-15 04:25:22 +01:00
|
|
|
fltsValues = tf.Cpt.store.getFilterValues(tf.fltsValuesCookie);
|
|
|
|
if(fltsValues && !Str.isEmpty(fltsValues.toString())){
|
2015-02-14 09:59:12 +01:00
|
|
|
if(this.isCustom){
|
2015-02-15 04:25:22 +01:00
|
|
|
fltArr.push(fltsValues[colIndex]);
|
2015-02-14 09:59:12 +01:00
|
|
|
} else {
|
2015-02-15 04:25:22 +01:00
|
|
|
fltArr = fltsValues[colIndex].split(' '+tf.orOperator+' ');
|
2015-02-14 09:59:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var excludedOpts = null,
|
|
|
|
filteredDataCol = null;
|
|
|
|
if(isRefreshed && tf.disableExcludedOptions){
|
|
|
|
excludedOpts = [];
|
|
|
|
filteredDataCol = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var k=tf.refRow; k<tf.nbRows; k++){
|
|
|
|
// always visible rows don't need to appear on selects as always
|
|
|
|
// valid
|
2015-05-30 14:23:33 +02:00
|
|
|
if(tf.hasVisibleRows && Arr.has(tf.visibleRows, k) &&
|
2015-02-14 09:59:12 +01:00
|
|
|
!tf.paging){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cell = rows[k].cells,
|
|
|
|
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
|
|
|
|
for(var j=0; j<nchilds; j++){
|
|
|
|
if((colIndex===j &&
|
|
|
|
(!isRefreshed ||
|
|
|
|
(isRefreshed && tf.disableExcludedOptions))) ||
|
|
|
|
(colIndex==j && isRefreshed &&
|
|
|
|
((rows[k].style.display === '' && !tf.paging) ||
|
|
|
|
(tf.paging && (!tf.validRowsIndex ||
|
|
|
|
(tf.validRowsIndex &&
|
2015-05-30 14:23:33 +02:00
|
|
|
Arr.has(tf.validRowsIndex, k))) &&
|
2015-02-14 09:59:12 +01:00
|
|
|
((activeFlt===undefined || activeFlt==colIndex) ||
|
|
|
|
(activeFlt!=colIndex &&
|
2015-05-30 14:23:33 +02:00
|
|
|
Arr.has(tf.validRowsIndex, k) ))) ))){
|
2015-02-21 04:39:09 +01:00
|
|
|
var cell_data = tf.getCellData(j, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isRefreshed && tf.disableExcludedOptions){
|
|
|
|
var filteredCol = filteredDataCol[j];
|
|
|
|
if(!filteredCol){
|
|
|
|
filteredCol = this.GetFilteredDataCol(j);
|
|
|
|
}
|
2015-05-30 14:23:33 +02:00
|
|
|
if(!Arr.has(filteredCol, cell_string, matchCase) &&
|
|
|
|
!Arr.has(
|
2015-02-14 09:59:12 +01:00
|
|
|
excludedOpts, cell_string, matchCase) &&
|
|
|
|
!this.isFirstLoad){
|
|
|
|
excludedOpts.push(cell_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}//if colIndex==j
|
|
|
|
}//for j
|
|
|
|
}//for k
|
|
|
|
|
|
|
|
//Retrieves custom values
|
|
|
|
if(this.isCustom){
|
2015-06-02 09:33:16 +02:00
|
|
|
var 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-05-30 14:23:33 +02:00
|
|
|
if(tf.sortNumAsc && Arr.has(tf.sortNumAsc, colIndex)){
|
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-05-30 14:23:33 +02:00
|
|
|
if(tf.sortNumDesc && Arr.has(tf.sortNumDesc, colIndex)){
|
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
|
2015-02-15 04:25:22 +01:00
|
|
|
this.addOptions(
|
|
|
|
colIndex, slc, isRefreshed, excludedOpts, fltsValues, fltArr);
|
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
|
|
|
|
* @param {Boolean} isRefreshed Enable linked refresh behaviour
|
|
|
|
* @param {Array} excludedOpts Array of excluded options
|
|
|
|
* @param {Array} fltsValues Collection of persisted filter values
|
|
|
|
* @param {Array} fltArr Collection of persisted filter values
|
2015-02-14 09:59:12 +01:00
|
|
|
*/
|
2015-02-15 04:25:22 +01:00
|
|
|
addOptions(colIndex, slc, isRefreshed, excludedOpts, fltsValues, fltArr){
|
2015-02-14 09:59:12 +01:00
|
|
|
var tf = this.tf,
|
|
|
|
fillMethod = Str.lower(this.slcFillingMethod),
|
|
|
|
slcValue = slc.value;
|
|
|
|
|
|
|
|
slc.innerHTML = '';
|
|
|
|
slc = this.addFirstOption(slc);
|
|
|
|
|
|
|
|
for(var y=0; y<this.opts.length; y++){
|
|
|
|
if(this.opts[y]===''){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var val = this.opts[y]; //option value
|
|
|
|
var lbl = this.isCustom ? this.optsTxt[y] : val; //option text
|
|
|
|
var isDisabled = false;
|
|
|
|
if(isRefreshed && this.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'){
|
|
|
|
var slcAttr = '';
|
|
|
|
if(tf.fillSlcOnDemand && slcValue===this.opts[y]){
|
|
|
|
slcAttr = 'selected="selected"';
|
|
|
|
}
|
|
|
|
this.slcInnerHtml += '<option value="'+val+'" ' + slcAttr +
|
|
|
|
(isDisabled ? 'disabled="disabled"' : '')+ '>' +
|
|
|
|
lbl+'</option>';
|
|
|
|
} else {
|
|
|
|
var opt;
|
|
|
|
//fill select on demand
|
|
|
|
if(tf.fillSlcOnDemand && slcValue===this.opts[y] &&
|
|
|
|
tf['col'+colIndex]===tf.fltTypeSlc){
|
|
|
|
opt = Dom.createOpt(lbl, val, true);
|
|
|
|
} else {
|
|
|
|
if(tf['col'+colIndex]!==tf.fltTypeMulti){
|
|
|
|
opt = Dom.createOpt(
|
|
|
|
lbl,
|
|
|
|
val,
|
2015-02-15 04:25:22 +01:00
|
|
|
(fltsValues[colIndex]!==' ' &&
|
|
|
|
val===fltsValues[colIndex]) ? true : false
|
2015-02-14 09:59:12 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
opt = Dom.createOpt(
|
|
|
|
lbl,
|
|
|
|
val,
|
2015-05-30 14:23:33 +02:00
|
|
|
(Arr.has(fltArr,
|
2015-02-14 09:59:12 +01:00
|
|
|
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){
|
|
|
|
var tf = this.tf,
|
|
|
|
fillMethod = Str.lower(this.slcFillingMethod);
|
|
|
|
|
|
|
|
if(fillMethod === 'innerhtml'){
|
|
|
|
this.slcInnerHtml += '<option value="">'+ tf.displayAllText +
|
|
|
|
'</option>';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var opt0 = Dom.createOpt(
|
|
|
|
(!this.enableSlcResetFilter ? '' : tf.displayAllText),'');
|
|
|
|
if(!this.enableSlcResetFilter){
|
|
|
|
opt0.style.display = 'none';
|
|
|
|
}
|
|
|
|
slc.appendChild(opt0);
|
|
|
|
if(tf.enableEmptyOption){
|
|
|
|
var opt1 = Dom.createOpt(tf.emptyText, tf.emOperator);
|
|
|
|
slc.appendChild(opt1);
|
|
|
|
}
|
|
|
|
if(tf.enableNonEmptyOption){
|
|
|
|
var opt2 = Dom.createOpt(tf.nonEmptyText, tf.nmOperator);
|
|
|
|
slc.appendChild(opt2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slc;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|