OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function() { | 5 (function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * @constructor | 9 * @constructor |
10 * @extends {PolymerElement} | 10 * @extends {PolymerElement} |
11 */ | 11 */ |
12 var VolumeControllerElement = function() {}; | 12 var VolumeControllerElement = function() {}; |
13 | 13 |
14 VolumeControllerElement.prototype = { | 14 VolumeControllerElement.prototype = { |
| 15 is: 'volume-controller', |
| 16 |
| 17 properties: { |
| 18 /** |
| 19 * Width of the element in pixels. Must be specified before ready() is |
| 20 * called. Dynamic change is not supported. |
| 21 * @type {number} |
| 22 */ |
| 23 width: { |
| 24 type: Number, |
| 25 value: 32 |
| 26 }, |
| 27 |
| 28 /** |
| 29 * Height of the element in pixels. Must be specified before ready() is |
| 30 * called. Dynamic change is not supported. |
| 31 * @type {number} |
| 32 */ |
| 33 height: { |
| 34 type: Number, |
| 35 value: 100 |
| 36 }, |
| 37 |
| 38 /** |
| 39 * Volume. 0 is silent, and 100 is maximum. |
| 40 * @type {number} |
| 41 */ |
| 42 value: { |
| 43 type: Number, |
| 44 value: 50, |
| 45 observer: 'valueChanged', |
| 46 notify: true |
| 47 }, |
| 48 |
| 49 /** |
| 50 * Volume. 100 is silent, and 0 is maximum. |
| 51 * @type {number} |
| 52 */ |
| 53 rawValue: { |
| 54 type: Number, |
| 55 value: 0, |
| 56 observer: 'rawValueChanged', |
| 57 notify: true |
| 58 } |
| 59 }, |
| 60 |
15 /** | 61 /** |
16 * Initializes an element. This method is called automatically when the | 62 * Initializes an element. This method is called automatically when the |
17 * element is ready. | 63 * element is ready. |
18 */ | 64 */ |
19 ready: function() { | 65 ready: function() { |
20 this.style.width = this.width + 'px'; | 66 this.style.width = this.width + 'px'; |
21 this.style.height = this.height + 'px'; | 67 this.style.height = this.height + 'px'; |
22 | 68 |
23 this.rawValueInput = this.$.rawValueInput; | 69 this.rawValueInput = this.$.rawValueInput; |
24 this.bar = this.$.bar; | 70 this.bar = this.$.bar; |
25 | 71 |
26 this.rawValueInput.style.width = this.height + 'px'; | 72 this.rawValueInput.style.width = this.height + 'px'; |
27 this.rawValueInput.style.height = this.width + 'px'; | 73 this.rawValueInput.style.height = this.width + 'px'; |
28 this.rawValueInput.style.webkitTransformOrigin = | 74 this.rawValueInput.style.webkitTransformOrigin = |
29 (this.width / 2) + 'px ' + | 75 (this.width / 2) + 'px ' + |
30 (this.width / 2 - 2) + 'px'; | 76 (this.width / 2 - 2) + 'px'; |
31 | 77 |
32 var barLeft = (this.width / 2 - 1); | 78 var barLeft = (this.width / 2 - 1); |
33 this.bar.style.left = barLeft + 'px'; | 79 this.bar.style.left = barLeft + 'px'; |
34 this.bar.style.right = barLeft + 'px'; | 80 this.bar.style.right = barLeft + 'px'; |
35 | 81 |
36 this.addEventListener('keydown', this.onKeyDown_.bind(this)); | 82 this.addEventListener('keydown', this.onKeyDown_.bind(this)); |
37 }, | 83 }, |
38 | 84 |
39 /** | 85 /** |
40 * Registers handlers for changing of external variables | 86 * Invoked when the 'volume' value is changed. |
| 87 * @param {number} newValue New value. |
| 88 * @param {number} oldValue Old value. |
41 */ | 89 */ |
42 observe: { | 90 valueChanged: function(newValue, oldValue) { |
43 'model.volume': 'onVolumeChanged', | |
44 }, | |
45 | |
46 /** | |
47 * Model object of the Audio Player. | |
48 * @type {AudioPlayerModel} | |
49 */ | |
50 model: null, | |
51 | |
52 /** | |
53 * Invoked when the model changed. | |
54 * @param {AudioPlayerModel} oldValue Old Value. | |
55 * @param {AudioPlayerModel} newValue New Value. | |
56 */ | |
57 modelChanged: function(oldValue, newValue) { | |
58 this.onVolumeChanged((oldValue || {}).volume, (newValue || {}).volume); | |
59 }, | |
60 | |
61 /** | |
62 * Volume. 0 is silent, and 100 is maximum. | |
63 * @type {number} | |
64 */ | |
65 value: 50, | |
66 | |
67 /** | |
68 * Volume. 1000 is silent, and 0 is maximum. | |
69 * @type {number} | |
70 */ | |
71 rawValue: 0, | |
72 | |
73 /** | |
74 * Height of the element in pixels. Must be specified before ready() is | |
75 * called. Dynamic change is not supported. | |
76 * @type {number} | |
77 */ | |
78 height: 100, | |
79 | |
80 /** | |
81 * Width of the element in pixels. Must be specified before ready() is | |
82 * called. Dynamic change is not supported. | |
83 * @type {number} | |
84 */ | |
85 width: 32, | |
86 | |
87 /** | |
88 * Invoked when the 'volume' value in the model is changed. | |
89 * @param {number} oldValue Old value. | |
90 * @param {number} newValue New value. | |
91 */ | |
92 onVolumeChanged: function(oldValue, newValue) { | |
93 if (oldValue != newValue) | 91 if (oldValue != newValue) |
94 this.rawValue = 100 - newValue; | 92 this.rawValue = 100 - newValue; |
95 }, | 93 }, |
96 | 94 |
97 /** | 95 /** |
98 * Invoked when the 'rawValue' property is changed. | 96 * Invoked when the 'rawValue' property is changed. |
| 97 * @param {number} newValue New value. |
99 * @param {number} oldValue Old value. | 98 * @param {number} oldValue Old value. |
100 * @param {number} newValue New value. | |
101 */ | 99 */ |
102 rawValueChanged: function(oldValue, newValue) { | 100 rawValueChanged: function(newValue, oldValue) { |
103 if (oldValue != newValue) | 101 if (oldValue !== newValue) |
104 this.model.volume = 100 - newValue; | 102 this.value = 100 - newValue; |
105 }, | 103 }, |
106 | 104 |
107 /** | 105 /** |
108 * Invoked when the 'keydown' event is fired. | 106 * Invoked when the 'keydown' event is fired. |
109 * @param {Event} event The event object. | 107 * @param {Event} event The event object. |
110 */ | 108 */ |
111 onKeyDown_: function(event) { | 109 onKeyDown_: function(event) { |
112 switch (event.keyIdentifier) { | 110 switch (event.keyIdentifier) { |
113 // Prevents the default behavior. These key should be handled in | 111 // Prevents the default behavior. These key should be handled in |
114 // <audio-player> element. | 112 // <audio-player> element. |
115 case 'Up': | 113 case 'Up': |
116 case 'Down': | 114 case 'Down': |
117 case 'PageUp': | 115 case 'PageUp': |
118 case 'PageDown': | 116 case 'PageDown': |
119 event.preventDefault(); | 117 event.preventDefault(); |
120 break; | 118 break; |
121 } | 119 } |
122 }, | 120 }, |
| 121 |
| 122 /** |
| 123 * Computes style for '.filled' element based on raw value. |
| 124 * @return {string} |
| 125 */ |
| 126 computeFilledStyle_: function(rawValue) { |
| 127 return 'height: ' + rawValue + '%;'; |
| 128 } |
123 }; | 129 }; |
124 | 130 |
125 Polymer('volume-controller', VolumeControllerElement.prototype); | 131 Polymer(VolumeControllerElement.prototype); |
126 })(); // Anonymous closure | 132 })(); // Anonymous closure |
OLD | NEW |