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
3 // found in the LICENSE file.
9 * Moves |target| element above |anchor| element, in order to match the
11 * @param {HTMLElement} target Target element.
12 * @param {HTMLElement} anchor Anchor element.
14 function matchBottomLine(target, anchor) {
15 var targetRect = target.getBoundingClientRect();
16 var anchorRect = anchor.getBoundingClientRect();
19 left: anchorRect.left + anchorRect.width / 2 - targetRect.width / 2,
20 bottom: window.innerHeight - anchorRect.bottom,
23 target.style.position = 'fixed';
24 target.style.left = pos.left + 'px';
25 target.style.bottom = pos.bottom + 'px';
29 * Converts the time into human friendly string.
30 * @param {number} time Time to be converted.
31 * @return {string} String representation of the given time
33 function time2string(time) {
34 return ~~(time / 60000) + ':' + ('0' + ~~(time / 1000 % 60)).slice(-2);
39 * @extends {PolymerElement}
41 var ControlPanelElement = function() {};
43 ControlPanelElement.prototype = {
45 * Initializes an element. This method is called automatically when the
49 var onFocusoutBound = this.onVolumeControllerFocusout_.bind(this);
51 this.volumeSlider = this.$.volumeSlider;
52 this.volumeButton = this.$.volumeButton;
53 this.volumeContainer = this.$.volumeContainer;
55 this.volumeSlider.addEventListener('focusout', onFocusoutBound);
56 this.volumeButton.addEventListener('focusout', onFocusoutBound);
60 * Model object of the Audio Player.
61 * @type {AudioPlayerModel}
66 * Invoked when the model changed.
67 * @param {AudioPlayerModel} oldValue Old Value.
68 * @param {AudioPlayerModel} newValue New Value.
70 modelChanged: function(oldValue, newValue) {
71 this.volumeSlider.model = newValue;
75 * Current elapsed time in the current music in millisecond.
81 * String representation of 'time'.
86 return time2string(this.time);
90 * Total length of the current music in millisecond.
96 * String representation of 'duration'.
100 get durationString_() {
101 return time2string(this.duration);
105 * Flag whether the volume slider is expanded or not.
108 volumeSliderShown: false,
111 * Flag whether the audio is playing or paused. True if playing, or false
118 * Invoked when the next button is clicked.
120 nextClick: function() {
121 this.fire('next-clicked');
125 * Invoked when the play button is clicked.
127 playClick: function() {
128 this.playing = !this.playing;
132 * Invoked when the previous button is clicked.
134 previousClick: function() {
135 this.fire('previous-clicked');
139 * Invoked the volume button is clicked.
140 * @param {!Event} event The event.
142 volumeButtonClick: function(event) {
143 this.showVolumeController_(this.volumeSliderShown);
144 event.stopPropagation();
148 * Invoked when the focus goes out of the volume elements.
149 * @param {!FocusEvent} event The focusout event.
152 onVolumeControllerFocusout_: function(event) {
153 if (this.volumeSliderShown) {
154 // If the focus goes out of the volume, hide the volume control.
155 if (!event.relatedTarget ||
156 (event.relatedTarget !== this.volumeButton &&
157 event.relatedTarget !== this.volumeSlider)) {
158 this.showVolumeController_(false);
159 this.volumeSliderShown = false;
165 * Shows/hides the volume controller.
166 * @param {boolean} show True to show the controller, false to hide.
169 showVolumeController_: function(show) {
171 matchBottomLine(this.$.volumeContainer, this.$.volumeButton);
172 this.volumeContainer.style.visibility = 'visible';
174 this.volumeContainer.style.visibility = 'hidden';
179 Polymer('control-panel', ControlPanelElement.prototype);
180 })(); // Anonymous closure