author | ymh <ymh.work@gmail.com> |
Tue, 20 Mar 2018 15:02:24 +0100 | |
changeset 572 | 190ae1dee68d |
parent 480 | 814468b0fc69 |
permissions | -rw-r--r-- |
477 | 1 |
import Ember from 'ember'; |
2 |
import * as d3s from 'd3-scale'; |
|
3 |
||
4 |
const speakerClassScale = d3s.scaleQuantize() |
|
5 |
.domain([0, 1]) |
|
6 |
.range(['fa-volume-down', 'fa-volume-up']); |
|
7 |
||
8 |
||
9 |
export default Ember.Component.extend({ |
|
10 |
classNames: ['player-sound-control'], |
|
11 |
||
12 |
popcorn: null, |
|
13 |
||
14 |
volume: Ember.computed('popcorn', { |
|
15 |
get: function() { |
|
16 |
let popcorn = this.get('popcorn'); |
|
17 |
if(!popcorn) { |
|
18 |
return 0; |
|
19 |
} |
|
20 |
return popcorn.volume(); |
|
21 |
}, |
|
22 |
set: function(key, value) { |
|
23 |
let popcorn = this.get('popcorn'); |
|
24 |
if(!popcorn) { |
|
25 |
return 0; |
|
26 |
} |
|
27 |
const newValue = Math.min(1.0, Math.max(0, value)); |
|
28 |
popcorn.volume(newValue); |
|
29 |
return newValue; |
|
30 |
} |
|
31 |
}), |
|
32 |
||
33 |
muted: Ember.computed('popcorn', { |
|
34 |
get: function() { |
|
35 |
let popcorn = this.get('popcorn'); |
|
36 |
if(!popcorn) { |
|
37 |
return false; |
|
38 |
} |
|
39 |
return popcorn.muted(); |
|
40 |
}, |
|
41 |
set: function(key, value) { |
|
42 |
let popcorn = this.get('popcorn'); |
|
43 |
if(!popcorn) { |
|
44 |
return false; |
|
45 |
} |
|
46 |
if(value) { |
|
47 |
popcorn.mute(); |
|
48 |
} else { |
|
49 |
popcorn.unmute(); |
|
50 |
} |
|
51 |
return value; |
|
52 |
} |
|
53 |
||
54 |
}), |
|
55 |
||
480
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
56 |
mutedObserver: Ember.observer('muted', function() { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
57 |
const rangeslider = this.$("#sound-control-range-slider"); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
58 |
if(!rangeslider) { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
59 |
return; |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
60 |
} |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
61 |
rangeslider.rangeslider('update', true); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
62 |
}), |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
63 |
|
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
64 |
volumeObserver: Ember.observer('volume', function() { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
65 |
const rangeslider = this.$("#sound-control-range-slider"); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
66 |
const volume = this.get('volume'); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
67 |
if(!rangeslider) { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
68 |
return; |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
69 |
} |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
70 |
rangeslider.val(volume).change(); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
71 |
}), |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
72 |
|
477 | 73 |
popcornObserver: Ember.observer('popcorn', function() { |
74 |
let popcorn = this.get('popcorn'); |
|
75 |
if(!popcorn) { |
|
76 |
return; |
|
77 |
} |
|
78 |
popcorn.on('volumechange', () => { this.notifyPropertyChange('volume'); this.notifyPropertyChange('muted'); }); |
|
79 |
||
80 |
}), |
|
81 |
||
82 |
speakerClass: Ember.computed('volume', 'muted', function() { |
|
83 |
const volume = this.get('volume'); |
|
84 |
const muted = this.get('muted'); |
|
85 |
if(muted || volume === 0 ) { |
|
86 |
return "fa-volume-off"; |
|
87 |
} else { |
|
88 |
return speakerClassScale(volume); |
|
89 |
} |
|
90 |
}), |
|
91 |
||
92 |
mouseEnter: function() { |
|
93 |
let baseOffset = this.$("#sound-control-speaker").offset(); |
|
94 |
if(this.get('muted')) { |
|
95 |
return; |
|
96 |
} |
|
97 |
this.$("#sound-control-scale").show(400); |
|
98 |
this.$("#sound-control-scale").offset({ top: baseOffset.top + 55, left: baseOffset.left}); |
|
99 |
}, |
|
100 |
mouseLeave: function() { |
|
101 |
this.$("#sound-control-scale").hide(400); |
|
102 |
}, |
|
103 |
||
104 |
didInsertElement: function() { |
|
480
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
105 |
const volume = this.get('volume'); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
106 |
let rangeslider = this.$('#sound-control-range-slider'); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
107 |
rangeslider.rangeslider({ |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
108 |
polyfill: false, |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
109 |
onSlide: (pos, value) => { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
110 |
this.set('volume', value); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
111 |
}, |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
112 |
onSlideEnd: (pos, value) => { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
113 |
this.set('volume', value); |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
114 |
}, |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
115 |
onInit: function() { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
116 |
Ember.$(this).value = volume; |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
117 |
} |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
118 |
}); |
477 | 119 |
}, |
120 |
||
121 |
willDestroyElement: function() { |
|
480
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
122 |
this.$('#sound-control-range-slider').rangeslider('destroy'); |
477 | 123 |
}, |
124 |
||
125 |
actions: { |
|
126 |
muteToggle() { |
|
480
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
127 |
if(!this.get('volume')) { |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
128 |
return; |
814468b0fc69
use rangeslider.js polyfill to ensure cross browser compatibility
ymh <ymh.work@gmail.com>
parents:
477
diff
changeset
|
129 |
} |
477 | 130 |
this.set('muted', !this.get('muted')); |
131 |
}, |
|
132 |
clickMinus() { |
|
133 |
if(this.get('muted')) { |
|
134 |
return; |
|
135 |
} |
|
136 |
let volume = this.get('volume'); |
|
137 |
this.set('volume', volume - 0.1); |
|
138 |
}, |
|
139 |
clickPlus() { |
|
140 |
if(this.get('muted')) { |
|
141 |
return; |
|
142 |
} |
|
143 |
let volume = this.get('volume'); |
|
144 |
this.set('volume', volume + 0.1); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
}); |