1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-23 08:42:18 +02:00
TableFilter/src/modules/clearButton.js

125 lines
3 KiB
JavaScript
Raw Normal View History

import {Feature} from '../feature';
2016-05-25 09:31:53 +02:00
import {createElm, createText, elm, removeElm} from '../dom';
2016-06-02 06:13:56 +02:00
import {addEvt} from '../event';
2015-02-19 06:28:50 +01:00
2016-07-02 06:06:12 +02:00
/**
* Clear button UI component
*/
2016-05-24 10:42:11 +02:00
export class ClearButton extends Feature {
2015-02-19 06:28:50 +01:00
/**
2016-07-02 06:06:12 +02:00
* Creates an instance of ClearButton
* @param {TableFilter} tf TableFilter instance
2015-02-19 06:28:50 +01:00
*/
2016-05-24 10:42:11 +02:00
constructor(tf) {
super(tf, 'btnReset');
2016-05-24 10:42:11 +02:00
let f = this.config;
2015-02-19 06:28:50 +01:00
2016-07-02 06:06:12 +02:00
/**
* Container element ID
* @type {String}
*/
this.targetId = f.btn_reset_target_id || null;
/**
* Clear button element
* @type {DOMElement}
* @private
*/
this.element = null;
/**
* Text for the clear button
* @type {String}
*/
this.text = f.btn_reset_text || 'Reset';
/**
* Tooltip text for the clear button
* @type {String}
*/
this.tooltip = f.btn_reset_tooltip || 'Clear filters';
/**
* Custom Html string for the clear button
* @type {String}
*/
this.html = f.btn_reset_html ||
2015-02-19 06:28:50 +01:00
(!tf.enableIcons ? null :
2016-05-24 10:42:11 +02:00
'<input type="button" value="" class="' + tf.btnResetCssClass +
2016-07-02 06:06:12 +02:00
'" ' + 'title="' + this.tooltip + '" />');
/**
* Prefix fot ID of container element
* @type {String}
* @private
*/
this.prfxCont = 'resetspan_';
2015-02-19 06:28:50 +01:00
}
2016-07-02 06:06:12 +02:00
/**
* Click event handler for clear button
* @private
*/
2016-05-24 10:42:11 +02:00
onClick() {
if (!this.isEnabled()) {
return;
}
this.tf.clearFilters();
2015-02-19 06:28:50 +01:00
}
/**
2016-07-02 06:06:12 +02:00
* Initialize clear button component
2015-02-19 06:28:50 +01:00
*/
2016-05-24 10:42:11 +02:00
init() {
let tf = this.tf;
2015-02-19 06:28:50 +01:00
2016-05-24 10:42:11 +02:00
if (this.initialized) {
2015-02-19 06:28:50 +01:00
return;
}
2016-07-02 06:06:12 +02:00
let resetspan = createElm('span', ['id', this.prfxCont + tf.id]);
2015-02-19 06:28:50 +01:00
// reset button is added to defined element
2016-07-02 06:06:12 +02:00
if (!this.targetId) {
tf.setToolbar();
2015-02-19 06:28:50 +01:00
}
2016-07-02 06:06:12 +02:00
let targetEl = !this.targetId ? tf.rDiv : elm(this.targetId);
2015-02-19 06:28:50 +01:00
targetEl.appendChild(resetspan);
2016-07-02 06:06:12 +02:00
if (!this.html) {
let fltreset = createElm('a', ['href', 'javascript:void(0);']);
2015-02-19 06:28:50 +01:00
fltreset.className = tf.btnResetCssClass;
2016-07-02 06:06:12 +02:00
fltreset.appendChild(createText(this.text));
2015-02-19 06:28:50 +01:00
resetspan.appendChild(fltreset);
2016-06-02 06:13:56 +02:00
addEvt(fltreset, 'click', () => this.onClick());
2015-02-19 06:28:50 +01:00
} else {
2016-07-02 06:06:12 +02:00
resetspan.innerHTML = this.html;
2016-05-24 10:42:11 +02:00
let resetEl = resetspan.firstChild;
2016-06-02 06:13:56 +02:00
addEvt(resetEl, 'click', () => this.onClick());
2015-02-19 06:28:50 +01:00
}
2016-07-02 06:06:12 +02:00
this.element = resetspan.firstChild;
this.initialized = true;
2015-02-19 06:28:50 +01:00
}
/**
2016-07-02 06:06:12 +02:00
* Destroy ClearButton instance
2015-02-19 06:28:50 +01:00
*/
2016-05-24 10:42:11 +02:00
destroy() {
let tf = this.tf;
2015-02-19 06:28:50 +01:00
2016-05-24 10:42:11 +02:00
if (!this.initialized) {
2015-02-19 06:28:50 +01:00
return;
}
2016-07-02 06:06:12 +02:00
let resetspan = elm(this.prfxCont + tf.id);
2016-05-24 10:42:11 +02:00
if (resetspan) {
removeElm(resetspan);
2015-02-19 06:28:50 +01:00
}
2016-07-02 06:06:12 +02:00
this.element = null;
this.initialized = false;
2015-02-19 06:28:50 +01:00
}
}