rename: user -> storage; new option: storageName

This commit is contained in:
DIYgod 2018-03-20 17:19:10 +08:00
parent 7afe441da1
commit a4bdc5345b
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
4 changed files with 30 additions and 38 deletions

View File

@ -14,7 +14,8 @@ export default (options) => {
volume: 0.7,
listFolded: false,
listMaxHeight: options.listmaxheight,
audio: options.music
audio: options.music,
storageName: 'aplayer-setting'
};
for (const defaultKey in defaultOption) {
if (defaultOption.hasOwnProperty(defaultKey) && !options.hasOwnProperty(defaultKey)) {

View File

@ -5,7 +5,7 @@ import Icons from './icons';
import handleOption from './options';
import Template from './template';
import Bar from './bar';
import User from './user';
import Storage from './storage';
import Lrc from './lrc';
import Controller from './controller';
import Timer from './timer';
@ -79,7 +79,7 @@ class APlayer {
});
}
this.events = new Events();
this.user = new User(this);
this.storage = new Storage(this);
this.bar = new Bar(this.template);
this.controller = new Controller(this);
this.timer = new Timer(this);
@ -191,7 +191,7 @@ class APlayer {
}
});
this.volume(this.user.get('volume'), true);
this.volume(this.storage.get('volume'), true);
}
setAudio (audio) {
@ -349,7 +349,7 @@ class APlayer {
percentage = Math.min(percentage, 1);
this.bar.set('volume', percentage, 'height');
if (!nostorage) {
this.user.set('volume', percentage);
this.storage.set('volume', percentage);
}
this.audio.volume = percentage;

24
src/js/storage.js Normal file
View File

@ -0,0 +1,24 @@
import utils from './utils';
class Storage {
constructor (player) {
this.storageName = player.options.storageName;
this.data = JSON.parse(utils.storage.get(this.storageName));
if (!this.data) {
this.data = {};
}
this.data.volume = this.data.volume || player.options.volume;
}
get (key) {
return this.data[key];
}
set (key, value) {
this.data[key] = value;
utils.storage.set(this.storageName, JSON.stringify(this.data));
}
}
export default Storage;

View File

@ -1,33 +0,0 @@
import utils from './utils';
class User {
constructor (player) {
this.storageName = {
volume: 'aplayer-volume',
};
this.default = {
volume: player.options.volume || 0.7,
};
this.data = {};
this.init();
}
init () {
for (const item in this.storageName) {
const name = this.storageName[item];
this.data[item] = parseFloat(utils.storage.get(name) || this.default[item]);
}
}
get (key) {
return this.data[key];
}
set (key, value) {
this.data[key] = value;
utils.storage.set(this.storageName[key], value);
}
}
export default User;