forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaption.js
More file actions
145 lines (137 loc) · 4.92 KB
/
caption.js
File metadata and controls
145 lines (137 loc) · 4.92 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
138
139
140
141
142
143
144
145
(function ($) {
AblePlayer.prototype.updateCaption = function (time) {
if (!this.usingYouTubeCaptions) {
if (this.captionsOn) {
this.$captionDiv.show();
this.showCaptions(time || this.getElapsed());
}
else if (this.$captionDiv) {
this.$captionDiv.hide();
}
}
};
// Returns the function used when a caption is clicked in the captions menu.
AblePlayer.prototype.getCaptionClickFunction = function (track) {
var thisObj = this;
return function () {
thisObj.selectedCaptions = track;
thisObj.captionLang = track.language;
thisObj.currentCaption = -1;
if (thisObj.usingYouTubeCaptions) {
if (thisObj.captionsOn) {
// captions are already on. Just need to change the language
thisObj.youTubePlayer.setOption(thisObj.ytCaptionModule, 'track', {'languageCode': thisObj.captionLang});
}
else {
// captions are off (i.e., captions module has been unloaded; need to reload it)
// user's selected language will be reset after module has successfully loaded
// (the onApiChange event will be fired -- see initialize.js > initYouTubePlayer())
thisObj.resettingYouTubeCaptions = true;
thisObj.youTubePlayer.loadModule(thisObj.ytCaptionModule);
}
}
else {
// Try and find a matching description track for rebuilding transcript
for (var ii in thisObj.descriptions) {
if (thisObj.descriptions[ii].language === track.language) {
thisObj.selectedDescriptions = thisObj.descriptions[ii];
thisObj.currentDescription = -1;
}
}
thisObj.updateCaption();
thisObj.updateDescription();
}
thisObj.captionsOn = true;
// stopgap to prevent spacebar in Firefox from reopening popup
// immediately after closing it (used in handleCaptionToggle())
thisObj.hidingPopup = true;
thisObj.captionsPopup.hide();
// Ensure stopgap gets cancelled if handleCaptionToggle() isn't called
// e.g., if user triggered button with Enter or mouse click, not spacebar
setTimeout(function() {
thisObj.hidingPopup = false;
}, 100);
thisObj.$ccButton.focus();
thisObj.refreshControls();
}
};
// Returns the function used when the "Captions Off" button is clicked in the captions tooltip.
AblePlayer.prototype.getCaptionOffFunction = function () {
var thisObj = this;
return function () {
if (thisObj.player == 'youtube') {
thisObj.youTubePlayer.unloadModule(thisObj.ytCaptionModule);
}
thisObj.captionsOn = false;
thisObj.currentCaption = -1;
// stopgap to prevent spacebar in Firefox from reopening popup
// immediately after closing it (used in handleCaptionToggle())
thisObj.hidingPopup = true;
thisObj.captionsPopup.hide();
// Ensure stopgap gets cancelled if handleCaptionToggle() isn't called
// e.g., if user triggered button with Enter or mouse click, not spacebar
setTimeout(function() {
thisObj.hidingPopup = false;
}, 100);
thisObj.$ccButton.focus();
thisObj.refreshControls();
thisObj.updateCaption();
}
};
AblePlayer.prototype.showCaptions = function(now) {
var c, thisCaption;
var cues;
if (this.selectedCaptions) {
cues = this.selectedCaptions.cues;
}
else if (this.captions.length >= 1) {
cues = this.captions[0].cues;
}
else {
cues = [];
}
for (c in cues) {
if ((cues[c].start <= now) && (cues[c].end > now)) {
thisCaption = c;
break;
}
}
if (typeof thisCaption !== 'undefined') {
if (this.currentCaption !== thisCaption) {
// it's time to load the new caption into the container div
this.$captionDiv.html(this.flattenCueForCaption(cues[thisCaption]).replace('\n', '<br>'));
this.currentCaption = thisCaption;
}
}
else {
this.$captionDiv.html('');
this.currentCaption = -1;
}
};
// Takes a cue and returns the caption text to display for it.
AblePlayer.prototype.flattenCueForCaption = function (cue) {
var result = [];
var flattenComponent = function (component) {
var result = [];
if (component.type === 'string') {
result.push(component.value);
}
else if (component.type === 'v') {
result.push('[' + component.value + ']');
for (var ii in component.children) {
result.push(flattenComponent(component.children[ii]));
}
}
else {
for (var ii in component.children) {
result.push(flattenComponent(component.children[ii]));
}
}
return result.join('');
}
for (var ii in cue.components.children) {
result.push(flattenComponent(cue.components.children[ii]));
}
return result.join('');
};
})(jQuery);