From 5f5a77acb104dc990c9b61189c298ea708fdb6e4 Mon Sep 17 00:00:00 2001 From: ataki Date: Mon, 16 Mar 2015 14:08:24 -0700 Subject: [PATCH] Add comment, refactor, handle edge cases --- .../xmodule/xmodule/js/src/video/00_sjson.js | 32 ++++++++++++------- .../xmodule/js/src/video/03_video_player.js | 2 +- .../js/src/video/06_video_progress_slider.js | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/common/lib/xmodule/xmodule/js/src/video/00_sjson.js b/common/lib/xmodule/xmodule/js/src/video/00_sjson.js index 2ba222d6db..6315f2d1e3 100644 --- a/common/lib/xmodule/xmodule/js/src/video/00_sjson.js +++ b/common/lib/xmodule/xmodule/js/src/video/00_sjson.js @@ -62,15 +62,24 @@ function() { }; 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. + */ var filteredTimes = []; var filteredCaptions = []; var startTimes = getStartTimes(); var captions = getCaptions(); - var currentStartTime; - var i; if (startTimes.length !== captions.length) { - return []; + throw new Exception("video caption and start time arrays do not match in length"); } // if end is null, then it's been set to @@ -80,17 +89,16 @@ function() { end = startTimes[startTimes.length - 1]; } - for (i = 0; i < startTimes.length; i++) { - currentStartTime = startTimes[i]; - if (currentStartTime >= start && currentStartTime <= end) { - filteredTimes.push(currentStartTime); - filteredCaptions.push(captions[i]); - } - } + _.filter(startTimes, function(currentStartTime, i) { + if (currentStartTime >= start && currentStartTime <= end) { + filteredTimes.push(currentStartTime); + filteredCaptions.push(captions[i]); + } + }); return { - 'start': filteredTimes, - 'captions': filteredCaptions + 'start': filteredTimes, + 'captions': filteredCaptions }; } diff --git a/common/lib/xmodule/xmodule/js/src/video/03_video_player.js b/common/lib/xmodule/xmodule/js/src/video/03_video_player.js index a95b9f2605..12b40c123c 100644 --- a/common/lib/xmodule/xmodule/js/src/video/03_video_player.js +++ b/common/lib/xmodule/xmodule/js/src/video/03_video_player.js @@ -818,7 +818,7 @@ function (HTML5Video, Resizer) { endTime = this.videoPlayer.duration(), youTubeId; - if (this.config.endTime !== null) { + if (this.config.endTime) { endTime = Math.min(this.config.endTime, endTime); } diff --git a/common/lib/xmodule/xmodule/js/src/video/06_video_progress_slider.js b/common/lib/xmodule/xmodule/js/src/video/06_video_progress_slider.js index 1fdaba37e2..61da5b7ce0 100644 --- a/common/lib/xmodule/xmodule/js/src/video/06_video_progress_slider.js +++ b/common/lib/xmodule/xmodule/js/src/video/06_video_progress_slider.js @@ -166,7 +166,7 @@ function () { var time = ui.value, endTime = this.videoPlayer.duration(); - if (this.config.endTime !== null) { + if (this.config.endTime) { endTime = Math.min(this.config.endTime, endTime); }