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