| 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 /** | 5 /** |
| 6 * Overrided metadata worker's path. | 6 * Overrided metadata worker's path. |
| 7 * @type {string} | 7 * @type {string} |
| 8 */ | 8 */ |
| 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; | 9 ContentMetadataProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * @param {Element} container Container element. | 12 * @param {Element} container Container element. |
| 13 * @constructor | 13 * @constructor |
| 14 */ | 14 */ |
| 15 function AudioPlayer(container) { | 15 function AudioPlayer(container) { |
| 16 this.container_ = container; | 16 this.container_ = container; |
| 17 this.volumeManager_ = new VolumeManagerWrapper( | 17 this.volumeManager_ = new VolumeManagerWrapper( |
| 18 VolumeManagerWrapper.NonNativeVolumeStatus.ENABLED); | 18 VolumeManagerWrapper.NonNativeVolumeStatus.ENABLED); |
| 19 this.metadataModel_ = MetadataModel.create(this.volumeManager_); | 19 this.metadataModel_ = MetadataModel.create(this.volumeManager_); |
| 20 this.selectedEntry_ = null; | 20 this.selectedEntry_ = null; |
| 21 this.invalidTracks_ = {}; | 21 this.invalidTracks_ = {}; |
| 22 | |
| 23 this.model_ = new AudioPlayerModel(); | |
| 24 Object.observe(this.model_, function(changes) { | |
| 25 for (var i = 0; i < changes.length; i++) { | |
| 26 var change = changes[i]; | |
| 27 if (change.name == 'expanded' && | |
| 28 (change.type == 'add' || change.type == 'update')) { | |
| 29 this.onModelExpandedChanged(change.oldValue, change.object.expanded); | |
| 30 break; | |
| 31 } | |
| 32 } | |
| 33 }.bind(this)); | |
| 34 | |
| 35 this.entries_ = []; | 22 this.entries_ = []; |
| 36 this.currentTrackIndex_ = -1; | 23 this.currentTrackIndex_ = -1; |
| 37 this.playlistGeneration_ = 0; | 24 this.playlistGeneration_ = 0; |
| 38 | 25 |
| 39 /** | 26 /** |
| 40 * Whether if the playlist is expanded or not. This value is changed by | 27 * Whether if the playlist is expanded or not. This value is changed by |
| 41 * this.syncExpanded(). | 28 * this.syncExpanded(). |
| 42 * True: expanded, false: collapsed, null: unset. | 29 * True: expanded, false: collapsed, null: unset. |
| 43 * | 30 * |
| 44 * @type {?boolean} | 31 * @type {?boolean} |
| 45 * @private | 32 * @private |
| 46 */ | 33 */ |
| 47 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). | 34 this.isExpanded_ = null; // Initial value is null. It'll be set in load(). |
| 48 | 35 |
| 49 this.player_ = | 36 this.player_ = |
| 50 /** @type {AudioPlayerElement} */ (document.querySelector('audio-player')); | 37 /** @type {AudioPlayerElement} */ (document.querySelector('audio-player')); |
| 51 // TODO(yoshiki): Move tracks into the model. | |
| 52 this.player_.tracks = []; | 38 this.player_.tracks = []; |
| 53 this.model_.initialize(function() { | 39 |
| 54 this.player_.model = this.model_; | 40 // Restore the saved state from local storage, and update the local storage |
| 41 // if the states are changed. |
| 42 var STORAGE_PREFIX = 'audioplayer-'; |
| 43 var KEYS_TO_SAVE_STATES = ['shuffle', 'repeat', 'volume', 'expanded']; |
| 44 var storageKeys = KEYS_TO_SAVE_STATES.map(a => STORAGE_PREFIX + a); |
| 45 chrome.storage.local.get(storageKeys, function(results) { |
| 46 // Update the UI by loaded state. |
| 47 for (var storageKey in results) { |
| 48 var key = storageKey.substr(STORAGE_PREFIX.length); |
| 49 this.player_[key] = results[storageKey]; |
| 50 } |
| 51 // Start listening to UI changes to write back the states to local storage. |
| 52 for (var i = 0; i < KEYS_TO_SAVE_STATES.length; i++) { |
| 53 this.player_.addEventListener( |
| 54 KEYS_TO_SAVE_STATES[i] + '-changed', |
| 55 function(storageKey, event) { |
| 56 var objectToBeSaved = {}; |
| 57 objectToBeSaved[storageKey] = event.detail.value; |
| 58 chrome.storage.local.set(objectToBeSaved); |
| 59 }.bind(this, storageKeys[i])); |
| 60 } |
| 61 }.bind(this)); |
| 62 |
| 63 // Update the window size when UI's 'expanded' state is changed. |
| 64 this.player_.addEventListener('expanded-changed', function(event) { |
| 65 this.onExpandedChanged_(event.detail.value); |
| 55 }.bind(this)); | 66 }.bind(this)); |
| 56 | 67 |
| 57 // Run asynchronously after an event of model change is delivered. | 68 // Run asynchronously after an event of model change is delivered. |
| 58 setTimeout(function() { | 69 setTimeout(function() { |
| 59 this.errorString_ = ''; | 70 this.errorString_ = ''; |
| 60 this.offlineString_ = ''; | 71 this.offlineString_ = ''; |
| 61 chrome.fileManagerPrivate.getStrings(function(strings) { | 72 chrome.fileManagerPrivate.getStrings(function(strings) { |
| 62 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; | 73 container.ownerDocument.title = strings['AUDIO_PLAYER_TITLE']; |
| 63 this.errorString_ = strings['AUDIO_ERROR']; | 74 this.errorString_ = strings['AUDIO_ERROR']; |
| 64 this.offlineString_ = strings['AUDIO_OFFLINE']; | 75 this.offlineString_ = strings['AUDIO_OFFLINE']; |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 /** | 376 /** |
| 366 * Minimum size of the window in the expanded mode in pixels. | 377 * Minimum size of the window in the expanded mode in pixels. |
| 367 * @type {number} | 378 * @type {number} |
| 368 * @const | 379 * @const |
| 369 */ | 380 */ |
| 370 AudioPlayer.EXPANDED_MODE_MIN_HEIGHT = AudioPlayer.CONTROLS_HEIGHT + | 381 AudioPlayer.EXPANDED_MODE_MIN_HEIGHT = AudioPlayer.CONTROLS_HEIGHT + |
| 371 AudioPlayer.TRACK_HEIGHT * 2; | 382 AudioPlayer.TRACK_HEIGHT * 2; |
| 372 | 383 |
| 373 /** | 384 /** |
| 374 * Invoked when the 'expanded' property in the model is changed. | 385 * Invoked when the 'expanded' property in the model is changed. |
| 375 * @param {boolean} oldValue Old value. | |
| 376 * @param {boolean} newValue New value. | 386 * @param {boolean} newValue New value. |
| 387 * @private |
| 377 */ | 388 */ |
| 378 AudioPlayer.prototype.onModelExpandedChanged = function(oldValue, newValue) { | 389 AudioPlayer.prototype.onExpandedChanged_ = function(newValue) { |
| 379 if (this.isExpanded_ !== null && | 390 if (this.isExpanded_ !== null && |
| 380 this.isExpanded_ === newValue) | 391 this.isExpanded_ === newValue) |
| 381 return; | 392 return; |
| 382 | 393 |
| 383 if (this.isExpanded_ && !newValue) | 394 if (this.isExpanded_ && !newValue) |
| 384 this.lastExpandedHeight_ = window.innerHeight; | 395 this.lastExpandedHeight_ = window.innerHeight; |
| 385 | 396 |
| 386 if (this.isExpanded_ !== newValue) { | 397 if (this.isExpanded_ !== newValue) { |
| 387 this.isExpanded_ = newValue; | 398 this.isExpanded_ = newValue; |
| 388 this.syncHeight_(); | 399 this.syncHeight_(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 // TODO(yoshiki): Handle error in better way. | 478 // TODO(yoshiki): Handle error in better way. |
| 468 // TODO(yoshiki): implement artwork (metadata.thumbnail) | 479 // TODO(yoshiki): implement artwork (metadata.thumbnail) |
| 469 this.title = metadata.mediaTitle || this.getDefaultTitle(); | 480 this.title = metadata.mediaTitle || this.getDefaultTitle(); |
| 470 this.artist = error || metadata.mediaArtist || this.getDefaultArtist(); | 481 this.artist = error || metadata.mediaArtist || this.getDefaultArtist(); |
| 471 }; | 482 }; |
| 472 | 483 |
| 473 // Starts loading the audio player. | 484 // Starts loading the audio player. |
| 474 window.addEventListener('DOMContentLoaded', function(e) { | 485 window.addEventListener('DOMContentLoaded', function(e) { |
| 475 AudioPlayer.load(); | 486 AudioPlayer.load(); |
| 476 }); | 487 }); |
| OLD | NEW |