790967bdcba047153e1ab8ba8869f277d9922d4d
[yaffs-website] / node_modules / video.js / es5 / control-bar / text-track-controls / chapters-button.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _textTrackButton = require('./text-track-button.js');
6
7 var _textTrackButton2 = _interopRequireDefault(_textTrackButton);
8
9 var _component = require('../../component.js');
10
11 var _component2 = _interopRequireDefault(_component);
12
13 var _chaptersTrackMenuItem = require('./chapters-track-menu-item.js');
14
15 var _chaptersTrackMenuItem2 = _interopRequireDefault(_chaptersTrackMenuItem);
16
17 var _toTitleCase = require('../../utils/to-title-case.js');
18
19 var _toTitleCase2 = _interopRequireDefault(_toTitleCase);
20
21 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
22
23 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
24
25 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; }
26
27 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; } /**
28                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 * @file chapters-button.js
29                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 */
30
31
32 /**
33  * The button component for toggling and selecting chapters
34  * Chapters act much differently than other text tracks
35  * Cues are navigation vs. other tracks of alternative languages
36  *
37  * @extends TextTrackButton
38  */
39 var ChaptersButton = function (_TextTrackButton) {
40   _inherits(ChaptersButton, _TextTrackButton);
41
42   /**
43    * Creates an instance of this class.
44    *
45    * @param {Player} player
46    *        The `Player` that this class should be attached to.
47    *
48    * @param {Object} [options]
49    *        The key/value store of player options.
50    *
51    * @param {Component~ReadyCallback} [ready]
52    *        The function to call when this function is ready.
53    */
54   function ChaptersButton(player, options, ready) {
55     _classCallCheck(this, ChaptersButton);
56
57     var _this = _possibleConstructorReturn(this, _TextTrackButton.call(this, player, options, ready));
58
59     _this.el_.setAttribute('aria-label', 'Chapters Menu');
60     return _this;
61   }
62
63   /**
64    * Builds the default DOM `className`.
65    *
66    * @return {string}
67    *         The DOM `className` for this object.
68    */
69
70
71   ChaptersButton.prototype.buildCSSClass = function buildCSSClass() {
72     return 'vjs-chapters-button ' + _TextTrackButton.prototype.buildCSSClass.call(this);
73   };
74
75   /**
76    * Update the menu based on the current state of its items.
77    *
78    * @param {EventTarget~Event} [event]
79    *        An event that triggered this function to run.
80    *
81    * @listens TextTrackList#addtrack
82    * @listens TextTrackList#removetrack
83    * @listens TextTrackList#change
84    */
85
86
87   ChaptersButton.prototype.update = function update(event) {
88     if (!this.track_ || event && (event.type === 'addtrack' || event.type === 'removetrack')) {
89       this.setTrack(this.findChaptersTrack());
90     }
91     _TextTrackButton.prototype.update.call(this);
92   };
93
94   /**
95    * Set the currently selected track for the chapters button.
96    *
97    * @param {TextTrack} track
98    *        The new track to select. Nothing will change if this is the currently selected
99    *        track.
100    */
101
102
103   ChaptersButton.prototype.setTrack = function setTrack(track) {
104     if (this.track_ === track) {
105       return;
106     }
107
108     if (!this.updateHandler_) {
109       this.updateHandler_ = this.update.bind(this);
110     }
111
112     // here this.track_ refers to the old track instance
113     if (this.track_) {
114       var remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
115
116       if (remoteTextTrackEl) {
117         remoteTextTrackEl.removeEventListener('load', this.updateHandler_);
118       }
119
120       this.track_ = null;
121     }
122
123     this.track_ = track;
124
125     // here this.track_ refers to the new track instance
126     if (this.track_) {
127       this.track_.mode = 'hidden';
128
129       var _remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
130
131       if (_remoteTextTrackEl) {
132         _remoteTextTrackEl.addEventListener('load', this.updateHandler_);
133       }
134     }
135   };
136
137   /**
138    * Find the track object that is currently in use by this ChaptersButton
139    *
140    * @return {TextTrack|undefined}
141    *         The current track or undefined if none was found.
142    */
143
144
145   ChaptersButton.prototype.findChaptersTrack = function findChaptersTrack() {
146     var tracks = this.player_.textTracks() || [];
147
148     for (var i = tracks.length - 1; i >= 0; i--) {
149       // We will always choose the last track as our chaptersTrack
150       var track = tracks[i];
151
152       if (track.kind === this.kind_) {
153         return track;
154       }
155     }
156   };
157
158   /**
159    * Get the caption for the ChaptersButton based on the track label. This will also
160    * use the current tracks localized kind as a fallback if a label does not exist.
161    *
162    * @return {string}
163    *         The tracks current label or the localized track kind.
164    */
165
166
167   ChaptersButton.prototype.getMenuCaption = function getMenuCaption() {
168     if (this.track_ && this.track_.label) {
169       return this.track_.label;
170     }
171     return this.localize((0, _toTitleCase2['default'])(this.kind_));
172   };
173
174   /**
175    * Create menu from chapter track
176    *
177    * @return {Menu}
178    *         New menu for the chapter buttons
179    */
180
181
182   ChaptersButton.prototype.createMenu = function createMenu() {
183     this.options_.title = this.getMenuCaption();
184     return _TextTrackButton.prototype.createMenu.call(this);
185   };
186
187   /**
188    * Create a menu item for each text track
189    *
190    * @return {TextTrackMenuItem[]}
191    *         Array of menu items
192    */
193
194
195   ChaptersButton.prototype.createItems = function createItems() {
196     var items = [];
197
198     if (!this.track_) {
199       return items;
200     }
201
202     var cues = this.track_.cues;
203
204     if (!cues) {
205       return items;
206     }
207
208     for (var i = 0, l = cues.length; i < l; i++) {
209       var cue = cues[i];
210       var mi = new _chaptersTrackMenuItem2['default'](this.player_, { track: this.track_, cue: cue });
211
212       items.push(mi);
213     }
214
215     return items;
216   };
217
218   return ChaptersButton;
219 }(_textTrackButton2['default']);
220
221 /**
222  * `kind` of TextTrack to look for to associate it with this menu.
223  *
224  * @type {string}
225  * @private
226  */
227
228
229 ChaptersButton.prototype.kind_ = 'chapters';
230
231 /**
232  * The text that should display over the `ChaptersButton`s controls. Added for localization.
233  *
234  * @type {string}
235  * @private
236  */
237 ChaptersButton.prototype.controlText_ = 'Chapters';
238
239 _component2['default'].registerComponent('ChaptersButton', ChaptersButton);
240 exports['default'] = ChaptersButton;