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