2022-06-27 11:55:54 +02:00
< script lang = "ts" >
2022-10-02 22:09:13 +02:00
import { Card , CardHeader , CardBody , Collapse , Tooltip } from 'sveltestrap';
2022-06-27 11:55:54 +02:00
import { Member , Group } from '../../api/types';
import { link } from 'svelte-navigator';
import FaLock from 'svelte-icons/fa/FaLock.svelte';
import FaUserCircle from 'svelte-icons/fa/FaUserCircle.svelte';
import FaUsers from 'svelte-icons/fa/FaUsers.svelte'
import MemberBody from '../member/Body.svelte';
import GroupBody from '../group/Body.svelte';
import CardsHeader from '../CardsHeader.svelte';
let settings = JSON.parse(localStorage.getItem("pk-settings"));
export let list: Member[]|Group[];
export let members: Member[] = [];
export let groups: Group[] = [];
export let isPublic: boolean;
export let itemType: string;
2022-06-27 14:45:04 +02:00
export let itemsPerPage: number;
export let currentPage: number;
2022-06-27 16:53:52 +02:00
export let fullLength: number;
2022-06-27 13:09:13 +02:00
export let openByDefault = false;
2022-06-27 14:45:04 +02:00
2022-08-22 12:28:01 +02:00
export let searchBy = "name";
export let sortBy = "name";
2022-06-27 14:45:04 +02:00
$: indexStart = itemsPerPage * (currentPage - 1);
2022-06-27 11:55:54 +02:00
function getItemLink(item: Member | Group): string {
let url: string;
2022-06-27 17:00:48 +02:00
if (!isPublic) url = "/dash/";
2022-06-27 11:55:54 +02:00
else url = "/profile/";
if (itemType === "member") url += "m/";
else if (itemType === "group") url += "g/";
url += item.id;
return url;
}
function skipToNextItem(event, index: number) {
let el;
if (event.key === "ArrowDown") {
2022-06-27 16:53:52 +02:00
if (index + 1 < indexStart + itemsPerPage && index + 1 < fullLength ) el = document.getElementById(`$ { itemType } -card-$ { index + 1 } `);
else el = document.getElementById(`${ itemType } -card-${ indexStart } `);
2022-06-27 11:55:54 +02:00
}
if (event.key === "ArrowUp") {
2022-06-27 16:53:52 +02:00
if (index - 1 >= indexStart) el = document.getElementById(`${ itemType } -card-${ index - 1 } `);
else if (fullLength < = indexStart + itemsPerPage) el = document.getElementById(`${ itemType } -card-${ fullLength - 1 } `);
else el = document.getElementById(`${ itemType } -card-${ indexStart + itemsPerPage - 1 } `);
2022-06-27 11:55:54 +02:00
}
if (el) {
event.preventDefault();
el.focus();
}
}
2022-09-27 20:53:18 +02:00
let isOpen = {} ;
2022-06-27 11:55:54 +02:00
2022-09-27 20:53:18 +02:00
function toggleCard(index: string) {
isOpen[index] = isOpen[index] || {} ;
if (isOpen[index] === true) {
isOpen[index] = false;
2022-06-27 11:55:54 +02:00
} else {
2022-09-27 20:53:18 +02:00
isOpen[index] = true;
2022-06-27 11:55:54 +02:00
}
}
2022-06-27 13:08:19 +02:00
function getShortLink(id: string) {
let url = "https://pk.mt"
if (itemType === "member") url += "/m/"
else if (itemType === "group") url += "/g/"
url += id;
return url;
}
let copiedArray = [];
async function copyShortLink(index: number, id: string, event?) {
if (event) {
if (event.key !== "Tab") event.preventDefault();
event.stopPropagation();
let ctrlDown = event.ctrlKey||event.metaKey; // mac support
if (!(ctrlDown & & event.key === "c") & & event.key !== "Enter") return;
}
try {
await navigator.clipboard.writeText(getShortLink(id));
copiedArray[index] = true;
await new Promise(resolve => setTimeout(resolve, 2000));
copiedArray[index] = false;
} catch (error) {
console.log(error);
}
}
2022-06-27 11:55:54 +02:00
< / script >
2022-06-27 13:09:13 +02:00
{ #if ! openByDefault && ( settings && settings . accessibility ? ( ! settings . accessibility . expandedcards && ! settings . accessibility . pagelinks ) : true )}
2022-06-27 11:55:54 +02:00
< div class = "mb-3" >
2022-09-27 20:53:18 +02:00
{ #each list as item , index ( item . uuid )}
2022-06-27 11:55:54 +02:00
< Card >
< h2 class = "accordion-header" >
2022-09-27 20:53:18 +02:00
< button class = "w-100 accordion-button collapsed card-header" id = { ` ${ itemType } -card- ${ indexStart + index } ` } on:click= {() => toggleCard ( item . uuid )} on:keydown = {( e ) => skipToNextItem ( e , indexStart + index )} >
2022-08-22 12:28:01 +02:00
< CardsHeader { item } { sortBy } { searchBy } >
2022-06-27 16:53:52 +02:00
< div slot = "icon" style = "cursor: pointer;" id = { ` ${ itemType } -copy- ${ item . id } - ${ indexStart + index } ` } on:click | stopPropagation= {() => copyShortLink ( indexStart + index , item . id )} on:keydown = {( e ) => copyShortLink ( indexStart + index , item . id , e )} tabindex= { 0 } >
2022-06-27 11:55:54 +02:00
{ #if isPublic || item . privacy . visibility === "public" }
{ #if itemType === "member" }
< FaUserCircle / >
{ :else if itemType === "group" }
< FaUsers / >
{ /if }
{ : else }
< FaLock / >
{ /if }
< / div >
< / CardsHeader >
2022-06-27 16:53:52 +02:00
< Tooltip placement = "top" target = { ` ${ itemType } -copy- ${ item . id } - ${ indexStart + index } ` } > { copiedArray [ indexStart + index ] ? "Copied!" : "Copy public link" } </Tooltip >
2022-06-27 11:55:54 +02:00
< / button >
< / h2 >
2022-09-27 20:53:18 +02:00
< Collapse isOpen = { isOpen [ item . uuid ]} >
2022-06-27 11:55:54 +02:00
< CardBody >
{ #if itemType === "member" }
2022-08-22 12:13:52 +02:00
< MemberBody on:update on:deletion bind:isPublic groups = { groups } member= { item } />
2022-06-27 11:55:54 +02:00
{ :else if itemType === "group" }
2022-08-22 12:13:52 +02:00
< GroupBody on:update on:deletion bind:isPublic { members } group = { item } / >
2022-06-27 11:55:54 +02:00
{ /if }
< / CardBody >
< / Collapse >
< / Card >
{ /each }
< / div >
2022-06-27 13:09:13 +02:00
{ :else if openByDefault || settings . accessibility . expandedcards }
2022-06-27 11:55:54 +02:00
{ #each list as item , index ( item . id + index )}
< Card class = "mb-3" >
2022-06-27 16:53:52 +02:00
< div class = "accordion-button collapsed p-0" id = { ` ${ itemType } -card- ${ indexStart + index } ` } on:keydown= {( e ) => skipToNextItem ( e , indexStart + index )} tabindex = { 0 } >
2022-06-27 11:55:54 +02:00
< CardHeader class = "w-100" >
2022-08-22 12:28:01 +02:00
< CardsHeader { item } { sortBy } { searchBy } >
2022-06-27 16:53:52 +02:00
< div slot = "icon" style = "cursor: pointer;" id = { ` ${ itemType } -copy- ${ item . id } - ${ indexStart + index } ` } on:click | stopPropagation= {() => copyShortLink ( indexStart + index , item . id )} on:keydown | stopPropagation = {( e ) => copyShortLink ( indexStart + index , item . id , e )} tabindex= { 0 } >
2022-06-27 11:55:54 +02:00
{ #if isPublic || item . privacy . visibility === "public" }
{ #if itemType === "member" }
< FaUserCircle / >
{ :else if itemType === "group" }
< FaUsers / >
{ /if }
{ : else }
< FaLock / >
{ /if }
< / div >
< / CardsHeader >
2022-06-27 16:53:52 +02:00
< Tooltip placement = "top" target = { ` ${ itemType } -copy- ${ item . id } - ${ indexStart + index } ` } > { copiedArray [ indexStart + index ] ? "Copied!" : "Copy public link" } </Tooltip >
2022-06-27 11:55:54 +02:00
< / CardHeader >
< / div >
< CardBody >
{ #if itemType === "member" }
2022-08-22 12:13:52 +02:00
< MemberBody on:update on:deletion bind:isPublic groups = { groups } member= { item } />
2022-06-27 11:55:54 +02:00
{ :else if itemType === "group" }
2022-08-22 12:13:52 +02:00
< GroupBody on:update on:deletion bind:isPublic { members } group = { item } / >
2022-06-27 11:55:54 +02:00
{ /if }
< / CardBody >
< / Card >
{ /each }
{ : else }
< div class = "my-3" >
{ #each list as item , index ( item . id + index )}
< Card >
2022-06-27 16:53:52 +02:00
< a class = "accordion-button collapsed" style = "text-decoration: none;" href = { getItemLink ( item )} id= { ` ${ itemType } -card- ${ indexStart + index } ` } on:keydown = {( e ) => skipToNextItem ( e , indexStart + index )} use:link >
2022-06-27 11:55:54 +02:00
< CardsHeader { item } >
2022-06-27 16:53:52 +02:00
< div slot = "icon" style = "cursor: pointer;" id = { ` ${ itemType } -copy- ${ item . id } - ${ indexStart + index } ` } on:click | stopPropagation= {() => copyShortLink ( indexStart + index , item . id )} on:keydown | stopPropagation = {( e ) => copyShortLink ( indexStart + index , item . id , e )} tabindex= { 0 } >
2022-06-27 11:55:54 +02:00
{ #if isPublic || item . privacy . visibility === "public" }
{ #if itemType === "member" }
< FaUserCircle / >
{ :else if itemType === "group" }
< FaUsers / >
{ /if }
{ : else }
< FaLock / >
{ /if }
< / div >
< / CardsHeader >
2022-06-27 16:53:52 +02:00
< Tooltip placement = "top" target = { ` ${ itemType } -copy- ${ item . id } - ${ indexStart + index } ` } > { copiedArray [ indexStart + index ] ? "Copied!" : "Copy public link" } </Tooltip >
2022-06-27 11:55:54 +02:00
< / a >
< / Card >
{ /each }
< / div >
{ /if }