popcorn.subtitle.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // PLUGIN: Subtitle
  2. (function ( Popcorn ) {
  3. var i = 0,
  4. createDefaultContainer = function( context, id ) {
  5. var ctxContainer = context.container = document.createElement( "div" ),
  6. style = ctxContainer.style,
  7. media = context.media;
  8. var updatePosition = function() {
  9. var position = context.position();
  10. // the video element must have height and width defined
  11. style.fontSize = "18px";
  12. style.width = media.offsetWidth + "px";
  13. style.top = position.top + media.offsetHeight - ctxContainer.offsetHeight - 40 + "px";
  14. style.left = position.left + "px";
  15. setTimeout( updatePosition, 10 );
  16. };
  17. ctxContainer.id = id || Popcorn.guid();
  18. style.position = "absolute";
  19. style.color = "white";
  20. style.textShadow = "black 2px 2px 6px";
  21. style.fontWeight = "bold";
  22. style.textAlign = "center";
  23. updatePosition();
  24. context.media.parentNode.appendChild( ctxContainer );
  25. return ctxContainer;
  26. };
  27. /**
  28. * Subtitle popcorn plug-in
  29. * Displays a subtitle over the video, or in the target div
  30. * Options parameter will need a start, and end.
  31. * Optional parameters are target and text.
  32. * Start is the time that you want this plug-in to execute
  33. * End is the time that you want this plug-in to stop executing
  34. * Target is the id of the document element that the content is
  35. * appended to, this target element must exist on the DOM
  36. * Text is the text of the subtitle you want to display.
  37. *
  38. * @param {Object} options
  39. *
  40. * Example:
  41. var p = Popcorn('#video')
  42. .subtitle({
  43. start: 5, // seconds, mandatory
  44. end: 15, // seconds, mandatory
  45. text: 'Hellow world', // optional
  46. target: 'subtitlediv', // optional
  47. } )
  48. *
  49. */
  50. Popcorn.plugin( "subtitle" , {
  51. manifest: {
  52. about: {
  53. name: "Popcorn Subtitle Plugin",
  54. version: "0.1",
  55. author: "Scott Downe",
  56. website: "http://scottdowne.wordpress.com/"
  57. },
  58. options: {
  59. start: {
  60. elem: "input",
  61. type: "text",
  62. label: "Start"
  63. },
  64. end: {
  65. elem: "input",
  66. type: "text",
  67. label: "End"
  68. },
  69. target: "subtitle-container",
  70. text: {
  71. elem: "input",
  72. type: "text",
  73. label: "Text"
  74. }
  75. }
  76. },
  77. _setup: function( options ) {
  78. var newdiv = document.createElement( "div" );
  79. newdiv.id = "subtitle-" + i++;
  80. newdiv.style.display = "none";
  81. // Creates a div for all subtitles to use
  82. ( !this.container && ( !options.target || options.target === "subtitle-container" ) ) &&
  83. createDefaultContainer( this );
  84. // if a target is specified, use that
  85. if ( options.target && options.target !== "subtitle-container" ) {
  86. // In case the target doesn't exist in the DOM
  87. options.container = document.getElementById( options.target ) || createDefaultContainer( this, options.target );
  88. } else {
  89. // use shared default container
  90. options.container = this.container;
  91. }
  92. document.getElementById( options.container.id ) && document.getElementById( options.container.id ).appendChild( newdiv );
  93. options.innerContainer = newdiv;
  94. options.showSubtitle = function() {
  95. options.innerContainer.innerHTML = options.text || "";
  96. };
  97. },
  98. /**
  99. * @member subtitle
  100. * The start function will be executed when the currentTime
  101. * of the video reaches the start time provided by the
  102. * options variable
  103. */
  104. start: function( event, options ){
  105. options.innerContainer.style.display = "inline";
  106. options.showSubtitle( options, options.text );
  107. },
  108. /**
  109. * @member subtitle
  110. * The end function will be executed when the currentTime
  111. * of the video reaches the end time provided by the
  112. * options variable
  113. */
  114. end: function( event, options ) {
  115. options.innerContainer.style.display = "none";
  116. options.innerContainer.innerHTML = "";
  117. },
  118. _teardown: function ( options ) {
  119. options.container.removeChild( options.innerContainer );
  120. }
  121. });
  122. })( Popcorn );