UNPKG

3.77 kBJavaScriptView Raw
1import {Feature} from '../feature';
2import {createElm, createText, elm, removeElm} from '../dom';
3import {addEvt} from '../event';
4import {defaultsStr} from '../settings';
5import {isNull} from '../types';
6import {RIGHT} from './toolbar';
7
8/**
9 * Clear button UI component
10 */
11export class ClearButton extends Feature {
12
13 /**
14 * Creates an instance of ClearButton
15 * @param {TableFilter} tf TableFilter instance
16 */
17 constructor(tf) {
18 super(tf, ClearButton);
19
20 let f = this.config.btn_reset || {};
21
22 /**
23 * Container element ID
24 * @type {String}
25 */
26 this.targetId = defaultsStr(f.target_id, null);
27
28 /**
29 * Text for the clear button
30 * @type {String}
31 */
32 this.text = defaultsStr(f.text, null);
33
34 /**
35 * Css class for reset button
36 * @type {String}
37 */
38 this.cssClass = defaultsStr(f.css_class, 'reset');
39
40 /**
41 * Tooltip text for the clear button
42 * @type {String}
43 */
44 this.tooltip = f.tooltip || 'Clear filters';
45
46 /**
47 * Custom Html string for the clear button
48 * @type {String}
49 */
50 this.html = defaultsStr(f.html,
51 (!tf.enableIcons || this.text ? null :
52 '<input type="button" value="" class="' + this.cssClass +
53 '" ' + 'title="' + this.tooltip + '" />'));
54
55 /**
56 * Default position in toolbar ('left'|'center'|'right')
57 * @type {String}
58 */
59 this.toolbarPosition = defaultsStr(f.toolbar_position, RIGHT);
60
61 /**
62 * Clear button container element
63 * @type {DOMElement}
64 * @private
65 */
66 this.container = null;
67
68 /**
69 * Clear button element
70 * @type {DOMElement}
71 * @private
72 */
73 this.element = null;
74 }
75
76 /**
77 * Click event handler for clear button
78 * @private
79 */
80 onClick() {
81 if (!this.isEnabled()) {
82 return;
83 }
84 this.tf.clearFilters();
85 }
86
87 /**
88 * Initialize clear button component
89 */
90 init() {
91 let tf = this.tf;
92
93 if (this.initialized) {
94 return;
95 }
96
97 this.emitter.emit('initializing-feature', this, !isNull(this.targetId));
98
99 let cont = createElm('span');
100
101 let targetEl = !this.targetId ?
102 tf.feature('toolbar').container(this.toolbarPosition) :
103 elm(this.targetId);
104 targetEl.appendChild(cont);
105
106 if (!this.html) {
107 let fltReset = createElm('a', ['href', 'javascript:void(0);']);
108 fltReset.className = this.cssClass;
109 fltReset.appendChild(createText(this.text));
110 cont.appendChild(fltReset);
111 addEvt(fltReset, 'click', () => this.onClick());
112 } else {
113 cont.innerHTML = this.html;
114 let resetEl = cont.firstChild;
115 addEvt(resetEl, 'click', () => this.onClick());
116 }
117 this.element = cont.firstChild;
118 this.container = cont;
119
120 /** @inherited */
121 this.initialized = true;
122
123 this.emitter.emit('feature-initialized', this);
124 }
125
126 /**
127 * Destroy ClearButton instance
128 */
129 destroy() {
130 if (!this.initialized) {
131 return;
132 }
133 removeElm(this.element);
134 removeElm(this.container);
135 this.element = null;
136 this.container = null;
137 this.initialized = false;
138 }
139}
140
141// TODO: remove as soon as feature name is fixed
142ClearButton.meta = {altName: 'btnReset'};