2019-02-09 14:38:11 +01:00
|
|
|
import {toCamelCase} from './string';
|
2015-11-14 16:14:13 +01:00
|
|
|
|
2017-07-02 13:53:40 +02:00
|
|
|
const NOT_IMPLEMENTED = 'Not implemented.';
|
2015-11-14 16:14:13 +01:00
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Base class defining the interface of a TableFilter feature
|
|
|
|
*/
|
2019-02-09 14:38:11 +01:00
|
|
|
export class Feature {
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Creates an instance of Feature
|
|
|
|
* @param {Object} tf TableFilter instance
|
2019-02-09 14:27:55 +01:00
|
|
|
* @param {Class} feature Feature class for TableFilter registration
|
2016-06-19 07:07:49 +02:00
|
|
|
*/
|
2019-02-09 14:27:55 +01:00
|
|
|
constructor(tf, cls) {
|
2019-02-09 14:38:11 +01:00
|
|
|
cls.meta = cls.meta || {};
|
2019-01-15 13:29:13 +01:00
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* TableFilter instance
|
|
|
|
* @type {TableFilter}
|
|
|
|
*/
|
2019-02-09 14:38:11 +01:00
|
|
|
this.tf = tf;
|
2016-06-19 07:07:49 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-10 13:14:46 +01:00
|
|
|
* Feature name is the camelised class name as per TableFilter's
|
|
|
|
* convention
|
2016-06-19 07:07:49 +02:00
|
|
|
* @type {String}
|
|
|
|
*/
|
2019-02-15 04:16:35 +01:00
|
|
|
this.feature = cls.meta.altName || cls.meta.name
|
|
|
|
|| toCamelCase(cls.name);
|
2016-06-19 07:07:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TableFilter feature setting
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2019-02-09 14:27:55 +01:00
|
|
|
this.enabled = tf[this.feature];
|
2016-06-19 07:07:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TableFilter configuration
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
this.config = tf.config();
|
2016-06-19 07:07:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TableFilter emitter instance
|
|
|
|
* @type {Emitter}
|
|
|
|
*/
|
2015-12-27 12:08:14 +01:00
|
|
|
this.emitter = tf.emitter;
|
2016-06-19 07:07:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Field indicating whether Feature is initialized
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
this.initialized = false;
|
2016-12-24 13:35:26 +01:00
|
|
|
|
|
|
|
/** Subscribe to destroy event */
|
|
|
|
this.emitter.on(['destroy'], () => this.destroy());
|
2015-11-14 16:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Initialize the feature
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
init() {
|
2017-07-02 13:53:40 +02:00
|
|
|
throw new Error(NOT_IMPLEMENTED);
|
2015-11-14 16:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Reset the feature after being disabled
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
reset() {
|
|
|
|
this.enable();
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Destroy the feature
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
destroy() {
|
2017-07-02 13:53:40 +02:00
|
|
|
throw new Error(NOT_IMPLEMENTED);
|
2015-11-14 16:14:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Enable the feature
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
enable() {
|
|
|
|
this.enabled = true;
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Disable the feature
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
disable() {
|
|
|
|
this.enabled = false;
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:07:49 +02:00
|
|
|
/**
|
|
|
|
* Indicate whether the feature is enabled or not
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
2015-11-14 16:14:13 +01:00
|
|
|
isEnabled() {
|
2017-05-13 12:51:10 +02:00
|
|
|
return this.enabled === true;
|
2015-11-14 16:14:13 +01:00
|
|
|
}
|
|
|
|
}
|