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(global) { | 5 /** |
| 6 * The model class for audio player. |
| 7 * @constructor |
| 8 */ |
| 9 function AudioPlayerModel() { |
6 'use strict'; | 10 'use strict'; |
7 | 11 |
8 /** | 12 /** |
9 * List of values to be stored into the model. | 13 * List of values to be stored into the model. |
10 * @type {Object<string, *>} | 14 * @type {!Object<string, *>} |
11 * @const | 15 * @const |
12 */ | 16 */ |
13 var VALUES = Object.freeze({ | 17 var VALUES = Object.freeze( |
14 shuffle: false, | 18 /** |
15 repeat: false, | 19 * They will be used as properties of AudioPlayerModel. |
16 volume: 100, | 20 * @lends {AudioPlayerModel.prototype} |
17 expanded: false, | 21 */ |
18 }); | 22 { |
| 23 shuffle: false, |
| 24 repeat: false, |
| 25 volume: 100, |
| 26 expanded: false, |
| 27 }); |
19 | 28 |
20 /** | 29 /** |
21 * Prefix of the stored values in the storage. | 30 * Prefix of the stored values in the storage. |
22 * @type {string} | 31 * @type {string} |
23 */ | 32 */ |
24 var STORAGE_PREFIX = 'audioplayer-'; | 33 var STORAGE_PREFIX = 'audioplayer-'; |
25 | 34 |
26 /** | 35 /** |
27 * Save the values in the model into the storage. | 36 * Save the values in the model into the storage. |
28 * @param {AudioPlayerModel} model The model. | 37 * @param {AudioPlayerModel} model The model. |
(...skipping 21 matching lines...) Expand all Loading... |
50 | 59 |
51 chrome.storage.local.get(objectsToBeRead, function(result) { | 60 chrome.storage.local.get(objectsToBeRead, function(result) { |
52 for (var key in result) { | 61 for (var key in result) { |
53 // Strips the prefix. | 62 // Strips the prefix. |
54 model[key.substr(STORAGE_PREFIX.length)] = result[key]; | 63 model[key.substr(STORAGE_PREFIX.length)] = result[key]; |
55 } | 64 } |
56 callback(); | 65 callback(); |
57 }); | 66 }); |
58 }; | 67 }; |
59 | 68 |
60 /** | 69 // Initializes values. |
61 * The model class for audio player. | 70 for (var key in VALUES) { |
62 * @constructor | 71 this[key] = VALUES[key]; |
63 */ | 72 } |
64 function AudioPlayerModel() { | 73 Object.seal(this); |
65 // Initializes values. | |
66 for (var key in VALUES) { | |
67 this[key] = VALUES[key]; | |
68 } | |
69 Object.seal(this); | |
70 | 74 |
71 // Restores the values from the storage | 75 // Restores the values from the storage |
72 var target = this; | 76 var target = this; |
73 loadModel(target, function() { | 77 loadModel(target, function() { |
74 // Installs observer to watch changes of the values. | 78 // Installs observer to watch changes of the values. |
75 Object.observe(target, function(changes) { | 79 Object.observe(target, function(changes) { |
76 saveModel(target); | 80 saveModel(target); |
77 }); | |
78 }); | 81 }); |
79 } | 82 }); |
80 | 83 } |
81 // Exports AudioPlayerModel class to the global. | |
82 global.AudioPlayerModel = AudioPlayerModel; | |
83 | |
84 })(this || window); | |
OLD | NEW |