-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
149 lines (123 loc) · 4.08 KB
/
events.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
function wikiaJWPlayerEvents(playerInstance, willAutoplay, logger) {
var state = getNewState(),
wasAlreadyUnmuted = false,
depth = 0,
prefixes = {
ad: 'ad',
video: 'video'
},
isPlayerPaused = false;
/**
* returns default state for video/ad
* @return {{wasFirstQuartileTriggered: boolean, wasMidPointTriggered: boolean, wasThirdQuartileTriggered: boolean, progress: {durationWatched: number, percentWatched: number}}}
*/
function getDefaultState() {
return {
wasFirstQuartileTriggered: false,
wasMidPointTriggered: false,
wasThirdQuartileTriggered: false,
progress: {
durationWatched: 0,
percentWatched: 0
}
}
}
/**
* retruns new state for ad and video
* @return {{ad: {wasFirstQuartileTriggered: boolean, wasMidPointTriggered: boolean, wasThirdQuartileTriggered: boolean, progress: {durationWatched: number, percentWatched: number}}, video: {wasFirstQuartileTriggered: boolean, wasMidPointTriggered: boolean, wasThirdQuartileTriggered: boolean, progress: {durationWatched: number, percentWatched: number}}}}
*/
function getNewState() {
return {
ad: getDefaultState(),
video: getDefaultState()
}
}
/**
* triggers video/ad time-based events
* @param prefix
* @param data
*/
function handleTime(prefix, data) {
var positionFloor = Math.floor(data.position),
percentPlayed = Math.floor(positionFloor * 100 / data.duration),
playlistItem = playerInstance.getPlaylistItem();
if (percentPlayed > 100 && playlistItem) {
data.mediaId = playlistItem.mediaid;
logger.error('played-percentage', data);
}
if (positionFloor > state[prefix].progress.durationWatched && positionFloor % 5 === 0) {
playerInstance.trigger(prefix + 'SecondsPlayed', { value: positionFloor });
state[prefix].progress.durationWatched = positionFloor;
}
if (percentPlayed >= 25 && !state[prefix].wasFirstQuartileTriggered) {
playerInstance.trigger(prefix + 'FirstQuartile');
state[prefix].wasFirstQuartileTriggered = true;
}
if (percentPlayed >= 50 && !state[prefix].wasMidPointTriggered) {
playerInstance.trigger(prefix + 'MidPoint');
state[prefix].wasMidPointTriggered = true;
}
if (percentPlayed >= 75 && !state[prefix].wasThirdQuartileTriggered) {
playerInstance.trigger(prefix + 'ThirdQuartile');
state[prefix].wasThirdQuartileTriggered = true;
}
if (percentPlayed > state[prefix].progress.percentWatched && percentPlayed % 10 === 0) {
playerInstance.trigger(prefix + 'PercentPlayed', { value: percentPlayed });
state[prefix].progress.percentWatched = percentPlayed;
}
}
logger.info('before ready');
playerInstance.once('relatedReady', function () {
logger.info('player ready');
var relatedPlugin = playerInstance.getPlugin('related');
relatedPlugin.on('open', function () {
logger.info('related plugin open');
playerInstance.trigger('relatedVideoImpression');
state[prefixes.video] = getDefaultState();
});
relatedPlugin.on('play', function (data) {
depth++;
playerInstance.trigger('relatedVideoPlay', {
auto: data.auto,
item: data.item,
position: data.position,
depth: depth
});
});
});
playerInstance.on('play', function (data) {
if (isPlayerPaused) {
playerInstance.trigger('videoResumed', data);
}
isPlayerPaused = false;
});
playerInstance.on('pause', function () {
isPlayerPaused = true;
});
playerInstance.on('firstFrame', function () {
if (depth === 0) {
playerInstance.trigger('playerStart', { auto: willAutoplay });
}
playerInstance.trigger('videoStart');
});
playerInstance.on('mute', function () {
if (!playerInstance.getMute() && !wasAlreadyUnmuted) {
playerInstance.trigger('firstUnmute');
wasAlreadyUnmuted = true;
}
});
playerInstance.on('time', function (data) {
handleTime(prefixes.video, data);
});
playerInstance.on('adTime', function (data) {
handleTime(prefixes.ad, data);
});
playerInstance.on('adRequest', function () {
state[prefixes.ad] = getDefaultState();
});
playerInstance.on('onScrollStateChanged', function (data) {
if (data.state === 'closed') {
playerInstance.trigger('jwplayerClosed')
}
});
}