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

225 lines
5.7 KiB
JavaScript
Raw Normal View History

2017-10-06 05:07:55 +02:00
import {Feature} from '../feature';
2017-10-08 12:14:25 +02:00
import {createElm, removeElm, elm, tag} from '../dom';
2017-10-06 05:07:55 +02:00
import {defaultsStr} from '../settings';
import {isUndef} from '../types';
const EVENTS = [
2017-10-08 12:14:25 +02:00
'initializing-feature',
'initializing-extension'
2017-10-06 05:07:55 +02:00
];
2017-10-09 13:15:08 +02:00
/** Left position in toolbar */
2017-10-08 12:14:25 +02:00
export const LEFT = 'left';
2017-10-09 13:15:08 +02:00
/** Right position in toolbar */
2017-10-08 12:14:25 +02:00
export const RIGHT = 'right';
2017-10-09 13:15:08 +02:00
/** Center position in toolbar */
2017-10-08 12:14:25 +02:00
export const CENTER = 'center';
2017-10-06 05:07:55 +02:00
/**
* Toolbar UI component
* @export
* @class Toolbar
* @extends {Feature}
*/
export class Toolbar extends Feature {
/**
* Create an instance of Toolbar
* @param {TableFilter} tf TableFilter instance
* @memberof Toolbar
*/
constructor(tf) {
2019-02-09 14:27:55 +01:00
super(tf, Toolbar);
2017-10-06 05:07:55 +02:00
// Configuration object
let f = this.config.toolbar || {};
/**
* Css class for toolbar's container DOM element
* @type {String}
*/
2017-10-09 12:56:58 +02:00
this.contCssClass = defaultsStr(f.container_css_class, 'inf');
2017-10-06 05:07:55 +02:00
/**
* Css class for left-side inner container DOM element
* @type {String}
*/
2017-10-09 12:56:58 +02:00
this.lContCssClass = defaultsStr(f.left_cont_css_class, 'ldiv');
2017-10-06 05:07:55 +02:00
/**
* Css class for right-side inner container DOM element
* @type {String}
*/
2017-10-09 12:56:58 +02:00
this.rContCssClass = defaultsStr(f.right_cont_css_class, 'rdiv');
2017-10-06 05:07:55 +02:00
/**
* Css class for middle inner container DOM element
* @type {String}
*/
2017-10-09 12:56:58 +02:00
this.cContCssClass = defaultsStr(f.center_cont_css_class, 'mdiv');
2017-10-06 05:07:55 +02:00
/**
* Toolbar's custom container ID
* @type {String}
*/
2017-10-08 12:56:12 +02:00
this.tgtId = defaultsStr(f.target_id, null);
2017-10-06 05:07:55 +02:00
/**
* Toolbar's container DOM element
* @type {DOMElement}
* @private
*/
2017-10-08 12:56:12 +02:00
this.cont = null;
2017-10-06 05:07:55 +02:00
/**
* Left-side inner container DOM element (rows counter in toolbar)
* @type {DOMElement}
* @private
*/
2017-10-09 12:56:58 +02:00
this.lCont = null;
2017-10-06 05:07:55 +02:00
/**
* Right-side inner container DOM element (reset button,
* page length selector in toolbar)
* @type {DOMElement}
* @private
*/
2017-10-09 12:56:58 +02:00
this.rCont = null;
2017-10-06 05:07:55 +02:00
/**
* Middle inner container DOM element (paging elements in toolbar)
* @type {DOMElement}
* @private
*/
2017-10-09 12:56:58 +02:00
this.cCont = null;
2017-10-06 05:07:55 +02:00
2017-10-08 12:14:25 +02:00
/**
* Container elements inside toolbar
* @private
*/
2017-10-08 12:56:12 +02:00
this.innerCont = {
2017-10-08 12:14:25 +02:00
left: null,
center: null,
right: null
};
2017-10-06 05:07:55 +02:00
2017-10-08 12:14:25 +02:00
this.emitter.on(EVENTS,
(feature, isExternal) => this.init(isExternal));
2017-10-09 12:56:58 +02:00
/** @inherited */
this.enabled = true;
2017-10-06 05:07:55 +02:00
}
2017-10-08 12:56:12 +02:00
/**
* Initialize toolbar components
* @param {Boolean} isExternal initialize only if component belongs
* to toolbar
*/
2017-10-08 12:14:25 +02:00
init(isExternal) {
if (this.initialized || isExternal) {
2017-10-06 05:07:55 +02:00
return;
}
let tf = this.tf;
// default container
2017-10-08 12:56:12 +02:00
let container = createElm('div');
2017-10-09 12:56:58 +02:00
container.className = this.contCssClass;
2017-10-06 05:07:55 +02:00
// custom container
2017-10-08 12:56:12 +02:00
if (this.tgtId) {
elm(this.tgtId).appendChild(container);
2017-10-06 05:07:55 +02:00
}
2017-10-08 12:14:25 +02:00
// grid-layout
2017-10-06 05:07:55 +02:00
else if (tf.gridLayout) {
let gridLayout = tf.Mod.gridLayout;
2017-10-08 12:56:12 +02:00
gridLayout.tblMainCont.appendChild(container);
container.className = gridLayout.infDivCssClass;
2017-10-06 05:07:55 +02:00
}
2017-10-08 12:14:25 +02:00
// default location: just above the table
2017-10-06 05:07:55 +02:00
else {
let cont = createElm('caption');
2017-10-08 12:56:12 +02:00
cont.appendChild(container);
2017-10-06 05:07:55 +02:00
tf.dom().insertBefore(cont, tf.dom().firstChild);
}
2017-10-08 12:56:12 +02:00
this.cont = container;
// left container
2017-10-09 12:56:58 +02:00
this.lCont = this.createContainer(container, this.lContCssClass);
2017-10-08 12:56:12 +02:00
// right container
2017-10-09 12:56:58 +02:00
this.rCont = this.createContainer(container, this.rContCssClass);
2017-10-08 12:56:12 +02:00
// middle container
2017-10-09 12:56:58 +02:00
this.cCont = this.createContainer(container, this.cContCssClass);
2017-10-08 12:56:12 +02:00
this.innerCont = {
2017-10-09 12:56:58 +02:00
left: this.lCont,
center: this.cCont,
right: this.rCont
2017-10-08 12:14:25 +02:00
};
/** @inherited */
this.initialized = true;
2017-10-06 05:07:55 +02:00
// emit help initialisation only if undefined
if (isUndef(tf.help)) {
// explicitily enable help to initialise feature by
// default, only if setting is undefined
tf.Mod.help.enable();
this.emitter.emit('init-help', tf);
}
}
2017-10-08 12:56:12 +02:00
/**
* Return the container based on requested position inside the toolbar
* @param {String} [position=RIGHT] 3 possible positions: 'left', 'center',
* 'right'
2017-10-09 12:56:58 +02:00
* @param {DOMElement} el optional DOM element to be inserter in container
2017-10-08 12:56:12 +02:00
* @returns {DOMElement}
*/
2017-10-09 12:56:58 +02:00
container(position = RIGHT, el) {
2017-10-08 12:56:12 +02:00
let cont = this.innerCont[position];
2017-10-09 12:56:58 +02:00
if (el) {
cont.appendChild(el);
2017-10-06 05:07:55 +02:00
}
2017-10-08 12:14:25 +02:00
return cont;
2017-10-06 05:07:55 +02:00
}
2017-10-08 12:56:12 +02:00
/**
* Create DOM element inside passed container
* @param {DOMElement} container
* @param {String} css
* @private
*/
2017-10-06 05:07:55 +02:00
createContainer(container, css) {
let div = createElm('div', ['class', css]);
container.appendChild(div);
return div;
}
2017-10-08 12:56:12 +02:00
/**
* Destroy Toolbar instance
*/
2017-10-06 05:07:55 +02:00
destroy() {
if (!this.initialized) {
return;
}
let tf = this.tf;
2017-10-08 12:56:12 +02:00
removeElm(this.cont);
this.cont = null;
2017-10-06 05:07:55 +02:00
let tbl = tf.dom();
let captions = tag(tbl, 'caption');
2017-10-09 12:56:58 +02:00
[].forEach.call(captions, (el) => removeElm(el));
2017-10-06 05:07:55 +02:00
/** @inherited */
this.initialized = false;
}
}
2019-01-15 13:29:13 +01:00
// TODO: remove as soon as feature name is fixed
2019-02-09 14:27:55 +01:00
Toolbar.meta = {alwaysInstantiate: true};