hardlounge/client/js/vue.js

175 lines
4.4 KiB
JavaScript
Raw Normal View History

"use strict";
const Vue = require("vue").default;
2019-10-17 16:56:44 +00:00
const store = require("./store").default;
const App = require("../components/App.vue").default;
const localetime = require("./helpers/localetime");
const storage = require("./localStorage");
2019-11-03 15:56:41 +00:00
const {router} = require("./router");
2019-10-17 16:56:44 +00:00
const constants = require("./constants");
Vue.filter("localetime", localetime);
const appName = document.title;
const vueApp = new Vue({
el: "#viewport",
2019-10-17 16:56:44 +00:00
router,
2018-07-10 09:40:55 +00:00
mounted() {
2019-11-03 17:09:10 +00:00
document.addEventListener("visibilitychange", this.synchronizeNotifiedState);
document.addEventListener("focus", this.synchronizeNotifiedState);
document.addEventListener("click", this.synchronizeNotifiedState);
// TODO: Hackfix because socket-events require vueApp somewhere
// and that breaks due to cyclical depenency as by this point vue.js
// does not export anything yet.
setTimeout(() => {
const socket = require("./socket");
require("./socket-events");
require("./contextMenuFactory");
require("./webpush");
require("./keybinds");
socket.open();
}, 1);
2018-07-10 09:40:55 +00:00
},
methods: {
setSidebar(state) {
this.$store.commit("sidebarOpen", state);
2019-10-17 16:56:44 +00:00
if (window.outerWidth > constants.mobileViewportPixels) {
storage.set("thelounge.state.sidebar", state);
}
this.$emit("resize");
},
toggleSidebar() {
this.setSidebar(!this.$store.state.sidebarOpen);
},
2019-10-17 16:56:44 +00:00
closeSidebarIfNeeded() {
if (window.innerWidth <= constants.mobileViewportPixels) {
this.setSidebar(false);
}
},
setUserlist(state) {
storage.set("thelounge.state.userlist", state);
this.$store.commit("userlistOpen", state);
this.$emit("resize");
},
toggleUserlist() {
this.setUserlist(!this.$store.state.userlistOpen);
},
2019-10-25 21:37:40 +00:00
switchToChannel(channel) {
if (
this.$store.state.activeChannel &&
this.$store.state.activeChannel.channel.id === channel.id
) {
2019-10-25 21:37:40 +00:00
return;
}
this.$router.push("/chan-" + channel.id);
2019-10-25 21:37:40 +00:00
},
2019-10-17 16:56:44 +00:00
switchOutOfChannel(channel) {
// When switching out of a channel, mark everything as read
if (channel.messages.length > 0) {
channel.firstUnread = channel.messages[channel.messages.length - 1].id;
}
if (channel.messages.length > 100) {
channel.messages.splice(0, channel.messages.length - 100);
channel.moreHistoryAvailable = true;
}
},
synchronizeNotifiedState() {
this.updateTitle();
let hasAnyHighlights = false;
for (const network of this.$store.state.networks) {
2019-10-17 16:56:44 +00:00
for (const chan of network.channels) {
if (chan.highlight > 0) {
hasAnyHighlights = true;
break;
}
}
}
this.toggleNotificationMarkers(hasAnyHighlights);
},
updateTitle() {
let title = appName;
2019-10-17 16:56:44 +00:00
if (this.$store.state.activeChannel) {
title = `${this.$store.state.activeChannel.channel.name}${title}`;
2019-10-17 16:56:44 +00:00
}
// add highlight count to title
let alertEventCount = 0;
for (const network of this.$store.state.networks) {
2019-10-17 16:56:44 +00:00
for (const channel of network.channels) {
alertEventCount += channel.highlight;
}
}
if (alertEventCount > 0) {
title = `(${alertEventCount}) ${title}`;
}
document.title = title;
},
toggleNotificationMarkers(newState) {
if (this.$store.state.isNotified !== newState) {
// Toggles a dot on the menu icon when there are unread notifications
this.$store.commit("isNotified", newState);
// Toggles the favicon to red when there are unread notifications
const favicon = document.getElementById("favicon");
const old = favicon.getAttribute("href");
favicon.setAttribute("href", favicon.dataset.other);
favicon.dataset.other = old;
}
},
},
render(createElement) {
return createElement(App, {
2019-02-18 09:18:32 +00:00
ref: "app",
props: this,
});
},
store,
});
2018-09-14 15:44:26 +00:00
Vue.config.errorHandler = function(e) {
store.commit("currentUserVisibleError", `Vue error: ${e.message}`);
2018-09-14 15:44:26 +00:00
console.error(e); // eslint-disable-line
};
function initChannel(channel) {
2018-09-09 12:23:12 +00:00
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;
}
}
2019-02-18 09:18:32 +00:00
function getActiveWindowComponent() {
return vueApp.$refs.app.$refs.window;
}
module.exports = {
vueApp,
initChannel,
2019-02-18 09:18:32 +00:00
getActiveWindowComponent,
};