1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-23 16:52:26 +02:00
TableFilter/src/modules/alternateRows.js

100 lines
2.3 KiB
JavaScript
Raw Normal View History

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) {
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-06-27 16:47:13 +02:00
var tf = this.tf;
if(!tf.hasGrid() && !tf.isFirstLoad){
2014-11-15 15:34:32 +01:00
return;
}
2015-06-27 16:47:13 +02:00
var validRowsIndex = tf.validRowsIndex;
var noValidRowsIndex = validRowsIndex===null;
2014-11-15 15:34:32 +01:00
//1st index
2015-06-27 16:47:13 +02:00
var beginIndex = noValidRowsIndex ? tf.refRow : 0;
2014-11-15 15:34:32 +01:00
// nb indexes
var indexLen = noValidRowsIndex ?
2015-06-27 16:47:13 +02:00
tf.nbFilterableRows+beginIndex :
validRowsIndex.length;
2014-11-15 15:34:32 +01:00
var idx = 0;
//alternates bg color
for(var j=beginIndex; j<indexLen; j++){
2015-06-27 16:47:13 +02:00
var rowIdx = noValidRowsIndex ? j : validRowsIndex[j];
2014-11-15 15:34:32 +01:00
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 = isNaN(idx) ? rowIdx : idx;
2014-11-15 15:34:32 +01:00
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
}
/**
2015-06-27 16:47:13 +02:00
* Removes all alternating backgrounds
2014-11-15 15:34:32 +01:00
*/
remove() {
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);
}
}
enable() {
this.tf.alternateBgs = true;
}
disable() {
this.tf.alternateBgs = false;
}
}