-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMarqueeController.js
381 lines (338 loc) · 8.69 KB
/
MarqueeController.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import {forward} from '@enact/core/handle';
import hoc from '@enact/core/hoc';
import {Job} from '@enact/core/util';
import React from 'react';
import PropTypes from 'prop-types';
const STATE = {
inactive: 0, // Marquee is not necessary (render or focus not happened)
active: 1, // Marquee in progress, awaiting complete
ready: 2 // Marquee completed or not needed, but state is active
};
/**
* Context propTypes for MarqueeController
*
* @memberof ui/Marquee.MarqueeController
* @private
*/
const contextTypes = {
/**
* Called by `Marquee` instances when marqueeing is canceled (e.g. when blurring a `Marquee`
* set to `marqueeOn='focus'`)
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
cancel: PropTypes.func,
/**
* Called by `Marquee` instances when marqueeing has completed
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
complete: PropTypes.func,
/**
* Called by `Marquee` instances when hovered
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
enter: PropTypes.func,
/**
* Called by `Marquee` instances when unhovered
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
leave: PropTypes.func,
/**
* Called to register a `Marquee` instance to be synchronized
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
register: PropTypes.func,
/**
* Called by `Marquee` instances when marqueeing is started (e.g. when focusing a `Marquee`
* set to `marqueeOn='focus'`). If the Marquee instance should not or does not need to marquee,
* the function can return `true` to mark itself complete.
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
start: PropTypes.func,
/**
* Called to unregister a synchronized `Marquee` instance
*
* @type {Function}
* @memberof ui/Marquee.MarqueeController.contextTypes
*/
unregister: PropTypes.func
};
/**
* Default configuration parameters for {@link ui/Marquee.MarqueeController}
*
* @type {Object}
* @memberof ui/Marquee.MarqueeController
* @hocconfig
*/
const defaultConfig = {
/**
* When `true`, any `onFocus` events that bubble to the controller will start the contained
* `Marquee` instances. This is useful when a component contains `Marquee` instances that need to be
* started with sibling components are focused.
*
* @type {Boolean}
* @default false
* @memberof ui/Marquee.MarqueeController.defaultConfig
*/
marqueeOnFocus: false
};
/**
* A higher-order component that synchronizes contained `Marquee`s.
*
* @memberof ui/Marquee
* @hoc
* @public
*/
const MarqueeController = hoc(defaultConfig, (config, Wrapped) => {
const {marqueeOnFocus} = config;
const forwardBlur = forward('onBlur');
const forwardFocus = forward('onFocus');
return class extends React.Component {
static displayName = 'MarqueeController'
static childContextTypes = contextTypes;
constructor (props) {
super(props);
this.controlled = [];
this.isFocused = false;
}
getChildContext () {
return {
cancel: this.handleCancel,
complete: this.handleComplete,
enter: this.handleEnter,
leave: this.handleLeave,
register: this.handleRegister,
start: this.handleStart,
unregister: this.handleUnregister
};
}
componentWillUnmount () {
this.cancelJob.stop();
}
cancelJob = new Job(() => this.doCancel(), 30)
/*
* Registers `component` with a set of handlers for `start` and `stop`.
*
* @param {Object} component A component, typically a React component instance, on
* which handlers will be dispatched.
* @param {Object} handlers An object containing `start` and `stop` functions
*
* @returns {undefined}
*/
handleRegister = (component, handlers) => {
const needsStart = !this.allInactive() || this.isFocused;
this.controlled.push({
...handlers,
state: STATE.inactive,
component
});
if (needsStart) {
this.dispatch('start');
}
}
/*
* Unregisters `component` for synchronization
*
* @param {Object} component A previously registered component
*
* @returns {undefined}
*/
handleUnregister = (component) => {
let wasRunning = false;
for (let i = 0; i < this.controlled.length; i++) {
if (this.controlled[i].component === component) {
wasRunning = this.controlled[i].state === STATE.active;
this.controlled.splice(i, 1);
break;
}
}
if (wasRunning && !this.anyRunning()) {
this.dispatch('start');
}
}
/*
* Handler for the `start` context function
*
* @param {Object} component A previously registered component
*
* @returns {undefined}
*/
handleStart = (component) => {
this.cancelJob.stop();
if (!this.anyRunning()) {
this.markAll(STATE.ready);
this.dispatch('start', component);
}
}
/*
* Handler for the `cancel` context function
*
* @param {Object} component A previously registered component
*
* @returns {undefined}
*/
handleCancel = () => {
if (this.anyRunning()) {
this.cancelJob.start();
}
}
doCancel = () => {
if (this.isHovered || this.isFocused) {
return;
}
this.markAll(STATE.inactive);
this.dispatch('stop');
}
/*
* Handler for the `complete` context function
*
* @param {Object} component A previously registered component
*
* @returns {undefined}
*/
handleComplete = (component) => {
const complete = this.markReady(component);
if (complete) {
this.markAll(STATE.ready);
this.dispatch('start');
}
}
handleEnter = () => {
this.isHovered = true;
if (!this.anyRunning()) {
this.dispatch('start');
}
this.cancelJob.stop();
}
handleLeave = () => {
this.isHovered = false;
this.cancelJob.start();
}
/*
* Handler for the focus event
*/
handleFocus = (ev) => {
this.isFocused = true;
if (!this.anyRunning()) {
this.dispatch('start');
}
this.cancelJob.stop();
forwardFocus(ev, this.props);
}
/*
* Handler for the blur event
*/
handleBlur = (ev) => {
this.isFocused = false;
if (this.anyRunning()) {
this.cancelJob.start();
}
forwardBlur(ev, this.props);
}
/*
* Invokes the `action` handler for each synchronized component except the invoking
* `component`.
*
* @param {String} action `'start'` or `'stop'`
* @param {Object} component A previously registered component
*
* @returns {undefined}
*/
dispatch (action, component) {
this.controlled.forEach((controlled) => {
const {component: controlledComponent, [action]: handler} = controlled;
if (component !== controlledComponent && typeof handler === 'function') {
const complete = handler.call(controlledComponent);
// Returning `true` from a start request means that the marqueeing is
// unnecessary and is therefore not awaiting a finish
if (action === 'start' && complete) {
controlled.state = STATE.ready;
} else if (action === 'start') {
controlled.state = STATE.active;
}
} else if ((action === 'start') && (component === controlledComponent)) {
controlled.state = STATE.active;
}
});
}
/*
* Marks all components with the passed-in state
*
* @param {Enum} state The state to set
*
* @returns {undefined}
*/
markAll (state) {
this.controlled.forEach(c => {
c.state = state;
});
}
/*
* Marks `component` as ready for next marquee action
*
* @param {Object} component A previously registered component
*
* @returns {Boolean} `true` if no components are STATE.active
*/
markReady (component) {
let complete = true;
this.controlled.forEach(c => {
if (c.component === component) {
c.state = STATE.ready;
}
complete = complete && (c.state !== STATE.active);
});
return complete;
}
/*
* Checks that all components are inactive
*
* @returns {Boolean} `true` if any components should be running
*/
allInactive () {
const activeOrReady = this.controlled.reduce((res, component) => {
return res || !(component.state === STATE.inactive);
}, false);
return !activeOrReady;
}
/*
* Checks for any components currently marqueeing
*
* @returns {Boolean} `true` if any component is marqueeing
*/
anyRunning () {
return this.controlled.reduce((res, component) => {
return res || (component.state === STATE.active);
}, false);
}
render () {
let props = this.props;
if (marqueeOnFocus) {
props = {
...this.props,
onBlur: this.handleBlur,
onFocus: this.handleFocus
};
}
return (
<Wrapped {...props} />
);
}
};
});
export default MarqueeController;
export {
contextTypes,
MarqueeController
};