-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
Copy pathvolume-control.js
148 lines (127 loc) · 4.08 KB
/
volume-control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @file volume-control.js
*/
import Component from '../../component.js';
import checkVolumeSupport from './check-volume-support';
import {isPlain} from '../../utils/obj';
import {throttle, bind_, UPDATE_REFRESH_INTERVAL} from '../../utils/fn.js';
/** @import Player from '../../player' */
// Required children
import './volume-bar.js';
/**
* The component for controlling the volume level
*
* @extends Component
*/
class VolumeControl extends Component {
/**
* Creates an instance of this class.
*
* @param {Player} player
* The `Player` that this class should be attached to.
*
* @param {Object} [options={}]
* The key/value store of player options.
*/
constructor(player, options = {}) {
options.vertical = options.vertical || false;
// Pass the vertical option down to the VolumeBar if
// the VolumeBar is turned on.
if (typeof options.volumeBar === 'undefined' || isPlain(options.volumeBar)) {
options.volumeBar = options.volumeBar || {};
options.volumeBar.vertical = options.vertical;
}
super(player, options);
// hide this control if volume support is missing
checkVolumeSupport(this, player);
this.throttledHandleMouseMove = throttle(bind_(this, this.handleMouseMove), UPDATE_REFRESH_INTERVAL);
this.handleMouseUpHandler_ = (e) => this.handleMouseUp(e);
this.on('mousedown', (e) => this.handleMouseDown(e));
this.on('touchstart', (e) => this.handleMouseDown(e));
this.on('mousemove', (e) => this.handleMouseMove(e));
// while the slider is active (the mouse has been pressed down and
// is dragging) or in focus we do not want to hide the VolumeBar
this.on(this.volumeBar, ['focus', 'slideractive'], () => {
this.volumeBar.addClass('vjs-slider-active');
this.addClass('vjs-slider-active');
this.trigger('slideractive');
});
this.on(this.volumeBar, ['blur', 'sliderinactive'], () => {
this.volumeBar.removeClass('vjs-slider-active');
this.removeClass('vjs-slider-active');
this.trigger('sliderinactive');
});
}
/**
* Create the `Component`'s DOM element
*
* @return {Element}
* The element that was created.
*/
createEl() {
let orientationClass = 'vjs-volume-horizontal';
if (this.options_.vertical) {
orientationClass = 'vjs-volume-vertical';
}
return super.createEl('div', {
className: `vjs-volume-control vjs-control ${orientationClass}`
});
}
/**
* Handle `mousedown` or `touchstart` events on the `VolumeControl`.
*
* @param {Event} event
* `mousedown` or `touchstart` event that triggered this function
*
* @listens mousedown
* @listens touchstart
*/
handleMouseDown(event) {
const doc = this.el_.ownerDocument;
this.on(doc, 'mousemove', this.throttledHandleMouseMove);
this.on(doc, 'touchmove', this.throttledHandleMouseMove);
this.on(doc, 'mouseup', this.handleMouseUpHandler_);
this.on(doc, 'touchend', this.handleMouseUpHandler_);
}
/**
* Handle `mouseup` or `touchend` events on the `VolumeControl`.
*
* @param {Event} event
* `mouseup` or `touchend` event that triggered this function.
*
* @listens touchend
* @listens mouseup
*/
handleMouseUp(event) {
const doc = this.el_.ownerDocument;
this.off(doc, 'mousemove', this.throttledHandleMouseMove);
this.off(doc, 'touchmove', this.throttledHandleMouseMove);
this.off(doc, 'mouseup', this.handleMouseUpHandler_);
this.off(doc, 'touchend', this.handleMouseUpHandler_);
}
/**
* Handle `mousedown` or `touchstart` events on the `VolumeControl`.
*
* @param {Event} event
* `mousedown` or `touchstart` event that triggered this function
*
* @listens mousedown
* @listens touchstart
*/
handleMouseMove(event) {
this.volumeBar.handleMouseMove(event);
}
}
/**
* Default options for the `VolumeControl`
*
* @type {Object}
* @private
*/
VolumeControl.prototype.options_ = {
children: [
'volumeBar'
]
};
Component.registerComponent('VolumeControl', VolumeControl);
export default VolumeControl;