forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
172 lines (152 loc) · 6.06 KB
/
search.js
File metadata and controls
172 lines (152 loc) · 6.06 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
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
(function ($) {
AblePlayer.prototype.showSearchResults = function() {
// search VTT file for all instances of searchTerms
// Currently just supports search terms separated with one or more spaces
// TODO: Add support for more robust search syntax:
// Search terms wrapped in quotation marks ("") must occur exactly as they appear in the quotes
// Search terms with an attached minus sign (e.g., -term) are to be excluded from results
// Boolean AND/OR operators
// ALSO: Add localization support
var thisObj = this;
if (this.searchDiv && this.searchString) {
if ($('#' + this.SearchDiv)) {
var resultsArray = this.searchFor(this.searchString);
if (resultsArray.length > 0) {
var resultsSummary = $('<p>',{
'class': 'able-search-results-summary'
});
var resultsSummaryText = 'Found <strong>' + resultsArray.length + '</strong> matching items. ';
resultsSummaryText += 'Click the time associated with any item ';
resultsSummaryText += 'to play the video from that point.';
resultsSummary.html(resultsSummaryText);
var resultsList = $('<ul>');
for (var i in resultsArray) {
var resultsItem = $('<li>',{
});
var itemStartTime = this.secondsToTime(resultsArray[i]['start']);
var itemStartSpan = $('<span>',{
'class': 'able-search-results-time',
'data-start': resultsArray[i]['start'],
'title': itemStartTime['title'],
'tabindex': '0'
});
itemStartSpan.text(itemStartTime['value']);
// add a listener for clisk on itemStart
itemStartSpan.click(function(event) {
var spanStart = parseFloat($(this).attr('data-start'));
// Add a tiny amount so that we're inside the span.
spanStart += .01;
thisObj.seeking = true;
thisObj.seekTo(spanStart);
});
var itemText = $('<span>',{
'class': 'able-search-result-text'
})
itemText.html('...' + resultsArray[i]['caption'] + '...');
resultsItem.append(itemStartSpan, itemText);
resultsList.append(resultsItem);
}
$('#' + this.searchDiv).append(resultsSummary, resultsList);
}
else {
var noResults = $('<p>').text('No results found.');
$('#' + this.searchDiv).append(noResults);
}
}
}
};
AblePlayer.prototype.searchFor = function(searchString) {
// return chronological array of caption cues that match searchTerms
var captionLang, captions, results, caption, c, i, j;
// split searchTerms into an array
var searchTerms = searchString.split(' ');
if (this.captions.length > 0) {
captionLang = this.captions[0].language; // in case it's needed later
captions = this.captions[0].cues;
if (captions.length > 0) {
var results = [];
c = 0;
for (i in captions) {
if (captions[i].components.children[0]['type'] === 'string') {
caption = captions[i].components.children[0]['value'];
for (j in searchTerms) {
if (caption.indexOf(searchTerms[j]) !== -1) {
results[c] = [];
results[c]['start'] = captions[i].start;
results[c]['caption'] = this.highlightSearchTerm(searchTerms,j,caption);
c++;
break;
}
}
}
}
}
}
return results;
};
AblePlayer.prototype.highlightSearchTerm = function(searchTerms, index, resultString) {
// highlight ALL found searchTerms in the current resultString
// index is the first index in the searchTerm array where a match has already been found
// Need to step through the remaining terms to see if they're present as well
var i, searchTerm, termIndex, termLength, str1, str2, str3;
for (i=index; i<searchTerms.length; i++) {
searchTerm = searchTerms[i];
termIndex = resultString.indexOf(searchTerm);
if (termIndex !== -1) {
termLength = searchTerm.length;
if (termLength > 0) {
str1 = resultString.substring(0, termIndex);
str2 = '<span class="able-search-term">' + searchTerm + '</span>';
str3 = resultString.substring(termIndex+termLength);
resultString = str1 + str2 + str3;
}
else {
str1 = '<span class="able-search-term">' + searchTerm + '</span>';
str2 = resultString.substring(termIndex+termLength);
resultString = str1 + str2;
}
}
}
return resultString;
};
AblePlayer.prototype.secondsToTime = function(totalSeconds) {
// return an array of totalSeconds converted into two formats
// time['value'] = HH:MM:SS with hours dropped if there are none
// time['title'] = a speakable rendering, so speech rec users can easily speak the link
// first, round down to nearest second
var totalSeconds = Math.floor(totalSeconds);
var hours = parseInt( totalSeconds / 3600 ) % 24;
var minutes = parseInt( totalSeconds / 60 ) % 60;
var seconds = totalSeconds % 60;
var value = '';
var title = '';
if (hours > 0) {
value += hours + ':';
title + hours + ' hours ';
}
if (minutes < 10) {
value += '0' + minutes + ':';
if (minutes > 0) {
title += minutes + ' minutes ';
}
}
else {
value += minutes + ':';
title += minutes + ' minutes ';
}
if (seconds < 10) {
value += '0' + seconds;
if (seconds > 0) {
title += seconds + ' seconds ';
}
}
else {
value += seconds;
title += seconds + ' seconds ';
}
var time = [];
time['value'] = value;
time['title'] = title;
return time;
};
})(jQuery);