| 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 cr.define('media', function() { | |
| 6 | |
| 7 /** | |
| 8 * This class stores hashes by their id field and provides basic methods for | |
| 9 * iterating over the collection. | |
| 10 * @constructor | |
| 11 */ | |
| 12 function ItemStore() { | |
| 13 this.items_ = {}; | |
| 14 } | |
| 15 | |
| 16 ItemStore.prototype = { | |
| 17 /** | |
| 18 * Get a sorted list of item ids. | |
| 19 * @return {Array} A sorted array of ids. | |
| 20 */ | |
| 21 ids: function() { | |
| 22 var ids = []; | |
| 23 for (var i in this.items_) | |
| 24 ids.push(i); | |
| 25 return ids.sort(); | |
| 26 }, | |
| 27 | |
| 28 /** | |
| 29 * Add an item to the store. | |
| 30 * @param {Object} item The item to be added. | |
| 31 * @param {string} item.id The id of the item. | |
| 32 */ | |
| 33 addItem: function(item) { | |
| 34 this.items_[item.id] = item; | |
| 35 }, | |
| 36 | |
| 37 /** | |
| 38 * Add a dictionary of items to the store. | |
| 39 * @param {Object} items A dictionary of individual items. The keys are | |
| 40 * irrelevant but each must have an id field. | |
| 41 */ | |
| 42 addItems: function(items) { | |
| 43 for (id in items) | |
| 44 this.addItem(items[id]); | |
| 45 }, | |
| 46 | |
| 47 /** | |
| 48 * Remove an item from the store. | |
| 49 * @param {string} id The id of the item to be removed. | |
| 50 */ | |
| 51 removeItem: function(id) { | |
| 52 delete this.items_[id]; | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * Map this itemStore to an Array. Items are sorted by id. | |
| 57 * @param {function(*)} mapper The mapping function applied to each item. | |
| 58 * @return {Array} An array of mapped items. | |
| 59 */ | |
| 60 map: function(mapper) { | |
| 61 var items = this.items_; | |
| 62 var ids = this.ids(); | |
| 63 return ids.map(function(id) { return mapper(items[id]); }); | |
| 64 } | |
| 65 }; | |
| 66 | |
| 67 return { | |
| 68 ItemStore: ItemStore | |
| 69 }; | |
| 70 }); | |
| OLD | NEW |