Skip to content

Commit 913bc78

Browse files
committed
Improve plugin options
1 parent a86b4c1 commit 913bc78

File tree

2 files changed

+61
-34
lines changed

2 files changed

+61
-34
lines changed

src/plugins/ima/ima.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import './ima.css'
2-
import type { Constructable, pluginParameter } from 'shared/assets/types/types.js'
2+
import type Player from 'core/player.js'
3+
import type { Constructable } from 'shared/assets/types/types.js'
4+
5+
type pluginParameter = {
6+
player: Player
7+
options: {
8+
adTagUrl?: string
9+
adsRenderingSettings?: {
10+
restoreCustomPlaybackStateOnAdBreakComplete: boolean
11+
enablePreloading: boolean
12+
}
13+
updateImaSettings?: (settings: any) => void
14+
adTimeout?: number
15+
debug?: boolean
16+
}
17+
}
318

419
declare global {
520
interface Window {
@@ -66,7 +81,6 @@ type ImaEvent = {
6681
*/
6782
export default class ImaPlugin {
6883
player: any
69-
options: any
7084
playerIsReady: boolean
7185
sdkIsReady: boolean
7286
currentAd!: any
@@ -83,6 +97,14 @@ export default class ImaPlugin {
8397
playIsWaiting: boolean
8498
isLinearAd: boolean
8599
playerIsEnded: boolean
100+
adTagUrl: string
101+
adsRenderingSettings: {
102+
restoreCustomPlaybackStateOnAdBreakComplete: boolean
103+
enablePreloading: boolean
104+
}
105+
updateImaSettings: (settings: any) => void
106+
adTimeout: number
107+
debug: boolean
86108

87109
providers = ['html5']
88110
types = ['video']
@@ -96,18 +118,14 @@ export default class ImaPlugin {
96118
constructor({ player, options = {} }: pluginParameter) {
97119
this.player = player
98120

99-
const defaultOptions = {
100-
adTagUrl: '',
101-
adsRenderingSettings: {
102-
restoreCustomPlaybackStateOnAdBreakComplete: true,
103-
enablePreloading: true
104-
},
105-
// eslint-disable-next-line @typescript-eslint/no-empty-function
106-
updateImaSettings: () => {},
107-
adTimeout: 5000,
108-
debug: false
121+
this.adTagUrl = options.adTagUrl ?? ''
122+
this.adsRenderingSettings = options.adsRenderingSettings ?? {
123+
restoreCustomPlaybackStateOnAdBreakComplete: true,
124+
enablePreloading: true
109125
}
110-
this.options = { ...defaultOptions, ...options }
126+
this.updateImaSettings = options.updateImaSettings ?? ((_settings: any) => {})
127+
this.adTimeout = options.adTimeout ?? 5000
128+
this.debug = options.debug ?? false
111129

112130
this.playerIsReady = false
113131
this.sdkIsReady = false
@@ -154,7 +172,7 @@ export default class ImaPlugin {
154172
const script = document.createElement('script')
155173
script.defer = true
156174
script.type = 'text/javascript'
157-
script.src = `//imasdk.googleapis.com/js/sdkloader/ima3${this.options.debug ? '_debug' : ''}.js`
175+
script.src = `//imasdk.googleapis.com/js/sdkloader/ima3${this.debug ? '_debug' : ''}.js`
158176
script.onload = () => {
159177
this.sdkIsReady = true
160178
this.onPlayerAndSdkReady()
@@ -236,8 +254,8 @@ export default class ImaPlugin {
236254
initAdObjects() {
237255
this.adsLoaded = false
238256
window.google.ima.settings.setDisableCustomPlaybackForIOS10Plus(this.player.options.playsinline)
239-
if (this.options.updateImaSettings instanceof Function) {
240-
this.options.updateImaSettings(window.google.ima.settings)
257+
if (this.updateImaSettings instanceof Function) {
258+
this.updateImaSettings(window.google.ima.settings)
241259
}
242260

243261
this.adDisplayContainer = new window.google.ima.AdDisplayContainer(
@@ -266,7 +284,7 @@ export default class ImaPlugin {
266284
*/
267285
requestAds() {
268286
const adsRequest = new window.google.ima.AdsRequest()
269-
adsRequest.adTagUrl = this.options.adTagUrl
287+
adsRequest.adTagUrl = this.adTagUrl
270288
adsRequest.linearAdSlotWidth = this.player.media.clientWidth
271289
adsRequest.linearAdSlotHeight = this.player.media.clientHeight
272290
adsRequest.nonLinearAdSlotWidth = this.player.media.clientWidth
@@ -289,7 +307,7 @@ export default class ImaPlugin {
289307
window.google.ima.UiElements.AD_ATTRIBUTION,
290308
window.google.ima.UiElements.COUNTDOWN
291309
],
292-
...this.options.adsRenderingSettings
310+
...this.adsRenderingSettings
293311
}
294312
this.adsManager = adsManagerLoadedEvent.getAdsManager(this.player.media, adsRenderingSettings)
295313

@@ -505,7 +523,7 @@ export default class ImaPlugin {
505523

506524
this.timerAdTimeout = window.setTimeout(() => {
507525
this.onAdTimeoutReached()
508-
}, this.options.adTimeout)
526+
}, this.adTimeout)
509527
}
510528

511529
/**

src/plugins/sticky/sticky.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
import './sticky.css'
22
import svgClose from 'shared/assets/svgs/close.svg'
3-
import type { pluginParameter } from 'shared/assets/types/types.js'
3+
import type Player from 'core/player.js'
44

55
type WindowSizes = {
66
clientWidth: number
77
innerHeight: number
88
}
99

10+
type pluginParameter = {
11+
player: Player
12+
options: {
13+
mode?: string
14+
width?: number
15+
offset?: number
16+
ratio?: number
17+
}
18+
}
19+
1020
/**
1121
* Vlitejs Sticky plugin
1222
* @module Vlitejs/plugins/sticky
1323
*/
1424
export default class Sticky {
1525
player: any
16-
options: any
1726
closeStickyButton!: HTMLElement
1827
windowSizes: WindowSizes
1928
isSticky: boolean
@@ -22,6 +31,10 @@ export default class Sticky {
2231
isOutViewport: null | boolean
2332
observer!: IntersectionObserver
2433
resizeTimer!: number
34+
mode: string
35+
width: number
36+
offset: number
37+
ratio: number
2538

2639
providers = ['html5', 'youtube', 'dailymotion', 'vimeo']
2740
types = ['video']
@@ -34,16 +47,12 @@ export default class Sticky {
3447
*/
3548
constructor({ player, options = {} }: pluginParameter) {
3649
this.player = player
37-
this.options = options
3850

39-
const DEFAULTS = {
40-
mode: 'on',
41-
width: 400,
42-
offset: 20,
43-
ratio: 16 / 9
44-
}
51+
this.mode = options.mode ?? 'on'
52+
this.width = options.width ?? 400
53+
this.offset = options.offset ?? 20
54+
this.ratio = options.ratio ?? 16 / 9
4555

46-
this.options = { ...DEFAULTS, ...this.options }
4756
this.windowSizes = {
4857
clientWidth: document.documentElement.clientWidth,
4958
innerHeight: window.innerHeight
@@ -173,7 +182,7 @@ export default class Sticky {
173182
return (
174183
!this.stickyIsClosed &&
175184
this.isOutViewport &&
176-
(this.options.mode === 'instant' || this.isPlayerSeen)
185+
(this.mode === 'instant' || this.isPlayerSeen)
177186
)
178187
}
179188

@@ -186,11 +195,11 @@ export default class Sticky {
186195
this.isSticky = true
187196
this.player.elements.outerContainer.classList.add('v-sticky')
188197

189-
const height = this.options.width / this.options.ratio
190-
const x = this.windowSizes.clientWidth - this.options.width - this.options.offset
191-
const y = this.windowSizes.innerHeight - height - this.options.offset
198+
const height = this.width / this.ratio
199+
const x = this.windowSizes.clientWidth - this.width - this.offset
200+
const y = this.windowSizes.innerHeight - height - this.offset
192201

193-
this.player.elements.container.style.width = `${this.options.width}px`
202+
this.player.elements.container.style.width = `${this.width}px`
194203
this.player.elements.container.style.height = `${height}px`
195204

196205
this.player.elements.container.style.transform = `translate3d(${x}px, ${y}px, 0)`

0 commit comments

Comments
 (0)