text-track-menu-item.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  4. var _menuItem = require('../../menu/menu-item.js');
  5. var _menuItem2 = _interopRequireDefault(_menuItem);
  6. var _component = require('../../component.js');
  7. var _component2 = _interopRequireDefault(_component);
  8. var _fn = require('../../utils/fn.js');
  9. var Fn = _interopRequireWildcard(_fn);
  10. var _window = require('global/window');
  11. var _window2 = _interopRequireDefault(_window);
  12. var _document = require('global/document');
  13. var _document2 = _interopRequireDefault(_document);
  14. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  16. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  17. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  18. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
  19. * @file text-track-menu-item.js
  20. */
  21. /**
  22. * The specific menu item type for selecting a language within a text track kind
  23. *
  24. * @extends MenuItem
  25. */
  26. var TextTrackMenuItem = function (_MenuItem) {
  27. _inherits(TextTrackMenuItem, _MenuItem);
  28. /**
  29. * Creates an instance of this class.
  30. *
  31. * @param {Player} player
  32. * The `Player` that this class should be attached to.
  33. *
  34. * @param {Object} [options]
  35. * The key/value store of player options.
  36. */
  37. function TextTrackMenuItem(player, options) {
  38. _classCallCheck(this, TextTrackMenuItem);
  39. var track = options.track;
  40. var tracks = player.textTracks();
  41. // Modify options for parent MenuItem class's init.
  42. options.label = track.label || track.language || 'Unknown';
  43. options.selected = track['default'] || track.mode === 'showing';
  44. var _this = _possibleConstructorReturn(this, _MenuItem.call(this, player, options));
  45. _this.track = track;
  46. if (tracks) {
  47. var changeHandler = Fn.bind(_this, _this.handleTracksChange);
  48. player.on(['loadstart', 'texttrackchange'], changeHandler);
  49. tracks.addEventListener('change', changeHandler);
  50. _this.on('dispose', function () {
  51. tracks.removeEventListener('change', changeHandler);
  52. });
  53. }
  54. // iOS7 doesn't dispatch change events to TextTrackLists when an
  55. // associated track's mode changes. Without something like
  56. // Object.observe() (also not present on iOS7), it's not
  57. // possible to detect changes to the mode attribute and polyfill
  58. // the change event. As a poor substitute, we manually dispatch
  59. // change events whenever the controls modify the mode.
  60. if (tracks && tracks.onchange === undefined) {
  61. var event = void 0;
  62. _this.on(['tap', 'click'], function () {
  63. if (_typeof(_window2['default'].Event) !== 'object') {
  64. // Android 2.3 throws an Illegal Constructor error for window.Event
  65. try {
  66. event = new _window2['default'].Event('change');
  67. } catch (err) {
  68. // continue regardless of error
  69. }
  70. }
  71. if (!event) {
  72. event = _document2['default'].createEvent('Event');
  73. event.initEvent('change', true, true);
  74. }
  75. tracks.dispatchEvent(event);
  76. });
  77. }
  78. return _this;
  79. }
  80. /**
  81. * This gets called when an `TextTrackMenuItem` is "clicked". See
  82. * {@link ClickableComponent} for more detailed information on what a click can be.
  83. *
  84. * @param {EventTarget~Event} event
  85. * The `keydown`, `tap`, or `click` event that caused this function to be
  86. * called.
  87. *
  88. * @listens tap
  89. * @listens click
  90. */
  91. TextTrackMenuItem.prototype.handleClick = function handleClick(event) {
  92. var kind = this.track.kind;
  93. var tracks = this.player_.textTracks();
  94. _MenuItem.prototype.handleClick.call(this, event);
  95. if (!tracks) {
  96. return;
  97. }
  98. for (var i = 0; i < tracks.length; i++) {
  99. var track = tracks[i];
  100. if (track.kind !== kind) {
  101. continue;
  102. }
  103. if (track === this.track) {
  104. track.mode = 'showing';
  105. } else {
  106. track.mode = 'disabled';
  107. }
  108. }
  109. };
  110. /**
  111. * Handle text track list change
  112. *
  113. * @param {EventTarget~Event} event
  114. * The `change` event that caused this function to be called.
  115. *
  116. * @listens TextTrackList#change
  117. */
  118. TextTrackMenuItem.prototype.handleTracksChange = function handleTracksChange(event) {
  119. this.selected(this.track.mode === 'showing');
  120. };
  121. return TextTrackMenuItem;
  122. }(_menuItem2['default']);
  123. _component2['default'].registerComponent('TextTrackMenuItem', TextTrackMenuItem);
  124. exports['default'] = TextTrackMenuItem;