OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @param {HTMLElement} container Container element. | 8 * @param {HTMLElement} container Container element. |
9 * @constructor | 9 * @constructor |
10 */ | 10 */ |
11 function AudioPlayer(container) { | 11 function AudioPlayer(container) { |
12 this.container_ = container; | 12 this.container_ = container; |
13 this.volumeManager_ = new VolumeManagerWrapper( | 13 this.volumeManager_ = new VolumeManagerWrapper( |
14 VolumeManagerWrapper.DriveEnabledStatus.DRIVE_ENABLED); | 14 VolumeManagerWrapper.DriveEnabledStatus.DRIVE_ENABLED); |
15 this.metadataCache_ = MetadataCache.createFull(this.volumeManager_); | 15 this.metadataCache_ = MetadataCache.createFull(this.volumeManager_); |
16 this.selectedEntry_ = null; | 16 this.selectedEntry_ = null; |
17 | 17 |
18 this.model_ = new AudioPlayerModel(); | 18 this.model_ = new AudioPlayerModel(); |
19 var observer = new PathObserver(this.model_, 'expanded'); | 19 var observer = new PathObserver(this.model_, 'expanded'); |
20 observer.open(function(newValue, oldValue) { | 20 observer.open(function(newValue, oldValue) { |
21 // Inverse arguments intentionally to match the Polymer way. | 21 // Inverse arguments intentionally to match the Polymer way. |
22 this.onModelExpandedChanged(oldValue, newValue); | 22 this.onModelExpandedChanged(oldValue, newValue); |
23 }.bind(this)); | 23 }.bind(this)); |
24 this.trackListItems_ = []; | |
25 | 24 |
26 this.entries_ = []; | 25 this.entries_ = []; |
27 this.currentTrackIndex_ = -1; | 26 this.currentTrackIndex_ = -1; |
28 this.playlistGeneration_ = 0; | 27 this.playlistGeneration_ = 0; |
29 | 28 |
30 /** | 29 /** |
31 * Whether if the playlist is expanded or not. This value is changed by | 30 * Whether if the playlist is expanded or not. This value is changed by |
32 * this.syncExpanded(). | 31 * this.syncExpanded(). |
33 * True: expanded, false: collapsed, null: unset. | 32 * True: expanded, false: collapsed, null: unset. |
34 * | 33 * |
35 * @type {?boolean} | 34 * @type {?boolean} |
36 * @private | 35 * @private |
37 */ | 36 */ |
38 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). | 37 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). |
39 | 38 |
40 this.player_ = document.querySelector('audio-player'); | 39 this.player_ = document.querySelector('audio-player'); |
41 this.player_.tracks = this.trackListItems_; | 40 // TODO(yoshiki): Move tracks into the model. |
| 41 this.player_.tracks = []; |
42 this.player_.model = this.model_; | 42 this.player_.model = this.model_; |
43 Platform.performMicrotaskCheckpoint(); | 43 Platform.performMicrotaskCheckpoint(); |
44 | 44 |
45 this.errorString_ = ''; | 45 this.errorString_ = ''; |
46 this.offlineString_ = ''; | 46 this.offlineString_ = ''; |
47 chrome.fileBrowserPrivate.getStrings(function(strings) { | 47 chrome.fileBrowserPrivate.getStrings(function(strings) { |
48 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; | 48 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; |
49 this.errorString_ = strings['AUDIO_ERROR']; | 49 this.errorString_ = strings['AUDIO_ERROR']; |
50 this.offlineString_ = strings['AUDIO_OFFLINE']; | 50 this.offlineString_ = strings['AUDIO_OFFLINE']; |
51 AudioPlayer.TrackInfo.DEFAULT_ARTIST = | 51 AudioPlayer.TrackInfo.DEFAULT_ARTIST = |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 */ | 96 */ |
97 AudioPlayer.prototype.load = function(playlist) { | 97 AudioPlayer.prototype.load = function(playlist) { |
98 this.playlistGeneration_++; | 98 this.playlistGeneration_++; |
99 this.currentTrackIndex_ = -1; | 99 this.currentTrackIndex_ = -1; |
100 | 100 |
101 // Save the app state, in case of restart. Make a copy of the object, so the | 101 // Save the app state, in case of restart. Make a copy of the object, so the |
102 // playlist member is not changed after entries are resolved. | 102 // playlist member is not changed after entries are resolved. |
103 window.appState = JSON.parse(JSON.stringify(playlist)); // cloning | 103 window.appState = JSON.parse(JSON.stringify(playlist)); // cloning |
104 util.saveAppState(); | 104 util.saveAppState(); |
105 | 105 |
| 106 this.isExpanded_ = this.model_.expanded; |
| 107 |
106 // Resolving entries has to be done after the volume manager is initialized. | 108 // Resolving entries has to be done after the volume manager is initialized. |
107 this.volumeManager_.ensureInitialized(function() { | 109 this.volumeManager_.ensureInitialized(function() { |
108 util.URLsToEntries(playlist.items, function(entries) { | 110 util.URLsToEntries(playlist.items, function(entries) { |
109 this.entries_ = entries; | 111 this.entries_ = entries; |
110 | 112 |
111 var position = playlist.position || 0; | 113 var position = playlist.position || 0; |
112 var time = playlist.time || 0; | 114 var time = playlist.time || 0; |
113 | 115 |
114 if (this.entries_.length == 0) | 116 if (this.entries_.length == 0) |
115 return; | 117 return; |
116 | 118 |
117 this.trackListItems_.splice(0); | 119 var newTracks = []; |
118 | 120 |
119 for (var i = 0; i != this.entries_.length; i++) { | 121 for (var i = 0; i != this.entries_.length; i++) { |
120 var entry = this.entries_[i]; | 122 var entry = this.entries_[i]; |
121 var onClick = this.select_.bind(this, i, false /* no restore */); | 123 var onClick = this.select_.bind(this, i, false /* no restore */); |
122 this.trackListItems_.push(new AudioPlayer.TrackInfo(entry, onClick)); | 124 newTracks.push(new AudioPlayer.TrackInfo(entry, onClick)); |
123 } | 125 } |
124 | 126 |
| 127 this.player_.tracks = newTracks; |
| 128 |
125 // Makes it sure that the handler of the track list is called, before the | 129 // Makes it sure that the handler of the track list is called, before the |
126 // handler of the track index. | 130 // handler of the track index. |
127 Platform.performMicrotaskCheckpoint(); | 131 Platform.performMicrotaskCheckpoint(); |
128 | 132 |
129 this.select_(position, !!time); | 133 this.select_(position, !!time); |
130 | 134 |
131 // Load the selected track metadata first, then load the rest. | 135 // Load the selected track metadata first, then load the rest. |
132 this.loadMetadata_(position); | 136 this.loadMetadata_(position); |
133 for (i = 0; i != this.entries_.length; i++) { | 137 for (i = 0; i != this.entries_.length; i++) { |
134 if (i != position) | 138 if (i != position) |
(...skipping 14 matching lines...) Expand all Loading... |
149 }; | 153 }; |
150 | 154 |
151 /** | 155 /** |
152 * Displays track's metadata. | 156 * Displays track's metadata. |
153 * @param {number} track Track number. | 157 * @param {number} track Track number. |
154 * @param {Object} metadata Metadata object. | 158 * @param {Object} metadata Metadata object. |
155 * @param {string=} opt_error Error message. | 159 * @param {string=} opt_error Error message. |
156 * @private | 160 * @private |
157 */ | 161 */ |
158 AudioPlayer.prototype.displayMetadata_ = function(track, metadata, opt_error) { | 162 AudioPlayer.prototype.displayMetadata_ = function(track, metadata, opt_error) { |
159 this.trackListItems_[track].setMetadata(metadata, opt_error); | 163 this.player_.tracks[track].setMetadata(metadata, opt_error); |
160 }; | 164 }; |
161 | 165 |
162 /** | 166 /** |
163 * Closes audio player when a volume containing the selected item is unmounted. | 167 * Closes audio player when a volume containing the selected item is unmounted. |
164 * @param {Event} event The unmount event. | 168 * @param {Event} event The unmount event. |
165 * @private | 169 * @private |
166 */ | 170 */ |
167 AudioPlayer.prototype.onExternallyUnmounted_ = function(event) { | 171 AudioPlayer.prototype.onExternallyUnmounted_ = function(event) { |
168 if (!this.selectedEntry_) | 172 if (!this.selectedEntry_) |
169 return; | 173 return; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 265 } |
262 }; | 266 }; |
263 | 267 |
264 /* Keep the below constants in sync with the CSS. */ | 268 /* Keep the below constants in sync with the CSS. */ |
265 | 269 |
266 /** | 270 /** |
267 * Window header size in pixels. | 271 * Window header size in pixels. |
268 * @type {number} | 272 * @type {number} |
269 * @const | 273 * @const |
270 */ | 274 */ |
271 AudioPlayer.HEADER_HEIGHT = 28; | 275 AudioPlayer.HEADER_HEIGHT = 33; // 32px + border 1px |
272 | 276 |
273 /** | 277 /** |
274 * Track height in pixels. | 278 * Track height in pixels. |
275 * @type {number} | 279 * @type {number} |
276 * @const | 280 * @const |
277 */ | 281 */ |
278 AudioPlayer.TRACK_HEIGHT = 44; | 282 AudioPlayer.TRACK_HEIGHT = 44; |
279 | 283 |
280 /** | 284 /** |
281 * Controls bar height in pixels. | 285 * Controls bar height in pixels. |
282 * @type {number} | 286 * @type {number} |
283 * @const | 287 * @const |
284 */ | 288 */ |
285 AudioPlayer.CONTROLS_HEIGHT = 72; | 289 AudioPlayer.CONTROLS_HEIGHT = 73; // 72px + border 1px |
286 | 290 |
287 /** | 291 /** |
288 * Default number of items in the expanded mode. | 292 * Default number of items in the expanded mode. |
289 * @type {number} | 293 * @type {number} |
290 * @const | 294 * @const |
291 */ | 295 */ |
292 AudioPlayer.DEFAULT_EXPANDED_ITEMS = 5; | 296 AudioPlayer.DEFAULT_EXPANDED_ITEMS = 5; |
293 | 297 |
294 /** | 298 /** |
295 * Minimum size of the window in the expanded mode in pixels. | 299 * Minimum size of the window in the expanded mode in pixels. |
(...skipping 12 matching lines...) Expand all Loading... |
308 if (this.isExpanded_ !== null && | 312 if (this.isExpanded_ !== null && |
309 this.isExpanded_ === newValue) | 313 this.isExpanded_ === newValue) |
310 return; | 314 return; |
311 | 315 |
312 if (this.isExpanded_ && !newValue) | 316 if (this.isExpanded_ && !newValue) |
313 this.lastExpandedHeight_ = window.innerHeight; | 317 this.lastExpandedHeight_ = window.innerHeight; |
314 | 318 |
315 if (this.isExpanded_ !== newValue) { | 319 if (this.isExpanded_ !== newValue) { |
316 this.isExpanded_ = newValue; | 320 this.isExpanded_ = newValue; |
317 this.syncHeight_(); | 321 this.syncHeight_(); |
| 322 |
| 323 // Saves new state. |
| 324 window.appState.expanded = newValue; |
| 325 util.saveAppState(); |
318 } | 326 } |
319 }; | 327 }; |
320 | 328 |
321 /** | 329 /** |
322 * @private | 330 * @private |
323 */ | 331 */ |
324 AudioPlayer.prototype.syncHeight_ = function() { | 332 AudioPlayer.prototype.syncHeight_ = function() { |
325 var targetHeight; | 333 var targetHeight; |
326 | 334 |
327 if (this.model_.expanded) { | 335 if (this.model_.expanded) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 this.title = (metadata.media && metadata.media.title) || | 413 this.title = (metadata.media && metadata.media.title) || |
406 this.getDefaultTitle(); | 414 this.getDefaultTitle(); |
407 this.artist = error || | 415 this.artist = error || |
408 (metadata.media && metadata.media.artist) || this.getDefaultArtist(); | 416 (metadata.media && metadata.media.artist) || this.getDefaultArtist(); |
409 }; | 417 }; |
410 | 418 |
411 // Starts loading the audio player. | 419 // Starts loading the audio player. |
412 window.addEventListener('WebComponentsReady', function(e) { | 420 window.addEventListener('WebComponentsReady', function(e) { |
413 AudioPlayer.load(); | 421 AudioPlayer.load(); |
414 }); | 422 }); |
OLD | NEW |