2019-02-26 20:23:41 +00:00
|
|
|
import Vue from "vue";
|
|
|
|
import Vuex from "vuex";
|
2019-11-05 20:51:55 +00:00
|
|
|
import {createSettingsStore} from "./store-settings";
|
2019-11-16 17:24:03 +00:00
|
|
|
import storage from "./localStorage";
|
2019-11-03 18:05:19 +00:00
|
|
|
|
2019-11-07 23:53:04 +00:00
|
|
|
const appName = document.title;
|
2019-02-26 20:23:41 +00:00
|
|
|
|
|
|
|
Vue.use(Vuex);
|
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
function detectDesktopNotificationState() {
|
|
|
|
if (!("Notification" in window)) {
|
|
|
|
return "unsupported";
|
|
|
|
} else if (Notification.permission === "granted") {
|
|
|
|
return "granted";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "blocked";
|
|
|
|
}
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
const store = new Vuex.Store({
|
2019-02-27 14:15:34 +00:00
|
|
|
state: {
|
2019-11-05 19:29:51 +00:00
|
|
|
appLoaded: false,
|
2019-11-02 19:40:59 +00:00
|
|
|
activeChannel: null,
|
|
|
|
currentUserVisibleError: null,
|
2019-11-05 20:51:55 +00:00
|
|
|
desktopNotificationState: detectDesktopNotificationState(),
|
2019-11-02 19:40:59 +00:00
|
|
|
isAutoCompleting: false,
|
2019-02-27 14:15:34 +00:00
|
|
|
isConnected: false,
|
2019-11-02 19:40:59 +00:00
|
|
|
networks: [],
|
2019-11-04 09:21:05 +00:00
|
|
|
mentions: [],
|
2019-11-16 17:24:03 +00:00
|
|
|
hasServiceWorker: false,
|
2019-11-02 19:40:59 +00:00
|
|
|
pushNotificationState: "unsupported",
|
2019-11-12 10:39:46 +00:00
|
|
|
serverConfiguration: null,
|
2019-03-03 17:47:49 +00:00
|
|
|
sessions: [],
|
2019-04-13 20:44:04 +00:00
|
|
|
sidebarOpen: false,
|
2019-08-05 12:24:44 +00:00
|
|
|
sidebarDragging: false,
|
2019-04-13 20:44:04 +00:00
|
|
|
userlistOpen: storage.get("thelounge.state.userlist") !== "false",
|
2019-08-05 14:29:35 +00:00
|
|
|
versionData: null,
|
|
|
|
versionStatus: "loading",
|
|
|
|
versionDataExpired: false,
|
2019-11-20 17:43:27 +00:00
|
|
|
serverHasSettings: false,
|
2019-12-31 16:21:34 +00:00
|
|
|
messageSearchResults: null,
|
|
|
|
messageSearchInProgress: false,
|
2019-02-27 14:15:34 +00:00
|
|
|
},
|
|
|
|
mutations: {
|
2019-11-05 19:29:51 +00:00
|
|
|
appLoaded(state) {
|
|
|
|
state.appLoaded = true;
|
|
|
|
},
|
2019-11-02 19:40:59 +00:00
|
|
|
activeChannel(state, channel) {
|
|
|
|
state.activeChannel = channel;
|
|
|
|
},
|
|
|
|
currentUserVisibleError(state, error) {
|
|
|
|
state.currentUserVisibleError = error;
|
|
|
|
},
|
2019-11-05 20:51:55 +00:00
|
|
|
refreshDesktopNotificationState(state) {
|
|
|
|
state.desktopNotificationState = detectDesktopNotificationState();
|
2019-11-02 19:40:59 +00:00
|
|
|
},
|
|
|
|
isAutoCompleting(state, isAutoCompleting) {
|
|
|
|
state.isAutoCompleting = isAutoCompleting;
|
|
|
|
},
|
2019-02-27 14:15:34 +00:00
|
|
|
isConnected(state, payload) {
|
|
|
|
state.isConnected = payload;
|
|
|
|
},
|
2019-11-02 19:40:59 +00:00
|
|
|
networks(state, networks) {
|
|
|
|
state.networks = networks;
|
|
|
|
},
|
2019-11-04 09:21:05 +00:00
|
|
|
mentions(state, mentions) {
|
|
|
|
state.mentions = mentions;
|
|
|
|
},
|
2019-11-02 19:40:59 +00:00
|
|
|
removeNetwork(state, networkId) {
|
2019-11-03 18:05:19 +00:00
|
|
|
state.networks.splice(
|
|
|
|
store.state.networks.findIndex((n) => n.uuid === networkId),
|
|
|
|
1
|
|
|
|
);
|
2019-11-02 19:40:59 +00:00
|
|
|
},
|
|
|
|
sortNetworks(state, sortFn) {
|
|
|
|
state.networks.sort(sortFn);
|
|
|
|
},
|
2019-11-16 17:24:03 +00:00
|
|
|
hasServiceWorker(state) {
|
|
|
|
state.hasServiceWorker = true;
|
|
|
|
},
|
2019-11-02 19:40:59 +00:00
|
|
|
pushNotificationState(state, pushNotificationState) {
|
|
|
|
state.pushNotificationState = pushNotificationState;
|
|
|
|
},
|
|
|
|
serverConfiguration(state, serverConfiguration) {
|
|
|
|
state.serverConfiguration = serverConfiguration;
|
2019-03-01 13:29:00 +00:00
|
|
|
},
|
2019-03-03 17:47:49 +00:00
|
|
|
sessions(state, payload) {
|
|
|
|
state.sessions = payload;
|
|
|
|
},
|
2019-04-13 20:44:04 +00:00
|
|
|
sidebarOpen(state, payload) {
|
|
|
|
state.sidebarOpen = payload;
|
|
|
|
},
|
2019-08-05 12:24:44 +00:00
|
|
|
sidebarDragging(state, payload) {
|
|
|
|
state.sidebarDragging = payload;
|
|
|
|
},
|
2019-11-07 23:47:45 +00:00
|
|
|
toggleSidebar(state) {
|
|
|
|
state.sidebarOpen = !state.sidebarOpen;
|
|
|
|
},
|
2019-11-07 23:50:51 +00:00
|
|
|
toggleUserlist(state) {
|
|
|
|
state.userlistOpen = !state.userlistOpen;
|
|
|
|
},
|
2019-04-13 20:44:04 +00:00
|
|
|
userlistOpen(state, payload) {
|
|
|
|
state.userlistOpen = payload;
|
|
|
|
},
|
2019-08-05 14:29:35 +00:00
|
|
|
versionData(state, payload) {
|
|
|
|
state.versionData = payload;
|
|
|
|
},
|
|
|
|
versionStatus(state, payload) {
|
|
|
|
state.versionStatus = payload;
|
|
|
|
},
|
|
|
|
versionDataExpired(state, payload) {
|
|
|
|
state.versionDataExpired = payload;
|
|
|
|
},
|
2019-11-20 17:43:27 +00:00
|
|
|
serverHasSettings(state, value) {
|
|
|
|
state.serverHasSettings = value;
|
|
|
|
},
|
2019-12-31 16:21:34 +00:00
|
|
|
messageSearchInProgress(state, value) {
|
|
|
|
state.messageSearchInProgress = value;
|
|
|
|
},
|
|
|
|
messageSearchResults(state, value) {
|
|
|
|
state.messageSearchResults = value;
|
|
|
|
},
|
2019-03-03 17:47:49 +00:00
|
|
|
},
|
|
|
|
getters: {
|
2019-11-03 11:23:03 +00:00
|
|
|
findChannelOnCurrentNetwork: (state) => (name) => {
|
|
|
|
name = name.toLowerCase();
|
|
|
|
return state.activeChannel.network.channels.find((c) => c.name.toLowerCase() === name);
|
|
|
|
},
|
2019-12-31 16:21:34 +00:00
|
|
|
findChannelOnNetwork: (state) => (networkUuid, channelName) => {
|
|
|
|
for (const network of state.networks) {
|
|
|
|
if (network.uuid !== networkUuid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const channel of network.channels) {
|
|
|
|
if (channel.name === channelName) {
|
|
|
|
return {network, channel};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2019-11-03 14:59:43 +00:00
|
|
|
findChannel: (state) => (id) => {
|
|
|
|
for (const network of state.networks) {
|
|
|
|
for (const channel of network.channels) {
|
|
|
|
if (channel.id === id) {
|
|
|
|
return {network, channel};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
findNetwork: (state) => (uuid) => {
|
|
|
|
for (const network of state.networks) {
|
|
|
|
if (network.uuid === uuid) {
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2019-11-08 00:03:41 +00:00
|
|
|
highlightCount(state) {
|
|
|
|
let highlightCount = 0;
|
|
|
|
|
|
|
|
for (const network of state.networks) {
|
|
|
|
for (const channel of network.channels) {
|
|
|
|
highlightCount += channel.highlight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return highlightCount;
|
|
|
|
},
|
2019-11-07 23:53:04 +00:00
|
|
|
title(state, getters) {
|
|
|
|
const alertEventCount = getters.highlightCount ? `(${getters.highlightCount}) ` : "";
|
|
|
|
|
|
|
|
const channelname = state.activeChannel ? `${state.activeChannel.channel.name} — ` : "";
|
|
|
|
|
|
|
|
return alertEventCount + channelname + appName;
|
|
|
|
},
|
2019-11-12 15:51:40 +00:00
|
|
|
initChannel: () => (channel) => {
|
|
|
|
// TODO: This should be a mutation
|
|
|
|
channel.pendingMessage = "";
|
|
|
|
channel.inputHistoryPosition = 0;
|
|
|
|
channel.inputHistory = [""];
|
|
|
|
channel.historyLoading = false;
|
|
|
|
channel.scrolledToBottom = true;
|
|
|
|
channel.editTopic = false;
|
|
|
|
|
|
|
|
channel.moreHistoryAvailable = channel.totalMessages > channel.messages.length;
|
|
|
|
delete channel.totalMessages;
|
|
|
|
|
|
|
|
if (channel.type === "channel") {
|
|
|
|
channel.usersOutdated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
},
|
2019-03-01 13:29:00 +00:00
|
|
|
},
|
2019-02-26 20:23:41 +00:00
|
|
|
});
|
2019-11-02 19:40:59 +00:00
|
|
|
|
2019-11-05 20:51:55 +00:00
|
|
|
// Settings module is registered dynamically because it benefits
|
|
|
|
// from a direct reference to the store
|
|
|
|
store.registerModule("settings", createSettingsStore(store));
|
|
|
|
|
2019-11-02 19:40:59 +00:00
|
|
|
export default store;
|