2015-05-30 14:23:33 +02:00
|
|
|
import Dom from '../dom';
|
|
|
|
import Types from '../types';
|
2014-11-16 01:29:07 +01:00
|
|
|
|
|
|
|
export class RowsCounter{
|
2014-11-16 11:01:29 +01:00
|
|
|
|
2014-11-16 01:29:07 +01:00
|
|
|
/**
|
|
|
|
* Rows counter
|
|
|
|
* @param {Object} tf TableFilter instance
|
|
|
|
*/
|
|
|
|
constructor(tf){
|
|
|
|
// TableFilter configuration
|
2015-02-21 04:39:09 +01:00
|
|
|
var f = tf.config();
|
2014-11-16 01:29:07 +01:00
|
|
|
|
|
|
|
//id of custom container element
|
|
|
|
this.rowsCounterTgtId = f.rows_counter_target_id || null;
|
|
|
|
//element containing tot nb rows
|
|
|
|
this.rowsCounterDiv = null;
|
|
|
|
//element containing tot nb rows label
|
|
|
|
this.rowsCounterSpan = null;
|
|
|
|
//defines rows counter text
|
|
|
|
this.rowsCounterText = f.rows_counter_text || 'Rows: ';
|
|
|
|
this.fromToTextSeparator = f.from_to_text_separator || '-';
|
|
|
|
this.overText = f.over_text || ' / ';
|
|
|
|
//defines css class rows counter
|
|
|
|
this.totRowsCssClass = f.tot_rows_css_class || 'tot';
|
2015-04-12 09:52:19 +02:00
|
|
|
//rows counter div
|
|
|
|
this.prfxCounter = 'counter_';
|
|
|
|
//nb displayed rows label
|
|
|
|
this.prfxTotRows = 'totrows_span_';
|
|
|
|
//label preceding nb rows label
|
|
|
|
this.prfxTotRowsTxt = 'totRowsTextSpan_';
|
2014-11-16 01:29:07 +01:00
|
|
|
//callback raised before counter is refreshed
|
2014-11-16 11:01:29 +01:00
|
|
|
this.onBeforeRefreshCounter = Types.isFn(f.on_before_refresh_counter) ?
|
|
|
|
f.on_before_refresh_counter : null;
|
2014-11-16 01:29:07 +01:00
|
|
|
//callback raised after counter is refreshed
|
|
|
|
this.onAfterRefreshCounter = Types.isFn(f.on_after_refresh_counter) ?
|
|
|
|
f.on_after_refresh_counter : null;
|
|
|
|
|
|
|
|
this.tf = tf;
|
|
|
|
}
|
|
|
|
|
|
|
|
init(){
|
|
|
|
var tf = this.tf;
|
|
|
|
|
2015-02-21 04:39:09 +01:00
|
|
|
if((!tf.hasGrid() && !tf.isFirstLoad) || this.rowsCounterSpan){
|
2014-11-16 01:29:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//rows counter container
|
2015-04-12 09:52:19 +02:00
|
|
|
var countDiv = Dom.create('div', ['id', this.prfxCounter+tf.id]);
|
2014-11-16 01:29:07 +01:00
|
|
|
countDiv.className = this.totRowsCssClass;
|
|
|
|
//rows counter label
|
2015-04-12 09:52:19 +02:00
|
|
|
var countSpan = Dom.create('span', ['id', this.prfxTotRows+tf.id]);
|
|
|
|
var countText = Dom.create('span', ['id', this.prfxTotRowsTxt+tf.id]);
|
2014-11-16 01:29:07 +01:00
|
|
|
countText.appendChild(Dom.text(this.rowsCounterText));
|
|
|
|
|
|
|
|
// counter is added to defined element
|
|
|
|
if(!this.rowsCounterTgtId){
|
2015-02-21 04:39:09 +01:00
|
|
|
tf.setToolbar();
|
2014-11-16 01:29:07 +01:00
|
|
|
}
|
|
|
|
var targetEl = !this.rowsCounterTgtId ?
|
|
|
|
tf.lDiv : Dom.id( this.rowsCounterTgtId );
|
|
|
|
|
|
|
|
//default container: 'lDiv'
|
|
|
|
if(!this.rowsCounterTgtId){
|
|
|
|
countDiv.appendChild(countText);
|
|
|
|
countDiv.appendChild(countSpan);
|
|
|
|
targetEl.appendChild(countDiv);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
//custom container, no need to append statusDiv
|
|
|
|
targetEl.appendChild(countText);
|
|
|
|
targetEl.appendChild(countSpan);
|
|
|
|
}
|
|
|
|
this.rowsCounterDiv = countDiv;
|
|
|
|
this.rowsCounterSpan = countSpan;
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh(p){
|
|
|
|
if(!this.rowsCounterSpan){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var tf = this.tf;
|
|
|
|
|
|
|
|
if(this.onBeforeRefreshCounter){
|
|
|
|
this.onBeforeRefreshCounter.call(null, tf, this.rowsCounterSpan);
|
|
|
|
}
|
|
|
|
|
|
|
|
var totTxt;
|
|
|
|
if(!tf.paging){
|
|
|
|
if(p && p !== ''){
|
|
|
|
totTxt = p;
|
|
|
|
} else{
|
2015-08-01 09:45:29 +02:00
|
|
|
totTxt = tf.nbFilterableRows - tf.nbHiddenRows;
|
2014-11-16 01:29:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-07 17:31:15 +02:00
|
|
|
var paging = tf.feature('paging');
|
2015-01-26 10:31:25 +01:00
|
|
|
if(paging){
|
|
|
|
//paging start row
|
|
|
|
var paging_start_row = parseInt(paging.startPagingRow, 10) +
|
|
|
|
((tf.nbVisibleRows>0) ? 1 : 0);
|
|
|
|
var paging_end_row = (paging_start_row+paging.pagingLength)-1 <=
|
|
|
|
tf.nbVisibleRows ?
|
|
|
|
paging_start_row+paging.pagingLength-1 :
|
|
|
|
tf.nbVisibleRows;
|
|
|
|
totTxt = paging_start_row + this.fromToTextSeparator +
|
|
|
|
paging_end_row + this.overText + tf.nbVisibleRows;
|
|
|
|
}
|
2014-11-16 01:29:07 +01:00
|
|
|
}
|
2015-08-01 09:45:29 +02:00
|
|
|
|
2014-11-16 01:29:07 +01:00
|
|
|
this.rowsCounterSpan.innerHTML = totTxt;
|
|
|
|
if(this.onAfterRefreshCounter){
|
|
|
|
this.onAfterRefreshCounter.call(
|
|
|
|
null, tf, this.rowsCounterSpan, totTxt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(){
|
|
|
|
var tf = this.tf;
|
2015-07-04 14:08:58 +02:00
|
|
|
if(!tf.hasGrid() || !this.rowsCounterSpan){
|
2014-11-16 01:29:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!this.rowsCounterTgtId && this.rowsCounterDiv){
|
2015-07-04 14:08:58 +02:00
|
|
|
this.rowsCounterDiv.parentNode.removeChild(this.rowsCounterDiv);
|
2014-11-16 01:29:07 +01:00
|
|
|
} else {
|
2015-07-04 14:08:58 +02:00
|
|
|
Dom.id(this.rowsCounterTgtId).innerHTML = '';
|
2014-11-16 01:29:07 +01:00
|
|
|
}
|
|
|
|
this.rowsCounterSpan = null;
|
|
|
|
this.rowsCounterDiv = null;
|
|
|
|
}
|
2015-07-04 14:08:58 +02:00
|
|
|
}
|