1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-01 21:42:20 +02:00
TableFilter/src/extensions/colsVisibility/colsVisibility.js

547 lines
18 KiB
JavaScript
Raw Normal View History

import {Feature} from '../../feature';
2016-05-25 09:31:53 +02:00
import {
addClass, removeClass, createCheckItem, createElm, elm, removeElm,
getText, tag
} from '../../dom';
2016-05-15 04:56:12 +02:00
import {isFn} from '../../types';
2016-06-02 06:13:56 +02:00
import {addEvt, targetEvt} from '../../event';
2015-04-12 10:32:24 +02:00
export default class ColsVisibility extends Feature {
2015-04-12 10:32:24 +02:00
/**
2015-04-12 12:24:32 +02:00
* Columns Visibility extension
2015-04-12 10:32:24 +02:00
* @param {Object} tf TableFilter instance
2016-04-17 11:59:52 +02:00
* @param {Object} f Extension's configuration
2015-04-12 10:32:24 +02:00
*/
2016-04-17 11:59:52 +02:00
constructor(tf, f) {
super(tf, f.name);
2015-04-20 09:20:28 +02:00
2015-04-12 10:32:24 +02:00
// Configuration object
2016-04-17 11:59:52 +02:00
let cfg = tf.config();
2015-04-12 10:32:24 +02:00
2015-04-24 12:38:20 +02:00
this.initialized = false;
2015-06-06 12:06:15 +02:00
this.name = f.name;
this.desc = f.description || 'Columns visibility manager';
2015-04-12 10:32:24 +02:00
//show/hide cols span element
2015-04-24 12:38:20 +02:00
this.spanEl = null;
2015-04-12 10:32:24 +02:00
//show/hide cols button element
2015-04-24 12:38:20 +02:00
this.btnEl = null;
2015-04-12 10:32:24 +02:00
//show/hide cols container div element
2015-04-24 12:38:20 +02:00
this.contEl = null;
2015-04-12 12:24:32 +02:00
//tick to hide or show column
2016-04-17 11:59:52 +02:00
this.tickToHide = f.tick_to_hide === false ? false : true;
2015-04-12 12:24:32 +02:00
//enables/disables cols manager generation
2016-04-17 11:59:52 +02:00
this.manager = f.manager === false ? false : true;
2015-04-12 12:24:32 +02:00
//only if external headers
2015-04-24 12:38:20 +02:00
this.headersTbl = f.headers_table || false;
2015-04-12 12:24:32 +02:00
//only if external headers
2015-04-24 12:38:20 +02:00
this.headersIndex = f.headers_index || 1;
2015-04-12 12:24:32 +02:00
//id of container element
2015-04-24 12:38:20 +02:00
this.contElTgtId = f.container_target_id || null;
2015-04-12 12:24:32 +02:00
//alternative headers text
2015-04-24 12:38:20 +02:00
this.headersText = f.headers_text || null;
2015-04-12 12:24:32 +02:00
//id of button container element
2015-04-24 12:38:20 +02:00
this.btnTgtId = f.btn_target_id || null;
2015-04-12 12:24:32 +02:00
//defines show/hide cols text
2015-04-24 12:38:20 +02:00
this.btnText = f.btn_text || 'Columns▼';
2015-07-06 10:08:37 +02:00
//defines show/hide cols button innerHtml
2015-04-24 12:38:20 +02:00
this.btnHtml = f.btn_html || null;
2015-04-12 12:24:32 +02:00
//defines css class for show/hide cols button
2015-04-24 12:38:20 +02:00
this.btnCssClass = f.btn_css_class || 'colVis';
2015-04-12 12:24:32 +02:00
//defines close link text
2015-04-24 12:38:20 +02:00
this.btnCloseText = f.btn_close_text || 'Close';
2015-04-12 12:24:32 +02:00
//defines close button innerHtml
2015-04-24 12:38:20 +02:00
this.btnCloseHtml = f.btn_close_html || null;
2015-04-12 12:24:32 +02:00
//defines css class for close button
2015-04-24 12:38:20 +02:00
this.btnCloseCssClass = f.btn_close_css_class || this.btnCssClass;
this.stylesheet = f.stylesheet || 'colsVisibility.css';
2015-04-15 09:09:20 +02:00
//span containing show/hide cols button
2015-04-24 12:38:20 +02:00
this.prfx = 'colVis_';
2015-04-15 09:09:20 +02:00
//defines css class span containing show/hide cols
2016-02-16 08:41:47 +01:00
this.spanCssClass = f.span_css_class || 'colVisSpan';
2015-04-24 12:38:20 +02:00
this.prfxCont = this.prfx + 'Cont_';
2015-04-15 09:09:20 +02:00
//defines css class div containing show/hide cols
2015-04-24 12:38:20 +02:00
this.contCssClass = f.cont_css_class || 'colVisCont';
2015-04-15 09:09:20 +02:00
//defines css class for cols list (ul)
2016-04-17 11:59:52 +02:00
this.listCssClass = cfg.list_css_class || 'cols_checklist';
2015-04-15 09:09:20 +02:00
//defines css class for list item (li)
2015-04-24 12:38:20 +02:00
this.listItemCssClass = cfg.checklist_item_css_class ||
2015-04-15 09:09:20 +02:00
'cols_checklist_item';
//defines css class for selected list item (li)
2015-04-24 12:38:20 +02:00
this.listSlcItemCssClass = cfg.checklist_selected_item_css_class ||
'cols_checklist_slc_item';
2015-04-15 09:09:20 +02:00
//text preceding columns list
2015-04-24 12:38:20 +02:00
this.text = f.text || (this.tickToHide ? 'Hide: ' : 'Show: ');
this.atStart = f.at_start || null;
this.enableHover = Boolean(f.enable_hover);
2015-04-15 09:09:20 +02:00
//enables select all option
2015-04-24 12:38:20 +02:00
this.enableTickAll = Boolean(f.enable_tick_all);
2015-04-15 09:09:20 +02:00
//text preceding columns list
2015-04-24 12:38:20 +02:00
this.tickAllText = f.tick_all_text || 'Select all:';
2015-04-15 09:09:20 +02:00
//array containing hidden columns indexes
2015-04-24 12:38:20 +02:00
this.hiddenCols = [];
2016-05-24 10:42:11 +02:00
this.tblHasColTag = tag(tf.tbl, 'col').length > 0;
2015-04-15 09:09:20 +02:00
//callback invoked just after cols manager is loaded
2016-05-15 04:56:12 +02:00
this.onLoaded = isFn(f.on_loaded) ? f.on_loaded : null;
2015-04-15 09:09:20 +02:00
//calls function before cols manager is opened
2016-05-15 04:56:12 +02:00
this.onBeforeOpen = isFn(f.on_before_open) ? f.on_before_open : null;
2015-04-15 09:09:20 +02:00
//calls function after cols manager is opened
2016-05-15 04:56:12 +02:00
this.onAfterOpen = isFn(f.on_after_open) ? f.on_after_open : null;
2015-04-15 09:09:20 +02:00
//calls function before cols manager is closed
2016-05-15 04:56:12 +02:00
this.onBeforeClose = isFn(f.on_before_close) ? f.on_before_close : null;
2015-04-15 09:09:20 +02:00
//calls function after cols manager is closed
2016-05-15 04:56:12 +02:00
this.onAfterClose = isFn(f.on_after_close) ? f.on_after_close : null;
2015-04-15 09:09:20 +02:00
2015-06-06 12:06:15 +02:00
//callback before col is hidden
2016-05-15 04:56:12 +02:00
this.onBeforeColHidden = isFn(f.on_before_col_hidden) ?
2015-04-24 12:38:20 +02:00
f.on_before_col_hidden : null;
2015-06-06 12:06:15 +02:00
//callback after col is hidden
2016-05-15 04:56:12 +02:00
this.onAfterColHidden = isFn(f.on_after_col_hidden) ?
2015-04-24 12:38:20 +02:00
f.on_after_col_hidden : null;
2015-06-06 12:06:15 +02:00
//callback before col is displayed
2016-05-15 04:56:12 +02:00
this.onBeforeColDisplayed = isFn(f.on_before_col_displayed) ?
2015-04-24 12:38:20 +02:00
f.on_before_col_displayed : null;
2015-06-06 12:06:15 +02:00
//callback after col is displayed
2016-05-15 04:56:12 +02:00
this.onAfterColDisplayed = isFn(f.on_after_col_displayed) ?
2015-04-24 12:38:20 +02:00
f.on_after_col_displayed : null;
2015-04-15 09:09:20 +02:00
//Grid layout compatibility
2016-04-17 11:59:52 +02:00
if (tf.gridLayout) {
this.headersTbl = tf.feature('gridLayout').headTbl; //headers table
2015-04-24 12:38:20 +02:00
this.headersIndex = 0; //headers index
2016-04-17 11:59:52 +02:00
this.onAfterColDisplayed = function () { };
this.onAfterColHidden = function () { };
2015-04-24 12:38:20 +02:00
}
2015-04-15 09:09:20 +02:00
//Loads extension stylesheet
2016-04-17 11:59:52 +02:00
tf.import(f.name + 'Style', tf.stylePath + this.stylesheet, null,
'link');
2015-04-15 09:09:20 +02:00
this.enable();
2015-04-17 07:01:06 +02:00
}
2016-04-17 11:59:52 +02:00
toggle() {
let contDisplay = this.contEl.style.display;
let onBeforeOpen = this.onBeforeOpen;
let onBeforeClose = this.onBeforeClose;
let onAfterOpen = this.onAfterOpen;
let onAfterClose = this.onAfterClose;
2016-04-17 11:59:52 +02:00
if (onBeforeOpen && contDisplay !== 'inline') {
2015-04-24 12:38:20 +02:00
onBeforeOpen.call(null, this);
}
2016-04-17 11:59:52 +02:00
if (onBeforeClose && contDisplay === 'inline') {
2015-04-24 12:38:20 +02:00
onBeforeClose.call(null, this);
}
2015-04-24 12:38:20 +02:00
this.contEl.style.display = contDisplay === 'inline' ?
'none' : 'inline';
2015-04-17 07:01:06 +02:00
2016-04-17 11:59:52 +02:00
if (onAfterOpen && contDisplay !== 'inline') {
2015-04-24 12:38:20 +02:00
onAfterOpen.call(null, this);
}
2016-04-17 11:59:52 +02:00
if (onAfterClose && contDisplay === 'inline') {
2015-04-24 12:38:20 +02:00
onAfterClose.call(null, this);
}
}
2016-04-17 11:59:52 +02:00
checkItem(lbl) {
let li = lbl.parentNode;
if (!li || !lbl) {
return;
}
2016-04-17 11:59:52 +02:00
let isChecked = lbl.firstChild.checked;
let colIndex = lbl.firstChild.getAttribute('id').split('_')[1];
2015-04-24 12:38:20 +02:00
colIndex = parseInt(colIndex, 10);
2016-04-17 11:59:52 +02:00
if (isChecked) {
2016-05-24 10:42:11 +02:00
addClass(li, this.listSlcItemCssClass);
} else {
2016-05-24 10:42:11 +02:00
removeClass(li, this.listSlcItemCssClass);
}
2015-04-24 12:38:20 +02:00
2016-04-17 11:59:52 +02:00
let hide = false;
if ((this.tickToHide && isChecked) ||
(!this.tickToHide && !isChecked)) {
hide = true;
}
this.setHidden(colIndex, hide);
2015-04-15 09:09:20 +02:00
}
2016-04-17 11:59:52 +02:00
init() {
if (this.initialized || !this.manager) {
2015-04-20 09:20:28 +02:00
return;
2015-04-17 07:01:06 +02:00
}
2016-04-20 16:03:35 +02:00
this.emitter.on(['hide-column'],
(tf, colIndex) => this.hideCol(colIndex));
2015-04-20 09:20:28 +02:00
this.buildBtn();
this.buildManager();
2015-04-24 12:38:20 +02:00
this.initialized = true;
2016-04-28 06:05:24 +02:00
this.emitter.emit('columns-visibility-initialized', this.tf, this);
// Hide columns at start at very end of initialization
2016-04-20 16:03:35 +02:00
this._hideAtStart();
2015-04-17 07:01:06 +02:00
}
2015-04-24 12:38:20 +02:00
/**
* Build main button UI
*/
2016-04-17 11:59:52 +02:00
buildBtn() {
if (this.btnEl) {
2015-04-17 07:01:06 +02:00
return;
}
2016-04-17 11:59:52 +02:00
let tf = this.tf;
2016-05-24 10:42:11 +02:00
let span = createElm('span', ['id', this.prfx + tf.id]);
2015-04-24 12:38:20 +02:00
span.className = this.spanCssClass;
2015-04-17 07:01:06 +02:00
//Container element (rdiv or custom element)
2016-04-17 11:59:52 +02:00
if (!this.btnTgtId) {
tf.setToolbar();
2015-04-17 07:01:06 +02:00
}
2016-05-25 09:31:53 +02:00
let targetEl = !this.btnTgtId ? tf.rDiv : elm(this.btnTgtId);
2015-04-15 09:09:20 +02:00
2016-04-17 11:59:52 +02:00
if (!this.btnTgtId) {
let firstChild = targetEl.firstChild;
2015-04-24 12:38:20 +02:00
firstChild.parentNode.insertBefore(span, firstChild);
2015-04-17 07:01:06 +02:00
} else {
2015-04-24 12:38:20 +02:00
targetEl.appendChild(span);
2015-04-17 07:01:06 +02:00
}
2016-04-17 11:59:52 +02:00
if (!this.btnHtml) {
2016-05-24 10:42:11 +02:00
let btn = createElm('a', ['href', 'javascript:;']);
2015-04-24 12:38:20 +02:00
btn.className = this.btnCssClass;
2015-06-06 12:06:15 +02:00
btn.title = this.desc;
2015-04-17 07:01:06 +02:00
2015-04-24 12:38:20 +02:00
btn.innerHTML = this.btnText;
span.appendChild(btn);
2016-04-17 11:59:52 +02:00
if (!this.enableHover) {
2016-06-02 06:13:56 +02:00
addEvt(btn, 'click', (evt) => this.toggle(evt));
2015-04-17 07:01:06 +02:00
} else {
2016-06-02 06:13:56 +02:00
addEvt(btn, 'mouseover', (evt) => this.toggle(evt));
2015-04-17 07:01:06 +02:00
}
} else { //Custom html
2015-04-24 12:38:20 +02:00
span.innerHTML = this.btnHtml;
2016-04-17 11:59:52 +02:00
let colVisEl = span.firstChild;
if (!this.enableHover) {
2016-06-02 06:13:56 +02:00
addEvt(colVisEl, 'click', (evt) => this.toggle(evt));
2015-04-17 07:01:06 +02:00
} else {
2016-06-02 06:13:56 +02:00
addEvt(colVisEl, 'mouseover', (evt) => this.toggle(evt));
2015-04-17 07:01:06 +02:00
}
}
2015-04-24 12:38:20 +02:00
this.spanEl = span;
this.btnEl = this.spanEl.firstChild;
2015-04-17 07:01:06 +02:00
2016-04-17 11:59:52 +02:00
if (this.onLoaded) {
2015-04-24 12:38:20 +02:00
this.onLoaded.call(null, this);
2015-04-17 07:01:06 +02:00
}
2015-04-12 10:32:24 +02:00
}
2015-04-17 07:01:06 +02:00
2015-04-24 12:38:20 +02:00
/**
* Build columns manager UI
*/
2016-04-17 11:59:52 +02:00
buildManager() {
let tf = this.tf;
2015-04-17 07:01:06 +02:00
2016-04-17 11:59:52 +02:00
let container = !this.contElTgtId ?
2016-05-24 10:42:11 +02:00
createElm('div', ['id', this.prfxCont + tf.id]) :
2016-05-25 09:31:53 +02:00
elm(this.contElTgtId);
2015-04-24 12:38:20 +02:00
container.className = this.contCssClass;
2015-04-17 07:01:06 +02:00
//Extension description
2016-05-24 10:42:11 +02:00
let extNameLabel = createElm('p');
2015-04-24 12:38:20 +02:00
extNameLabel.innerHTML = this.text;
2015-04-17 07:01:06 +02:00
container.appendChild(extNameLabel);
//Headers list
2016-05-24 10:42:11 +02:00
let ul = createElm('ul', ['id', 'ul' + this.name + '_' + tf.id]);
2015-04-24 12:38:20 +02:00
ul.className = this.listCssClass;
2015-04-17 07:01:06 +02:00
2016-04-17 11:59:52 +02:00
let tbl = this.headersTbl ? this.headersTbl : tf.tbl;
let headerIndex = this.headersTbl ?
2015-04-24 12:38:20 +02:00
this.headersIndex : tf.getHeadersRowIndex();
2016-04-17 11:59:52 +02:00
let headerRow = tbl.rows[headerIndex];
2015-04-17 07:01:06 +02:00
//Tick all option
2016-04-17 11:59:52 +02:00
if (this.enableTickAll) {
2016-05-24 10:42:11 +02:00
let li = createCheckItem('col__' + tf.id, this.tickAllText,
this.tickAllText);
addClass(li, this.listItemCssClass);
2015-04-17 07:01:06 +02:00
ul.appendChild(li);
2015-04-24 12:38:20 +02:00
li.check.checked = !this.tickToHide;
2015-04-17 08:32:42 +02:00
2016-06-02 06:13:56 +02:00
addEvt(li.check, 'click', () => {
2016-04-17 11:59:52 +02:00
for (let h = 0; h < headerRow.cells.length; h++) {
2016-05-25 09:31:53 +02:00
let itm = elm('col_' + h + '_' + tf.id);
2016-04-17 11:59:52 +02:00
if (itm && li.check.checked !== itm.checked) {
2015-04-17 08:32:42 +02:00
itm.click();
2015-04-24 12:38:20 +02:00
itm.checked = li.check.checked;
2015-04-17 08:32:42 +02:00
}
2015-04-17 07:01:06 +02:00
}
2015-04-17 08:32:42 +02:00
});
2015-04-17 07:01:06 +02:00
}
2016-04-17 11:59:52 +02:00
for (let i = 0; i < headerRow.cells.length; i++) {
let cell = headerRow.cells[i];
let cellText = this.headersText && this.headersText[i] ?
2015-04-24 12:38:20 +02:00
this.headersText[i] : this._getHeaderText(cell);
2016-05-24 10:42:11 +02:00
let liElm = createCheckItem('col_' + i + '_' + tf.id, cellText,
cellText);
addClass(liElm, this.listItemCssClass);
2016-04-17 11:59:52 +02:00
if (!this.tickToHide) {
2016-05-24 10:42:11 +02:00
addClass(liElm, this.listSlcItemCssClass);
2015-04-17 08:32:42 +02:00
}
ul.appendChild(liElm);
2016-04-17 11:59:52 +02:00
if (!this.tickToHide) {
2015-04-17 08:32:42 +02:00
liElm.check.checked = true;
2015-04-17 07:01:06 +02:00
}
2015-04-24 12:38:20 +02:00
2016-06-02 06:13:56 +02:00
addEvt(liElm.check, 'click', (evt) => {
let elm = targetEvt(evt);
2016-04-17 11:59:52 +02:00
let lbl = elm.parentNode;
2015-04-20 09:20:28 +02:00
this.checkItem(lbl);
2015-04-17 08:32:42 +02:00
});
2015-04-17 07:01:06 +02:00
}
//separator
2016-05-24 10:42:11 +02:00
let p = createElm('p', ['align', 'center']);
2016-04-17 11:59:52 +02:00
let btn;
2015-04-17 07:01:06 +02:00
//Close link
2016-04-17 11:59:52 +02:00
if (!this.btnCloseHtml) {
2016-05-24 10:42:11 +02:00
btn = createElm('a', ['href', 'javascript:;']);
2015-04-24 12:38:20 +02:00
btn.className = this.btnCloseCssClass;
btn.innerHTML = this.btnCloseText;
2016-06-02 06:13:56 +02:00
addEvt(btn, 'click', (evt) => this.toggle(evt));
2015-04-17 07:01:06 +02:00
p.appendChild(btn);
} else {
2015-04-24 12:38:20 +02:00
p.innerHTML = this.btnCloseHtml;
2015-04-17 08:32:42 +02:00
btn = p.firstChild;
2016-06-02 06:13:56 +02:00
addEvt(btn, 'click', (evt) => this.toggle(evt));
2015-04-17 07:01:06 +02:00
}
container.appendChild(ul);
container.appendChild(p);
2015-04-24 12:38:20 +02:00
this.btnEl.parentNode.insertBefore(container, this.btnEl);
this.contEl = container;
2015-04-17 08:32:42 +02:00
}
2015-04-24 12:38:20 +02:00
/**
* Hide or show specified columns
* @param {Numner} colIndex Column index
2016-04-17 11:59:52 +02:00
* @param {Boolean} hide Hide column if true or show if false
2015-04-24 12:38:20 +02:00
*/
setHidden(colIndex, hide) {
2016-04-17 11:59:52 +02:00
let tf = this.tf;
let tbl = tf.tbl;
2015-04-24 12:38:20 +02:00
2016-04-17 11:59:52 +02:00
if (this.onBeforeColHidden && hide) {
2015-04-24 12:38:20 +02:00
this.onBeforeColHidden.call(null, this, colIndex);
}
2016-04-17 11:59:52 +02:00
if (this.onBeforeColDisplayed && !hide) {
2015-04-24 12:38:20 +02:00
this.onBeforeColDisplayed.call(null, this, colIndex);
}
this._hideCells(tbl, colIndex, hide);
2016-04-17 11:59:52 +02:00
if (this.headersTbl) {
2015-04-24 12:38:20 +02:00
this._hideCells(this.headersTbl, colIndex, hide);
}
2016-04-17 11:59:52 +02:00
let hiddenCols = this.hiddenCols;
let itemIndex = hiddenCols.indexOf(colIndex);
if (hide) {
if (itemIndex === -1) {
2015-04-24 12:38:20 +02:00
this.hiddenCols.push(colIndex);
}
} else {
2016-04-17 11:59:52 +02:00
if (itemIndex !== -1) {
2015-04-24 12:38:20 +02:00
this.hiddenCols.splice(itemIndex, 1);
}
}
2016-04-17 11:59:52 +02:00
let gridLayout;
let headTbl;
let gridColElms;
if (hide) {
//This event is fired just after a column is displayed for
2015-07-04 14:08:58 +02:00
//grid_layout support
//TODO: grid layout module should be responsible for those
//calculations
2016-04-17 11:59:52 +02:00
if (tf.gridLayout) {
gridLayout = tf.feature('gridLayout');
headTbl = gridLayout.headTbl;
2016-06-05 10:44:21 +02:00
gridColElms = gridLayout.colElms;
2016-04-17 11:59:52 +02:00
let hiddenWidth = parseInt(
2015-07-04 14:08:58 +02:00
gridColElms[colIndex].style.width, 10);
2016-04-17 11:59:52 +02:00
let headTblW = parseInt(headTbl.style.width, 10);
2015-07-04 14:08:58 +02:00
headTbl.style.width = headTblW - hiddenWidth + 'px';
tbl.style.width = headTbl.style.width;
}
if (this.onAfterColHidden) {
this.onAfterColHidden.call(null, this, colIndex);
}
this.emitter.emit('column-hidden', tf, this, colIndex,
this.hiddenCols);
}
if (!hide) {
//This event is fired just after a column is displayed for
2015-07-04 14:08:58 +02:00
//grid_layout support
//TODO: grid layout module should be responsible for those
//calculations
2016-04-17 11:59:52 +02:00
if (tf.gridLayout) {
gridLayout = tf.feature('gridLayout');
headTbl = gridLayout.headTbl;
2016-06-05 10:44:21 +02:00
gridColElms = gridLayout.colElms;
2016-04-17 11:59:52 +02:00
let width = parseInt(gridColElms[colIndex].style.width, 10);
headTbl.style.width =
(parseInt(headTbl.style.width, 10) + width) + 'px';
tf.tbl.style.width = headTbl.style.width;
}
if (this.onAfterColDisplayed) {
this.onAfterColDisplayed.call(null, this, colIndex);
}
this.emitter.emit('column-shown', tf, this, colIndex,
this.hiddenCols);
}
}
2015-04-24 12:38:20 +02:00
/**
* Show specified column
* @param {Number} colIndex Column index
*/
2016-04-17 11:59:52 +02:00
showCol(colIndex) {
if (colIndex === undefined || !this.isColHidden(colIndex)) {
return;
}
2016-04-17 11:59:52 +02:00
if (this.manager && this.contEl) {
2016-05-25 09:31:53 +02:00
let itm = elm('col_' + colIndex + '_' + this.tf.id);
2016-04-17 11:59:52 +02:00
if (itm) {
itm.click();
}
} else {
this.setHidden(colIndex, false);
}
}
2015-04-24 12:38:20 +02:00
/**
* Hide specified column
* @param {Number} colIndex Column index
*/
hideCol(colIndex) {
2016-04-17 11:59:52 +02:00
if (colIndex === undefined || this.isColHidden(colIndex)) {
return;
}
2016-04-17 11:59:52 +02:00
if (this.manager && this.contEl) {
2016-05-25 09:31:53 +02:00
let itm = elm('col_' + colIndex + '_' + this.tf.id);
2016-04-17 11:59:52 +02:00
if (itm) {
itm.click();
}
} else {
this.setHidden(colIndex, true);
}
}
2015-04-24 12:38:20 +02:00
/**
* Determine if specified column is hidden
* @param {Number} colIndex Column index
*/
2016-04-17 11:59:52 +02:00
isColHidden(colIndex) {
if (this.hiddenCols.indexOf(colIndex) !== -1) {
return true;
}
return false;
}
2015-04-24 12:38:20 +02:00
/**
* Toggle visibility of specified column
* @param {Number} colIndex Column index
*/
2016-04-17 11:59:52 +02:00
toggleCol(colIndex) {
if (colIndex === undefined || this.isColHidden(colIndex)) {
2015-04-24 12:38:20 +02:00
this.showCol(colIndex);
} else {
2015-04-24 12:38:20 +02:00
this.hideCol(colIndex);
}
}
2015-04-24 12:38:20 +02:00
/**
* Return the indexes of the columns currently hidden
2015-04-24 12:38:20 +02:00
* @return {Array} column indexes
*/
2016-04-17 11:59:52 +02:00
getHiddenCols() {
2015-04-24 12:38:20 +02:00
return this.hiddenCols;
}
2015-04-24 12:38:20 +02:00
/**
* Remove the columns manager
*/
2016-04-17 11:59:52 +02:00
destroy() {
if (!this.initialized) {
return;
}
2016-05-25 09:31:53 +02:00
if (elm(this.contElTgtId)) {
elm(this.contElTgtId).innerHTML = '';
} else {
2015-04-24 12:38:20 +02:00
this.contEl.innerHTML = '';
2016-05-24 10:42:11 +02:00
removeElm(this.contEl);
2015-04-24 12:38:20 +02:00
this.contEl = null;
}
this.btnEl.innerHTML = '';
2016-05-24 10:42:11 +02:00
removeElm(this.btnEl);
2015-04-24 12:38:20 +02:00
this.btnEl = null;
this.emitter.off(['hide-column'],
(tf, colIndex) => this.hideCol(colIndex));
2015-04-24 12:38:20 +02:00
this.initialized = false;
}
2015-04-17 08:32:42 +02:00
2016-04-17 11:59:52 +02:00
_getHeaderText(cell) {
if (!cell.hasChildNodes) {
2015-04-17 08:32:42 +02:00
return '';
}
2016-04-17 11:59:52 +02:00
for (let i = 0; i < cell.childNodes.length; i++) {
let n = cell.childNodes[i];
if (n.nodeType === 3) {
2015-04-17 08:32:42 +02:00
return n.nodeValue;
2016-04-17 11:59:52 +02:00
} else if (n.nodeType === 1) {
if (n.id && n.id.indexOf('popUp') !== -1) {
2015-04-17 08:32:42 +02:00
continue;
} else {
2016-05-24 10:42:11 +02:00
return getText(n);
2015-04-17 08:32:42 +02:00
}
2015-04-17 07:01:06 +02:00
}
2015-04-17 08:32:42 +02:00
continue;
2015-04-17 07:01:06 +02:00
}
2015-04-17 08:32:42 +02:00
return '';
2015-04-17 07:01:06 +02:00
}
2016-04-17 11:59:52 +02:00
_hideCells(tbl, colIndex, hide) {
for (let i = 0; i < tbl.rows.length; i++) {
let row = tbl.rows[i];
let cell = row.cells[colIndex];
if (cell) {
cell.style.display = hide ? 'none' : '';
}
}
}
2016-04-20 16:03:35 +02:00
_hideAtStart() {
if (!this.atStart) {
return;
}
this.atStart.forEach((colIdx) => {
this.hideCol(colIdx);
});
}
2015-04-12 10:32:24 +02:00
}