-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
219 lines (200 loc) · 5.11 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function(){
if(typeof module !== "undefined"){
var storage = require('local-storage-json');
}
else{
console.error('local-storage-json module must exist');
}
function VolumeBar(opts){
this.back = opts.back || null;
this.thumb = opts.thumb || null;
this.speaker = opts.speaker || null;
this.speakerOnClass = opts.speakerOnClass || "on";
this.speakerOffClass = opts.speakerOffClass || "off";
this.audio = opts.audio || null;
this.useLocalStorage = opts.useLocalStorage || false;
this.localStorageNS = opts.localStorageNS ||'audioVolumeControl';
this.settingTimeout = null;
this.manualSet = true;
this.addListeners();
this.onWindowResize();
this.initLocalStorage();
}
// add event listeners
VolumeBar.prototype.addListeners = function(){
if(this.back){
$(this.back).on(
'click',
this.onBackClick.bind(this)
);
}
if(this.thumb){
$(this.thumb).on(
'mousedown',
this.onMouseDown.bind(this)
);
$(this.thumb).on(
'mouseup',
this.onMouseUp.bind(this)
);
}
if(this.speaker){
$(this.speaker).on(
'click',
this.onSpeakerClick.bind(this)
);
}
$(window).on(
'resize',
this.onWindowResize.bind(this)
);
}
// calculate widths
VolumeBar.prototype.onWindowResize = function(){
this.thumbWidth = 0;
if(this.thumb){
this.thumbWidth = $(this.thumb).width();
}
if(this.back){
this.width = $(this.back).width() - this.thumbWidth / 2;
this.left = $(this.back).offset().left + this.thumbWidth / 2;
this.right = this.left + this.width;
}
}
// mouseDown on thumb listener
VolumeBar.prototype.onMouseDown = function(e){
this.bindedMouseMove = this.onMouseMove.bind(this);
this.bindedMouseUp = this.onMouseUp.bind(this);
$(document).on(
'mousemove',
this.bindedMouseMove
);
$(document).on(
'mouseup',
this.bindedMouseUp
);
e.preventDefault();
}
// mouseMove on thumb listener
VolumeBar.prototype.onMouseMove = function(e){
var x = e.clientX;
if (x < this.left){
x = this.left;
}
if (x > this.right){
x = this.right;
}
this.thumbLeft = x - this.left;
this.value = this.thumbLeft / this.width;
this.set();
}
// mouseUp on thumb listener
VolumeBar.prototype.onMouseUp = function(e){
$(document).off(
'mousemove',
this.bindedMouseMove
);
$(document).off(
'mouseup',
this.bindedMouseUp
);
}
// click on back listener
VolumeBar.prototype.onBackClick = function(e){
this.onMouseMove(e);
this.value = this.thumbLeft / this.width;
}
// click on speaker listener
VolumeBar.prototype.onSpeakerClick = function(e){
if ($(this.speaker).hasClass(this.speakerOnClass)){
this.onMouseMove(
{
'clientX': 0
}
);
}
else{
this.onMouseMove(
{
'clientX': 1000
}
);
}
}
// update display
// optionally update audio volume
// fire 'change' event
// don't allow manual setting for 1 sec
VolumeBar.prototype.set = function(){
this.manualSet = false;
clearTimeout(this.settingTimeout);
this.updateDisplay();
if(this.audio){
this.audio.volume = this.value;
}
$(this.back).trigger(
{
'type': 'change',
'value': this.value
}
);
if(this.useLocalStorage === true){
storage.set(
this.localStorageNS + '_volume',
this.value
);
}
this.settingTimeout = setTimeout(
function(){
this.manualSet = true;
}.bind(this),
1000
);
}
// move the thumb and set speaker class
VolumeBar.prototype.updateDisplay = function(){
$(this.thumb).css('left', this.thumbLeft);
if (this.value <= 0){
this.value = 0;
$(this.speaker).removeClass(this.speakerOnClass);
$(this.speaker).addClass(this.speakerOffClass);
} else {
$(this.speaker).removeClass(this.speakerOffClass);
$(this.speaker).addClass(this.speakerOnClass);
}
if (this.value >= 1){
this.value = 1;
}
}
// give a volume to set the thumb position
// cannot be set if 'manualSet' var is false
// to avoid infinite loop
VolumeBar.prototype.setManual = function(value){
if(this.manualSet === true){
this.value = value;
this.thumbLeft = this.value * this.width;
this.updateDisplay();
}
}
// if localStoarge is enabled
// set volume and display to
// value in localStorage
VolumeBar.prototype.initLocalStorage = function(){
if(this.useLocalStorage === true){
var value = storage.get(this.localStorageNS + '_volume');
if(value !== null){
if(this.audio){
this.audio.volume = value;
this.setManual(value);
}
}
}
}
// check if we've got require
if(typeof module !== "undefined"){
module.exports = VolumeBar;
}
else{
window.VolumeBar = VolumeBar;
}
}()); // end wrapper