| 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 Polymer('track-list', { | 8 Polymer('track-list', { |
| 9 /** | 9 /** |
| 10 * Initializes an element. This method is called automatically when the | 10 * Initializes an element. This method is called automatically when the |
| 11 * element is ready. | 11 * element is ready. |
| 12 */ | 12 */ |
| 13 ready: function() { | 13 ready: function() { |
| 14 this.tracksObserver_ = new ArrayObserver( | 14 this.observeTrackList(); |
| 15 this.tracks, | |
| 16 this.tracksValueChanged_.bind(this)); | |
| 17 | 15 |
| 18 window.addEventListener('resize', this.onWindowResize_.bind(this)); | 16 window.addEventListener('resize', this.onWindowResize_.bind(this)); |
| 19 }, | 17 }, |
| 20 | 18 |
| 19 observeTrackList: function() { |
| 20 // Unobserve the previous track list. |
| 21 if (this.unobserveTrackList_) |
| 22 this.unobserveTrackList_(); |
| 23 |
| 24 // Observe the new track list. |
| 25 var observer = this.tracksValueChanged_.bind(this); |
| 26 Array.observe(this.tracks, observer); |
| 27 |
| 28 // Set the function to unobserve it. |
| 29 this.unobserveTrackList_ = function(tracks, observer) { |
| 30 Array.unobserve(tracks, observer); |
| 31 }.bind(null, this.tracks, observer); |
| 32 }, |
| 33 |
| 21 /** | 34 /** |
| 22 * Registers handlers for changing of external variables | 35 * Registers handlers for changing of external variables |
| 23 */ | 36 */ |
| 24 observe: { | 37 observe: { |
| 25 'model.shuffle': 'onShuffleChanged', | 38 'model.shuffle': 'onShuffleChanged', |
| 26 }, | 39 }, |
| 27 | 40 |
| 28 /** | 41 /** |
| 29 * Model object of the Audio Player. | 42 * Model object of the Audio Player. |
| 30 * @type {AudioPlayerModel} | 43 * @type {AudioPlayerModel} |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 /** | 107 /** |
| 95 * Invoked when 'tracks' property is changed. | 108 * Invoked when 'tracks' property is changed. |
| 96 * @param {Array.<TrackInfo>} oldValue Old value. | 109 * @param {Array.<TrackInfo>} oldValue Old value. |
| 97 * @param {Array.<TrackInfo>} newValue New value. | 110 * @param {Array.<TrackInfo>} newValue New value. |
| 98 */ | 111 */ |
| 99 tracksChanged: function(oldValue, newValue) { | 112 tracksChanged: function(oldValue, newValue) { |
| 100 // Note: Sometimes both oldValue and newValue are null though the actual | 113 // Note: Sometimes both oldValue and newValue are null though the actual |
| 101 // values are not null. Maybe it's a bug of Polymer. | 114 // values are not null. Maybe it's a bug of Polymer. |
| 102 | 115 |
| 103 // Re-register the observer of 'this.tracks'. | 116 // Re-register the observer of 'this.tracks'. |
| 104 this.tracksObserver_.close(); | 117 this.observeTrackList(); |
| 105 this.tracksObserver_ = new ArrayObserver(this.tracks); | |
| 106 this.tracksObserver_.open(this.tracksValueChanged_.bind(this)); | |
| 107 | 118 |
| 108 if (this.tracks.length !== 0) { | 119 if (this.tracks.length !== 0) { |
| 109 // Restore the active track. | 120 // Restore the active track. |
| 110 if (this.currentTrackIndex !== -1 && | 121 if (this.currentTrackIndex !== -1 && |
| 111 this.currentTrackIndex < this.tracks.length) { | 122 this.currentTrackIndex < this.tracks.length) { |
| 112 this.tracks[this.currentTrackIndex].active = true; | 123 this.tracks[this.currentTrackIndex].active = true; |
| 113 } | 124 } |
| 114 | 125 |
| 115 // Reset play order and current index. | 126 // Reset play order and current index. |
| 116 this.generatePlayOrder(false /* no need to keep the current track */); | 127 this.generatePlayOrder(false /* no need to keep the current track */); |
| 117 } else { | 128 } else { |
| 118 this.playOrder = []; | 129 this.playOrder = []; |
| 119 this.currentTrackIndex = -1; | 130 this.currentTrackIndex = -1; |
| 120 } | 131 } |
| 121 }, | 132 }, |
| 122 | 133 |
| 123 /** | 134 /** |
| 124 * Invoked when the value in the 'tracks' is changed. | 135 * Invoked when the value in the 'tracks' is changed. |
| 125 * @param {Array.<Object>} splices The detail of the change. | 136 * @param {Array.<Object>} changes The detail of the change. |
| 126 */ | 137 */ |
| 127 tracksValueChanged_: function(splices) { | 138 tracksValueChanged_: function(changes) { |
| 128 if (this.tracks.length === 0) | 139 if (this.tracks.length === 0) |
| 129 this.currentTrackIndex = -1; | 140 this.currentTrackIndex = -1; |
| 130 else | 141 else |
| 131 this.tracks[this.currentTrackIndex].active = true; | 142 this.tracks[this.currentTrackIndex].active = true; |
| 132 }, | 143 }, |
| 133 | 144 |
| 134 /** | 145 /** |
| 135 * Invoked when the track element is clicked. | 146 * Invoked when the track element is clicked. |
| 136 * @param {Event} event Click event. | 147 * @param {Event} event Click event. |
| 137 */ | 148 */ |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 | 299 |
| 289 var newTrackIndex = this.playOrder[newPlayOrder]; | 300 var newTrackIndex = this.playOrder[newPlayOrder]; |
| 290 console.assert( | 301 console.assert( |
| 291 (0 <= newTrackIndex && newTrackIndex < this.tracks.length), | 302 (0 <= newTrackIndex && newTrackIndex < this.tracks.length), |
| 292 'Insufficient TrackList.playOrder. New Play Order: ' + newPlayOrder); | 303 'Insufficient TrackList.playOrder. New Play Order: ' + newPlayOrder); |
| 293 | 304 |
| 294 return newTrackIndex; | 305 return newTrackIndex; |
| 295 }, | 306 }, |
| 296 }); // Polymer('track-list') block | 307 }); // Polymer('track-list') block |
| 297 })(); // Anonymous closure | 308 })(); // Anonymous closure |
| OLD | NEW |