1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-06-19 22:25:18 +02:00
TableFilter/src/modules/toolbar.js

256 lines
6.3 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-08 12:14:25 +02:00
export const LEFT = 'left';
export const RIGHT = 'right';
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) {
super(tf, 'toolbar');
// Configuration object
let f = this.config.toolbar || {};
/**
* Css class for toolbar's container DOM element
* @type {String}
*/
this.infDivCssClass = defaultsStr(f.inf_div_css_class, 'inf');
/**
* Css class for left-side inner container DOM element
* @type {String}
*/
this.lDivCssClass = defaultsStr(f.left_div_css_class, 'ldiv');
/**
* Css class for right-side inner container DOM element
* @type {String}
*/
this.rDivCssClass = defaultsStr(f.right_div_css_class, 'rdiv');
/**
* Css class for middle inner container DOM element
* @type {String}
*/
this.mDivCssClass = defaultsStr(f.middle_div_css_class, 'mdiv');
/**
* Toolbar's custom container ID
* @type {String}
*/
this.toolBarTgtId = defaultsStr(f.toolbar_target_id, null);
/**
* Toolbar's container DOM element
* @type {DOMElement}
* @private
*/
this.infDiv = null;
/**
* Left-side inner container DOM element (rows counter in toolbar)
* @type {DOMElement}
* @private
*/
this.lDiv = null;
/**
* Right-side inner container DOM element (reset button,
* page length selector in toolbar)
* @type {DOMElement}
* @private
*/
this.rDiv = null;
/**
* Middle inner container DOM element (paging elements in toolbar)
* @type {DOMElement}
* @private
*/
this.mDiv = null;
2017-10-08 12:14:25 +02:00
/**
2017-10-06 05:07:55 +02:00
* Toolbar container ID prefix
* @private
*/
this.prfxInfDiv = 'inf_';
/**
* Toolbar left element ID prefix
* @private
*/
this.prfxLDiv = 'ldiv_';
/**
* Toolbar right element ID prefix
* @private
*/
this.prfxRDiv = 'rdiv_';
/**
* Toolbar middle element ID prefix
* @private
*/
this.prfxMDiv = 'mdiv_';
2017-10-08 12:14:25 +02:00
/**
* Container elements inside toolbar
* @private
*/
this.cont = {
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-06 05:07:55 +02:00
}
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
let infDiv = createElm('div');
infDiv.className = this.infDivCssClass;
// custom container
if (this.toolBarTgtId) {
elm(this.toolBarTgtId).appendChild(infDiv);
}
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;
gridLayout.tblMainCont.appendChild(infDiv);
infDiv.className = gridLayout.infDivCssClass;
}
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');
cont.appendChild(infDiv);
tf.dom().insertBefore(cont, tf.dom().firstChild);
}
this.infDiv = infDiv;
/*** left div containing rows # displayer ***/
// let lDiv = createElm('div');
// lDiv.className = this.lDivCssClass;
// infDiv.appendChild(lDiv);
2017-10-08 12:14:25 +02:00
this.lDiv = this.createContainer(infDiv, this.lDivCssClass);
2017-10-06 05:07:55 +02:00
/*** right div containing reset button
+ nb results per page select ***/
// let rDiv = createElm('div');
// rDiv.className = this.rDivCssClass;
// infDiv.appendChild(rDiv);
2017-10-08 12:14:25 +02:00
this.rDiv = this.createContainer(infDiv, this.rDivCssClass);
2017-10-06 05:07:55 +02:00
/*** mid div containing paging elements ***/
// let mDiv = createElm('div');
// mDiv.className = this.mDivCssClass;
// infDiv.appendChild(mDiv);
2017-10-08 12:14:25 +02:00
this.mDiv = this.createContainer(infDiv, this.mDivCssClass);
this.cont = {
left: this.lDiv,
center: this.mDiv,
right: this.rDiv
};
/** @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:14:25 +02:00
// left(elm) {
// if (elm) {
// this.lDiv.appendChild(elm);
// }
// return this.lDiv;
// }
// middle(elm) {
// if (elm) {
// this.mDiv.appendChild(elm);
// }
// return this.mDiv;
// }
// right(elm) {
// if (elm) {
// this.rDiv.appendChild(elm);
// }
// return this.rDiv;
// }
container(position = RIGHT, elm) {
let cont = this.cont[position];
2017-10-06 05:07:55 +02:00
if (elm) {
2017-10-08 12:14:25 +02:00
cont.appendChild(elm);
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:14:25 +02:00
// insertIn(elm, position = RIGHT) {
// let cont = this.container[position];
// cont.appendChild(elm);
// return cont;
// }
2017-10-06 05:07:55 +02:00
createContainer(container, css) {
let div = createElm('div', ['class', css]);
container.appendChild(div);
return div;
}
destroy() {
if (!this.initialized) {
return;
}
let tf = this.tf;
removeElm(this.infDiv);
this.infDiv = null;
let tbl = tf.dom();
let captions = tag(tbl, 'caption');
[].forEach.call(captions, (elm) => removeElm(elm));
/** @inherited */
this.initialized = false;
}
}