Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 //////////////////////////////////////////////////////////////////////////////// | |
| 6 // | |
| 7 // ItemStore | |
| 8 // | |
| 9 //////////////////////////////////////////////////////////////////////////////// | |
| 10 | |
| 11 // This class stores hashes by their id field. It provides methods for rendering | |
| 12 // them into html. | |
| 13 function ItemStore() { | |
| 14 this.items_ = {}; | |
| 15 }; | |
| 16 | |
| 17 // Gets a sorted list of ids. | |
| 18 ItemStore.prototype.ids = function() { | |
| 19 var ids = []; | |
| 20 for (var i in this.items_) | |
| 21 ids.push(i); | |
| 22 return ids.sort(); | |
| 23 }; | |
| 24 | |
| 25 // Adds an item to the store. | |
| 26 ItemStore.prototype.addItem = function(item) { | |
| 27 this.items_[item.id] = item; | |
| 28 }; | |
| 29 | |
| 30 // Adds a hash of items to the store. | |
| 31 ItemStore.prototype.addItems = function(items) { | |
| 32 for (id in items) | |
| 33 this.addItem(items[id]); | |
| 34 }; | |
| 35 | |
| 36 // Removes an item from the store. | |
| 37 ItemStore.prototype.removeItem = function(id) { | |
| 38 delete this.items_[id]; | |
| 39 }; | |
| 40 | |
| 41 // Produces an html rendering of items_[|id|]. | |
| 42 // Overridden for each type of item. | |
| 43 ItemStore.prototype.printItem = null; | |
| 44 | |
| 45 // Produces an html rendering of the item store. | |
| 46 // Overridden for each type of item. | |
| 47 ItemStore.prototype.print = null; | |
| 48 | |
| 49 // Concatenates the html representations of the items together, sorted by ids | |
| 50 // and separated by |separator|. | |
| 51 ItemStore.prototype.joinItems = function(separator) { | |
| 52 var ids = this.ids(); | |
| 53 var items = []; | |
| 54 for (var i = 0; i < ids.length; i++) | |
| 55 items[i] = this.printItem(ids[i]); | |
| 56 | |
| 57 return items.join(separator); | |
| 58 }; | |
| 59 | |
| 60 //////////////////////////////////////////////////////////////////////////////// | |
| 61 // | |
| 62 // AudioStreamStore | |
| 63 // | |
| 64 //////////////////////////////////////////////////////////////////////////////// | |
| 65 | |
| 66 function AudioStreamStore() { | |
| 67 this.items_ = {}; | |
| 68 }; | |
| 69 | |
| 70 // Inherit from ItemStore. | |
| 71 AudioStreamStore.prototype = new ItemStore(); | |
|
Scott Franklin
2011/07/02 00:57:20
I'm tempted to ditch the inheritance and do someth
scherkus (not reviewing)
2011/07/07 20:57:30
from what I've experienced/read JS + inheritance i
Scott Franklin
2011/07/09 00:33:22
Yeah, I think I'll ditch the inheritance altogethe
| |
| 72 | |
| 73 // Prints a stream as a list item. | |
| 74 AudioStreamStore.prototype.printItem = function(id) { | |
| 75 var stream = this.items_[id]; | |
| 76 var out = '<li id=' + id + ' class="audio-stream-' + stream.status + '">'; | |
| 77 out += 'Audio stream ' + stream.id.split('.')[1]; | |
| 78 out += ' is ' + (stream.playing ? 'playing' : 'paused'); | |
| 79 out += ' at ' + Math.round(stream.volume * 100) + '% volume.'; | |
| 80 out += '</li>'; | |
| 81 return out; | |
| 82 }; | |
| 83 | |
| 84 // Produces a ul of all audio streams. | |
| 85 AudioStreamStore.prototype.print = function() { | |
| 86 var out = '<h2>Active audio streams:</h2>'; | |
| 87 out += '<ul>'; | |
| 88 out += this.joinItems(''); | |
| 89 out += '</ul>'; | |
| 90 return out; | |
| 91 }; | |
| OLD | NEW |