hardlounge/client/components/Sidebar.vue

270 lines
6.5 KiB
Vue
Raw Normal View History

2019-02-22 14:21:32 +00:00
<template>
2019-08-05 14:35:53 +00:00
<aside id="sidebar" ref="sidebar">
<div class="scrollable-area">
<div class="logo-container">
<img
:src="`img/logo-${isPublic() ? 'horizontal-' : ''}transparent-bg.svg`"
class="logo"
alt="SuperNETs"
role="presentation"
2019-08-05 14:35:53 +00:00
/>
<img
:src="`img/logo-${isPublic() ? 'horizontal-' : ''}transparent-bg-inverted.svg`"
class="logo-inverted"
alt="SuperNETs"
role="presentation"
2019-08-05 14:35:53 +00:00
/>
<span
v-if="isDevelopment"
title="The Lounge has been built in development mode"
:style="{
backgroundColor: '#ff9e18',
color: '#000',
padding: '2px',
borderRadius: '4px',
fontSize: '12px',
}"
>DEVELOPER</span
>
2019-02-22 14:21:32 +00:00
</div>
<NetworkList />
2019-08-05 14:35:53 +00:00
</div>
<footer id="footer">
<span
class="tooltipped tooltipped-n tooltipped-no-touch"
aria-label="Connect to network"
2019-10-20 16:17:21 +00:00
><router-link
v-slot:default="{navigate, isActive}"
2019-10-20 16:17:21 +00:00
to="/connect"
2019-08-05 14:35:53 +00:00
role="tab"
aria-controls="connect"
>
<button
:class="['icon', 'connect', {active: isActive}]"
:aria-selected="isActive"
@click="navigate"
@keypress.enter="navigate"
/> </router-link
></span>
2019-08-05 14:35:53 +00:00
<span class="tooltipped tooltipped-n tooltipped-no-touch" aria-label="Settings"
2019-10-20 16:17:21 +00:00
><router-link
v-slot:default="{navigate, isActive}"
2019-10-20 16:17:21 +00:00
to="/settings"
2019-08-05 14:35:53 +00:00
role="tab"
aria-controls="settings"
>
<button
:class="['icon', 'settings', {active: isActive}]"
:aria-selected="isActive"
@click="navigate"
@keypress.enter="navigate"
></button> </router-link
></span>
<span
class="tooltipped tooltipped-n tooltipped-no-touch"
:aria-label="
store.state.serverConfiguration?.isUpdateAvailable
? 'Help\n(update available)'
: 'Help'
"
2019-10-20 16:17:21 +00:00
><router-link
v-slot:default="{navigate, isActive}"
2019-10-20 16:17:21 +00:00
to="/help"
2019-08-05 14:35:53 +00:00
role="tab"
aria-controls="help"
>
<button
:aria-selected="route.name === 'Help'"
:class="[
'icon',
'help',
{notified: store.state.serverConfiguration?.isUpdateAvailable},
{active: isActive},
]"
@click="navigate"
@keypress.enter="navigate"
></button> </router-link
></span>
2019-08-05 14:35:53 +00:00
</footer>
</aside>
2019-02-22 14:21:32 +00:00
</template>
<script lang="ts">
import {defineComponent, nextTick, onMounted, onUnmounted, PropType, ref} from "vue";
import {useRoute} from "vue-router";
import {useStore} from "../js/store";
2019-02-22 14:21:32 +00:00
import NetworkList from "./NetworkList.vue";
export default defineComponent({
2019-02-22 14:21:32 +00:00
name: "Sidebar",
components: {
NetworkList,
},
props: {
overlay: {type: Object as PropType<HTMLElement | null>, required: true},
},
setup(props) {
const isDevelopment = process.env.NODE_ENV !== "production";
2019-08-05 12:24:44 +00:00
const store = useStore();
const route = useRoute();
2019-08-05 12:24:44 +00:00
const touchStartPos = ref<Touch | null>();
const touchCurPos = ref<Touch | null>();
const touchStartTime = ref<number>(0);
const menuWidth = ref<number>(0);
const menuIsMoving = ref<boolean>(false);
const menuIsAbsolute = ref<boolean>(false);
2019-08-05 12:24:44 +00:00
const sidebar = ref<HTMLElement | null>(null);
2019-08-05 12:24:44 +00:00
const toggle = (state: boolean) => {
store.commit("sidebarOpen", state);
};
2019-08-05 12:24:44 +00:00
const onTouchMove = (e: TouchEvent) => {
const touch = (touchCurPos.value = e.touches.item(0));
2019-08-05 12:24:44 +00:00
if (
!touch ||
!touchStartPos.value ||
!touchStartPos.value.screenX ||
!touchStartPos.value.screenY
) {
return;
2019-08-05 12:24:44 +00:00
}
let distX = touch.screenX - touchStartPos.value.screenX;
const distY = touch.screenY - touchStartPos.value.screenY;
2019-08-05 12:24:44 +00:00
if (!menuIsMoving.value) {
2019-08-05 12:24:44 +00:00
// tan(45°) is 1. Gestures in 0°-45° (< 1) are considered horizontal, so
// menu must be open; gestures in 45°-90° (>1) are considered vertical, so
// chat windows must be scrolled.
if (Math.abs(distY / distX) >= 1) {
// eslint-disable-next-line no-use-before-define
onTouchEnd();
2019-08-05 12:24:44 +00:00
return;
}
const devicePixelRatio = window.devicePixelRatio || 2;
if (Math.abs(distX) > devicePixelRatio) {
store.commit("sidebarDragging", true);
menuIsMoving.value = true;
2019-08-05 12:24:44 +00:00
}
}
// Do not animate the menu on desktop view
if (!menuIsAbsolute.value) {
2019-08-05 12:24:44 +00:00
return;
}
if (store.state.sidebarOpen) {
distX += menuWidth.value;
2019-08-05 12:24:44 +00:00
}
if (distX > menuWidth.value) {
distX = menuWidth.value;
2019-08-05 12:24:44 +00:00
} else if (distX < 0) {
distX = 0;
}
if (sidebar.value) {
sidebar.value.style.transform = "translate3d(" + distX.toString() + "px, 0, 0)";
}
if (props.overlay) {
props.overlay.style.opacity = `${distX / menuWidth.value}`;
}
2019-08-05 12:24:44 +00:00
};
const onTouchEnd = () => {
if (!touchStartPos.value?.screenX || !touchCurPos.value?.screenX) {
return;
}
const diff = touchCurPos.value.screenX - touchStartPos.value.screenX;
2019-08-05 12:24:44 +00:00
const absDiff = Math.abs(diff);
if (
absDiff > menuWidth.value / 2 ||
(Date.now() - touchStartTime.value < 180 && absDiff > 50)
2019-08-05 12:24:44 +00:00
) {
toggle(diff > 0);
2019-08-05 12:24:44 +00:00
}
document.body.removeEventListener("touchmove", onTouchMove);
document.body.removeEventListener("touchend", onTouchEnd);
2019-08-05 12:24:44 +00:00
store.commit("sidebarDragging", false);
2019-08-05 12:24:44 +00:00
touchStartPos.value = null;
touchCurPos.value = null;
touchStartTime.value = 0;
menuIsMoving.value = false;
void nextTick(() => {
if (sidebar.value) {
sidebar.value.style.transform = "";
}
if (props.overlay) {
props.overlay.style.opacity = "";
}
});
2019-08-05 12:24:44 +00:00
};
const onTouchStart = (e: TouchEvent) => {
if (!sidebar.value) {
return;
}
touchStartPos.value = touchCurPos.value = e.touches.item(0);
if (e.touches.length !== 1) {
onTouchEnd();
return;
}
const styles = window.getComputedStyle(sidebar.value);
menuWidth.value = parseFloat(styles.width);
menuIsAbsolute.value = styles.position === "absolute";
if (
!store.state.sidebarOpen ||
(touchStartPos.value?.screenX && touchStartPos.value.screenX > menuWidth.value)
) {
touchStartTime.value = Date.now();
document.body.addEventListener("touchmove", onTouchMove, {passive: true});
document.body.addEventListener("touchend", onTouchEnd, {passive: true});
}
2019-08-05 12:24:44 +00:00
};
onMounted(() => {
document.body.addEventListener("touchstart", onTouchStart, {passive: true});
});
onUnmounted(() => {
document.body.removeEventListener("touchstart", onTouchStart);
});
const isPublic = () => document.body.classList.contains("public");
return {
isDevelopment,
store,
route,
sidebar,
toggle,
onTouchStart,
onTouchMove,
onTouchEnd,
isPublic,
};
2019-02-22 14:21:32 +00:00
},
});
2019-02-22 14:21:32 +00:00
</script>