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