-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathsearch.js
More file actions
205 lines (195 loc) · 7.45 KB
/
search.js
File metadata and controls
205 lines (195 loc) · 7.45 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
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
import $ from 'jquery';
import DOMPurify from 'dompurify';
function addSearchFunctions(AblePlayer) {
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) {
// sanitize search string
var cleanSearchString = DOMPurify.sanitize(this.searchString);
if ($("#" + this.SearchDiv)) {
var searchStringHtml = "<p>" + this.translate( 'resultsSummary1', 'You searched for:') + ' ';
searchStringHtml +=
'<span id="able-search-term-echo">' + cleanSearchString + "</span>";
searchStringHtml += "</p>";
var resultsArray = this.searchFor(
cleanSearchString,
this.searchIgnoreCaps
);
if (resultsArray.length > 0) {
var $resultsSummary = $("<p>", {
class: "able-search-results-summary",
});
var resultsSummaryText = this.translate( 'resultsSummary2', 'Found %1 matching items.', [ '<strong>' + resultsArray.length + '</strong>' ] );
resultsSummaryText += ' ' + this.translate( 'resultsSummary3', 'Click the time associated with any item to play the video from that point.' );
$resultsSummary.html( resultsSummaryText );
var $resultsList = $("<ul>");
for (var i = 0; i < resultsArray.length; i++) {
var resultId = "aria-search-result-" + i;
var $resultsItem = $("<li>", {});
var itemStartTime = this.secondsToTime(resultsArray[i]["start"]);
var itemLabel =
this.translate( 'searchButtonLabel', 'Play at %1', [ itemStartTime["title"] ] );
var itemStartSpan = $("<button>", {
class: "able-search-results-time",
"data-start": resultsArray[i]["start"],
title: itemLabel,
"aria-label": itemLabel,
"aria-describedby": resultId,
});
itemStartSpan.text(itemStartTime["value"]);
// add a listener for clisk on itemStart
itemStartSpan.on("click", function (e) {
thisObj.seekTrigger = "search";
var spanStart = parseFloat($(this).attr("data-start"));
// Add a tiny amount so that we're inside the span.
spanStart += 0.01;
thisObj.seeking = true;
thisObj.seekTo(spanStart);
});
var itemText = $("<span>", {
class: "able-search-result-text",
id: resultId,
});
itemText.html('...' + resultsArray[i]["caption"] + '...');
$resultsItem.append(itemStartSpan, itemText);
$resultsList.append($resultsItem);
}
$('#' + this.searchDiv)
.html(searchStringHtml)
.append($resultsSummary, $resultsList);
} else {
var noResults = $('<p>').text( this.translate( 'noResultsFound', 'No results found.' ) );
$('#' + this.searchDiv)
.html(searchStringHtml)
.append(noResults);
}
}
}
};
AblePlayer.prototype.searchFor = function (searchString, ignoreCaps) {
// return chronological array of caption cues that match searchTerms
var captionLang, captions, results, caption, c, i, j;
results = [];
// split searchTerms into an array
var searchTerms = searchString.split(" ");
if (this.captions.length > 0) {
// Get caption track that matches this.searchLang
for (i = 0; i < this.captions.length; i++) {
if (this.captions[i].language === this.searchLang) {
captionLang = this.searchLang;
captions = this.captions[i].cues;
}
}
if (captions.length > 0) {
c = 0;
for (i = 0; i < captions.length; i++) {
if (
$.inArray(captions[i].components.children[0]["type"], [
"string",
"i",
"b",
"u",
"v",
"c",
]) !== -1
) {
caption = this.flattenCueForCaption(captions[i]);
var captionNormalized = ignoreCaps
? caption.toLowerCase()
: caption;
for (j = 0; j < searchTerms.length; j++) {
var searchTermNormalized = ignoreCaps
? searchTerms[j].toLowerCase()
: searchTerms[j];
if (captionNormalized.indexOf(searchTermNormalized) !== -1) {
results[c] = [];
results[c]["start"] = captions[i].start;
results[c]["lang"] = captionLang;
results[c]["caption"] = this.highlightSearchTerm(
searchTerms,
caption
);
c++;
break;
}
}
}
}
}
}
return results;
};
AblePlayer.prototype.highlightSearchTerm = function (
searchTerms,
resultString
) {
// highlight ALL found searchTerms in the current resultString
// Need to step through the remaining terms to see if they're present as well
searchTerms.forEach(function (searchTerm) {
var reg = new RegExp(searchTerm, "gi");
resultString = resultString.replace(
reg,
'<span class="able-search-term">$&</span>'
);
});
return resultString;
};
AblePlayer.prototype.secondsToTime = function (totalSecondsFloat) {
// 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(totalSecondsFloat);
var hours = parseInt(totalSeconds / 3600, 10) % 24;
var minutes = parseInt(totalSeconds / 60, 10) % 60;
var seconds = totalSeconds % 60;
var value = "";
var title = "";
if (hours > 0) {
value += hours + ":";
if (hours == 1) {
title += "1 " + this.translate( 'hour', 'hour' ) + " ";
} else {
title += hours + " " + this.translate( 'hours', 'hours' ) + " ";
}
}
if (minutes < 10) {
value += "0" + minutes + ":";
if (minutes > 0) {
if (minutes == 1) {
title += "1 " + this.translate( 'minute', 'minute' ) + " ";
} else {
title += minutes + " " + this.translate( 'minutes', 'minutes' ) + " ";
}
}
} else {
value += minutes + ":";
title += minutes + " " + this.translate( 'minutes', 'minutes' ) + " ";
}
if (seconds < 10) {
value += "0" + seconds;
if (seconds > 0) {
if (seconds == 1) {
title += "1 " + this.translate( 'second', 'second' ) + " ";
} else {
title += seconds + " " + this.translate( 'seconds', 'seconds' ) + " ";
}
}
} else {
value += seconds;
title += seconds + " " + this.translate( 'seconds', 'seconds' ) + " ";
}
var time = [];
time["value"] = value;
time["title"] = title;
return time;
};
}
export default addSearchFunctions;