1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-08 00:42:28 +02:00
TableFilter/src/modules/gridLayout.js

351 lines
12 KiB
JavaScript
Raw Normal View History

import {Feature} from '../feature';
2016-05-25 09:31:53 +02:00
import {createElm, removeElm, elm, tag} from '../dom';
2016-05-15 05:33:16 +02:00
import {isFn, isNull, isUndef} from '../types';
2015-05-30 14:23:33 +02:00
import Event from '../event';
2016-05-20 10:08:39 +02:00
import {contains} from '../string';
import {NONE} from '../const';
2014-11-22 15:01:29 +01:00
2016-05-15 05:33:16 +02: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
*/
2016-05-15 05:33:16 +02:00
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
2016-05-15 05:33:16 +02:00
this.gridEnableFilters = !isUndef(f.grid_enable_default_filters) ?
2014-11-22 15:01:29 +01:00
f.grid_enable_default_filters : true;
2016-03-06 11:24:29 +01:00
this.noHeaders = Boolean(f.grid_no_headers);
2014-11-22 15:01:29 +01:00
//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
*/
2016-05-15 05:33:16 +02: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
2016-05-15 05:33:16 +02: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
2016-05-15 04:56:12 +02:00
tf.refRow = isNull(tf.startRow) ? 0 : tf.startRow;
2016-01-04 07:59:30 +01:00
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
2016-05-15 05:33:16 +02:00
if (!tf.hasColWidths) {
2015-05-28 15:44:23 +02:00
tf.colWidths = [];
2016-05-15 05:33:16 +02:00
for (let k = 0; k < tf.nbCells; k++) {
2016-01-04 07:59:30 +01:00
let colW,
2014-11-22 15:01:29 +01:00
cell = tbl.rows[this.gridHeadRowIndex].cells[k];
2016-05-15 05:33:16 +02:00
if (cell.width !== '') {
2014-11-22 15:01:29 +01:00
colW = cell.width;
2016-05-15 05:33:16 +02: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
2016-05-15 05:33:16 +02:00
if (tbl.width !== '') {
2014-11-22 15:01:29 +01:00
tblW = tbl.width;
}
2016-05-15 05:33:16 +02: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
2016-05-24 10:42:11 +02:00
this.tblMainCont = createElm('div',
['id', this.prfxMainTblCont + tf.id]);
2014-11-22 15:01:29 +01:00
this.tblMainCont.className = this.gridMainContCssClass;
2016-05-15 05:33:16 +02:00
if (this.gridWidth) {
2014-11-22 15:01:29 +01:00
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
2016-05-24 10:42:11 +02:00
this.tblCont = createElm('div', ['id', this.prfxTblCont + tf.id]);
2014-11-22 15:01:29 +01:00
this.tblCont.className = this.gridContCssClass;
2016-05-15 05:33:16 +02:00
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
}
2016-05-15 05:33:16 +02:00
if (this.gridHeight) {
2014-11-22 15:01:29 +01:00
this.tblCont.style.height = this.gridHeight;
}
2014-11-23 04:34:57 +01:00
tbl.parentNode.insertBefore(this.tblCont, tbl);
2016-05-24 10:42:11 +02:00
let t = removeElm(tbl);
2014-11-22 15:01:29 +01:00
this.tblCont.appendChild(t);
//In case table width is expressed in %
2016-05-15 05:33:16 +02:00
if (tbl.style.width === '') {
2016-05-20 10:08:39 +02:00
tbl.style.width = (contains('%', tblW) ?
2014-11-22 15:01:29 +01:00
tbl.clientWidth : tblW) + 'px';
}
2016-05-24 10:42:11 +02:00
let d = removeElm(this.tblCont);
2014-11-22 15:01:29 +01:00
this.tblMainCont.appendChild(d);
//Headers table container: div wrapping headers table
2016-05-24 10:42:11 +02:00
this.headTblCont = createElm(
2016-05-15 05:33:16 +02:00
'div', ['id', this.prfxHeadTblCont + tf.id]);
2014-11-22 15:01:29 +01:00
this.headTblCont.className = this.gridHeadContCssClass;
2016-05-15 05:33:16 +02:00
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
2016-05-24 10:42:11 +02:00
this.headTbl = createElm('table', ['id', this.prfxHeadTbl + tf.id]);
let tH = createElm('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 = [];
2016-05-15 05:33:16 +02:00
for (let n = 0; n < tf.nbCells; n++) {
2016-01-04 07:59:30 +01:00
let c = hRow.cells[n];
let thId = c.getAttribute('id');
2016-05-15 05:33:16 +02: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-05-24 10:42:11 +02:00
let filtersRow = createElm('tr');
2016-05-15 05:33:16 +02:00
if (this.gridEnableFilters && tf.fltGrid) {
2014-11-22 15:01:29 +01:00
tf.externalFltTgtIds = [];
2016-05-15 05:33:16 +02:00
for (let j = 0; j < tf.nbCells; j++) {
let fltTdId = tf.prfxFlt + j + this.prfxGridFltTd + tf.id;
2016-05-24 10:42:11 +02:00
let cl = createElm(tf.fltCellTag, ['id', fltTdId]);
2014-11-22 15:01:29 +01:00
filtersRow.appendChild(cl);
tf.externalFltTgtIds[j] = fltTdId;
}
}
2016-03-06 11:24:29 +01:00
2014-11-22 15:01:29 +01:00
//Headers row are moved from content table to headers table
2016-05-15 05:33:16 +02:00
if (!this.noHeaders) {
for (let i = 0; i < this.gridHeadRows.length; i++) {
2016-03-06 11:24:29 +01:00
let headRow = tbl.rows[this.gridHeadRows[0]];
tH.appendChild(headRow);
}
} else {
// Handle table with no headers, assuming here headers do not
// exist
2016-05-24 10:42:11 +02:00
tH.appendChild(createElm('tr'));
2014-11-22 15:01:29 +01:00
}
2016-03-06 11:24:29 +01:00
2014-11-22 15:01:29 +01:00
this.headTbl.appendChild(tH);
2016-05-15 05:33:16 +02:00
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-05-24 10:42:11 +02:00
let thead = tag(tbl, 'thead');
2016-05-15 05:33:16 +02:00
if (thead.length > 0) {
2014-11-22 15:01:29 +01:00
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
2016-05-15 05:33:16 +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-05-15 05:33:16 +02:00
let sort = (f.extensions || []).filter(function (itm) {
return itm.name === 'sort';
});
2016-05-15 05:33:16 +02:00
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
2016-05-24 10:42:11 +02:00
this.tblHasColTag = 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-05-15 05:33:16 +02:00
let createColTags = function () {
for (let k = (tf.nbCells - 1); k >= 0; k--) {
2016-05-24 10:42:11 +02:00
let col = createElm('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
2016-05-15 05:33:16 +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-05-24 10:42:11 +02:00
let cols = tag(tbl, 'col');
2016-05-15 05:33:16 +02:00
for (let ii = 0; ii < tf.nbCells; ii++) {
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-05-15 04:56:12 +02:00
let afterColResizedFn = isFn(f.on_after_col_resized) ?
2014-11-22 15:01:29 +01:00
f.on_after_col_resized : null;
2016-05-15 05:33:16 +02:00
f.on_after_col_resized = function (o, colIndex) {
if (!colIndex) {
2014-11-22 15:01:29 +01:00
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
2016-05-15 05:33:16 +02:00
if (thCW !== tdCW) {
o.headTbl.style.width = tbl.clientWidth + 'px';
2014-11-22 15:01:29 +01:00
}
2016-05-15 05:33:16 +02:00
if (afterColResizedFn) {
2015-12-05 14:37:59 +01:00
afterColResizedFn.call(null, o, colIndex);
2014-11-22 15:01:29 +01:00
}
};
2016-05-15 05:33:16 +02:00
if (tf.popupFilters) {
filtersRow.style.display = NONE;
}
2016-05-15 05:33:16 +02:00
if (tbl.clientWidth !== this.headTbl.clientWidth) {
tbl.style.width = this.headTbl.clientWidth + 'px';
2014-11-22 15:01:29 +01:00
}
this.initialized = true;
2014-11-22 15:01:29 +01:00
}
2014-11-23 11:31:55 +01:00
/**
* Removes the grid layout
*/
2016-05-15 05:33:16 +02: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
2016-05-15 05:33:16 +02:00
if (!this.initialized) {
2014-11-22 15:01:29 +01:00
return;
}
2016-05-24 10:42:11 +02:00
let t = removeElm(tbl);
2014-11-22 15:01:29 +01:00
this.tblMainCont.parentNode.insertBefore(t, this.tblMainCont);
2016-05-24 10:42:11 +02:00
removeElm(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
2016-05-25 09:31:53 +02:00
this.tf.tbl = elm(tf.id);
this.initialized = false;
2014-11-22 15:01:29 +01:00
}
}