1 | import {toCamelCase} from './string';
|
2 |
|
3 | const NOT_IMPLEMENTED = 'Not implemented.';
|
4 |
|
5 | /**
|
6 | * Base class defining the interface of a TableFilter feature
|
7 | */
|
8 | export class Feature {
|
9 | /**
|
10 | * Creates an instance of Feature
|
11 | * @param {Object} tf TableFilter instance
|
12 | * @param {Class} feature Feature class for TableFilter registration
|
13 | */
|
14 | constructor(tf, cls) {
|
15 | cls.meta = cls.meta || {};
|
16 |
|
17 | /**
|
18 | * TableFilter instance
|
19 | * @type {TableFilter}
|
20 | */
|
21 | this.tf = tf;
|
22 |
|
23 | /**
|
24 | * Feature name is the camelised class name as per TableFilter's
|
25 | * convention
|
26 | * @type {String}
|
27 | */
|
28 | this.feature = cls.meta.altName || cls.meta.name
|
29 | || toCamelCase(cls.name);
|
30 |
|
31 | /**
|
32 | * TableFilter feature setting
|
33 | * @type {Boolean}
|
34 | */
|
35 | this.enabled = tf[this.feature];
|
36 |
|
37 | /**
|
38 | * TableFilter configuration
|
39 | * @type {Object}
|
40 | */
|
41 | this.config = tf.config();
|
42 |
|
43 | /**
|
44 | * TableFilter emitter instance
|
45 | * @type {Emitter}
|
46 | */
|
47 | this.emitter = tf.emitter;
|
48 |
|
49 | /**
|
50 | * Field indicating whether Feature is initialized
|
51 | * @type {Boolean}
|
52 | */
|
53 | this.initialized = false;
|
54 |
|
55 | /** Subscribe to destroy event */
|
56 | this.emitter.on(['destroy'], () => this.destroy());
|
57 | }
|
58 |
|
59 | /**
|
60 | * Initialize the feature
|
61 | */
|
62 | init() {
|
63 | throw new Error(NOT_IMPLEMENTED);
|
64 | }
|
65 |
|
66 | /**
|
67 | * Reset the feature after being disabled
|
68 | */
|
69 | reset() {
|
70 | this.enable();
|
71 | this.init();
|
72 | }
|
73 |
|
74 | /**
|
75 | * Destroy the feature
|
76 | */
|
77 | destroy() {
|
78 | throw new Error(NOT_IMPLEMENTED);
|
79 | }
|
80 |
|
81 | /**
|
82 | * Enable the feature
|
83 | */
|
84 | enable() {
|
85 | this.enabled = true;
|
86 | }
|
87 |
|
88 | /**
|
89 | * Disable the feature
|
90 | */
|
91 | disable() {
|
92 | this.enabled = false;
|
93 | }
|
94 |
|
95 | /**
|
96 | * Indicate whether the feature is enabled or not
|
97 | * @returns {Boolean}
|
98 | */
|
99 | isEnabled() {
|
100 | return this.enabled === true;
|
101 | }
|
102 | }
|