start.js 1.9 KB

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