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

341 lines
12 KiB
JavaScript
Raw Normal View History

import {Feature} from './feature';
2015-05-30 14:23:33 +02:00
import Dom from '../dom';
import Types from '../types';
import Event from '../event';
2015-12-05 14:37:59 +01:00
import Str from '../string';
2014-11-22 15:01:29 +01:00
export class GridLayout extends Feature{
2014-11-22 15:01:29 +01:00
2014-11-23 11:31:55 +01:00
/**
* Grid layout, table with fixed headers
* @param {Object} tf TableFilter instance
*/
constructor(tf){
super(tf, 'gridLayout');
2016-01-04 07:59:30 +01:00
let f = this.config;
2014-11-22 15:01:29 +01:00
//defines grid width
this.gridWidth = f.grid_width || null;
//defines grid height
this.gridHeight = f.grid_height || null;
//defines css class for main container
this.gridMainContCssClass = f.grid_cont_css_class || 'grd_Cont';
//defines css class for div containing table
this.gridContCssClass = f.grid_tbl_cont_css_class || 'grd_tblCont';
//defines css class for div containing headers' table
this.gridHeadContCssClass = f.grid_tblHead_cont_css_class ||
'grd_headTblCont';
//defines css class for div containing rows counter, paging etc.
this.gridInfDivCssClass = f.grid_inf_grid_css_class || 'grd_inf';
//defines which row contains column headers
this.gridHeadRowIndex = f.grid_headers_row_index || 0;
//array of headers row indexes to be placed in header table
this.gridHeadRows = f.grid_headers_rows || [0];
//generate filters in table headers
this.gridEnableFilters = f.grid_enable_default_filters!==undefined ?
f.grid_enable_default_filters : true;
//default col width
this.gridDefaultColWidth = f.grid_default_col_width || '100px';
2014-11-23 11:31:55 +01:00
this.gridColElms = [];
//div containing grid elements if grid_layout true
this.prfxMainTblCont = 'gridCont_';
//div containing table if grid_layout true
this.prfxTblCont = 'tblCont_';
//div containing headers table if grid_layout true
this.prfxHeadTblCont = 'tblHeadCont_';
//headers' table if grid_layout true
this.prfxHeadTbl = 'tblHead_';
//id of td containing the filter if grid_layout true
this.prfxGridFltTd = '_td_';
//id of th containing column header if grid_layout true
this.prfxGridTh = 'tblHeadTh_';
this.sourceTblHtml = tf.tbl.outerHTML;
// filters flag at TF level
tf.fltGrid = this.gridEnableFilters;
2014-11-22 15:01:29 +01:00
}
2014-11-23 11:31:55 +01:00
/**
* Generates a grid with fixed headers
*/
2014-11-22 15:01:29 +01:00
init(){
2016-01-04 07:59:30 +01:00
let tf = this.tf;
let f = this.config;
let tbl = tf.tbl;
2014-11-22 15:01:29 +01:00
if(this.initialized){
2014-11-23 11:31:55 +01:00
return;
}
2016-01-04 07:59:30 +01:00
// Override reference rows indexes
tf.refRow = Types.isNull(tf.startRow) ? 0 : tf.startRow;
tf.headersRow = 0;
tf.filtersRowIndex = 1;
2014-11-22 15:01:29 +01:00
tf.isExternalFlt = true;
2014-11-23 04:34:57 +01:00
// default width of 100px if column widths not set
2015-05-28 15:44:23 +02:00
if(!tf.hasColWidths){
tf.colWidths = [];
2016-01-04 07:59:30 +01:00
for(let k=0; k<tf.nbCells; k++){
let colW,
2014-11-22 15:01:29 +01:00
cell = tbl.rows[this.gridHeadRowIndex].cells[k];
2014-11-23 04:34:57 +01:00
if(cell.width !== ''){
2014-11-22 15:01:29 +01:00
colW = cell.width;
2014-11-23 04:34:57 +01:00
} else if(cell.style.width !== ''){
2014-11-22 15:01:29 +01:00
colW = parseInt(cell.style.width, 10);
} else {
colW = this.gridDefaultColWidth;
}
2015-05-28 15:44:23 +02:00
tf.colWidths[k] = colW;
2014-11-22 15:01:29 +01:00
}
2015-05-28 15:44:23 +02:00
tf.hasColWidths = true;
2014-11-22 15:01:29 +01:00
}
tf.setColWidths();
2014-11-22 15:01:29 +01:00
2016-01-04 07:59:30 +01:00
let tblW;//initial table width
2014-11-23 04:34:57 +01:00
if(tbl.width !== ''){
2014-11-22 15:01:29 +01:00
tblW = tbl.width;
}
2014-11-23 04:34:57 +01:00
else if(tbl.style.width !== ''){
2014-11-22 15:01:29 +01:00
tblW = parseInt(tbl.style.width, 10);
} else {
tblW = tbl.clientWidth;
}
//Main container: it will contain all the elements
this.tblMainCont = Dom.create('div',
['id', this.prfxMainTblCont + tf.id]);
2014-11-22 15:01:29 +01:00
this.tblMainCont.className = this.gridMainContCssClass;
if(this.gridWidth){
this.tblMainCont.style.width = this.gridWidth;
}
2014-11-23 04:34:57 +01:00
tbl.parentNode.insertBefore(this.tblMainCont, tbl);
2014-11-22 15:01:29 +01:00
//Table container: div wrapping content table
this.tblCont = Dom.create('div',['id', this.prfxTblCont + tf.id]);
2014-11-22 15:01:29 +01:00
this.tblCont.className = this.gridContCssClass;
if(this.gridWidth){
if(this.gridWidth.indexOf('%') != -1){
this.tblCont.style.width = '100%';
} else {
this.tblCont.style.width = this.gridWidth;
}
2014-11-22 15:01:29 +01:00
}
if(this.gridHeight){
this.tblCont.style.height = this.gridHeight;
}
2014-11-23 04:34:57 +01:00
tbl.parentNode.insertBefore(this.tblCont, tbl);
2016-01-04 07:59:30 +01:00
let t = Dom.remove(tbl);
2014-11-22 15:01:29 +01:00
this.tblCont.appendChild(t);
//In case table width is expressed in %
if(tbl.style.width === ''){
2015-12-05 14:37:59 +01:00
tbl.style.width = (Str.contains('%', tblW) ?
2014-11-22 15:01:29 +01:00
tbl.clientWidth : tblW) + 'px';
}
2016-01-04 07:59:30 +01:00
let d = Dom.remove(this.tblCont);
2014-11-22 15:01:29 +01:00
this.tblMainCont.appendChild(d);
//Headers table container: div wrapping headers table
this.headTblCont = Dom.create(
'div',['id', this.prfxHeadTblCont + tf.id]);
2014-11-22 15:01:29 +01:00
this.headTblCont.className = this.gridHeadContCssClass;
if(this.gridWidth){
if(this.gridWidth.indexOf('%') != -1){
this.headTblCont.style.width = '100%';
} else {
this.headTblCont.style.width = this.gridWidth;
}
2014-11-22 15:01:29 +01:00
}
//Headers table
this.headTbl = Dom.create('table', ['id', this.prfxHeadTbl + tf.id]);
2016-01-04 07:59:30 +01:00
let tH = Dom.create('tHead');
2014-11-22 15:01:29 +01:00
//1st row should be headers row, ids are added if not set
//Those ids are used by the sort feature
2016-01-04 07:59:30 +01:00
let hRow = tbl.rows[this.gridHeadRowIndex];
let sortTriggers = [];
for(let n=0; n<tf.nbCells; n++){
let c = hRow.cells[n];
let thId = c.getAttribute('id');
2014-11-22 15:01:29 +01:00
if(!thId || thId===''){
thId = this.prfxGridTh+n+'_'+tf.id;
2014-11-22 15:01:29 +01:00
c.setAttribute('id', thId);
}
sortTriggers.push(thId);
}
//Filters row is created
2016-01-04 07:59:30 +01:00
let filtersRow = Dom.create('tr');
2014-11-22 15:01:29 +01:00
if(this.gridEnableFilters && tf.fltGrid){
tf.externalFltTgtIds = [];
2016-01-04 07:59:30 +01:00
for(let j=0; j<tf.nbCells; j++){
let fltTdId = tf.prfxFlt+j+ this.prfxGridFltTd +tf.id;
let cl = Dom.create(tf.fltCellTag, ['id', fltTdId]);
2014-11-22 15:01:29 +01:00
filtersRow.appendChild(cl);
tf.externalFltTgtIds[j] = fltTdId;
}
}
//Headers row are moved from content table to headers table
2016-01-04 07:59:30 +01:00
for(let i=0; i<this.gridHeadRows.length; i++){
let headRow = tbl.rows[this.gridHeadRows[0]];
2014-11-22 15:01:29 +01:00
tH.appendChild(headRow);
}
this.headTbl.appendChild(tH);
if(tf.filtersRowIndex === 0){
2016-01-04 07:59:30 +01:00
tH.insertBefore(filtersRow, hRow);
2014-11-22 15:01:29 +01:00
} else {
tH.appendChild(filtersRow);
}
this.headTblCont.appendChild(this.headTbl);
this.tblCont.parentNode.insertBefore(this.headTblCont, this.tblCont);
//THead needs to be removed in content table for sort feature
2016-01-04 07:59:30 +01:00
let thead = Dom.tag(tbl, 'thead');
2014-11-22 15:01:29 +01:00
if(thead.length>0){
tbl.removeChild(thead[0]);
}
//Headers table style
this.headTbl.style.tableLayout = 'fixed';
tbl.style.tableLayout = 'fixed';
this.headTbl.cellPadding = tbl.cellPadding;
this.headTbl.cellSpacing = tbl.cellSpacing;
2015-04-24 12:38:20 +02:00
// this.headTbl.style.width = tbl.style.width;
2014-11-22 15:01:29 +01:00
//content table without headers needs col widths to be reset
tf.setColWidths(this.headTbl);
2014-11-22 15:01:29 +01:00
2014-11-23 11:31:55 +01:00
//Headers container width
// this.headTblCont.style.width = this.tblCont.clientWidth+'px';
2014-11-23 11:31:55 +01:00
2014-11-22 15:01:29 +01:00
tbl.style.width = '';
2015-04-24 12:38:20 +02:00
//
this.headTbl.style.width = tbl.clientWidth + 'px';
//
2014-11-22 15:01:29 +01:00
//scroll synchronisation
2015-04-24 12:38:20 +02:00
Event.add(this.tblCont, 'scroll', (evt)=> {
2016-01-04 07:59:30 +01:00
let elm = Event.target(evt);
let scrollLeft = elm.scrollLeft;
2015-04-24 12:38:20 +02:00
this.headTblCont.scrollLeft = scrollLeft;
2014-11-22 15:01:29 +01:00
//New pointerX calc taking into account scrollLeft
2015-04-24 12:38:20 +02:00
// if(!o.isPointerXOverwritten){
// try{
// o.Evt.pointerX = function(evt){
2016-01-04 07:59:30 +01:00
// let e = evt || global.event;
// let bdScrollLeft = tf_StandardBody().scrollLeft +
2015-04-24 12:38:20 +02:00
// scrollLeft;
// return (e.pageX + scrollLeft) ||
// (e.clientX + bdScrollLeft);
// };
// o.isPointerXOverwritten = true;
// } catch(err) {
// o.isPointerXOverwritten = false;
// }
// }
2014-11-22 15:01:29 +01:00
});
//Configure sort extension if any
2016-01-04 07:59:30 +01:00
let sort = (f.extensions || []).filter(function(itm){
return itm.name === 'sort';
});
if(sort.length === 1){
sort[0].async_sort = true;
sort[0].trigger_ids = sortTriggers;
2014-11-22 15:01:29 +01:00
}
//Cols generation for all browsers excepted IE<=7
2015-04-24 12:38:20 +02:00
this.tblHasColTag = Dom.tag(tbl, 'col').length > 0 ? true : false;
2014-11-23 11:31:55 +01:00
2015-01-11 11:22:52 +01:00
//Col elements are enough to keep column widths after sorting and
//filtering
2016-01-04 07:59:30 +01:00
let createColTags = function(){
for(let k=(tf.nbCells-1); k>=0; k--){
let col = Dom.create('col', ['id', tf.id+'_col_'+k]);
2015-06-06 12:06:15 +02:00
tbl.insertBefore(col, tbl.firstChild);
2015-05-28 15:44:23 +02:00
col.style.width = tf.colWidths[k];
2015-06-06 12:06:15 +02:00
this.gridColElms[k] = col;
2015-01-11 11:22:52 +01:00
}
2015-06-06 12:06:15 +02:00
this.tblHasColTag = true;
2015-01-11 11:22:52 +01:00
};
2015-06-06 12:06:15 +02:00
2015-04-24 12:38:20 +02:00
if(!this.tblHasColTag){
2015-06-06 12:06:15 +02:00
createColTags.call(this);
2015-01-11 11:22:52 +01:00
} else {
2016-01-04 07:59:30 +01:00
let cols = Dom.tag(tbl, 'col');
for(let ii=0; ii<tf.nbCells; ii++){
2015-01-11 11:22:52 +01:00
cols[ii].setAttribute('id', tf.id+'_col_'+ii);
2015-05-28 15:44:23 +02:00
cols[ii].style.width = tf.colWidths[ii];
2015-06-06 12:06:15 +02:00
this.gridColElms.push(cols[ii]);
2015-01-11 11:22:52 +01:00
}
}
2014-11-22 15:01:29 +01:00
2016-01-04 07:59:30 +01:00
let afterColResizedFn = Types.isFn(f.on_after_col_resized) ?
2014-11-22 15:01:29 +01:00
f.on_after_col_resized : null;
2015-04-24 12:38:20 +02:00
f.on_after_col_resized = function(o, colIndex){
2014-11-22 15:01:29 +01:00
if(!colIndex){
return;
}
2016-01-04 07:59:30 +01:00
let w = o.crWColsRow.cells[colIndex].style.width;
let col = o.gridColElms[colIndex];
2014-11-22 15:01:29 +01:00
col.style.width = w;
2016-01-04 07:59:30 +01:00
let thCW = o.crWColsRow.cells[colIndex].clientWidth;
let tdCW = o.crWRowDataTbl.cells[colIndex].clientWidth;
2014-11-22 15:01:29 +01:00
if(thCW != tdCW){
2014-11-22 15:01:29 +01:00
o.headTbl.style.width = tbl.clientWidth+'px';
}
if(afterColResizedFn){
2015-12-05 14:37:59 +01:00
afterColResizedFn.call(null, o, colIndex);
2014-11-22 15:01:29 +01:00
}
};
if(tf.popupFilters){
filtersRow.style.display = 'none';
}
2014-11-22 15:01:29 +01:00
if(tbl.clientWidth !== this.headTbl.clientWidth){
tbl.style.width = this.headTbl.clientWidth+'px';
}
this.initialized = true;
2014-11-22 15:01:29 +01:00
}
2014-11-23 11:31:55 +01:00
/**
* Removes the grid layout
*/
2014-11-22 15:01:29 +01:00
destroy(){
2016-01-04 07:59:30 +01:00
let tf = this.tf;
let tbl = tf.tbl;
2014-11-22 15:01:29 +01:00
if(!this.initialized){
2014-11-22 15:01:29 +01:00
return;
}
2016-01-04 07:59:30 +01:00
let t = Dom.remove(tbl);
2014-11-22 15:01:29 +01:00
this.tblMainCont.parentNode.insertBefore(t, this.tblMainCont);
2015-12-05 14:37:59 +01:00
Dom.remove(this.tblMainCont);
2014-11-22 15:01:29 +01:00
this.tblMainCont = null;
this.headTblCont = null;
this.headTbl = null;
this.tblCont = null;
tbl.outerHTML = this.sourceTblHtml;
2015-12-09 07:24:45 +01:00
//needed to keep reference of table element for future usage
this.tf.tbl = Dom.id(tf.id);
this.initialized = false;
2014-11-22 15:01:29 +01:00
}
}