Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: ui/file_manager/audio_player/elements/track_list.js

Issue 1495873002: AudioPlayer: Stop using Object.observe() and Array.observe(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Listen to property-change events for four states explicitly. Stop using Array.observe. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 /** 5 /**
6 * @typedef {?{ 6 * @typedef {?{
7 * url: string, 7 * url: string,
8 * title: string, 8 * title: string,
9 * artist: string, 9 * artist: string,
10 * artwork: Object, 10 * artwork: Object,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 value: false, 49 value: false,
50 observer: 'shuffleChanged' 50 observer: 'shuffleChanged'
51 } 51 }
52 }, 52 },
53 53
54 /** 54 /**
55 * Initializes an element. This method is called automatically when the 55 * Initializes an element. This method is called automatically when the
56 * element is ready. 56 * element is ready.
57 */ 57 */
58 ready: function() { 58 ready: function() {
59 this.observeTrackList();
60
61 window.addEventListener('resize', this.onWindowResize_.bind(this)); 59 window.addEventListener('resize', this.onWindowResize_.bind(this));
62 }, 60 },
63 61
64 observeTrackList: function() {
65 // Unobserve the previous track list.
66 if (this.unobserveTrackList_)
67 this.unobserveTrackList_();
68
69 // Observe the new track list.
70 var observer = this.tracksValueChanged_.bind(this);
71 Array.observe(this.tracks, observer);
72
73 // Set the function to unobserve it.
74 this.unobserveTrackList_ = function(tracks, observer) {
75 Array.unobserve(tracks, observer);
76 }.bind(null, this.tracks, observer);
77 },
78
79 /** 62 /**
80 * Play order of the tracks. Each value is the index of 'this.tracks'. 63 * Play order of the tracks. Each value is the index of 'this.tracks'.
81 * @type {Array<number>} 64 * @type {Array<number>}
82 */ 65 */
83 playOrder: [], 66 playOrder: [],
84 67
85 /** 68 /**
86 * Invoked when 'shuffle' property is changed. 69 * Invoked when 'shuffle' property is changed.
87 * @param {boolean} newValue New value. 70 * @param {boolean} newValue New value.
88 * @param {boolean} oldValue Old value. 71 * @param {boolean} oldValue Old value.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 106
124 /** 107 /**
125 * Invoked when 'tracks' property is changed. 108 * Invoked when 'tracks' property is changed.
126 * @param {Array<!TrackInfo>} newValue New value. 109 * @param {Array<!TrackInfo>} newValue New value.
127 * @param {Array<!TrackInfo>} oldValue Old value. 110 * @param {Array<!TrackInfo>} oldValue Old value.
128 */ 111 */
129 tracksChanged: function(newValue, oldValue) { 112 tracksChanged: function(newValue, oldValue) {
130 // Note: Sometimes both oldValue and newValue are null though the actual 113 // Note: Sometimes both oldValue and newValue are null though the actual
131 // values are not null. Maybe it's a bug of Polymer. 114 // values are not null. Maybe it's a bug of Polymer.
132 115
133 // Re-register the observer of 'this.tracks'.
134 this.observeTrackList();
135
136 if (this.tracks.length !== 0) { 116 if (this.tracks.length !== 0) {
137 // Restore the active track. 117 // Restore the active track.
138 if (this.currentTrackIndex !== -1 && 118 if (this.currentTrackIndex !== -1 &&
139 this.currentTrackIndex < this.tracks.length) { 119 this.currentTrackIndex < this.tracks.length) {
140 this.set('tracks.' + this.currentTrackIndex + '.active', true); 120 this.set('tracks.' + this.currentTrackIndex + '.active', true);
141 } 121 }
142 122
143 // Reset play order and current index. 123 // Reset play order and current index.
144 this.generatePlayOrder(false /* no need to keep the current track */); 124 this.generatePlayOrder(false /* no need to keep the current track */);
145 } else { 125 } else {
146 this.playOrder = []; 126 this.playOrder = [];
147 this.currentTrackIndex = -1; 127 this.currentTrackIndex = -1;
148 } 128 }
149 }, 129 },
150 130
151 /** 131 /**
152 * Invoked when the value in the 'tracks' is changed.
153 * @param {Array<Object>} changes The detail of the change.
154 */
155 tracksValueChanged_: function(changes) {
156 if (this.tracks.length === 0)
157 this.currentTrackIndex = -1;
158 else
159 this.set('tracks.' + this.currentTrackIndex + '.active', true);
160 },
161
162 /**
163 * Invoked when the track element is clicked. 132 * Invoked when the track element is clicked.
164 * @param {Event} event Click event. 133 * @param {Event} event Click event.
165 */ 134 */
166 trackClicked: function(event) { 135 trackClicked: function(event) {
167 var index = ~~event.currentTarget.getAttribute('index'); 136 var index = ~~event.currentTarget.getAttribute('index');
168 var track = this.tracks[index]; 137 var track = this.tracks[index];
169 if (track) { 138 if (track) {
170 if (event.target.classList.contains('icon')) { 139 if (event.target.classList.contains('icon')) {
171 // If the play icon on the track is clicked, change the current track 140 // If the play icon on the track is clicked, change the current track
172 // and start playing it regardless of current play state. 141 // and start playing it regardless of current play state.
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 297
329 var newTrackIndex = this.playOrder[newPlayOrder]; 298 var newTrackIndex = this.playOrder[newPlayOrder];
330 console.assert( 299 console.assert(
331 (0 <= newTrackIndex && newTrackIndex < this.tracks.length), 300 (0 <= newTrackIndex && newTrackIndex < this.tracks.length),
332 'Insufficient TrackList.playOrder. New Play Order: ' + newPlayOrder); 301 'Insufficient TrackList.playOrder. New Play Order: ' + newPlayOrder);
333 302
334 return newTrackIndex; 303 return newTrackIndex;
335 }, 304 },
336 }); 305 });
337 })(); // Anonymous closure 306 })(); // Anonymous closure
OLDNEW
« no previous file with comments | « ui/file_manager/audio_player/elements/audio_player.js ('k') | ui/file_manager/audio_player/js/audio_player.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698