Skip to content

Commit aec1298

Browse files
committed
feat: Support toggling the select icon with the source label (#28)
User options can now be passed to the plugin. Options are passed upon initialization and use the following syntax: require('@silvermine/videojs-quality-selector')(videojs, options); If you're not using require, you can pass options using a window config: <script> window.SILVERMINE_VIDEOJS_QUALITY_SELECTOR_CONFIG = { showQualitySelectionLabelInControlBar: true, }; </script> <script src="path/to/silvermine-videojs-quality-selector.js"></script> The options are sent from the constructor to `/components/QualitySelector.js`. Currently only one option is available: showQualitySelectionLabelInControlBar: (default: `false`) - Enabling this flag will swap the selector icon with the label of the currently selected source. More options should be possible by adding them to the object. A class is added to the Menu Item (Has class vjs-icon-placeholder) when the flag is disabled to include the icon. When enabled, basic HTML is simply included into the element. Refs #28
1 parent 06b18e4 commit aec1298

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ var videojs = require('videojs');
6464
require('@silvermine/videojs-quality-selector')(videojs);
6565
```
6666

67+
### Initialization options
68+
69+
* **`showQualitySelectionLabelInControlBar`** (default: `false`) - Enabling this flag will swap the selector
70+
icon with the label of the currently selected source.
71+
72+
#### Providing initialization options via `require()`
73+
74+
If requiring this plugin via NPM, any desired initialization options can be supplied to
75+
the constructor function exported by the module. For example:
76+
77+
```js
78+
require('@silvermine/videojs-quality-selector')(videojs, { showQualitySelectionLabelInControlBar: true });
79+
```
80+
81+
#### Providing initialization options via `<script>`
82+
83+
If using the prebuilt JS, the initialization options can be provided via
84+
`window.SILVERMINE_VIDEOJS_QUALITY_SELECTOR_CONFIG`. Note that these options need to be set
85+
before the `<script>` tag to include the plugin.
86+
87+
```html
88+
<script>
89+
window.SILVERMINE_VIDEOJS_QUALITY_SELECTOR_CONFIG = {
90+
showQualitySelectionLabelInControlBar: true,
91+
};
92+
</script>
93+
<script src="path/to/silvermine-videojs-quality-selector.js"></script>
94+
```
95+
6796
### Providing video sources
6897

6998
Sources can be provided with either the `<source>` tag or via the `src` function on the

src/js/components/QualitySelector.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ var _ = require('underscore'),
55
qualityOptionFactory = require('./QualityOption'),
66
QUALITY_CHANGE_CLASS = 'vjs-quality-changing';
77

8-
module.exports = function(videojs) {
8+
module.exports = function(videojs, userOpts) {
9+
var userOptions = _.defaults(_.extend({}, userOpts), { showQualitySelectionLabelInControlBar: false });
10+
911
var MenuButton = videojs.getComponent('MenuButton'),
1012
QualityOption = qualityOptionFactory(videojs),
1113
QualitySelector;
@@ -42,6 +44,17 @@ module.exports = function(videojs) {
4244
player.on(events.QUALITY_SELECTED, function(event, newSource) {
4345
// Update the selected source with the source that was actually selected
4446
this.setSelectedSource(newSource);
47+
48+
var qualitySelectMenuButton = this.children_[0].controlTextEl_.previousSibling;
49+
50+
// Check for quality select label flag
51+
if (userOptions.showQualitySelectionLabelInControlBar !== false) {
52+
qualitySelectMenuButton.innerHTML = newSource.label;
53+
} else if (qualitySelectMenuButton.className.indexOf('vjs-quality-selector-icon') === -1) {
54+
// Add class to show gear icon. Blank out innerHTML for safety precations.
55+
qualitySelectMenuButton.className += ' vjs-quality-selector-icon';
56+
qualitySelectMenuButton.innerHTML = '';
57+
}
4558
}.bind(this));
4659

4760
// Since it's possible for the player to get a source before the selector is

src/js/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ var _ = require('underscore'),
66
sourceInterceptorFactory = require('./middleware/SourceInterceptor'),
77
SafeSeek = require('./util/SafeSeek');
88

9-
module.exports = function(videojs) {
9+
module.exports = function(videojs, userOpts) {
1010
videojs = videojs || window.videojs;
1111

12-
qualitySelectorFactory(videojs);
12+
qualitySelectorFactory(videojs, userOpts);
1313
sourceInterceptorFactory(videojs);
1414

1515
videojs.hook('setup', function(player) {

src/js/standalone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict';
22

3-
require('./index')();
3+
require('./index')(undefined, window.SILVERMINE_VIDEOJS_QUALITY_SELECTOR_CONFIG);

src/sass/quality-selector.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
height: 100%;
66
width: 100%;
77
}
8-
.vjs-icon-placeholder {
8+
.vjs-quality-selector-icon {
99
// From video.js font: https://github.com/videojs/font
1010
font-family: 'VideoJS';
1111
font-weight: normal;

0 commit comments

Comments
 (0)