-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponentStates.js
228 lines (199 loc) · 5.62 KB
/
ComponentStates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Requires
*/
import { Exception, isPojo, mergeObject } from '@squirrel-forge/ui-util';
/**
* Component states exception
* @class
* @extends Exception
*/
class ComponentStatesException extends Exception {}
/**
* @typedef {Object|true} ComponentStateDefinition - Component state definition
* @property {undefined|null|boolean} global - By default state is global, set to: false for non global states
* @property {undefined|null|string} classOn - CSS class to set when active
* @property {undefined|null|string} classOff - CSS class to set when inactive
* @property {undefined|null|Array<string>} unsets - Unset given states when this one becomes active
*/
/**
* Component states
* @class
*/
export class ComponentStates {
/**
* Component element
* @private
* @property
* @type {null}
*/
#component = null;
/**
* Component states
* @private
* @property
* @type {null}
*/
#states = null;
/**
* States by name
* @private
* @property
* @type {Object}
*/
#named = {};
/**
* Global state
* @private
* @property
* @type {null|string}
*/
#global = null;
/**
* Global state attribute
* @private
* @property
* @type {string}
*/
#attribute = 'data-state';
/**
* Constructor
* @constructor
* @param {UiComponent} component - Component element
* @param {null|Object} states - States map
*/
constructor( component, states = null ) {
this.#component = component;
if ( states !== null && !isPojo( states ) ) {
throw new ComponentStatesException( 'Argument states must be null or a plain Object' );
} else if ( states === null ) {
states = { initialized : { classOn : 'initialized' } };
}
this.#states = states;
}
/**
* Global state getter
* @public
* @return {null|string} - Global state
*/
get global() {
return this.#global;
}
/**
* Direct states access
* @public
* @return {Object} - States data object
*/
get exposed() {
return this.#states;
}
/**
* Extend states
* @public
* @param {Object} states - States map
* @return {void}
*/
extend( states ) {
mergeObject( this.#states, states, true, true, true, false );
}
/**
* State active
* @public
* @param {string} name - State name
* @return {boolean} - State is active
*/
is( name ) {
return !!this.#named[ name ];
}
/**
* State defined
* @public
* @param {string} name - State name
* @return {boolean} - State exists
*/
has( name ) {
return !!this.#states[ name ];
}
/**
* Get state info
* @public
* @param {string} name - State name
* @return {Object} - State info object
*/
get( name ) {
const state = this.#states[ name ];
if ( !state ) throw new ComponentStatesException( 'Unknown state: ' + name );
return state;
}
/**
* Set state by name
* @public
* @param {string} name - State name
* @return {void}
*/
set( name ) {
const state = this.get( name );
const is_global = state === true || state.global !== false;
let is_global_changed = false, to = null;
const from = this.#global;
if ( is_global ) {
// Do not set globals unless changed
if ( name !== from ) {
is_global_changed = true;
to = name;
} else {
// Skip along and ignore the set command
return;
}
}
// Complex state options
if ( state !== true ) {
// Unset any states
if ( state.unsets instanceof Array ) {
for ( let i = 0; i < state.unsets.length; i++ ) {
this.unset( state.unsets[ i ] );
}
}
// Set/unset any class states
if ( state.classOn ) this.#component.dom.classList.add( state.classOn );
if ( state.classOff ) this.#component.dom.classList.remove( state.classOff );
}
// Default and global states are both global
if ( is_global ) {
this.#component.dom.setAttribute( this.#attribute, name );
this.#global = name;
}
// Set individual state
this.#named[ name ] = true;
// Dispatch corresponding event
if ( is_global_changed ) {
this.#component.dispatchEvent( 'state.changed', { from, to } );
} else {
this.#component.dispatchEvent( 'state.set', { set : name } );
}
}
/**
* Set state by name
* @public
* @param {string} name - State name
* @return {void}
*/
unset( name ) {
if ( !this.#named[ name ] ) return;
const state = this.get( name );
const is_global = state === true || state.global !== false;
// Complex state options
if ( state !== true ) {
// Set/unset any class states
if ( state.classOn ) this.#component.dom.classList.remove( state.classOn );
if ( state.classOff ) this.#component.dom.classList.add( state.classOff );
}
// Default and global states are both global
if ( is_global ) {
this.#component.dom.setAttribute( this.#attribute, 'null' );
this.#global = null;
}
// Set individual state
this.#named[ name ] = false;
this.#component.dispatchEvent( 'state.unset', { unset : name } );
}
}