-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAwayPlayerComponent.ts
378 lines (314 loc) · 10.3 KB
/
AwayPlayerComponent.ts
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
//@ts-ignore
import template from "./embed.html";
import { IGameConfigBase } from "../loader/iConfigBase";
interface IAwayPlayer {
new(config: IGameConfigBase);
loadAndPlay(): Promise<void>;
loadBuffer(buffer: ArrayBuffer): Promise<void>;
play(): void;
stop(): void;
}
const scriptSrc = (<HTMLScriptElement>document.currentScript).src;
const scriptBaseUrl = new URL('.', scriptSrc).href;
const defaultSplashUrl = new URL('./assets/splash.jpg', scriptBaseUrl).href;
const defaultProgress = {
"direction": "lr",
"back": "#130d02",
"line": "#f29f01",
"rect": [
0.25,
0.77,
0.5,
0.01
]
};
export interface IBindingConfig {
version?: string;
runtimeBaseUrl: string;
src: string;
avm1?: boolean;
width: string | number;
height: string | number;
onLoad?: string;
onProgress?: string;
onError?: string;
scaleMode?: string;
autoplay?: boolean;
backgroundColor?: string;
splash?: string | boolean,
progress?: boolean | {
back: string;
rect: [number, number, number, number],
line: string;
direction?: 'lr' | 'tb'
},
/**
* @description A decimal value between 0 and 1. Limits stage scale to improve performance.
* For example, 0.5 limits the player to half resolution.
*/
maxStageScale?: number;
/**
* @description Hide player content before loading
*/
hideBeforeLoad?: boolean;
/**
* @kind Runtime
* @description Smooth bitmaps by default. Flash Player defaults to pixelated scaling.
*/
smoothBitmaps?: boolean;
/**
* @kind Runtime
* @description Enable blend modes (experimental)
*/
enableBlends?: boolean;
/**
* @kind Runtime
* @description Enable filters such as blur and glow (experimental)
*/
enableFilters?: boolean;
}
type TBindingScheme = {[key in keyof IBindingConfig]: {required?: boolean, default?: any}};
export class AwayPlayerComponent extends HTMLElement {
static get observedAttributes() {
return [
'src',
'width',
'height'
];
}
private static BINDING_CONFIG: TBindingScheme = {
version: {required: false, default: 'latest'},
runtimeBaseUrl: {required: false, default: scriptBaseUrl},
src: {required: true},
avm1: {required: false, default: false},
width: {required: false, default: 550},
height: {required: false, default: 400},
onLoad: {required: false},
onProgress: {required: false},
onError: {required: false},
scaleMode: {required: false, default: 'all'},
autoplay: {required: false, default: true},
hideBeforeLoad: {required: false, default: false},
maxStageScale: {required: false, default: undefined},
backgroundColor: {required: false, default: 'black'},
splash: {required: false, default: defaultSplashUrl},
progress: {required: false, default: defaultProgress},
// runtime
smoothBitmaps: {required: false, default: false},
enableBlends: {required: false, default: false},
enableFilters: {required: false, default: false},
};
_loaderHolder: HTMLDivElement;
_runConfig: IBindingConfig = <any>{};
_gameFrame: HTMLIFrameElement;
_player: IAwayPlayer;
_root: ShadowRoot;
get player() {
return this._player;
}
get config() {
return this._runConfig;
}
set config(cfg) {
this._runConfig = cfg;
}
get src() {
return this._runConfig.src || this.getAttribute('src');
}
set src(v) {
this._runConfig.src = v;
}
set width(v: number | string) {
this._runConfig.width = v;
}
get width() {
return this._runConfig.width || this.getAttribute('width');
}
set height(v: number | string) {
this._runConfig.height = v;
}
get height() {
return this._runConfig.height || this.getAttribute('height');
}
set runtimeBaseUrl(v: string) {
this._runConfig.runtimeBaseUrl = v;
}
get runtimeBaseUrl() {
return this._runConfig.runtimeBaseUrl;
}
constructor(){
super();
this.onError = this.onError.bind(this);
this._root = this.attachShadow({mode:'closed'});
}
_getRuntimeUrl() {
const baseUrl = this._runConfig.runtimeBaseUrl;
const loader = new URL('loader.js', baseUrl).href;
const runtime = new URL('runtime.js', baseUrl).href;
/**
* @todo support runtime version
**/
return {
loader,
runtime,
baseUrl,
}
}
_dropPlayer() {
if (this._gameFrame) {
this._gameFrame.remove();
this._player = null;
}
}
onError(_arg: any) {
const f = this._runConfig.onError && self[this._runConfig.onError];
if (typeof f === 'function') {
f(_arg);
}
this.dispatchEvent(new ErrorEvent('error', {
error: new Error('Awayfl runtime error'),
message: _arg?.message || _arg,
}));
}
_attachRuntimeEvents() {
const fd = this._gameFrame.contentWindow;
//@ts-ignore
fd.addEventListener('awayfl-player-init', ({detail}) =>{
this._player = detail;
}, {once: true});
fd.addEventListener('awayfl-player-load', () => {
const f = this._runConfig.onLoad && self[this._runConfig.onLoad];
if (typeof f === 'function') {
f();
}
this.dispatchEvent(new CustomEvent('load'));
this._gameFrame.style.display = '';
}, {once: true});
//@ts-ignore
fd.addEventListener('awayfl-player-progress', ({detail}) => {
const f = this._runConfig.onProgress && self[this._runConfig.onProgress];
if (typeof f === 'function') {
f(detail);
}
this.dispatchEvent(new CustomEvent('progress', {detail}));
});
}
_buildTemplate(frame: HTMLIFrameElement): string {
const t: string = template;
const urls = this._getRuntimeUrl();
const gameConfig = {
width: frame.clientWidth,
height: frame.clientHeight,
backgroundColor: this._runConfig.backgroundColor,
splash: this._runConfig.splash,
progress: this._runConfig.progress,
runtime: [urls.runtime],
binary: [{
path: this._runConfig.src,
resourceType: 'GAME',
name: 'Game',
meta: {}
}],
//debug: true,
baseUrl: urls.baseUrl,
maxStageScale: +this._runConfig.maxStageScale,
runtimeFlags: {
defaultSmoothBitmap: !!this._runConfig.smoothBitmaps,
enableBlends: !!this._runConfig.enableBlends,
enableFilters: !!this._runConfig.enableFilters,
}
}
return t
.replace(/__LOADER_URL__/, urls.loader)
.replace(/__GAME_CONFIG__/, JSON.stringify(JSON.stringify(gameConfig)));
}
_constructPlayer() {
const root = this._root;
const frame = document.createElement('iframe');
frame.style.border = 'none';
frame.style.display = this._runConfig.hideBeforeLoad ? 'none' : ''
frame.width = '' + this._runConfig.width;
frame.height = '' + this._runConfig.height;
this._gameFrame = frame;
root.appendChild(frame);
frame.addEventListener('load', this._attachRuntimeEvents.bind(this), {
once: true
});
frame.addEventListener('error', this.onError);
frame.srcdoc = this._buildTemplate(frame);
}
_mapAttrs() {
const params: NodeListOf<HTMLParamElement> = this.querySelectorAll('param');
const attrs = this.getAttributeNames();
const scheme = AwayPlayerComponent.BINDING_CONFIG;
params.forEach(node => {
if (node.name in scheme) {
this._runConfig[node.name] = node.value;
}
});
attrs.forEach(name => {
if (name in scheme) {
this._runConfig[name] = this.getAttribute(name);
}
});
}
_loadGlobalConfig() {
const global = <any>window;
const globalCfg = global.AWAY_EMBED_CFG;
const scheme = AwayPlayerComponent.BINDING_CONFIG;
for(const [key, value] of Object.entries(globalCfg)) {
if (key in scheme) {
this._runConfig[key] = value;
}
}
}
_setDefaults() {
const scheme = AwayPlayerComponent.BINDING_CONFIG;
for(const key in scheme) {
if (scheme[key].required && typeof this._runConfig[key] === 'undefined') {
throw `Parameter ${key} is required!`;
}
if (typeof this._runConfig[key] === 'undefined') {
this._runConfig[key] = scheme[key].default;
}
}
}
connectedCallback() {
setTimeout(() => {
this._loaderHolder = this.querySelector('div.awayfl__loader');
this._loadGlobalConfig();
this._mapAttrs();
this._setDefaults();
this._constructPlayer();
const style = document.createElement('style');
style.textContent = `
:host {
display: block;
contain: content;
width: ${this._runConfig.width};
height: ${this._runConfig.height};
}
`;
this._root.appendChild(style);
});
}
disconnectedCallback() {
this._dropPlayer();
}
attributeChangedCallback(name: string, _oldValue: any, newValue: any) {
if(!this.isConnected) {
return;
}
// if SRC changed, drop runtime and load again
if (name === 'src' && this._player) {
this._dropPlayer();
this._runConfig.src = newValue;
this._constructPlayer();
return;
}
if (name === 'width' || name === 'height') {
this._root.styleSheets[0].insertRule(`:host { ${name}: ${newValue};}`);
}
}
}
customElements.define('awayfl-player', AwayPlayerComponent);