Files
edx-platform/xmodule/assets/video/public/js/00_sjson.js
Muhammad Farhan Khan 5c759f1e13 refactor: Update and migrate Video Block JS files into xmodule/assets
- Move Video Block JS files from xmodule/js/src/video/ to xmodule/assets/video/public/js/ 
- Update JavaScript files from  RequireJS to ES6 import/export
- test: Enable and fix Karma Js tests for Video XBlock (#37351)

---------

Co-authored-by: salmannawaz <salman.nawaz@arbisoft.com>
2025-10-07 19:01:50 +05:00

109 lines
2.9 KiB
JavaScript

'use strict';
let Sjson = function(data) {
let sjson = {
start: data.start.concat(),
text: data.text.concat()
},
module = {};
let getter = function(propertyName) {
return function() {
return sjson[propertyName];
};
};
let getStartTimes = getter('start');
let getCaptions = getter('text');
let size = function() {
return sjson.text.length;
};
function search(time, startTime, endTime) {
let start = getStartTimes(),
max = size() - 1,
min = 0,
results,
index;
// if we specify a start and end time to search,
// search the filtered list of captions in between
// the start / end times.
// Else, search the unfiltered list.
if (typeof startTime !== 'undefined'
&& typeof endTime !== 'undefined') {
results = filter(startTime, endTime);
start = results.start;
max = results.captions.length - 1;
} else {
start = getStartTimes();
}
while (min < max) {
index = Math.ceil((max + min) / 2);
if (time < start[index]) {
max = index - 1;
}
if (time >= start[index]) {
min = index;
}
}
return min;
}
function filter(start, end) {
/* filters captions that occur between inputs
* `start` and `end`. Start and end should
* be Numbers (doubles) corresponding to the
* number of seconds elapsed since the beginning
* of the video.
*
* Returns an object with properties
* "start" and "captions" representing
* parallel arrays of start times and
* their corresponding captions.
*/
let filteredTimes = [];
let filteredCaptions = [];
let startTimes = getStartTimes();
let captions = getCaptions();
if (startTimes.length !== captions.length) {
console.warn('video caption and start time arrays do not match in length');
}
// if end is null, then it's been set to
// some erroneous value, so filter using the
// entire array as long as it's not empty
if (end === null && startTimes.length) {
end = startTimes[startTimes.length - 1];
}
_.filter(startTimes, function(currentStartTime, i) {
if (currentStartTime >= start && currentStartTime <= end) {
filteredTimes.push(currentStartTime);
filteredCaptions.push(captions[i]);
}
});
return {
start: filteredTimes,
captions: filteredCaptions
};
}
return {
getCaptions: getCaptions,
getStartTimes: getStartTimes,
getSize: size,
filter: filter,
search: search
};
};
export default Sjson;