Sharkey/src/client/app/store.ts

396 lines
9.2 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';
import * as nestedProperty from 'nested-property';
2018-05-20 19:13:39 +02:00
2018-04-29 14:37:51 +02:00
import MiOS from './mios';
2018-09-06 17:02:55 +02:00
import { erase } from '../../prelude/array';
2018-10-19 07:34:51 +02:00
import getNoteSummary from '../../misc/get-note-summary';
2018-04-29 10:17:15 +02:00
const defaultSettings = {
2018-06-05 20:03:56 +02:00
home: null,
mobileHome: [],
keepCw: false,
tagTimelines: [],
2018-04-29 10:17:15 +02:00
fetchOnScroll: true,
showMaps: true,
remainDeletedNote: false,
2018-04-29 10:17:15 +02:00
showPostFormOnTopOfTl: false,
2018-08-16 23:03:03 +02:00
suggestRecentHashtags: true,
showClockOnHeader: true,
2018-09-22 13:39:12 +02:00
useShadow: true,
roundedCorners: false,
2018-04-29 10:17:15 +02:00
circleIcons: true,
contrastedAcct: true,
showFullAcct: false,
showVia: true,
2018-04-29 10:17:15 +02:00
showReplyTarget: true,
showMyRenotes: true,
showRenotedMyNotes: true,
2018-08-16 16:59:22 +02:00
showLocalRenotes: true,
loadRemoteMedia: true,
2018-05-27 15:39:20 +02:00
disableViaMobile: false,
2018-06-18 10:25:20 +02:00
memo: null,
iLikeSushi: false,
rememberNoteVisibility: false,
defaultNoteVisibility: 'public',
webSearchEngine: 'https://www.google.com/?#q={{query}}',
mutedWords: [],
games: {
reversi: {
showBoardLabels: false,
2018-12-30 06:05:13 +01:00
useAvatarStones: true,
}
}
2018-04-29 10:17:15 +02:00
};
2018-05-20 19:13:39 +02:00
const defaultDeviceSettings = {
deck: null,
deckMode: false,
deckColumnAlign: 'center',
deckColumnWidth: 'normal',
2018-09-16 14:40:48 +02:00
reduceMotion: false,
2018-05-20 19:13:39 +02:00
autoPopout: false,
2018-05-23 22:28:46 +02:00
darkmode: false,
2018-09-28 17:01:11 +02:00
darkTheme: 'dark',
lightTheme: 'light',
lineWidth: 1,
2018-09-28 17:01:11 +02:00
themes: [],
2018-05-20 19:13:39 +02:00
enableSounds: true,
soundVolume: 0.5,
mediaVolume: 0.5,
2018-05-20 19:13:39 +02:00
lang: null,
preventUpdate: false,
debug: false,
lightmode: false,
loadRawImages: false,
alwaysShowNsfw: false,
2018-09-05 06:47:26 +02:00
postStyle: 'standard',
2018-10-14 12:44:30 +02:00
navbar: 'top',
mobileNotificationPosition: 'bottom',
useOsDefaultEmojis: false,
disableShowingAnimatedImages: false,
expandUsersPhotos: true,
expandUsersActivity: true,
2018-05-20 19:13:39 +02:00
};
2018-04-29 10:17:15 +02:00
export default (os: MiOS) => new Vuex.Store({
2018-05-27 06:49:09 +02:00
plugins: [createPersistedState({
paths: ['i', 'device', 'settings']
2018-05-20 19:13:39 +02:00
})],
2018-04-29 10:17:15 +02:00
state: {
2018-05-27 06:49:09 +02:00
i: null,
2018-05-17 16:53:55 +02:00
indicate: false,
uiHeaderHeight: 0,
2018-10-19 07:34:51 +02:00
behindNotes: []
2018-04-29 10:17:15 +02:00
},
2018-05-27 06:49:09 +02:00
getters: {
isSignedIn: state => state.i != null
},
2018-04-29 10:17:15 +02:00
mutations: {
2018-05-27 06:49:09 +02:00
updateI(state, x) {
state.i = x;
},
updateIKeyValue(state, x) {
state.i[x.key] = x.value;
},
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;
},
2018-10-19 07:34:51 +02:00
pushBehindNote(state, note) {
if (note.userId === state.i.id) return;
if (state.behindNotes.some(n => n.id === note.id)) return;
state.behindNotes.push(note);
document.title = `(${state.behindNotes.length}) ${getNoteSummary(note)}`;
},
clearBehindNotes(state) {
state.behindNotes = [];
document.title = os.instanceName;
2018-04-29 10:17:15 +02:00
}
},
2018-05-27 06:49:09 +02:00
actions: {
login(ctx, i) {
ctx.commit('updateI', i);
ctx.dispatch('settings/merge', i.clientSettings);
},
logout(ctx) {
ctx.commit('updateI', null);
2018-12-11 12:59:25 +01:00
document.cookie = 'i=;';
2018-11-28 08:19:02 +01:00
localStorage.removeItem('i');
2018-05-27 06:49:09 +02:00
},
mergeMe(ctx, me) {
for (const [key, value] of Object.entries(me)) {
2018-05-27 06:49:09 +02:00
ctx.commit('updateIKeyValue', { key, value });
}
2018-05-27 06:49:09 +02:00
if (me.clientSettings) {
ctx.dispatch('settings/merge', me.clientSettings);
}
},
},
2018-04-29 10:17:15 +02:00
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-05-26 17:18:44 +02:00
},
setTl(state, x) {
state.tl = {
src: x.src,
arg: x.arg
};
2018-08-17 09:35:04 +02:00
},
setVisibility(state, visibility) {
state.visibility = visibility;
2018-06-05 22:18:08 +02:00
},
addDeckColumn(state, column) {
if (column.name == undefined) column.name = null;
2018-06-05 22:18:08 +02:00
state.deck.columns.push(column);
state.deck.layout.push([column.id]);
2018-06-05 22:18:08 +02:00
},
removeDeckColumn(state, id) {
state.deck.columns = state.deck.columns.filter(c => c.id != id);
2018-09-06 17:02:55 +02:00
state.deck.layout = state.deck.layout.map(ids => erase(id, ids));
2018-06-09 15:25:15 +02:00
state.deck.layout = state.deck.layout.filter(ids => ids.length > 0);
2018-06-05 22:18:08 +02:00
},
swapDeckColumn(state, x) {
const a = x.a;
const b = x.b;
const aX = state.deck.layout.findIndex(ids => ids.indexOf(a) != -1);
const aY = state.deck.layout[aX].findIndex(id => id == a);
const bX = state.deck.layout.findIndex(ids => ids.indexOf(b) != -1);
const bY = state.deck.layout[bX].findIndex(id => id == b);
state.deck.layout[aX][aY] = b;
state.deck.layout[bX][bY] = a;
},
2018-06-05 22:18:08 +02:00
swapLeftDeckColumn(state, id) {
state.deck.layout.some((ids, i) => {
if (ids.indexOf(id) != -1) {
const left = state.deck.layout[i - 1];
2018-06-05 22:18:08 +02:00
if (left) {
// https://vuejs.org/v2/guide/list.html#Caveats
//state.deck.layout[i - 1] = state.deck.layout[i];
//state.deck.layout[i] = left;
state.deck.layout.splice(i - 1, 1, state.deck.layout[i]);
state.deck.layout.splice(i, 1, left);
2018-06-05 22:18:08 +02:00
}
return true;
}
});
},
swapRightDeckColumn(state, id) {
state.deck.layout.some((ids, i) => {
if (ids.indexOf(id) != -1) {
const right = state.deck.layout[i + 1];
2018-06-05 22:18:08 +02:00
if (right) {
// https://vuejs.org/v2/guide/list.html#Caveats
//state.deck.layout[i + 1] = state.deck.layout[i];
//state.deck.layout[i] = right;
state.deck.layout.splice(i + 1, 1, state.deck.layout[i]);
state.deck.layout.splice(i, 1, right);
2018-06-05 22:18:08 +02:00
}
return true;
}
});
},
swapUpDeckColumn(state, id) {
const ids = state.deck.layout.find(ids => ids.indexOf(id) != -1);
ids.some((x, i) => {
if (x == id) {
const up = ids[i - 1];
if (up) {
// https://vuejs.org/v2/guide/list.html#Caveats
//ids[i - 1] = id;
//ids[i] = up;
ids.splice(i - 1, 1, id);
ids.splice(i, 1, up);
}
return true;
}
});
},
swapDownDeckColumn(state, id) {
const ids = state.deck.layout.find(ids => ids.indexOf(id) != -1);
ids.some((x, i) => {
if (x == id) {
const down = ids[i + 1];
if (down) {
// https://vuejs.org/v2/guide/list.html#Caveats
//ids[i + 1] = id;
//ids[i] = down;
ids.splice(i + 1, 1, id);
ids.splice(i, 1, down);
}
return true;
}
});
},
stackLeftDeckColumn(state, id) {
const i = state.deck.layout.findIndex(ids => ids.indexOf(id) != -1);
2018-09-06 17:02:55 +02:00
state.deck.layout = state.deck.layout.map(ids => erase(id, ids));
const left = state.deck.layout[i - 1];
if (left) state.deck.layout[i - 1].push(id);
state.deck.layout = state.deck.layout.filter(ids => ids.length > 0);
},
popRightDeckColumn(state, id) {
const i = state.deck.layout.findIndex(ids => ids.indexOf(id) != -1);
2018-09-06 17:02:55 +02:00
state.deck.layout = state.deck.layout.map(ids => erase(id, ids));
state.deck.layout.splice(i + 1, 0, [id]);
state.deck.layout = state.deck.layout.filter(ids => ids.length > 0);
},
addDeckWidget(state, x) {
const column = state.deck.columns.find(c => c.id == x.id);
if (column == null) return;
column.widgets.unshift(x.widget);
},
removeDeckWidget(state, x) {
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-06 23:13:57 +02: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 10:54:50 +02:00
},
updateDeckColumn(state, x) {
let column = state.deck.columns.find(c => c.id == x.id);
if (column == null) return;
column = x;
}
}
},
2018-04-29 10:17:15 +02:00
settings: {
namespaced: true,
2018-04-29 10:17:15 +02:00
state: defaultSettings,
2018-06-05 22:18:08 +02:00
mutations: {
set(state, x: { key: string; value: any }) {
nestedProperty.set(state, x.key, x.value);
},
setHome(state, data) {
state.home = data;
2018-06-05 22:18:08 +02:00
},
addHomeWidget(state, widget) {
state.home.unshift(widget);
},
setMobileHome(state, data) {
state.mobileHome = data;
2018-06-05 22:18:08 +02:00
},
updateWidget(state, x) {
let w;
2018-06-05 22:18:08 +02:00
//#region Desktop 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
},
addMobileHomeWidget(state, widget) {
state.mobileHome.unshift(widget);
},
removeMobileHomeWidget(state, widget) {
state.mobileHome = state.mobileHome.filter(w => w.id != widget.id);
},
},
actions: {
merge(ctx, settings) {
if (settings == null) return;
for (const [key, value] of Object.entries(settings)) {
ctx.commit('set', { key, value });
}
},
set(ctx, x) {
ctx.commit('set', x);
2018-06-05 22:18:08 +02:00
if (ctx.rootGetters.isSignedIn) {
os.api('i/update_client_setting', {
name: x.key,
value: x.value
});
}
2018-06-06 23:13:57 +02:00
},
2018-04-29 10:17:15 +02:00
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
}
}
}
}
});