diff --git a/src/js/options.js b/src/js/options.js index 2e1c64f..b39a8e7 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -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)) { diff --git a/src/js/player.js b/src/js/player.js index d1b344e..0f7cc0f 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -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; diff --git a/src/js/storage.js b/src/js/storage.js new file mode 100644 index 0000000..545b89e --- /dev/null +++ b/src/js/storage.js @@ -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; \ No newline at end of file diff --git a/src/js/user.js b/src/js/user.js deleted file mode 100644 index 9e7757d..0000000 --- a/src/js/user.js +++ /dev/null @@ -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; \ No newline at end of file