-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathplayer-area.tsx
307 lines (268 loc) · 8.62 KB
/
player-area.tsx
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
import {h, Component, toChildArray, Fragment, VNode, ComponentChild, ComponentChildren} from 'preact';
import {connect} from 'react-redux';
import {withLogger} from '../../components/logger';
import {KPUIComponent} from '@playkit-js/kaltura-player-js';
/**
* mapping state to props
* @param {*} state - redux store state
* @returns {Object} - mapped state to this component
*/
const mapStateToProps = state => ({
activePresetName: state.shell.activePresetName
});
export const Remove = 'remove';
/**
* get PlayerArea component item by key
* @param {*} dictionary dictionary
* @param {*} componentName componentName
* @returns {*} positionedComponent
*/
function getPositionedPlayerAreaItem(dictionary, componentName) {
dictionary[componentName] = dictionary[componentName] || {
before: [],
after: [],
replace: null
};
return dictionary[componentName];
}
const initialState = {
playerAreaComponents: null,
hasPositionedComponents: false,
presetComponentsOnlyMode: true
};
/**
* get component name
* @param {*} component component
* @returns {*} component name
*/
function getComponentName(component: any) {
if (!component || !component.type) {
return null;
}
return component.type.displayName;
}
/**
* A video PlayerArea enabling injecting components by preset, PlayerArea and position
*/
@withLogger('PlayerArea')
@connect(mapStateToProps)
class PlayerArea extends Component<any, any> {
static defaultProps = {
show: true
};
_unregisterListenerCallback!: ((args?: any) => any) | null;
_actualChildren: ComponentChildren;
/**
* should component update handler
*
* @returns {boolean} - always update component
* @param {Object} nextProps - next props of the component
* @param {Object} nextState - next state of the component
* @memberof OverlayAction
*/
shouldComponentUpdate(nextProps: any, nextState: any): boolean {
return (
nextProps.shouldUpdate ||
this.state.playerAreaComponents !== nextState.playerAreaComponents ||
nextProps.activePresetName !== this.props.activePresetName
);
}
/**
* component did update handler
*
* @param {Object} prevProps - prev props of the component
* @returns {void}
* @memberof OverlayAction
*/
componentDidUpdate(prevProps: any) {
if (prevProps.activePresetName !== this.props.activePresetName) {
this._registerListener();
}
}
/**
* @returns {void}
*/
_unregisterListener() {
this.props.logger.debug(`Player area '${this.props.name}' - unregister to changes`);
if (typeof this._unregisterListenerCallback === 'function') {
this._unregisterListenerCallback();
this._unregisterListenerCallback = null;
}
}
/**
* @returns {void}
*/
_registerListener() {
const {activePresetName, name: playerAreaName} = this.props;
this._unregisterListener();
if (!activePresetName || !playerAreaName) {
return;
}
this.props.logger.debug(`Player area '${playerAreaName}' in preset '${activePresetName}' - register to changes`);
this._unregisterListenerCallback = this.context.playerAreaComponentsStore.listen(activePresetName, playerAreaName, this._updateAreaComponents);
}
/**
* @param {Array<Object>} playerAreaComponents - playerAreaComponents
* @returns {void}
* @private
*/
_updateAreaComponents = (playerAreaComponents: Array<any>): void => {
const {activePresetName, name: playerAreaName} = this.props;
this.props.logger.debug(`Player area '${playerAreaName}' in preset '${activePresetName}' - update children components`);
const positionedComponentMap = {};
const nextPlayerAreaComponents: any = {
appendedComponents: [],
positionedComponentMap
};
let hasPositionedComponents = false;
playerAreaComponents.forEach(component => {
if (component.beforeComponent) {
getPositionedPlayerAreaItem(positionedComponentMap, component.beforeComponent).before.push(component);
hasPositionedComponents = true;
} else if (component.afterComponent) {
getPositionedPlayerAreaItem(positionedComponentMap, component.afterComponent).after.push(component);
hasPositionedComponents = true;
} else if (component.replaceComponent) {
getPositionedPlayerAreaItem(positionedComponentMap, component.replaceComponent).replace = component;
hasPositionedComponents = true;
} else {
nextPlayerAreaComponents.appendedComponents.push(component);
}
});
this.setState({
playerAreaComponents: nextPlayerAreaComponents,
hasPositionedComponents,
presetComponentsOnlyMode: false
});
};
/**
* component did mount
* @return {void}
*/
componentDidMount(): void {
this.props.logger.debug(`Player area '${this.props.name}' - handle did mount`);
this.setState(initialState);
this._registerListener();
this._actualChildren = [];
}
/**
* component will update
* @param {Object} nextProps - the next props
* @return {void}
*/
componentWillUpdate(nextProps: any): void {
const {children} = nextProps;
this._actualChildren = children && children.type === Fragment ? children.props.children : children;
}
/**
* componentWillUnmount
*
* @returns {void}
*/
componentWillUnmount(): void {
const {name} = this.props;
this.props.logger.debug(`Player area '${name}' - handle will unmount`);
this._unregisterListener();
}
/**
* render preset component
* @param {KPUIComponent} uiComponent uiComponent
* @returns {*} component
* @private
*/
_renderUIComponent(uiComponent: KPUIComponent): VNode<any> | null {
if (!uiComponent.get) {
return null;
}
return h(uiComponent.get as () => any, uiComponent.props!);
}
/**
* render PlayerArea content
* @param {*} children children
* @returns {*} component
*/
renderContent(children: ComponentChildren) {
return <Fragment>{children}</Fragment>;
}
/**
* get positioned components
* @param {*} children children
* @returns {*} new children
*/
_getPositionedComponents(children: ComponentChildren): Array<any> {
const {playerAreaComponents} = this.state;
const newChildren: any[] = [];
toChildArray(children).forEach((child: VNode) => {
if (child.type === 'div' || child.type === Fragment) {
child.props.children = this._getPositionedComponents(child.props.children);
newChildren.push(child);
return;
}
let childName = getComponentName(child);
if (!childName) {
newChildren.push(child);
return;
}
const positionedComponent = playerAreaComponents.positionedComponentMap[childName];
if (!positionedComponent) {
newChildren.push(child);
return;
}
const {replace, before, after} = positionedComponent;
if (replace) {
// if remove string was given then don't add the component to the newChildren array - hence it will be removed
if (replace.get === Remove) {
return;
}
if (typeof replace.get !== 'string') {
// pass the replaced component props to the override one (if it's not an html element e.g. "div")
replace.props = replace.props || {};
replace.props.replacedComponentProps = child.props;
}
newChildren.push(this._renderUIComponent(replace));
return;
}
if (before.length) {
before.forEach(component => {
newChildren.push(this._renderUIComponent(component));
});
}
newChildren.push(child);
if (after.length) {
after.forEach(component => {
newChildren.push(this._renderUIComponent(component));
});
}
});
return newChildren;
}
/**
* render component
*
* @returns {null | *} - component
* @memberof PlayerArea
*/
render(): ComponentChild {
const {show} = this.props;
const {playerAreaComponents, hasPositionedComponents, presetComponentsOnlyMode} = this.state;
if (presetComponentsOnlyMode) {
return this.renderContent(this._actualChildren);
}
if (!playerAreaComponents || !show) {
return null;
}
let newChildren: any[] = [];
if (hasPositionedComponents) {
newChildren = this._getPositionedComponents(this._actualChildren);
} else {
newChildren.push(...toChildArray(this._actualChildren));
}
const appendedChildren = playerAreaComponents.appendedComponents.map(component => {
return this._renderUIComponent(component);
});
let startIndex = newChildren.length;
newChildren.splice(startIndex, 0, ...appendedChildren);
return this.renderContent(newChildren);
}
}
export {PlayerArea};