1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-07 16:32:39 +02:00
TableFilter/src/extensions/filtersVisibility/filtersVisibility.js

308 lines
7.6 KiB
JavaScript
Raw Normal View History

import {Feature} from '../../feature';
2016-05-25 09:31:53 +02:00
import {createElm, removeElm, elm} from '../../dom';
2016-12-22 12:01:59 +01:00
import {isFn, isUndef, EMPTY_FN} from '../../types';
2016-06-02 06:13:56 +02:00
import {addEvt} from '../../event';
2015-06-06 12:06:15 +02:00
2016-09-04 10:17:54 +02:00
/**
* Filters Visibility extension
*/
export default class FiltersVisibility extends Feature {
2015-06-06 12:06:15 +02:00
/**
2016-09-04 10:17:54 +02:00
* Creates an instance of FiltersVisibility
* @param {TableFilter} tf TableFilter instance
* @param {Object} Configuration object
2015-06-06 12:06:15 +02:00
*/
2016-04-24 17:12:45 +02:00
constructor(tf, f) {
super(tf, f.name);
2015-06-06 12:06:15 +02:00
2016-09-04 10:17:54 +02:00
/**
* Module name
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.name = f.name;
2016-09-04 10:17:54 +02:00
/**
* Module description
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.desc = f.description || 'Filters row visibility manager';
2016-09-04 10:17:54 +02:00
/**
* Extension's stylesheet filename
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.stylesheet = f.stylesheet || 'filtersVisibility.css';
2016-09-04 10:17:54 +02:00
/**
* Expand icon filename
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.icnExpand = f.expand_icon_name || 'icn_exp.png';
2016-09-04 10:17:54 +02:00
/**
* Collapse icon filename
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.icnCollapse = f.collapse_icon_name || 'icn_clp.png';
2016-09-04 10:17:54 +02:00
/**
* Main container element
* @private
*/
2015-06-06 12:06:15 +02:00
this.contEl = null;
2016-09-04 10:17:54 +02:00
/**
* Button element
* @private
*/
2015-06-06 12:06:15 +02:00
this.btnEl = null;
2016-09-04 10:17:54 +02:00
/**
* Expand icon HTML
* @private
*/
2016-04-24 17:12:45 +02:00
this.icnExpandHtml = '<img src="' + tf.themesPath + this.icnExpand +
2015-06-06 12:06:15 +02:00
'" alt="Expand filters" >';
2016-09-04 10:17:54 +02:00
/**
* Collapse icon HTML
* @private
*/
2016-04-24 17:12:45 +02:00
this.icnCollapseHtml = '<img src="' + tf.themesPath + this.icnCollapse +
2015-06-06 12:06:15 +02:00
'" alt="Collapse filters" >';
2016-09-04 10:17:54 +02:00
/**
* Default text
* @private
*/
this.defaultText = 'Toggle filters';
2015-06-06 12:06:15 +02:00
2016-09-04 10:17:54 +02:00
/**
* ID of main container element
* @type {String}
*/
2016-02-16 08:41:47 +01:00
this.targetId = f.target_id || null;
2016-09-04 10:17:54 +02:00
/**
* Enable expand/collapse icon, defaults to true
* @type {Boolean}
*/
2016-04-24 17:12:45 +02:00
this.enableIcon = f.enable_icon === false ? false : true;
2016-09-04 10:17:54 +02:00
/**
* Custom text for button
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.btnText = f.btn_text || '';
2016-09-04 10:17:54 +02:00
/**
* Collapse button HTML
* @private
*/
2015-06-06 12:06:15 +02:00
this.collapseBtnHtml = this.enableIcon ?
this.icnCollapseHtml + this.btnText :
this.btnText || this.defaultText;
2016-09-04 10:17:54 +02:00
/**
* Expand button HTML
* @private
*/
2016-02-16 08:41:47 +01:00
this.expandBtnHtml = this.enableIcon ?
2015-06-06 12:06:15 +02:00
this.icnExpandHtml + this.btnText :
this.btnText || this.defaultText;
2016-09-04 10:17:54 +02:00
/**
* Button's custom HTML
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.btnHtml = f.btn_html || null;
2016-09-04 10:17:54 +02:00
/**
* Css class for expand/collapse filters button
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.btnCssClass = f.btn_css_class || 'btnExpClpFlt';
2016-09-04 10:17:54 +02:00
/**
* Css class for main container
* @type {String}
*/
2015-06-06 12:06:15 +02:00
this.contCssClass = f.cont_css_class || 'expClpFlt';
2016-09-04 10:17:54 +02:00
/**
* Filters row index
* @type {Number}
*/
2016-05-15 04:56:12 +02:00
this.filtersRowIndex = !isUndef(f.filters_row_index) ?
2016-04-24 17:12:45 +02:00
f.filters_row_index : tf.getFiltersRowIndex();
2015-06-06 12:06:15 +02:00
2016-09-04 10:17:54 +02:00
/**
* Make filters visible at initialization, defaults to true
* @type {Boolean}
*/
2016-05-15 04:56:12 +02:00
this.visibleAtStart = !isUndef(f.visible_at_start) ?
2015-06-06 12:06:15 +02:00
Boolean(f.visible_at_start) : true;
2016-09-04 10:17:54 +02:00
/**
* Callback fired before filters row is shown
* @type {Function}
*/
2016-12-22 12:01:59 +01:00
this.onBeforeShow = isFn(f.on_before_show) ?
f.on_before_show : EMPTY_FN;
2016-09-04 10:17:54 +02:00
/**
* Callback fired after filters row is shown
* @type {Function}
*/
2016-12-22 12:01:59 +01:00
this.onAfterShow = isFn(f.on_after_show) ? f.on_after_show : EMPTY_FN;
2016-09-04 10:17:54 +02:00
/**
* Callback fired before filters row is hidden
* @type {Function}
*/
2016-12-22 12:01:59 +01:00
this.onBeforeHide = isFn(f.on_before_hide) ?
f.on_before_hide : EMPTY_FN;
2016-09-04 10:17:54 +02:00
/**
* Callback fired after filters row is hidden
* @type {Function}
*/
2016-12-22 12:01:59 +01:00
this.onAfterHide = isFn(f.on_after_hide) ? f.on_after_hide : EMPTY_FN;
2015-06-06 12:06:15 +02:00
2016-09-04 10:17:54 +02:00
//Import extension's stylesheet
2016-04-24 17:12:45 +02:00
tf.import(f.name + 'Style', tf.stylePath + this.stylesheet, null,
'link');
2015-06-06 12:06:15 +02:00
this.enable();
2015-06-06 12:06:15 +02:00
}
2015-06-06 14:22:13 +02:00
/**
* Initialise extension
*/
2016-04-24 17:12:45 +02:00
init() {
if (this.initialized) {
2015-06-06 12:06:15 +02:00
return;
}
this.buildUI();
2016-09-04 10:17:54 +02:00
/**
* @inherited
*/
2015-06-06 12:06:15 +02:00
this.initialized = true;
2016-09-04 10:17:54 +02:00
this.emitter.on(['show-filters'], (tf, visible) => this.show(visible));
2016-04-28 06:05:24 +02:00
this.emitter.emit('filters-visibility-initialized', this.tf, this);
2015-06-06 12:06:15 +02:00
}
2015-06-06 14:22:13 +02:00
/**
* Build UI elements
*/
2016-04-24 17:12:45 +02:00
buildUI() {
2015-06-06 12:06:15 +02:00
let tf = this.tf;
let span = createElm('span');
2015-06-06 12:06:15 +02:00
span.className = this.contCssClass;
//Container element (rdiv or custom element)
2016-04-24 17:12:45 +02:00
if (!this.targetId) {
2015-06-06 12:06:15 +02:00
tf.setToolbar();
}
2016-05-25 09:31:53 +02:00
let targetEl = !this.targetId ? tf.rDiv : elm(this.targetId);
2015-06-06 12:06:15 +02:00
2016-04-24 17:12:45 +02:00
if (!this.targetId) {
2015-06-06 12:06:15 +02:00
let firstChild = targetEl.firstChild;
firstChild.parentNode.insertBefore(span, firstChild);
} else {
targetEl.appendChild(span);
}
let btn;
2016-04-24 17:12:45 +02:00
if (!this.btnHtml) {
2016-05-24 10:42:11 +02:00
btn = createElm('a', ['href', 'javascript:void(0);']);
2015-06-06 12:06:15 +02:00
btn.className = this.btnCssClass;
btn.title = this.btnText || this.defaultText;
btn.innerHTML = this.collapseBtnHtml;
span.appendChild(btn);
} else { //Custom html
span.innerHTML = this.btnHtml;
btn = span.firstChild;
}
2016-06-02 06:13:56 +02:00
addEvt(btn, 'click', () => this.toggle());
2015-06-06 12:06:15 +02:00
this.contEl = span;
this.btnEl = btn;
2016-04-24 17:12:45 +02:00
if (!this.visibleAtStart) {
2015-06-06 12:06:15 +02:00
this.toggle();
}
}
2015-06-06 14:22:13 +02:00
/**
* Toggle filters visibility
*/
toggle() {
2015-06-06 12:06:15 +02:00
let tf = this.tf;
2016-04-24 17:12:45 +02:00
let tbl = tf.gridLayout ? tf.feature('gridLayout').headTbl : tf.tbl;
2015-06-06 12:06:15 +02:00
let fltRow = tbl.rows[this.filtersRowIndex];
let isDisplayed = fltRow.style.display === '';
2015-06-06 12:06:15 +02:00
this.show(!isDisplayed);
}
/**
* Show or hide filters
*
* @param {boolean} [visible=true] Visibility flag
*/
show(visible = true) {
let tf = this.tf;
let tbl = tf.gridLayout ? tf.feature('gridLayout').headTbl : tf.tbl;
let fltRow = tbl.rows[this.filtersRowIndex];
2016-12-22 12:01:59 +01:00
if (visible) {
this.onBeforeShow(this);
2015-06-06 12:06:15 +02:00
}
2016-12-22 12:01:59 +01:00
if (!visible) {
this.onBeforeHide(this);
2015-06-06 12:06:15 +02:00
}
fltRow.style.display = visible ? '' : 'none';
2016-04-24 17:12:45 +02:00
if (this.enableIcon && !this.btnHtml) {
this.btnEl.innerHTML = visible ?
this.collapseBtnHtml : this.expandBtnHtml;
2015-06-06 12:06:15 +02:00
}
2016-12-22 12:01:59 +01:00
if (visible) {
this.onAfterShow(this);
2015-06-06 12:06:15 +02:00
}
2016-12-22 12:01:59 +01:00
if (!visible) {
this.onAfterHide(this);
2015-06-06 12:06:15 +02:00
}
this.emitter.emit('filters-toggled', tf, this, visible);
2015-06-06 12:06:15 +02:00
}
2015-06-06 14:22:13 +02:00
/**
* Destroy the UI
*/
2016-04-24 17:12:45 +02:00
destroy() {
if (!this.initialized) {
2015-06-06 12:06:15 +02:00
return;
}
this.emitter.off(['show-filters'], (tf, visible) => this.show(visible));
2016-04-24 17:12:45 +02:00
2015-06-06 12:06:15 +02:00
this.btnEl.innerHTML = '';
2016-05-24 10:42:11 +02:00
removeElm(this.btnEl);
2015-06-06 12:06:15 +02:00
this.btnEl = null;
this.contEl.innerHTML = '';
2016-05-24 10:42:11 +02:00
removeElm(this.contEl);
2015-06-06 12:06:15 +02:00
this.contEl = null;
this.initialized = false;
}
}