forked from boronia/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.js
More file actions
137 lines (127 loc) · 3.91 KB
/
metadata.js
File metadata and controls
137 lines (127 loc) · 3.91 KB
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
var jQuery = require("jquery");
(function ($) {
AblePlayer.prototype.updateMeta = function (time) {
if (this.hasMeta) {
if (this.metaType === 'text') {
this.$metaDiv.show();
this.showMeta(time || this.elapsed);
}
else {
this.showMeta(time || this.elapsed);
}
}
};
AblePlayer.prototype.showMeta = function(now) {
var tempSelectors, m, thisMeta,
cues, cueText, cueLines, i, line,
showDuration, focusTarget;
tempSelectors = [];
if (this.meta.length >= 1) {
cues = this.meta;
}
else {
cues = [];
}
for (m = 0; m < cues.length; m++) {
if ((cues[m].start <= now) && (cues[m].end > now)) {
thisMeta = m;
break;
}
}
if (typeof thisMeta !== 'undefined') {
if (this.currentMeta !== thisMeta) {
if (this.metaType === 'text') {
// it's time to load the new metadata cue into the container div
this.$metaDiv.html(this.flattenCueForMeta(cues[thisMeta]).replace('\n', '<br>'));
}
else if (this.metaType === 'selector') {
// it's time to show content referenced by the designated selector(s)
cueText = this.flattenCueForMeta(cues[thisMeta]);
cueLines = cueText.split('\n');
for (i=0; i<cueLines.length; i++) {
line = $.trim(cueLines[i]);
if (line.toLowerCase().trim() === 'pause') {
// don't show big play button when pausing via metadata
this.hideBigPlayButton = true;
this.pauseMedia();
}
else if (line.toLowerCase().substring(0,6) == 'focus:') {
focusTarget = line.substring(6).trim();
if ($(focusTarget).length) {
$(focusTarget).focus();
}
}
else {
if ($(line).length) {
// selector exists
this.currentMeta = thisMeta;
showDuration = parseInt($(line).attr('data-duration'));
if (typeof showDuration !== 'undefined' && !isNaN(showDuration)) {
$(line).show().delay(showDuration).fadeOut();
}
else {
// no duration specified. Just show the element until end time specified in VTT file
$(line).show();
}
// add to array of visible selectors so it can be hidden at end time
this.visibleSelectors.push(line);
tempSelectors.push(line);
}
}
}
// now step through this.visibleSelectors and remove anything that's stale
if (this.visibleSelectors && this.visibleSelectors.length) {
if (this.visibleSelectors.length !== tempSelectors.length) {
for (i=this.visibleSelectors.length-1; i>=0; i--) {
if ($.inArray(this.visibleSelectors[i],tempSelectors) == -1) {
$(this.visibleSelectors[i]).hide();
this.visibleSelectors.splice(i,1);
}
}
}
}
}
}
}
else {
// there is currently no metadata. Empty stale content
if (typeof this.$metaDiv !== 'undefined') {
this.$metaDiv.html('');
}
if (this.visibleSelectors && this.visibleSelectors.length) {
for (i=0; i<this.visibleSelectors.length; i++) {
$(this.visibleSelectors[i]).hide();
}
// reset array
this.visibleSelectors = [];
}
this.currentMeta = -1;
}
};
// Takes a cue and returns the metadata text to display for it.
AblePlayer.prototype.flattenCueForMeta = function (cue) {
var result = [];
var flattenComponent = function (component) {
var result = [], ii;
if (component.type === 'string') {
result.push(component.value);
}
else if (component.type === 'v') {
result.push('[' + component.value + ']');
for (ii = 0; ii < component.children.length; ii++) {
result.push(flattenComponent(component.children[ii]));
}
}
else {
for (ii = 0; ii < component.children.length; ii++) {
result.push(flattenComponent(component.children[ii]));
}
}
return result.join('');
}
for (var ii = 0; ii < cue.components.children.length; ii++) {
result.push(flattenComponent(cue.components.children[ii]));
}
return result.join('');
};
})(jQuery);