MediaElementKeyActionHandler.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import $ from 'jquery'
  2. import { findKey } from 'lodash'
  3. const KeyCodes = {
  4. ENTER: 13,
  5. ESC: 27,
  6. SPACE: 32,
  7. LEFT: 37,
  8. UP: 38,
  9. RIGHT: 39,
  10. DOWN: 40,
  11. PAGE_UP: 33,
  12. PAGE_DOWN: 34,
  13. M: 77,
  14. F: 70,
  15. // Google TV
  16. G_REWIND: 227,
  17. G_FORWARD: 228,
  18. };
  19. const controlSelectors = {
  20. captions: '.mejs-captions-button',
  21. fullscreen: '.mejs-fullscreen-button',
  22. playpause: '.mejs-playpause-button',
  23. progress: '.mejs-time-rail',
  24. source: '.mejs-sourcechooser-button',
  25. speed: '.mejs-speed-button',
  26. volume: '.mejs-volume-button'
  27. };
  28. // helper to find the index of the first checked option
  29. function focusPosition (optionElements, checkedFunction) {
  30. const checkedOption = optionElements.filter(checkedFunction).first();
  31. const focusPos = optionElements.index(checkedOption);
  32. return focusPos < 0 ? 0 : focusPos;
  33. }
  34. function MediaElementKeyActionHandler (mejs, player, media, event) {
  35. this.player = player;
  36. this.media = media;
  37. this.event = event;
  38. this.keyCode = event.keyCode;
  39. this.isFullScreen = (mejs.MediaFeatures.hasTrueNativeFullScreen && mejs.MediaFeatures.isFullScreen()) || player.isFullScreen;
  40. this.isFirefox = mejs.MediaFeatures.isFirefox;
  41. }
  42. MediaElementKeyActionHandler.keyCodes = KeyCodes;
  43. MediaElementKeyActionHandler.prototype._targetControl = function (selector) {
  44. return $(this.event.target).closest(selector);
  45. };
  46. MediaElementKeyActionHandler.prototype.handlerKey = function () {
  47. const self = this;
  48. // Check whether one of the controls was the event target
  49. const target = findKey(controlSelectors, selector => self._targetControl(selector).length);
  50. // If none of the controls were the target, then let the player handle it
  51. return target || 'player';
  52. };
  53. MediaElementKeyActionHandler.prototype.dispatch = function () {
  54. this.event.preventDefault();
  55. const handler = `${this.handlerKey()}Handler`;
  56. this[handler]();
  57. };
  58. MediaElementKeyActionHandler.prototype.captionsHandler = function () {
  59. let newFocusPosition;
  60. const { player, event } = this;
  61. const srcOptions = $(player.captionsButton).find('.mejs-captions-selector input[type=radio]');
  62. const currentlyFocused = focusPosition(srcOptions, (i, el) => (el.value === 'none' && player.selectedTrack == null) ||
  63. (player.selectedTrack && el.value === player.selectedTrack.srclang));
  64. switch (this.keyCode) {
  65. case KeyCodes.DOWN:
  66. newFocusPosition = Math.min(currentlyFocused + 1, srcOptions.length - 1);
  67. srcOptions.slice(newFocusPosition).first().focus().click();
  68. break;
  69. case KeyCodes.UP:
  70. newFocusPosition = Math.max(currentlyFocused - 1, 0);
  71. srcOptions.slice(newFocusPosition).first().focus().click();
  72. break;
  73. case KeyCodes.ENTER:
  74. if (event.target.tagName.toLowerCase() === 'a') {
  75. $(event.target)[0].click();
  76. }
  77. break;
  78. default:
  79. }
  80. };
  81. MediaElementKeyActionHandler.prototype.fullscreenHandler = function () {
  82. const { player, event } = this;
  83. switch (this.keyCode) {
  84. case KeyCodes.SPACE:
  85. // SPACE sends the click event in firefox, which is already bound to in the plugin.
  86. if (this.isFirefox) {
  87. break;
  88. }
  89. /* falls through */
  90. case KeyCodes.ENTER:
  91. // IE seems to treat the request for fullscreen differently based on the
  92. // event type. So instead of calling the function as a keypress handler,
  93. // we simulate a click.
  94. $(event.target)[0].click();
  95. break;
  96. case KeyCodes.ESC:
  97. if (this.isFullScreen) {
  98. player.exitFullScreen();
  99. }
  100. break;
  101. default:
  102. }
  103. };
  104. MediaElementKeyActionHandler.prototype.playpauseHandler = function () {
  105. const { player, media } = this;
  106. let newTime;
  107. switch (this.keyCode) {
  108. case KeyCodes.LEFT:
  109. case KeyCodes.DOWN:
  110. case KeyCodes.G_REWIND:
  111. newTime = Math.max(media.currentTime - player.options.defaultSeekBackwardInterval(media), 0);
  112. media.setCurrentTime(newTime);
  113. break;
  114. case KeyCodes.RIGHT:
  115. case KeyCodes.UP:
  116. case KeyCodes.G_FORWARD:
  117. newTime = Math.min(media.currentTime + player.options.defaultSeekForwardInterval(media), media.duration);
  118. media.setCurrentTime(newTime);
  119. break;
  120. case KeyCodes.PAGE_DOWN:
  121. newTime = Math.max(media.currentTime - player.options.defaultJumpBackwardInterval(media), 0);
  122. media.setCurrentTime(newTime);
  123. break;
  124. case KeyCodes.PAGE_UP:
  125. newTime = Math.min(media.currentTime + player.options.defaultJumpForwardInterval(media), media.duration);
  126. media.setCurrentTime(newTime);
  127. break;
  128. case KeyCodes.SPACE:
  129. // SPACE sends the click event in firefox, which is already bound to in the plugin.
  130. if (this.isFirefox) {
  131. break;
  132. }
  133. /* falls through */
  134. case KeyCodes.ENTER:
  135. if (media.paused) {
  136. media.play();
  137. } else {
  138. media.pause();
  139. }
  140. break;
  141. default:
  142. }
  143. };
  144. MediaElementKeyActionHandler.prototype.progressHandler = function () {};
  145. MediaElementKeyActionHandler.prototype.sourceHandler = function () {
  146. let newFocusPosition;
  147. const { player } = this;
  148. const srcOptions = $(player.sourcechooserButton).find('.mejs-sourcechooser-selector input[type=radio]');
  149. const currentlyFocused = focusPosition(srcOptions, (i, el) => el.value === player.media.src);
  150. switch (this.keyCode) {
  151. case KeyCodes.DOWN:
  152. newFocusPosition = Math.min(currentlyFocused + 1, srcOptions.length - 1);
  153. srcOptions.slice(newFocusPosition).first().focus().click();
  154. break;
  155. case KeyCodes.UP:
  156. newFocusPosition = Math.max(currentlyFocused - 1, 0);
  157. srcOptions.slice(newFocusPosition).first().focus().click();
  158. break;
  159. default:
  160. }
  161. };
  162. MediaElementKeyActionHandler.prototype.speedHandler = function () {
  163. let newFocusPosition;
  164. const { player } = this;
  165. const srcOptions = $(player.speedButton).find('.mejs-speed-selector input[type=radio]');
  166. const currentlyFocused = focusPosition(srcOptions, (i, el) => parseFloat(el.value) === player.media.playbackRate);
  167. switch (this.keyCode) {
  168. case KeyCodes.DOWN:
  169. newFocusPosition = Math.min(currentlyFocused + 1, srcOptions.length - 1);
  170. srcOptions.slice(newFocusPosition).first().focus().click();
  171. break;
  172. case KeyCodes.UP:
  173. newFocusPosition = Math.max(currentlyFocused - 1, 0);
  174. srcOptions.slice(newFocusPosition).first().focus().click();
  175. break;
  176. default:
  177. }
  178. };
  179. MediaElementKeyActionHandler.prototype.volumeHandler = function () {
  180. const { player, media } = this;
  181. let volume;
  182. switch (this.keyCode) {
  183. case KeyCodes.SPACE:
  184. // SPACE sends the click event in firefox, which is already bound to in the plugin.
  185. if (this.isFirefox) {
  186. break;
  187. }
  188. /* falls through */
  189. case KeyCodes.ENTER:
  190. player.setMuted(!player.media.muted);
  191. break;
  192. case KeyCodes.LEFT: // DOWN is handled by the plugin
  193. volume = Math.max(0, media.volume - 0.1);
  194. media.setVolume(volume);
  195. break;
  196. case KeyCodes.RIGHT: // UP is handled by the plugin
  197. volume = Math.min(media.volume + 0.1, 1);
  198. media.setVolume(volume);
  199. break;
  200. case KeyCodes.PAGE_DOWN:
  201. volume = Math.max(0, media.volume - 0.5);
  202. media.setVolume(volume);
  203. break;
  204. case KeyCodes.PAGE_UP:
  205. volume = Math.min(media.volume + 0.5, 1);
  206. media.setVolume(volume);
  207. break;
  208. default:
  209. }
  210. };
  211. MediaElementKeyActionHandler.prototype.playerHandler = function () {
  212. const { player, media, event } = this;
  213. let newTime;
  214. let volume;
  215. switch (this.keyCode) {
  216. case KeyCodes.LEFT:
  217. case KeyCodes.G_REWIND:
  218. newTime = Math.max(media.currentTime - player.options.defaultSeekBackwardInterval(media), 0);
  219. media.setCurrentTime(newTime);
  220. break;
  221. case KeyCodes.RIGHT:
  222. case KeyCodes.G_FORWARD:
  223. newTime = Math.min(media.currentTime + player.options.defaultSeekForwardInterval(media), media.duration);
  224. media.setCurrentTime(newTime);
  225. break;
  226. case KeyCodes.PAGE_DOWN:
  227. newTime = Math.max(media.currentTime - player.options.defaultJumpBackwardInterval(media), 0);
  228. media.setCurrentTime(newTime);
  229. break;
  230. case KeyCodes.PAGE_UP:
  231. newTime = Math.min(media.currentTime + player.options.defaultJumpForwardInterval(media), media.duration);
  232. media.setCurrentTime(newTime);
  233. break;
  234. case KeyCodes.F:
  235. // IE seems to treat the request for fullscreen differently based on the
  236. // event type. So instead of calling the function as a keypress handler,
  237. // we simulate a click on the fullscreen button.
  238. $(event.target).find('.mejs-fullscreen-button > button')[0].click();
  239. break;
  240. case KeyCodes.UP:
  241. volume = media.volume;
  242. media.setVolume(Math.min(volume + 0.1, 1));
  243. break;
  244. case KeyCodes.DOWN:
  245. volume = media.volume;
  246. media.setVolume(Math.max(0, volume - 0.1));
  247. break;
  248. case KeyCodes.M:
  249. player.setMuted(!player.media.muted);
  250. break;
  251. case KeyCodes.SPACE:
  252. // SPACE sends the click event in firefox, which is already bound to in the plugin.
  253. if (this.isFirefox) {
  254. break;
  255. }
  256. /* falls through */
  257. case KeyCodes.ENTER:
  258. if (media.paused) {
  259. media.play();
  260. } else {
  261. media.pause();
  262. }
  263. break;
  264. default:
  265. }
  266. };
  267. export default MediaElementKeyActionHandler