2018-06-11 21:01:00 +08:00
|
|
|
import Cookies from 'js-cookie'
|
|
|
|
|
|
2018-06-05 14:43:43 +08:00
|
|
|
export default {
|
|
|
|
|
state: {
|
2018-06-11 13:53:48 +08:00
|
|
|
list: [
|
|
|
|
|
{
|
|
|
|
|
name: 'd2admin 经典',
|
|
|
|
|
value: 'd2',
|
2018-06-22 01:15:28 +08:00
|
|
|
preview: 'static/image/theme/d2/preview@2x.png'
|
2018-06-11 13:53:48 +08:00
|
|
|
},
|
2018-06-27 01:34:01 +08:00
|
|
|
{
|
|
|
|
|
name: '紫罗兰',
|
|
|
|
|
value: 'violet',
|
|
|
|
|
preview: 'static/image/theme/violet/preview@2x.png'
|
|
|
|
|
},
|
2018-06-12 00:08:48 +08:00
|
|
|
{
|
|
|
|
|
name: '简约线条',
|
|
|
|
|
value: 'line',
|
2018-06-22 01:15:28 +08:00
|
|
|
backgroundImage: 'static/image/bg/line-squashed.jpg',
|
|
|
|
|
preview: 'static/image/theme/line/preview@2x.png'
|
2018-06-12 00:08:48 +08:00
|
|
|
},
|
2018-06-11 13:53:48 +08:00
|
|
|
{
|
|
|
|
|
name: '流星',
|
|
|
|
|
value: 'star',
|
2018-06-22 01:15:28 +08:00
|
|
|
backgroundImage: 'static/image/bg/star-squashed.jpg',
|
|
|
|
|
preview: 'static/image/theme/star/preview@2x.png'
|
2018-06-12 15:30:26 +08:00
|
|
|
},
|
|
|
|
|
{
|
2018-06-12 15:39:51 +08:00
|
|
|
name: 'Tomorrow Night Blue (vsCode)',
|
2018-06-12 15:30:26 +08:00
|
|
|
value: 'tomorrow-night-blue',
|
2018-06-22 01:15:28 +08:00
|
|
|
preview: 'static/image/theme/tomorrow-night-blue/preview@2x.png'
|
2018-06-11 13:53:48 +08:00
|
|
|
}
|
|
|
|
|
],
|
2018-06-22 01:15:28 +08:00
|
|
|
name: '',
|
|
|
|
|
backGroundImage: ''
|
2018-06-05 14:43:43 +08:00
|
|
|
},
|
|
|
|
|
mutations: {
|
2018-06-11 21:12:50 +08:00
|
|
|
// 从 cookie 加载主题
|
2018-06-11 21:01:00 +08:00
|
|
|
loadTheme (state) {
|
|
|
|
|
const name = Cookies.get('themeName')
|
|
|
|
|
if (name) {
|
2018-06-11 21:12:50 +08:00
|
|
|
// 设置 store
|
|
|
|
|
state.name = name
|
|
|
|
|
// 激活主题
|
|
|
|
|
this.commit('activeTheme')
|
2018-06-11 21:01:00 +08:00
|
|
|
} else {
|
2018-06-11 21:12:50 +08:00
|
|
|
// 设置新的主题为列表第一项
|
2018-06-11 21:01:00 +08:00
|
|
|
this.commit('setTheme', state.list[0].value)
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-06-11 21:12:50 +08:00
|
|
|
// 设置新的主题
|
2018-06-11 13:53:48 +08:00
|
|
|
setTheme (state, name) {
|
2018-06-11 21:12:50 +08:00
|
|
|
// 设置 store
|
2018-06-11 13:53:48 +08:00
|
|
|
state.name = name
|
2018-06-11 21:12:50 +08:00
|
|
|
// 设置 Cookie
|
2018-06-16 19:03:26 +08:00
|
|
|
Cookies.set('themeName', name, {
|
|
|
|
|
expires: 365
|
|
|
|
|
})
|
2018-06-11 21:12:50 +08:00
|
|
|
// 激活主题
|
|
|
|
|
this.commit('activeTheme')
|
|
|
|
|
},
|
|
|
|
|
// 激活当前主题
|
|
|
|
|
activeTheme (state) {
|
2018-06-22 01:15:28 +08:00
|
|
|
// 设置背景图片
|
|
|
|
|
const themeSetting = state.list.find(e => e.value === state.name)
|
|
|
|
|
if (themeSetting) {
|
|
|
|
|
state.backGroundImage = themeSetting.backgroundImage || ''
|
|
|
|
|
}
|
2018-06-11 13:53:48 +08:00
|
|
|
document.body.className = `theme-${state.name}`
|
2018-06-05 14:43:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|