Sharkey/src/client/app/store.ts

322 lines
6.6 KiB
TypeScript
Raw Normal View History

2018-04-29 11:17:15 +03:00
import Vuex from 'vuex';
2018-05-20 20:13:39 +03:00
import createPersistedState from 'vuex-persistedstate';
2018-04-29 15:37:51 +03:00
import MiOS from './mios';
2018-05-27 07:49:09 +03:00
import { hostname } from './config';
2018-04-29 11:17:15 +03:00
const defaultSettings = {
2018-06-05 21:03:56 +03:00
home: null,
mobileHome: [],
2018-06-05 21:12:06 +03:00
deck: null,
2018-04-29 11:17:15 +03:00
fetchOnScroll: true,
showMaps: true,
showPostFormOnTopOfTl: false,
circleIcons: true,
gradientWindowHeader: false,
showReplyTarget: true,
showMyRenotes: true,
showRenotedMyNotes: true,
loadRemoteMedia: true,
2018-05-27 16:39:20 +03:00
disableViaMobile: false,
memo: null
2018-04-29 11:17:15 +03:00
};
2018-05-20 20:13:39 +03:00
const defaultDeviceSettings = {
apiViaStream: true,
autoPopout: false,
2018-05-23 23:28:46 +03:00
darkmode: false,
2018-05-20 20:13:39 +03:00
enableSounds: true,
soundVolume: 0.5,
lang: null,
preventUpdate: false,
debug: false,
lightmode: false,
loadRawImages: false,
postStyle: 'standard'
2018-05-20 20:13:39 +03:00
};
2018-04-29 11:17:15 +03:00
export default (os: MiOS) => new Vuex.Store({
2018-05-27 07:49:09 +03:00
plugins: [createPersistedState({
paths: ['i', 'device', 'settings']
2018-05-20 20:13:39 +03:00
})],
2018-04-29 11:17:15 +03:00
state: {
2018-05-27 07:49:09 +03:00
i: null,
2018-05-17 17:53:55 +03:00
indicate: false,
2018-04-29 11:17:15 +03:00
uiHeaderHeight: 0
},
2018-05-27 07:49:09 +03:00
getters: {
isSignedIn: state => state.i != null
},
2018-04-29 11:17:15 +03:00
mutations: {
2018-05-27 07:49:09 +03:00
updateI(state, x) {
state.i = x;
},
updateIKeyValue(state, x) {
state.i[x.key] = x.value;
},
2018-05-17 17:53:55 +03:00
indicate(state, x) {
state.indicate = x;
},
2018-04-29 11:17:15 +03:00
setUiHeaderHeight(state, height) {
state.uiHeaderHeight = height;
}
},
2018-05-27 07:49:09 +03:00
actions: {
login(ctx, i) {
ctx.commit('updateI', i);
ctx.dispatch('settings/merge', i.clientSettings);
},
logout(ctx) {
ctx.commit('updateI', null);
document.cookie = `i=; domain=${hostname}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
},
mergeMe(ctx, me) {
Object.entries(me).forEach(([key, value]) => {
ctx.commit('updateIKeyValue', { key, value });
});
if (me.clientSettings) {
ctx.dispatch('settings/merge', me.clientSettings);
}
},
},
2018-04-29 11:17:15 +03:00
modules: {
2018-05-20 20:13:39 +03:00
device: {
namespaced: true,
state: defaultDeviceSettings,
mutations: {
set(state, x: { key: string; value: any }) {
state[x.key] = x.value;
2018-05-26 18:18:44 +03:00
},
setTl(state, x) {
state.tl = {
src: x.src,
arg: x.arg
};
2018-05-20 20:13:39 +03:00
}
}
},
2018-04-29 11:17:15 +03:00
settings: {
namespaced: true,
2018-05-20 20:13:39 +03:00
state: defaultSettings,
2018-04-29 11:17:15 +03:00
mutations: {
set(state, x: { key: string; value: any }) {
2018-05-20 20:13:39 +03:00
state[x.key] = x.value;
2018-04-29 11:17:15 +03:00
},
setHome(state, data) {
2018-05-20 20:13:39 +03:00
state.home = data;
2018-04-29 11:17:15 +03:00
},
addHomeWidget(state, widget) {
2018-05-20 20:13:39 +03:00
state.home.unshift(widget);
},
setMobileHome(state, data) {
2018-05-20 20:13:39 +03:00
state.mobileHome = data;
},
setWidget(state, x) {
let w;
//#region Decktop home
if (state.home) {
w = state.home.find(w => w.id == x.id);
if (w) {
w.data = x.data;
}
}
//#endregion
//#region Mobile home
if (state.mobileHome) {
w = state.mobileHome.find(w => w.id == x.id);
if (w) {
w.data = x.data;
}
}
//#endregion
//#region Deck
if (state.deck && state.deck.columns) {
state.deck.columns.filter(c => c.type == 'widgets').forEach(c => {
c.widgets.forEach(w => {
if (w.id == x.id) w.data = x.data;
});
});
}
//#endregion
},
addMobileHomeWidget(state, widget) {
2018-05-20 20:13:39 +03:00
state.mobileHome.unshift(widget);
},
removeMobileHomeWidget(state, widget) {
2018-05-20 20:13:39 +03:00
state.mobileHome = state.mobileHome.filter(w => w.id != widget.id);
2018-06-05 23:18:08 +03:00
},
addDeckColumn(state, column) {
if (state.deck.columns == null) state.deck.columns = [];
state.deck.columns.push(column);
},
removeDeckColumn(state, id) {
if (state.deck.columns == null) return;
state.deck.columns = state.deck.columns.filter(c => c.id != id);
},
swapLeftDeckColumn(state, id) {
if (state.deck.columns == null) return;
state.deck.columns.some((c, i) => {
if (c.id == id) {
const left = state.deck.columns[i - 1];
if (left) {
state.deck.columns[i - 1] = state.deck.columns[i];
state.deck.columns[i] = left;
}
return true;
}
});
},
swapRightDeckColumn(state, id) {
if (state.deck.columns == null) return;
state.deck.columns.some((c, i) => {
if (c.id == id) {
const right = state.deck.columns[i + 1];
if (right) {
state.deck.columns[i + 1] = state.deck.columns[i];
state.deck.columns[i] = right;
}
return true;
}
});
},
addDeckWidget(state, x) {
if (state.deck.columns == null) return;
const column = state.deck.columns.find(c => c.id == x.id);
if (column == null) return;
column.widgets.unshift(x.widget);
},
removeDeckWidget(state, x) {
if (state.deck.columns == null) return;
const column = state.deck.columns.find(c => c.id == x.id);
if (column == null) return;
column.widgets = column.widgets.filter(w => w.id != x.widget.id);
2018-06-07 00:13:57 +03:00
},
renameDeckColumn(state, x) {
const column = state.deck.columns.find(c => c.id == x.id);
if (column == null) return;
column.name = x.name;
2018-04-29 11:17:15 +03:00
}
},
actions: {
2018-04-29 11:54:50 +03:00
merge(ctx, settings) {
2018-06-05 20:48:26 +03:00
if (settings == null) return;
2018-04-29 11:54:50 +03:00
Object.entries(settings).forEach(([key, value]) => {
ctx.commit('set', { key, value });
});
},
2018-04-29 11:17:15 +03:00
set(ctx, x) {
ctx.commit('set', x);
2018-05-27 07:49:09 +03:00
if (ctx.rootGetters.isSignedIn) {
2018-04-29 11:17:15 +03:00
os.api('i/update_client_setting', {
name: x.key,
value: x.value
});
}
},
saveDeck(ctx) {
2018-06-05 23:18:08 +03:00
os.api('i/update_client_setting', {
name: 'deck',
value: ctx.state.deck
});
},
addDeckColumn(ctx, column) {
ctx.commit('addDeckColumn', column);
ctx.dispatch('saveDeck');
},
2018-06-05 23:18:08 +03:00
removeDeckColumn(ctx, id) {
ctx.commit('removeDeckColumn', id);
ctx.dispatch('saveDeck');
2018-06-05 23:18:08 +03:00
},
swapLeftDeckColumn(ctx, id) {
ctx.commit('swapLeftDeckColumn', id);
ctx.dispatch('saveDeck');
2018-06-05 23:18:08 +03:00
},
swapRightDeckColumn(ctx, id) {
ctx.commit('swapRightDeckColumn', id);
ctx.dispatch('saveDeck');
},
2018-06-05 23:18:08 +03:00
addDeckWidget(ctx, x) {
ctx.commit('addDeckWidget', x);
ctx.dispatch('saveDeck');
},
removeDeckWidget(ctx, x) {
ctx.commit('removeDeckWidget', x);
ctx.dispatch('saveDeck');
2018-06-05 23:18:08 +03:00
},
2018-06-07 00:13:57 +03:00
renameDeckColumn(ctx, x) {
ctx.commit('renameDeckColumn', x);
ctx.dispatch('saveDeck');
},
2018-04-29 11:17:15 +03:00
addHomeWidget(ctx, widget) {
ctx.commit('addHomeWidget', widget);
os.api('i/update_home', {
2018-05-20 20:13:39 +03:00
home: ctx.state.home
2018-04-29 11:17:15 +03:00
});
},
addMobileHomeWidget(ctx, widget) {
ctx.commit('addMobileHomeWidget', widget);
os.api('i/update_mobile_home', {
2018-05-20 20:13:39 +03:00
home: ctx.state.mobileHome
});
},
removeMobileHomeWidget(ctx, widget) {
ctx.commit('removeMobileHomeWidget', widget);
os.api('i/update_mobile_home', {
2018-05-20 20:13:39 +03:00
home: ctx.state.mobileHome.filter(w => w.id != widget.id)
});
2018-04-29 11:17:15 +03:00
}
}
}
}
});