1 | import {Feature} from '../feature';
|
2 | import {createElm, createText, elm, removeElm} from '../dom';
|
3 | import {addEvt} from '../event';
|
4 | import {defaultsStr} from '../settings';
|
5 | import {isNull} from '../types';
|
6 | import {RIGHT} from './toolbar';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export class ClearButton extends Feature {
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 | constructor(tf) {
|
18 | super(tf, ClearButton);
|
19 |
|
20 | let f = this.config.btn_reset || {};
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 | this.targetId = defaultsStr(f.target_id, null);
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 | this.text = defaultsStr(f.text, null);
|
33 |
|
34 | |
35 |
|
36 |
|
37 |
|
38 | this.cssClass = defaultsStr(f.css_class, 'reset');
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 | this.tooltip = f.tooltip || 'Clear filters';
|
45 |
|
46 | |
47 |
|
48 |
|
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 |
|
57 |
|
58 |
|
59 | this.toolbarPosition = defaultsStr(f.toolbar_position, RIGHT);
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | this.container = null;
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 | this.element = null;
|
74 | }
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 | onClick() {
|
81 | if (!this.isEnabled()) {
|
82 | return;
|
83 | }
|
84 | this.tf.clearFilters();
|
85 | }
|
86 |
|
87 | |
88 |
|
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 |
|
121 | this.initialized = true;
|
122 |
|
123 | this.emitter.emit('feature-initialized', this);
|
124 | }
|
125 |
|
126 | |
127 |
|
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 |
|
142 | ClearButton.meta = {altName: 'btnReset'};
|