-
Notifications
You must be signed in to change notification settings - Fork 15
/
demo.js
292 lines (238 loc) · 7.91 KB
/
demo.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
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
$(function() {
window.players = {};
var currentPageUrl = window.location.href.split('?')[0];
var embedCount;
var defaults;
var refNodeRoot = $('#ref-node-root');
var form = $('#embed-it');
var paramFields = {
accountId: $('#account-id'),
adConfigId: $('#ad-config-id'),
applicationId: $('#app-id'),
deliveryConfigId: $('#delivery-config-id'),
embedId: $('#embed-id'),
embedType: $('#embed-type'),
playerId: $('#player-id'),
mediaType: $('#media-type'),
mediaValue: $('#media-value'),
options: $('#vjs-options'),
poster: $('#poster-value')
};
var allowedParams = {
accountId: 1,
adConfigId: 1,
applicationId: 1,
catalogSearch: 1,
catalogSequence: 1,
deliveryConfigId: 1,
embedId: 1,
embedOptions: {
pip: 1,
playlist: {
legacy: 1
},
responsive: {
aspectRatio: 1,
iframeHorizontalPlaylist: 1,
maxWidth: 1
},
tagName: 1,
unminified: 1
},
embedType: 1,
playerId: 1,
playlistId: 1,
poster: 1,
videoId: 1
};
var embedOptionsFields = {
aspectRatio: $('#eo-resp-ar'),
iframeHorizontalPlaylist: $('#eo-resp-ihp'),
maxWidth: $('#eo-resp-mw'),
pip: $('#eo-pip'),
playlist: $('#eo-playlist'),
responsive: $('#eo-resp'),
tagName: $('#eo-tag-name'),
unminified: $('#eo-unmin')
};
function isObj(v) {
return Object.prototype.toString.call(v) === '[object Object]';
}
function parseFields(fields) {
return Object.keys(fields).reduce(function(accum, key) {
var field = fields[key];
if (field.is(':checkbox')) {
accum[key] = field.is(':checked');
} else {
accum[key] = (field.val() || field.attr('placeholder') || '').trim();
}
return accum;
}, {});
}
function normalizeParams(params) {
// Normalize media type/value
if (params.mediaType && params.mediaValue) {
try {
params[params.mediaType] = JSON.parse(params.mediaValue);
} catch (x) {
params[params.mediaType] = params.mediaValue;
}
}
delete params.mediaType;
delete params.mediaValue;
// Normalize Video.js options
if (params.options) {
try {
params.options = JSON.parse(params.options);
} catch (x) {
console.warn('Ignoring invalid JSON in Video.js options!');
console.error(x);
}
}
if (!params.options || typeof params.options !== 'object') {
delete params.options;
}
// Normalize embedOptions
var eo = params.embedOptions;
if (eo.playlist === 'on') {
eo.playlist = true;
} else if (eo.playlist === 'legacy') {
eo.playlist = {legacy: true};
} else {
eo.playlist = false;
}
if (eo.responsive) {
if (eo.iframeHorizontalPlaylist || eo.maxWidth || eo.aspectRatio !== '16:9') {
eo.responsive = {
aspectRatio: eo.aspectRatio
};
if (eo.iframeHorizontalPlaylist && params.embedType === 'iframe') {
eo.responsive.iframeHorizontalPlaylist = true;
}
if (eo.maxWidth) {
eo.responsive.maxWidth = eo.maxWidth;
}
}
}
delete eo.aspectRatio;
delete eo.iframeHorizontalPlaylist;
delete eo.maxWidth;
return params;
}
function filterParams(params, allowed) {
allowed = allowed || allowedParams;
Object.keys(params).forEach(function(key) {
if (!allowed.hasOwnProperty(key)) {
delete params[key];
} else if (isObj(params[key]) && isObj(allowed[key])) {
params[key] = filterParams(params[key], allowed[key]);
}
});
return params;
}
function readParams() {
var params = parseFields(paramFields);
params.embedOptions = parseFields(embedOptionsFields);
normalizeParams(params);
return params;
}
function removeDefaults(params, customDefaults) {
customDefaults = customDefaults || defaults;
Object.keys(customDefaults).forEach(function(key) {
if (customDefaults.hasOwnProperty(key) && params[key] === customDefaults[key]) {
delete params[key];
}
});
if (params.embedOptions && customDefaults.embedOptions) {
removeDefaults(params.embedOptions, customDefaults.embedOptions);
if (!Object.keys(params.embedOptions).length) {
delete params.embedOptions;
}
}
return params;
}
function createEmbed(params) {
var refNode = $('<div class="list-group-item"></div>');
embedCount += 1;
refNode.append([
'<p>',
'<a class="btn btn-secondary btn-remove-player" href="#">Remove</a>',
' ',
'<a class="btn btn-secondary btn-toggle-params" href="#" aria-expanded="false" aria-controls="toggle-params-' + embedCount + '" data-toggle="collapse" data-target="#toggle-params-' + embedCount + '">Params</a>',
' ',
'<a class="btn btn-secondary btn-toggle-share" href="#" aria-expanded="false" aria-controls="toggle-share-' + embedCount + '" data-toggle="collapse" data-target="#toggle-share-' + embedCount + '">Share</a>',
'</p>',
'<div id="toggle-params-' + embedCount + '" class="collapse">',
'<pre class="alert alert-secondary"><code>',
JSON.stringify(params, null, 2),
'</code></pre>',
'</div>',
'<div id="toggle-share-' + embedCount + '" class="collapse">',
'<div class="alert alert-secondary">',
'<p>Copy the following URL to share this page with this embed!</p>',
'<code>',
currentPageUrl, '?params=', window.encodeURIComponent(JSON.stringify(params)),
'</code>',
'</div>',
'</div>'
].join(''));
refNodeRoot.append(refNode);
params.refNode = refNode.get(0);
console.log('creating embed with params', params);
brightcovePlayerLoader(params).then(function(success) {
if (success.type !== brightcovePlayerLoader.EMBED_TYPE_IN_PAGE) {
return;
}
var player = success.ref;
window.players[(params.playerId || 'default') + '_' + embedCount] = player;
refNode.find('.btn-remove-player').data({player});
// When an in-page player is disposed, we need to clean up its
// surrounding DOM elements. This needs to wait a tick, because
// the player will remove its own DOM element first.
player.on('dispose', function() {
var listGroupItem = $(player.el()).closest('.list-group-item');
delete window.players[ + embedCount];
window.setTimeout(function() {
if (listGroupItem.length) {
listGroupItem.remove();
}
}, 1);
});
var bc = window.bc;
var major = bc && bc.VERSION ? Number(bc.VERSION.split('.')[0]) : 0;
if (major < 6) {
$(player.el()).before('<div class="alert alert-warning" role="alert">This player appears to be an unsupported version! Brightcove Player Loader supports v6.0.0 and higher for in-page embeds.</div>');
}
}, function(err) {
refNode.append('<div class="alert alert-danger" role="alert">' + err + '</div>');
});
}
embedCount = 0;
defaults = readParams();
delete defaults.accountId;
console.log('defaults', defaults);
try {
createEmbed(filterParams(JSON.parse(Qs.parse(window.location.search.substring(1)).params)));
} catch (x) {}
form.on('submit', function(e) {
e.preventDefault();
createEmbed(removeDefaults(readParams()));
});
refNodeRoot.on('click', '.btn-remove-player', function(e) {
var btn = $(this);
e.preventDefault();
// In-page players need to be disposed, iframe players can simply be
// removed from the DOM.
if (btn.data('player')) {
btn.data('player').dispose();
} else {
btn.closest('.list-group-item').remove();
}
// If there are no more players on the page, go ahead and reset the
// global environment.
if (!Object.keys(window.players).length) {
brightcovePlayerLoader.reset();
console.log('reset global state');
}
});
});