Files
classroomio/src/lib/components/Apps/index.svelte
T

317 lines
8.4 KiB
Svelte
Raw Normal View History

2021-05-22 22:25:33 +03:00
<script>
import { onDestroy, onMount } from 'svelte';
import hotkeys from 'hotkeys-js';
import Forum from 'carbon-icons-svelte/lib/Forum.svelte';
import AlignBoxTopLeft from 'carbon-icons-svelte/lib/AlignBoxTopLeft.svelte';
import SendAlt from 'carbon-icons-svelte/lib/SendAlt.svelte';
import ChartPie from 'carbon-icons-svelte/lib/ChartPie.svelte';
import IconButton from '$lib/components/IconButton/index.svelte';
import Settings from 'carbon-icons-svelte/lib/Settings.svelte';
import { apps } from './store';
2021-05-29 20:47:46 +03:00
import QandA from './components/QandA/index.svelte';
import LiveChat from './components/LiveChat/index.svelte';
import Notes from './components/Notes/index.svelte';
import Poll from './components/Poll/index.svelte';
import APPS_CONSTANTS from './constants';
import { browser } from '$app/environment';
2021-05-29 20:47:46 +03:00
let selectedApp;
let resize = false;
let isDragging = false;
let startX;
let initialWidth;
let appBarRef;
let appContentRef;
2023-10-04 08:18:33 +01:00
const getResizableSidebar = () => {
if (!browser) return;
return window.innerWidth >= 1024;
};
2021-05-29 20:47:46 +03:00
function handleClose() {
selectedApp = null;
2023-10-04 08:18:33 +01:00
if (getResizableSidebar()) {
appBarRef.style.width = '60px';
}
2021-05-29 20:47:46 +03:00
}
function handleAppClick(appName) {
2023-10-04 08:18:33 +01:00
if (!selectedApp && getResizableSidebar()) {
if (window.innerWidth <= 1024) {
appBarRef.style.width = '300px';
} else {
2023-10-04 08:18:33 +01:00
appBarRef.style.width = '500px';
}
}
2021-05-29 20:47:46 +03:00
if (appName === selectedApp) {
handleClose();
} else {
selectedApp = appName;
}
$apps.dropdown = false;
2021-05-29 20:47:46 +03:00
}
function handleCursor(event) {
2023-10-04 08:18:33 +01:00
if (!getResizableSidebar()) return;
if (!resize && selectedApp) {
const isNearLeftBorder = event.clientX - appContentRef.getBoundingClientRect().left < 8;
const isNearRightBorder = appContentRef.getBoundingClientRect().right - event.clientX < 8;
if (isNearLeftBorder || isNearRightBorder) {
appContentRef.style.cursor = 'ew-resize';
} else {
appContentRef.style.cursor = 'auto';
}
}
}
function startDragging(event) {
2023-10-04 08:18:33 +01:00
if (!getResizableSidebar()) return;
if (event.button === 0 && selectedApp) {
event.preventDefault();
const isNearLeftBorder = event.clientX - appContentRef.getBoundingClientRect().left < 8;
const isNearRightBorder = appContentRef.getBoundingClientRect().right - event.clientX < 8;
if (
(isNearRightBorder || isNearLeftBorder) &&
event.clientX >= 0 &&
event.clientX <= window.innerWidth
) {
isDragging = true;
resize = true;
startX = event.clientX;
initialWidth = parseInt(getComputedStyle(appBarRef).width, 10);
}
}
}
function stopDragging() {
2023-10-04 08:18:33 +01:00
if (!getResizableSidebar()) return;
isDragging = false;
resize = false;
}
function dragSidebar(event) {
2023-10-04 08:18:33 +01:00
if (!getResizableSidebar()) return;
if (!isDragging || !selectedApp) return;
const deltaX = startX - event.clientX + 60;
let newWidth = initialWidth + deltaX;
if (newWidth <= 100) {
handleClose();
appBarRef.style.width = '60px';
appContentRef.style.width = '0';
} else {
appBarRef.style.width = newWidth + 'px';
}
}
2021-05-31 11:28:46 +03:00
onMount(() => {
2023-10-04 08:18:33 +01:00
if (getResizableSidebar()) {
appBarRef.addEventListener('mousedown', startDragging);
document.addEventListener('mousemove', dragSidebar);
document.addEventListener('mouseup', stopDragging);
document.addEventListener('mousemove', handleCursor);
}
hotkeys('A+1,A+2,A+3,A+4', function (event, handler) {
2021-05-31 11:28:46 +03:00
event.preventDefault();
switch (handler.key) {
case 'A+1':
2021-05-31 11:28:46 +03:00
handleAppClick(APPS_CONSTANTS.APPS.LIVE_CHAT);
break;
case 'A+2':
2021-05-31 11:28:46 +03:00
handleAppClick(APPS_CONSTANTS.APPS.QANDA);
break;
case 'A+3':
2021-05-31 11:28:46 +03:00
handleAppClick(APPS_CONSTANTS.APPS.NOTES);
break;
case 'A+4':
2021-05-31 11:28:46 +03:00
handleAppClick(APPS_CONSTANTS.APPS.POLL);
break;
}
});
});
onDestroy(() => {
2023-10-04 08:18:33 +01:00
if (getResizableSidebar()) {
appBarRef.removeEventListener('mousedown', startDragging);
document.removeEventListener('mousemove', dragSidebar);
document.removeEventListener('mouseup', stopDragging);
document.removeEventListener('mousemove', handleCursor);
}
});
2023-10-04 08:18:33 +01:00
$: getResizableSidebar();
2021-05-22 22:25:33 +03:00
</script>
<div
2023-10-04 08:18:33 +01:00
class={`${$apps.open ? 'open dark:bg-black' : 'close dark:bg-black'} root`}
bind:this={appBarRef}
>
2023-10-04 08:18:33 +01:00
<div class="apps">
<div class="lg:hidden">
<IconButton
buttonClassName="lg:hidden"
toolTipProps={{ title: 'Settings', hotkeys: ['A', '0'] }}
onClick={() => {
2023-10-04 08:18:33 +01:00
handleAppClick();
$apps.dropdown = !$apps.dropdown;
}}
>
<Settings size={24} class="carbon-icon dark:text-white lg:hidden" />
</IconButton>
</div>
2021-05-31 11:28:46 +03:00
<IconButton
toolTipProps={{ title: 'Live Chat', hotkeys: ['A', '1'] }}
2021-05-31 11:28:46 +03:00
value={APPS_CONSTANTS.APPS.LIVE_CHAT}
onClick={handleAppClick}
2021-06-26 21:32:42 +03:00
selected={APPS_CONSTANTS.APPS.LIVE_CHAT === selectedApp}
>
<SendAlt size={24} class="carbon-icon dark:text-white" />
2021-05-31 11:28:46 +03:00
</IconButton>
<IconButton
toolTipProps={{ title: 'QandA', hotkeys: ['A', '2'] }}
2021-05-31 11:28:46 +03:00
value={APPS_CONSTANTS.APPS.QANDA}
onClick={handleAppClick}
2021-06-26 21:32:42 +03:00
selected={APPS_CONSTANTS.APPS.QANDA === selectedApp}
>
<Forum size={24} class="carbon-icon dark:text-white" />
2021-05-31 11:28:46 +03:00
</IconButton>
<IconButton
toolTipProps={{ title: 'Notes', hotkeys: ['A', '3'] }}
2021-05-31 11:28:46 +03:00
value={APPS_CONSTANTS.APPS.NOTES}
onClick={handleAppClick}
2021-06-26 21:32:42 +03:00
selected={APPS_CONSTANTS.APPS.NOTES === selectedApp}
>
<AlignBoxTopLeft size={24} class="carbon-icon dark:text-white" />
2021-05-31 11:28:46 +03:00
</IconButton>
<IconButton
toolTipProps={{ title: 'Poll', hotkeys: ['A', '4'] }}
2021-05-31 11:28:46 +03:00
value={APPS_CONSTANTS.APPS.POLL}
onClick={handleAppClick}
2021-06-26 21:32:42 +03:00
selected={APPS_CONSTANTS.APPS.POLL === selectedApp}
>
<ChartPie size={24} class="carbon-icon dark:text-white" />
2021-05-31 11:28:46 +03:00
</IconButton>
</div>
{#if !!selectedApp}
<div
class={`app cursor-auto lg:cursor-ew-resize ${
resize && ' border-l-8 border-l-blue-500 transition-none'
} `}
bind:this={appContentRef}
>
2021-05-31 11:28:46 +03:00
{#if selectedApp === APPS_CONSTANTS.APPS.QANDA}
<QandA {handleClose} />
{:else if selectedApp === APPS_CONSTANTS.APPS.LIVE_CHAT}
<LiveChat {handleClose} />
{:else if selectedApp === APPS_CONSTANTS.APPS.POLL}
<Poll {handleClose} />
{:else if selectedApp === APPS_CONSTANTS.APPS.NOTES}
<Notes {handleClose} />
{/if}
</div>
{/if}
</div>
2021-06-26 21:32:42 +03:00
<style lang="scss">
.root {
height: calc(100vh - 48px);
2021-06-26 21:32:42 +03:00
position: sticky;
top: 0;
border-left: 1px solid var(--border-color);
display: flex;
flex-direction: row;
z-index: 1;
& .apps {
padding-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
width: 60px;
border-right: 1px solid var(--border-color);
height: 100%;
}
& .app {
width: 100%;
position: relative;
overflow: auto;
}
}
@media screen and (min-width: 768px) and (max-width: 1023px) {
.app {
min-width: 280px;
max-width: 300px;
width: 300px;
position: relative;
overflow: auto;
}
}
@media screen and (max-width: 767px) {
.app {
width: 100%;
2021-06-26 21:32:42 +03:00
position: relative;
2021-07-05 10:52:20 +00:00
overflow: auto;
2021-06-26 21:32:42 +03:00
}
}
@media screen and (max-width: 1023px) {
.root {
position: absolute;
height: calc(100vh - 109px);
top: 109px;
right: 0;
z-index: 999;
}
.open {
position: absolute;
transform: translateX(0);
transition: all 0.2s ease-in-out;
}
.close {
position: absolute;
transform: translateX(100%);
display: none;
transition: all 0.2s ease-in-out;
}
}
@media screen and (min-width: 1024px) {
.root {
height: calc(100vh - 48px);
position: sticky;
top: 0;
border-left: 1px solid var(--border-color);
display: flex;
flex-direction: row;
z-index: 1;
& .apps {
padding-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
width: 60px;
border-right: 1px solid var(--border-color);
height: 100%;
}
& .app {
width: 100%;
position: relative;
overflow: auto;
}
}
}
2021-06-26 21:32:42 +03:00
</style>