PageSourceSearch

https://snn.ir/media/vendors/videojs/js/videojs-contrib-quality-levels.js

js snn.ir collected 2026-09-24 19:39:56 UTC 11,126 bytes, 373 lines download raw bytes

1/*! @name videojs-contrib-quality-levels @version 2.0.5 @license Apache-2.0 */
2(function (global, factory) {
3  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('video.js'), require('global/document')) :
4  typeof define === 'function' && define.amd ? define(['video.js', 'global/document'], factory) :
5  (global.videojsContribQualityLevels = factory(global.videojs,global.document));
6}(this, (function (videojs,document) { 'use strict';
7
8  videojs = videojs && videojs.hasOwnProperty('default') ? videojs['default'] : videojs;
9  document = document && document.hasOwnProperty('default') ? document['default'] : document;
10
11  var classCallCheck = function (instance, Constructor) {
12    if (!(instance instanceof Constructor)) {
13      throw new TypeError("Cannot call a class as a function");
14    }
15  };
16
17  var inherits = function (subClass, superClass) {
18    if (typeof superClass !== "function" && superClass !== null) {
19      throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
20    }
21
22    subClass.prototype = Object.create(superClass && superClass.prototype, {
23      constructor: {
24        value: subClass,
25        enumerable: false,
26        writable: true,
27        configurable: true
28      }
29    });
30    if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
31  };
32
33  var possibleConstructorReturn = function (self, call) {
34    if (!self) {
35      throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
36    }
37
38    return call && (typeof call === "object" || typeof call === "function") ? call : self;
39  };
40
41  /**
42   * A single QualityLevel.
43   *
44   * interface QualityLevel {
45   *   readonly attribute DOMString id;
46   *            attribute DOMString label;
47   *   readonly attribute long width;
48   *   readonly attribute long height;
49   *   readonly attribute long bitrate;
50   *            attribute boolean enabled;
51   * };
52   *
53   * @class QualityLevel
54   */
55
56  var QualityLevel =
57
58  /**
59   * Creates a QualityLevel
60   *
61   * @param {Representation|Object} representation The representation of the quality level
62   * @param {string}   representation.id        Unique id of the QualityLevel
63   * @param {number=}  representation.width     Resolution width of the QualityLevel
64   * @param {number=}  representation.height    Resolution height of the QualityLevel
65   * @param {number}   representation.bandwidth Bitrate of the QualityLevel
66   * @param {Function} representation.enabled   Callback to enable/disable QualityLevel
67   */
68  function QualityLevel(representation) {
69    classCallCheck(this, QualityLevel);
70
71
72    var level = this; // eslint-disable-line
73
74    if (videojs.browser.IS_IE8) {
75      level = document.createElement('custom');
76      for (var prop in QualityLevel.prototype) {
77        if (prop !== 'constructor') {
78          level[prop] = QualityLevel.prototype[prop];
79        }
80      }
81    }
82
83    level.id = representation.id;
84    level.label = level.id;
85    level.width = representation.width;
86    level.height = representation.height;
87    level.bitrate = representation.bandwidth;
88    level.enabled_ = representation.enabled;
89
90    Object.defineProperty(level, 'enabled', {
91      /**
92       * Get whether the QualityLevel is enabled.
93       *
94       * @returns {boolean} True if the QualityLevel is enabled.
95       */
96      get: function get$$1() {
97        return level.enabled_();
98      },
99
100
101      /**
102       * Enable or disable the QualityLevel.
103       *
104       * @param {boolean} enable true to enable QualityLevel, false to disable.
105       */
106      set: function set$$1(enable) {
107        level.enabled_(enable);
108      }
109    });
110
111    return level;
112  };
113
114  /**
115   * A list of QualityLevels.
116   *
117   * interface QualityLevelList : EventTarget {
118   *   getter QualityLevel (unsigned long index);
119   *   readonly attribute unsigned long length;
120   *   readonly attribute long selectedIndex;
121   *
122   *   void addQualityLevel(QualityLevel qualityLevel)
123   *   void removeQualityLevel(QualityLevel remove)
124   *   QualityLevel? getQualityLevelById(DOMString id);
125   *
126   *   attribute EventHandler onchange;
127   *   attribute EventHandler onaddqualitylevel;
128   *   attribute EventHandler onremovequalitylevel;
129   * };
130   *
131   * @extends videojs.EventTarget
132   * @class QualityLevelList
133   */
134
135  var QualityLevelList = function (_videojs$EventTarget) {
136    inherits(QualityLevelList, _videojs$EventTarget);
137
138    function QualityLevelList() {
139      var _ret;
140
141      classCallCheck(this, QualityLevelList);
142
143      var _this = possibleConstructorReturn(this, _videojs$EventTarget.call(this));
144
145      var list = _this; // eslint-disable-line
146
147      if (videojs.browser.IS_IE8) {
148        list = document.createElement('custom');
149        for (var prop in QualityLevelList.prototype) {
150          if (prop !== 'constructor') {
151            list[prop] = QualityLevelList.prototype[prop];
152          }
153        }
154      }
155
156      list.levels_ = [];
157      list.selectedIndex_ = -1;
158
159      /**
160       * Get the index of the currently selected QualityLevel.
161       *
162       * @returns {number} The index of the selected QualityLevel. -1 if none selected.
163       * @readonly
164       */
165      Object.defineProperty(list, 'selectedIndex', {
166        get: function get$$1() {
167          return list.selectedIndex_;
168        }
169      });
170
171      /**
172       * Get the length of the list of QualityLevels.
173       *
174       * @returns {number} The length of the list.
175       * @readonly
176       */
177      Object.defineProperty(list, 'length', {
178        get: function get$$1() {
179          return list.levels_.length;
180        }
181      });
182
183      return _ret = list, possibleConstructorReturn(_this, _ret);
184    }
185
186    /**
187     * Adds a quality level to the list.
188     *
189     * @param {Representation|Object} representation The representation of the quality level
190     * @param {string}   representation.id        Unique id of the QualityLevel
191     * @param {number=}  representation.width     Resolution width of the QualityLevel
192     * @param {number=}  representation.height    Resolution height of the QualityLevel
193     * @param {number}   representation.bandwidth Bitrate of the QualityLevel
194     * @param {Function} representation.enabled   Callback to enable/disable QualityLevel
195     * @return {QualityLevel} the QualityLevel added to the list
196     * @method addQualityLevel
197     */
198
199
200    QualityLevelList.prototype.addQualityLevel = function addQualityLevel(representation) {
201      var qualityLevel = this.getQualityLevelById(representation.id);
202
203      // Do not add duplicate quality levels
204      if (qualityLevel) {
205        return qualityLevel;
206      }
207
208      var index = this.levels_.length;
209
210      qualityLevel = new QualityLevel(representation);
211
212      if (!('' + index in this)) {
213        Object.defineProperty(this, index, {
214          get: function get$$1() {
215            return this.levels_[index];
216          }
217        });
218      }
219
220      this.levels_.push(qualityLevel);
221
222      this.trigger({
223        qualityLevel: qualityLevel,
224        type: 'addqualitylevel'
225      });
226
227      return qualityLevel;
228    };
229
230    /**
231     * Removes a quality level from the list.
232     *
233     * @param {QualityLevel} remove QualityLevel to remove to the list.
234     * @return {QualityLevel|null} the QualityLevel removed or null if nothing removed
235     * @method removeQualityLevel
236     */
237
238
239    QualityLevelList.prototype.removeQualityLevel = function removeQualityLevel(qualityLevel) {
240      var removed = null;
241
242      for (var i = 0, l = this.length; i < l; i++) {
243        if (this[i] === qualityLevel) {
244          removed = this.levels_.splice(i, 1)[0];
245
246          if (this.selectedIndex_ === i) {
247            this.selectedIndex_ = -1;
248          } else if (this.selectedIndex_ > i) {
249            this.selectedIndex_--;
250          }
251          break;
252        }
253      }
254
255      if (removed) {
256        this.trigger({
257          qualityLevel: qualityLevel,
258          type: 'removequalitylevel'
259        });
260      }
261
262      return removed;
263    };
264
265    /**
266     * Searches for a QualityLevel with the given id.
267     *
268     * @param {string} id The id of the QualityLevel to find.
269     * @returns {QualityLevel|null} The QualityLevel with id, or null if not found.
270     * @method getQualityLevelById
271     */
272
273
274    QualityLevelList.prototype.getQualityLevelById = function getQualityLevelById(id) {
275      for (var i = 0, l = this.length; i < l; i++) {
276        var level = this[i];
277
278        if (level.id === id) {
279          return level;
280        }
281      }
282      return null;
283    };
284
285    /**
286     * Resets the list of QualityLevels to empty
287     *
288     * @method dispose
289     */
290
291
292    QualityLevelList.prototype.dispose = function dispose() {
293      this.selectedIndex_ = -1;
294      this.levels_.length = 0;
295    };
296
297    return QualityLevelList;
298  }(videojs.EventTarget);
299
300  /**
301   * change - The selected QualityLevel has changed.
302   * addqualitylevel - A QualityLevel has been added to the QualityLevelList.
303   * removequalitylevel - A QualityLevel has been removed from the QualityLevelList.
304   */
305
306
307  QualityLevelList.prototype.allowedEvents_ = {
308    change: 'change',
309    addqualitylevel: 'addqualitylevel',
310    removequalitylevel: 'removequalitylevel'
311  };
312
313  // emulate attribute EventHandler support to allow for feature detection
314  for (var event in QualityLevelList.prototype.allowedEvents_) {
315    QualityLevelList.prototype['on' + event] = null;
316  }
317
318  // vjs 5/6 support
319  var registerPlugin = videojs.registerPlugin || videojs.plugin;
320
321  /**
322   * Initialization function for the qualityLevels plugin. Sets up the QualityLevelList and
323   * event handlers.
324   *
325   * @param {Player} player Player object.
326   * @param {Object} options Plugin options object.
327   * @function initPlugin
328   */
329  var initPlugin = function initPlugin(player, options) {
330    var originalPluginFn = player.qualityLevels;
331
332    var qualityLevelList = new QualityLevelList();
333
334    var disposeHandler = function disposeHandler() {
335      qualityLevelList.dispose();
336      player.qualityLevels = originalPluginFn;
337      player.off('dispose', disposeHandler);
338    };
339
340    player.on('dispose', disposeHandler);
341
342    player.qualityLevels = function () {
343      return qualityLevelList;
344    };
345    player.qualityLevels.VERSION = '__VERSION__';
346
347    return qualityLevelList;
348  };
349
350  /**
351   * A video.js plugin.
352   *
353   * In the plugin function, the value of `this` is a video.js `Player`
354   * instance. You cannot rely on the player being in a "ready" state here,
355   * depending on how the plugin is invoked. This may or may not be important
356   * to you; if not, remove the wait for "ready"!
357   *
358   * @param {Object} options Plugin options object
359   * @function qualityLevels
360   */
361  var qualityLevels = function qualityLevels(options) {
362    return initPlugin(this, videojs.mergeOptions({}, options));
363  };
364
365  // Register the plugin with video.js.
366  registerPlugin('qualityLevels', qualityLevels);
367
368  // Include the version number.
369  qualityLevels.VERSION = '__VERSION__';
370
371  return qualityLevels;
372
373})));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.