UNPKG

6.06 kBJavaScriptView Raw
1import {Feature} from '../feature';
2import {createElm, removeElm, elm, tag} from '../dom';
3import {defaultsStr} from '../settings';
4import {isUndef} from '../types';
5
6const EVENTS = [
7 'initializing-feature',
8 'initializing-extension'
9];
10
11/** Left position in toolbar */
12export const LEFT = 'left';
13/** Right position in toolbar */
14export const RIGHT = 'right';
15/** Center position in toolbar */
16export const CENTER = 'center';
17
18/**
19 * Toolbar UI component
20 * @export
21 * @class Toolbar
22 * @extends {Feature}
23 */
24export class Toolbar extends Feature {
25
26 /**
27 * Create an instance of Toolbar
28 * @param {TableFilter} tf TableFilter instance
29 * @memberof Toolbar
30 */
31 constructor(tf) {
32 super(tf, Toolbar);
33
34 // Configuration object
35 let f = this.config.toolbar || {};
36
37 /**
38 * Css class for toolbar's container DOM element
39 * @type {String}
40 */
41 this.contCssClass = defaultsStr(f.container_css_class, 'inf');
42
43 /**
44 * Css class for left-side inner container DOM element
45 * @type {String}
46 */
47 this.lContCssClass = defaultsStr(f.left_cont_css_class, 'ldiv');
48
49 /**
50 * Css class for right-side inner container DOM element
51 * @type {String}
52 */
53 this.rContCssClass = defaultsStr(f.right_cont_css_class, 'rdiv');
54
55 /**
56 * Css class for middle inner container DOM element
57 * @type {String}
58 */
59 this.cContCssClass = defaultsStr(f.center_cont_css_class, 'mdiv');
60
61 /**
62 * Toolbar's custom container ID
63 * @type {String}
64 */
65 this.tgtId = defaultsStr(f.target_id, null);
66
67 /**
68 * Toolbar's container DOM element
69 * @type {DOMElement}
70 * @private
71 */
72 this.cont = null;
73
74 /**
75 * Left-side inner container DOM element (rows counter in toolbar)
76 * @type {DOMElement}
77 * @private
78 */
79 this.lCont = null;
80
81 /**
82 * Right-side inner container DOM element (reset button,
83 * page length selector in toolbar)
84 * @type {DOMElement}
85 * @private
86 */
87 this.rCont = null;
88
89 /**
90 * Middle inner container DOM element (paging elements in toolbar)
91 * @type {DOMElement}
92 * @private
93 */
94 this.cCont = null;
95
96 /**
97 * Container elements inside toolbar
98 * @private
99 */
100 this.innerCont = {
101 left: null,
102 center: null,
103 right: null
104 };
105
106 this.emitter.on(EVENTS,
107 (feature, isExternal) => this.init(isExternal));
108
109 /** @inherited */
110 this.enabled = true;
111 }
112
113 /**
114 * Initialize toolbar components
115 * @param {Boolean} isExternal initialize only if component belongs
116 * to toolbar
117 */
118 init(isExternal) {
119 if (this.initialized || isExternal) {
120 return;
121 }
122
123 let tf = this.tf;
124
125 // default container
126 let container = createElm('div');
127 container.className = this.contCssClass;
128
129 // custom container
130 if (this.tgtId) {
131 elm(this.tgtId).appendChild(container);
132 }
133 // grid-layout
134 else if (tf.gridLayout) {
135 let gridLayout = tf.Mod.gridLayout;
136 gridLayout.tblMainCont.appendChild(container);
137 container.className = gridLayout.infDivCssClass;
138 }
139 // default location: just above the table
140 else {
141 let cont = createElm('caption');
142 cont.appendChild(container);
143 tf.dom().insertBefore(cont, tf.dom().firstChild);
144 }
145 this.cont = container;
146
147 // left container
148 this.lCont = this.createContainer(container, this.lContCssClass);
149
150 // right container
151 this.rCont = this.createContainer(container, this.rContCssClass);
152
153 // middle container
154 this.cCont = this.createContainer(container, this.cContCssClass);
155
156 this.innerCont = {
157 left: this.lCont,
158 center: this.cCont,
159 right: this.rCont
160 };
161
162 /** @inherited */
163 this.initialized = true;
164
165 // emit help initialisation only if undefined
166 if (isUndef(tf.help)) {
167 // explicitily enable help to initialise feature by
168 // default, only if setting is undefined
169 tf.Mod.help.enable();
170 this.emitter.emit('init-help', tf);
171 }
172 }
173
174 /**
175 * Return the container based on requested position inside the toolbar
176 * @param {String} [position=RIGHT] 3 possible positions: 'left', 'center',
177 * 'right'
178 * @param {DOMElement} el optional DOM element to be inserter in container
179 * @returns {DOMElement}
180 */
181 container(position = RIGHT, el) {
182 let cont = this.innerCont[position];
183 if (el) {
184 cont.appendChild(el);
185 }
186 return cont;
187 }
188
189 /**
190 * Create DOM element inside passed container
191 * @param {DOMElement} container
192 * @param {String} css
193 * @private
194 */
195 createContainer(container, css) {
196 let div = createElm('div', ['class', css]);
197 container.appendChild(div);
198 return div;
199 }
200
201 /**
202 * Destroy Toolbar instance
203 */
204 destroy() {
205 if (!this.initialized) {
206 return;
207 }
208
209 let tf = this.tf;
210
211 removeElm(this.cont);
212 this.cont = null;
213
214 let tbl = tf.dom();
215 let captions = tag(tbl, 'caption');
216 [].forEach.call(captions, (el) => removeElm(el));
217
218 /** @inherited */
219 this.initialized = false;
220 }
221}
222
223// TODO: remove as soon as feature name is fixed
224Toolbar.meta = {alwaysInstantiate: true};