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

129 lines
3.3 KiB
JavaScript
Raw Normal View History

2015-11-14 16:14:13 +01:00
import {Feature} from './feature';
2015-05-30 14:23:33 +02:00
import Dom from '../dom';
2014-11-15 15:34:32 +01:00
2015-11-14 16:14:13 +01:00
export class AlternateRows extends Feature {
2014-11-15 15:34:32 +01:00
/**
* Alternating rows color
* @param {Object} tf TableFilter instance
*/
constructor(tf) {
2015-11-14 16:14:13 +01:00
super(tf, 'alternateRows');
var config = this.config;
2014-11-15 15:34:32 +01:00
//defines css class for even rows
2015-11-14 16:14:13 +01:00
this.evenCss = config.even_row_css_class || 'even';
2014-11-15 15:34:32 +01:00
//defines css class for odd rows
2015-11-14 16:14:13 +01:00
this.oddCss = config.odd_row_css_class || 'odd';
2014-11-15 15:34:32 +01:00
}
/**
* Sets alternating rows color
*/
2014-11-16 01:29:07 +01:00
init() {
2015-11-14 16:14:13 +01:00
if(this.initialized){
2014-11-15 15:34:32 +01:00
return;
}
2015-11-14 16:14:13 +01:00
this.processAll();
2016-01-03 03:49:04 +01:00
// Subscribe to events
this.emitter.on(['row-processed', 'row-paged'],
(tf, rowIndex, arrIndex, isValid)=>
this.processRow(rowIndex, arrIndex, isValid));
this.emitter.on(['column-sorted'], ()=> this.processAll());
this.initialized = true;
}
processAll() {
if(!this.isEnabled()){
return;
}
2015-11-14 16:14:13 +01:00
var tf = this.tf;
var validRowsIndex = tf.getValidRows(true);
2015-12-31 05:20:51 +01:00
var noValidRowsIndex = validRowsIndex.length === 0;
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++;
}
}
2015-12-31 02:56:50 +01:00
/**
* Set/remove row background based on row validation
* @param {Number} rowIdx Row index
* @param {Boolean} isValid Valid row flag
*/
processRow(rowIdx, arrIdx, isValid) {
2015-12-30 07:59:20 +01:00
if(isValid){
this.setRowBg(rowIdx, arrIdx);
2015-12-30 07:59:20 +01:00
} else {
this.removeRowBg(rowIdx);
}
}
2014-11-15 15:34:32 +01:00
/**
* Sets row background color
* @param {Number} rowIdx Row index
* @param {Number} idx Valid rows collection index needed to calculate bg
* color
*/
setRowBg(rowIdx, idx) {
2015-11-14 16:14:13 +01:00
if(!this.isEnabled() || isNaN(rowIdx)){
2014-11-15 15:34:32 +01:00
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
*/
2015-11-14 16:14:13 +01:00
destroy() {
if(!this.initialized){
2014-11-15 15:34:32 +01:00
return;
}
2016-01-17 08:27:12 +01:00
for(var i=0; i<this.tf.nbRows; i++){
2014-11-15 15:34:32 +01:00
this.removeRowBg(i);
}
2015-12-30 07:59:20 +01:00
2016-01-03 03:49:04 +01:00
// Unsubscribe to events
this.emitter.off(['row-processed', 'row-paged'],
(tf, rowIndex, arrIndex, isValid)=>
this.processRow(rowIndex, arrIndex, isValid));
this.emitter.off(['column-sorted'], ()=> this.processAll());
2015-12-30 07:59:20 +01:00
2015-11-14 16:14:13 +01:00
this.initialized = false;
2014-11-15 15:34:32 +01:00
}
}