1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-17 22:06:41 +02:00
TableFilter/src/modules/noResults.js

216 lines
5.1 KiB
JavaScript
Raw Normal View History

import {Feature} from '../feature';
2016-05-25 09:31:53 +02:00
import {createElm, elm, removeElm} from '../dom';
2016-05-15 04:56:12 +02:00
import {isEmpty, isFn} from '../types';
import {NONE} from '../const';
2015-12-08 12:53:12 +01:00
/**
* UI when filtering yields no matches
* @export
* @class NoResults
* @extends {Feature}
*/
2016-05-24 10:42:11 +02:00
export class NoResults extends Feature {
2015-12-08 12:53:12 +01:00
/**
* Creates an instance of NoResults
* @param {TableFilter} tf TableFilter instance
2015-12-08 12:53:12 +01:00
*/
2016-05-24 10:42:11 +02:00
constructor(tf) {
2015-12-08 12:53:12 +01:00
super(tf, 'noResults');
//configuration object
let f = this.config.no_results_message;
/**
* Text (accepts HTML)
* @type {String}
*/
2015-12-09 07:24:45 +01:00
this.content = f.content || 'No results';
/**
* Custom container DOM element
* @type {DOMElement}
*/
2015-12-08 12:53:12 +01:00
this.customContainer = f.custom_container || null;
/**
* ID of custom container element
* @type {String}
*/
2015-12-08 12:53:12 +01:00
this.customContainerId = f.custom_container_id || null;
/**
* Indicates if UI is contained in a external element
* @type {Boolean}
* @private
*/
2016-05-15 04:56:12 +02:00
this.isExternal = !isEmpty(this.customContainer) ||
!isEmpty(this.customContainerId);
/**
* Css class assigned to container element
* @type {String}
*/
2015-12-08 12:53:12 +01:00
this.cssClass = f.css_class || 'no-results';
/**
* Stores container DOM element
* @type {DOMElement}
*/
2015-12-08 12:53:12 +01:00
this.cont = null;
/**
2016-07-31 13:49:38 +02:00
* Callback fired before the message is displayed
* @type {Function}
*/
this.onBeforeShow = isFn(f.on_before_show_msg) ?
2015-12-08 12:53:12 +01:00
f.on_before_show_msg : null;
/**
2016-07-31 13:49:38 +02:00
* Callback fired after the message is displayed
* @type {Function}
*/
this.onAfterShow = isFn(f.on_after_show_msg) ?
2015-12-08 12:53:12 +01:00
f.on_after_show_msg : null;
/**
2016-07-31 13:49:38 +02:00
* Callback fired before the message is hidden
* @type {Function}
*/
this.onBeforeHide = isFn(f.on_before_hide_msg) ?
2015-12-08 13:13:18 +01:00
f.on_before_hide_msg : null;
/**
2016-07-31 13:49:38 +02:00
* Callback fired after the message is hidden
* @type {Function}
*/
this.onAfterHide = isFn(f.on_after_hide_msg) ?
2015-12-08 13:13:18 +01:00
f.on_after_hide_msg : null;
2015-12-08 12:53:12 +01:00
/**
* Prefix for container ID
* @type {String}
* @private
*/
this.prfx = 'nores_';
2015-12-08 12:53:12 +01:00
}
/**
* Initializes NoResults instance
*/
2016-05-24 10:42:11 +02:00
init() {
if (this.initialized) {
2015-12-08 12:53:12 +01:00
return;
}
let tf = this.tf;
2016-05-25 09:31:53 +02:00
let target = this.customContainer || elm(this.customContainerId) ||
2015-12-08 12:53:12 +01:00
tf.tbl;
//container
let cont = createElm('div', ['id', this.prfx + tf.id]);
2015-12-08 12:53:12 +01:00
cont.className = this.cssClass;
2015-12-09 07:24:45 +01:00
cont.innerHTML = this.content;
2015-12-10 23:10:11 +01:00
2016-05-24 10:42:11 +02:00
if (this.isExternal) {
2015-12-10 23:10:11 +01:00
target.appendChild(cont);
} else {
target.parentNode.insertBefore(cont, target.nextSibling);
}
2015-12-08 12:53:12 +01:00
this.cont = cont;
// subscribe to after-filtering event
2016-05-24 10:42:11 +02:00
this.emitter.on(['after-filtering'], () => this.toggle());
/**
* @inherited
*/
2015-12-08 12:53:12 +01:00
this.initialized = true;
2015-12-09 07:24:45 +01:00
this.hide();
2015-12-08 12:53:12 +01:00
}
/**
* Toggle no results message
*/
2016-05-24 10:42:11 +02:00
toggle() {
if (this.tf.getValidRowsNb() > 0) {
this.hide();
} else {
this.show();
}
}
/**
* Show no results message
*/
2016-05-24 10:42:11 +02:00
show() {
if (!this.initialized || !this.isEnabled()) {
2015-12-08 13:13:18 +01:00
return;
}
if (this.onBeforeShow) {
this.onBeforeShow.call(null, this.tf, this);
2015-12-08 13:13:18 +01:00
}
2015-12-08 12:53:12 +01:00
this.setWidth();
2015-12-09 07:24:45 +01:00
this.cont.style.display = 'block';
2015-12-08 13:13:18 +01:00
if (this.onAfterShow) {
this.onAfterShow.call(null, this.tf, this);
2015-12-08 13:13:18 +01:00
}
2015-12-08 12:53:12 +01:00
}
/**
* Hide no results message
*/
2016-05-24 10:42:11 +02:00
hide() {
if (!this.initialized || !this.isEnabled()) {
2015-12-08 13:13:18 +01:00
return;
}
if (this.onBeforeHide) {
this.onBeforeHide.call(null, this.tf, this);
2015-12-08 13:13:18 +01:00
}
this.cont.style.display = NONE;
2015-12-08 13:13:18 +01:00
if (this.onAfterHide) {
this.onAfterHide.call(null, this.tf, this);
2015-12-08 13:13:18 +01:00
}
2015-12-08 12:53:12 +01:00
}
/**
* Sets no results container width
* @private
*/
2016-05-24 10:42:11 +02:00
setWidth() {
if (!this.initialized || this.isExternal || !this.isEnabled()) {
2015-12-08 12:53:12 +01:00
return;
}
let tf = this.tf;
if (tf.gridLayout) {
let gridLayout = tf.feature('gridLayout');
2015-12-09 07:24:45 +01:00
this.cont.style.width = gridLayout.tblCont.clientWidth + 'px';
} else {
2016-07-31 13:49:38 +02:00
this.cont.style.width = (tf.tbl.tHead ? tf.tbl.tHead.clientWidth :
tf.tbl.tBodies[0].clientWidth) + 'px';
2015-12-09 07:24:45 +01:00
}
2015-12-08 12:53:12 +01:00
}
/**
* Remove feature
*/
2016-05-24 10:42:11 +02:00
destroy() {
if (!this.initialized) {
2015-12-08 12:53:12 +01:00
return;
}
2016-05-24 10:42:11 +02:00
removeElm(this.cont);
2015-12-08 12:53:12 +01:00
this.cont = null;
// unsubscribe to after-filtering event
2016-05-24 10:42:11 +02:00
this.emitter.off(['after-filtering'], () => this.toggle());
2016-01-03 03:49:04 +01:00
2015-12-08 12:53:12 +01:00
this.initialized = false;
}
}