Skip to content

Commit 62fc37b

Browse files
committed
Plugin de Ads - Parte 2
1 parent 3e15836 commit 62fc37b

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

assets/MediaPlayer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
class MediaPlayer {
22
media: HTMLMediaElement;
33
plugins: Array<any>;
4+
container: HTMLElement;
45

56
constructor(config) {
67
this.media = config.el;
78
this.plugins = config.plugins || [];
9+
this.initPlayer();
810
this.initPlugins();
911
}
1012

13+
initPlayer() {
14+
this.container = document.createElement('div');
15+
this.container.style.position = 'relative';
16+
this.media.parentNode.insertBefore(this.container, this.media);
17+
this.container.appendChild(this.media);
18+
}
19+
1120
private initPlugins() {
1221
this.plugins.forEach(plugin => {
1322
plugin.run(this);

assets/plugins/Ads/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ class AdsPlugin {
66
private player: MediaPlayer;
77
private media: HTMLMediaElement;
88
private currentAd: Ad;
9+
private adsContainer: HTMLElement;
910

1011
constructor() {
1112
this.ads = Ads.getInstance();
13+
this.adsContainer = document.createElement('div');
1214
this.handleTimeUpdate = this.handleTimeUpdate.bind(this);
1315
}
1416

1517
run(player: MediaPlayer) {
1618
this.player = player;
19+
this.player.container.appendChild(this.adsContainer);
1720
this.media = this.player.media;
1821
this.media.addEventListener('timeupdate', this.handleTimeUpdate);
1922
}
@@ -32,7 +35,22 @@ class AdsPlugin {
3235

3336
const ad = this.ads.getAd();
3437
this.currentAd = ad;
35-
console.log(this.currentAd);
38+
this.adsContainer.innerHTML = `
39+
<div class="ads">
40+
<a class="ads__link" href="${this.currentAd.url}" target="_blank">
41+
<img class="ads__img" src="${this.currentAd.imageUrl}" />
42+
<div class="ads__info">
43+
<h5 class="ads__title">${this.currentAd.title}</h5>
44+
<p class="ads__body">${this.currentAd.body}</p>
45+
</div>
46+
</a>
47+
</div>
48+
`;
49+
50+
setTimeout(() => {
51+
this.currentAd = null;
52+
this.adsContainer.innerHTML = '';
53+
}, 10000);
3654
}
3755
}
3856

0 commit comments

Comments
 (0)