start.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* For licensing terms, see /license.txt */
  2. $(function () {
  3. var parent$ = window.parent.$,
  4. $player = parent$('audio#lp_audio_media_player_html5');
  5. if ($player.length === 0) {
  6. $player = parent$('audio#lp_audio_media_player');
  7. }
  8. var player = $player.get(0),
  9. def = $.Deferred();
  10. if (!$player.length) {
  11. processText(wordsCount);
  12. return;
  13. }
  14. player.preload = 'auto';
  15. function processText(turns) {
  16. var tagEnd = '</span> ',
  17. tagStart = tagEnd + '<span class="text-highlight">',
  18. wordsPerSecond = Math.ceil(wordsCount / turns);
  19. var indexes = Object.keys(words);
  20. var output = '';
  21. for (var i = 0; i < turns; i++) {
  22. var block = indexes.slice(i * wordsPerSecond, i * wordsPerSecond + wordsPerSecond),
  23. index = block[0];
  24. if (!index) {
  25. continue;
  26. }
  27. output += tagStart + words[index];
  28. for (var j = 1; j < block.length; j++) {
  29. index = block[j];
  30. output += ' ' + words[index];
  31. }
  32. }
  33. output += tagEnd;
  34. output = output.slice(tagEnd.length);
  35. $('.page-blank').html(output);
  36. def.resolve(output);
  37. return def.promise();
  38. }
  39. player.ontimeupdate = function () {
  40. var block = Math.ceil(this.currentTime);
  41. $('.text-highlight')
  42. .removeClass('active')
  43. .filter(function (index) {
  44. return index + 1 == block;
  45. })
  46. .addClass('active');
  47. };
  48. player.onloadedmetadata = function () {
  49. var turns = Math.ceil(this.duration);
  50. processText(turns)
  51. .then(function (output) {
  52. var to = window.setTimeout(function () {
  53. player.play();
  54. window.clearTimeout(to);
  55. }, 1500);
  56. });
  57. }
  58. });