2015-05-30 14:23:33 +02:00
|
|
|
import Dom from '../dom';
|
2014-11-15 15:34:32 +01:00
|
|
|
|
|
|
|
export class AlternateRows{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternating rows color
|
|
|
|
* @param {Object} tf TableFilter instance
|
|
|
|
*/
|
|
|
|
constructor(tf) {
|
2015-02-21 04:39:09 +01:00
|
|
|
var f = tf.config();
|
2014-11-15 15:34:32 +01:00
|
|
|
//defines css class for even rows
|
|
|
|
this.evenCss = f.even_row_css_class || 'even';
|
|
|
|
//defines css class for odd rows
|
|
|
|
this.oddCss = f.odd_row_css_class || 'odd';
|
|
|
|
|
|
|
|
this.tf = tf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets alternating rows color
|
|
|
|
*/
|
2014-11-16 01:29:07 +01:00
|
|
|
init() {
|
2015-02-21 04:39:09 +01:00
|
|
|
if(!this.tf.hasGrid() && !this.tf.isFirstLoad){
|
2014-11-15 15:34:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var noValidRowsIndex = this.tf.validRowsIndex===null;
|
|
|
|
//1st index
|
|
|
|
var beginIndex = noValidRowsIndex ? this.tf.refRow : 0;
|
|
|
|
// nb indexes
|
|
|
|
var indexLen = noValidRowsIndex ?
|
|
|
|
this.tf.nbFilterableRows+beginIndex :
|
|
|
|
this.tf.validRowsIndex.length;
|
|
|
|
var idx = 0;
|
|
|
|
|
|
|
|
//alternates bg color
|
|
|
|
for(var j=beginIndex; j<indexLen; j++){
|
|
|
|
var rowIdx = noValidRowsIndex ? j : this.tf.validRowsIndex[j];
|
|
|
|
this.setRowBg(rowIdx, idx);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets row background color
|
|
|
|
* @param {Number} rowIdx Row index
|
|
|
|
* @param {Number} idx Valid rows collection index needed to calculate bg
|
|
|
|
* color
|
|
|
|
*/
|
|
|
|
setRowBg(rowIdx, idx) {
|
|
|
|
if(!this.tf.alternateBgs || isNaN(rowIdx)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var rows = this.tf.tbl.rows;
|
|
|
|
var i = !idx ? rowIdx : idx;
|
|
|
|
this.removeRowBg(rowIdx);
|
2014-11-16 01:29:07 +01:00
|
|
|
Dom.addClass(
|
2014-11-15 15:34:32 +01:00
|
|
|
rows[rowIdx],
|
|
|
|
(i%2) ? this.evenCss : this.oddCss
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes row background color
|
|
|
|
* @param {Number} idx Row index
|
|
|
|
*/
|
|
|
|
removeRowBg(idx) {
|
|
|
|
if(isNaN(idx)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var rows = this.tf.tbl.rows;
|
2014-11-16 01:29:07 +01:00
|
|
|
Dom.removeClass(rows[idx], this.oddCss);
|
|
|
|
Dom.removeClass(rows[idx], this.evenCss);
|
2014-11-15 15:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all row background color
|
|
|
|
*/
|
|
|
|
remove() {
|
2015-02-21 04:39:09 +01:00
|
|
|
if(!this.tf.hasGrid()){
|
2014-11-15 15:34:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(var i=this.tf.refRow; i<this.tf.nbRows; i++){
|
|
|
|
this.removeRowBg(i);
|
|
|
|
}
|
|
|
|
this.tf.isStartBgAlternate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
|
|
|
this.tf.alternateBgs = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {
|
|
|
|
this.tf.alternateBgs = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|