trashposs/src/client/app/store.ts

161 lines
3.0 KiB
TypeScript
Raw Normal View History

2018-04-29 10:17:15 +02:00
import Vuex from 'vuex';
2018-05-20 19:13:39 +02:00
import createPersistedState from 'vuex-persistedstate';
2018-04-29 14:37:51 +02:00
import MiOS from './mios';
2018-04-29 10:17:15 +02:00
const defaultSettings = {
home: [],
mobileHome: [],
2018-04-29 10:17:15 +02:00
fetchOnScroll: true,
showMaps: true,
showPostFormOnTopOfTl: false,
circleIcons: true,
gradientWindowHeader: false,
showReplyTarget: true,
showMyRenotes: true,
showRenotedMyNotes: true
};
2018-05-20 19:13:39 +02:00
const defaultDeviceSettings = {
apiViaStream: true,
autoPopout: false,
enableSounds: true,
soundVolume: 0.5,
lang: null,
preventUpdate: false,
debug: false,
lightmode: false,
postStyle: 'standard'
2018-05-20 19:13:39 +02:00
};
2018-04-29 10:17:15 +02:00
export default (os: MiOS) => new Vuex.Store({
plugins: [store => {
store.subscribe((mutation, state) => {
if (mutation.type.startsWith('settings/')) {
2018-05-20 19:13:39 +02:00
localStorage.setItem('settings', JSON.stringify(state.settings));
2018-04-29 10:17:15 +02:00
}
});
2018-05-20 19:13:39 +02:00
}, createPersistedState({
paths: ['device'],
filter: mut => mut.type.startsWith('device/')
})],
2018-04-29 10:17:15 +02:00
state: {
2018-05-17 16:53:55 +02:00
indicate: false,
2018-04-29 10:17:15 +02:00
uiHeaderHeight: 0
},
mutations: {
2018-05-17 16:53:55 +02:00
indicate(state, x) {
state.indicate = x;
},
2018-04-29 10:17:15 +02:00
setUiHeaderHeight(state, height) {
state.uiHeaderHeight = height;
}
},
modules: {
2018-05-20 19:13:39 +02:00
device: {
namespaced: true,
state: defaultDeviceSettings,
mutations: {
set(state, x: { key: string; value: any }) {
state[x.key] = x.value;
}
}
},
2018-04-29 10:17:15 +02:00
settings: {
namespaced: true,
2018-05-20 19:13:39 +02:00
state: defaultSettings,
2018-04-29 10:17:15 +02:00
mutations: {
set(state, x: { key: string; value: any }) {
2018-05-20 19:13:39 +02:00
state[x.key] = x.value;
2018-04-29 10:17:15 +02:00
},
setHome(state, data) {
2018-05-20 19:13:39 +02:00
state.home = data;
2018-04-29 10:17:15 +02:00
},
setHomeWidget(state, x) {
2018-05-20 19:13:39 +02:00
const w = state.home.find(w => w.id == x.id);
2018-04-29 10:17:15 +02:00
if (w) {
w.data = x.data;
}
},
addHomeWidget(state, widget) {
2018-05-20 19:13:39 +02:00
state.home.unshift(widget);
},
setMobileHome(state, data) {
2018-05-20 19:13:39 +02:00
state.mobileHome = data;
},
setMobileHomeWidget(state, x) {
2018-05-20 19:13:39 +02:00
const w = state.mobileHome.find(w => w.id == x.id);
if (w) {
w.data = x.data;
}
},
addMobileHomeWidget(state, widget) {
2018-05-20 19:13:39 +02:00
state.mobileHome.unshift(widget);
},
removeMobileHomeWidget(state, widget) {
2018-05-20 19:13:39 +02:00
state.mobileHome = state.mobileHome.filter(w => w.id != widget.id);
2018-04-29 10:17:15 +02:00
}
},
actions: {
2018-04-29 10:54:50 +02:00
merge(ctx, settings) {
Object.entries(settings).forEach(([key, value]) => {
ctx.commit('set', { key, value });
});
},
2018-04-29 10:17:15 +02:00
set(ctx, x) {
ctx.commit('set', x);
if (os.isSignedIn) {
os.api('i/update_client_setting', {
name: x.key,
value: x.value
});
}
},
addHomeWidget(ctx, widget) {
ctx.commit('addHomeWidget', widget);
os.api('i/update_home', {
2018-05-20 19:13:39 +02:00
home: ctx.state.home
2018-04-29 10:17:15 +02:00
});
},
addMobileHomeWidget(ctx, widget) {
ctx.commit('addMobileHomeWidget', widget);
os.api('i/update_mobile_home', {
2018-05-20 19:13:39 +02:00
home: ctx.state.mobileHome
});
},
removeMobileHomeWidget(ctx, widget) {
ctx.commit('removeMobileHomeWidget', widget);
os.api('i/update_mobile_home', {
2018-05-20 19:13:39 +02:00
home: ctx.state.mobileHome.filter(w => w.id != widget.id)
});
2018-04-29 10:17:15 +02:00
}
}
}
}
});