- 1 :
/**
- 2 :
* @file track-list.js
- 3 :
*/
- 4 :
import EventTarget from '../event-target';
- 5 :
import {isEvented} from '../mixins/evented';
- 6 :
- 7 :
/**
- 8 :
* @typedef { import('./track').default } Track
- 9 :
*/
- 10 :
- 11 :
/**
- 12 :
* Common functionaliy between {@link TextTrackList}, {@link AudioTrackList}, and
- 13 :
* {@link VideoTrackList}
- 14 :
*
- 15 :
* @extends EventTarget
- 16 :
*/
- 17 :
class TrackList extends EventTarget {
- 18 :
/**
- 19 :
* Create an instance of this class
- 20 :
*
- 21 :
* @param {Track[]} tracks
- 22 :
* A list of tracks to initialize the list with.
- 23 :
*
- 24 :
* @abstract
- 25 :
*/
- 26 :
constructor(tracks = []) {
- 27 :
super();
- 28 :
- 29 :
this.tracks_ = [];
- 30 :
- 31 :
/**
- 32 :
* @memberof TrackList
- 33 :
* @member {number} length
- 34 :
* The current number of `Track`s in the this Trackist.
- 35 :
* @instance
- 36 :
*/
- 37 :
Object.defineProperty(this, 'length', {
- 38 :
get() {
- 39 :
return this.tracks_.length;
- 40 :
}
- 41 :
});
- 42 :
- 43 :
for (let i = 0; i < tracks.length; i++) {
- 44 :
this.addTrack(tracks[i]);
- 45 :
}
- 46 :
}
- 47 :
- 48 :
/**
- 49 :
* Add a {@link Track} to the `TrackList`
- 50 :
*
- 51 :
* @param {Track} track
- 52 :
* The audio, video, or text track to add to the list.
- 53 :
*
- 54 :
* @fires TrackList#addtrack
- 55 :
*/
- 56 :
addTrack(track) {
- 57 :
const index = this.tracks_.length;
- 58 :
- 59 :
if (!('' + index in this)) {
- 60 :
Object.defineProperty(this, index, {
- 61 :
get() {
- 62 :
return this.tracks_[index];
- 63 :
}
- 64 :
});
- 65 :
}
- 66 :
- 67 :
// Do not add duplicate tracks
- 68 :
if (this.tracks_.indexOf(track) === -1) {
- 69 :
this.tracks_.push(track);
- 70 :
/**
- 71 :
* Triggered when a track is added to a track list.
- 72 :
*
- 73 :
* @event TrackList#addtrack
- 74 :
* @type {Event}
- 75 :
* @property {Track} track
- 76 :
* A reference to track that was added.
- 77 :
*/
- 78 :
this.trigger({
- 79 :
track,
- 80 :
type: 'addtrack',
- 81 :
target: this
- 82 :
});
- 83 :
}
- 84 :
- 85 :
/**
- 86 :
* Triggered when a track label is changed.
- 87 :
*
- 88 :
* @event TrackList#addtrack
- 89 :
* @type {Event}
- 90 :
* @property {Track} track
- 91 :
* A reference to track that was added.
- 92 :
*/
- 93 :
track.labelchange_ = () => {
- 94 :
this.trigger({
- 95 :
track,
- 96 :
type: 'labelchange',
- 97 :
target: this
- 98 :
});
- 99 :
};
- 100 :
- 101 :
if (isEvented(track)) {
- 102 :
track.addEventListener('labelchange', track.labelchange_);
- 103 :
}
- 104 :
}
- 105 :
- 106 :
/**
- 107 :
* Remove a {@link Track} from the `TrackList`
- 108 :
*
- 109 :
* @param {Track} rtrack
- 110 :
* The audio, video, or text track to remove from the list.
- 111 :
*
- 112 :
* @fires TrackList#removetrack
- 113 :
*/
- 114 :
removeTrack(rtrack) {
- 115 :
let track;
- 116 :
- 117 :
for (let i = 0, l = this.length; i < l; i++) {
- 118 :
if (this[i] === rtrack) {
- 119 :
track = this[i];
- 120 :
if (track.off) {
- 121 :
track.off();
- 122 :
}
- 123 :
- 124 :
this.tracks_.splice(i, 1);
- 125 :
- 126 :
break;
- 127 :
}
- 128 :
}
- 129 :
- 130 :
if (!track) {
- 131 :
return;
- 132 :
}
- 133 :
- 134 :
/**
- 135 :
* Triggered when a track is removed from track list.
- 136 :
*
- 137 :
* @event TrackList#removetrack
- 138 :
* @type {Event}
- 139 :
* @property {Track} track
- 140 :
* A reference to track that was removed.
- 141 :
*/
- 142 :
this.trigger({
- 143 :
track,
- 144 :
type: 'removetrack',
- 145 :
target: this
- 146 :
});
- 147 :
}
- 148 :
- 149 :
/**
- 150 :
* Get a Track from the TrackList by a tracks id
- 151 :
*
- 152 :
* @param {string} id - the id of the track to get
- 153 :
* @method getTrackById
- 154 :
* @return {Track}
- 155 :
* @private
- 156 :
*/
- 157 :
getTrackById(id) {
- 158 :
let result = null;
- 159 :
- 160 :
for (let i = 0, l = this.length; i < l; i++) {
- 161 :
const track = this[i];
- 162 :
- 163 :
if (track.id === id) {
- 164 :
result = track;
- 165 :
break;
- 166 :
}
- 167 :
}
- 168 :
- 169 :
return result;
- 170 :
}
- 171 :
}
- 172 :
- 173 :
/**
- 174 :
* Triggered when a different track is selected/enabled.
- 175 :
*
- 176 :
* @event TrackList#change
- 177 :
* @type {Event}
- 178 :
*/
- 179 :
- 180 :
/**
- 181 :
* Events that can be called with on + eventName. See {@link EventHandler}.
- 182 :
*
- 183 :
* @property {Object} TrackList#allowedEvents_
- 184 :
* @private
- 185 :
*/
- 186 :
TrackList.prototype.allowedEvents_ = {
- 187 :
change: 'change',
- 188 :
addtrack: 'addtrack',
- 189 :
removetrack: 'removetrack',
- 190 :
labelchange: 'labelchange'
- 191 :
};
- 192 :
- 193 :
// emulate attribute EventHandler support to allow for feature detection
- 194 :
for (const event in TrackList.prototype.allowedEvents_) {
- 195 :
TrackList.prototype['on' + event] = null;
- 196 :
}
- 197 :
- 198 :
export default TrackList;