Skip to content

Commit

Permalink
Quality of Coding
Browse files Browse the repository at this point in the history
  • Loading branch information
MrVauxs committed Jan 2, 2024
1 parent c20e89d commit d1c7fde
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/lib/components/ItemList.svelte
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
<script context="module" lang="ts">
export type columnType<T> = {
enabled: boolean;
order: number;
label: string;
hover: string;
key: string;
sortable: (a: T, b: T) => number;
parser: (item: T) => string;
classes: string;
span: number;
sorted?: 0 | 1 | -1;
sortedHidden?: boolean;
};
</script>

<script lang="ts">
import { settings } from '$lib/settings';
import type { classTypes } from '$lib/data/contentManager';
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
export let items: classTypes[keyof classTypes][] = [];
export let selected = items[0];
export let columns: columnType<any>[];
/*
[
{
Expand All @@ -49,6 +24,31 @@
];
*/
export type columnType<T> = {
enabled: boolean;
order: number;
label: string;
hover: string;
key: string;
sortable: (a: T, b: T) => number;
parser: (item: T) => string;
classes: string;
span: number;
sorted?: 0 | 1 | -1;
sortedHidden?: boolean;
};
</script>

<script lang="ts">
import { settings } from '$lib/settings';
import type { classTypes } from '$lib/data/contentManager';
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
export let items: classTypes[keyof classTypes][] = [];
export let selected = items[0];
export let columns: columnType<any>[];
let remainingSpan = Math.max(
1,
columns.filter((col) => col.enabled).reduce((a, b) => Math.max(a, 0) - Math.max(b.span, 0), 24)
Expand Down Expand Up @@ -76,15 +76,20 @@
});
}
// Set the selected item to the "real" first item on the list, as the list might be filtered or sorted by default.
function findByHash(hash: string) {
if (hash.includes('#')) hash = hash.split('#')[1];
return items.find((item) => item.hash === hash);
}
onMount(() => {
// If there is a hash, set the selected item to the item with the same title as the hash.
if ($page.url.hash) {
const hash = decodeURI($page.url.hash).replace('#', '');
const found = items.find((item) => item.title === hash);
const found = findByHash($page.url.hash);
if (found) selected = found;
} else {
// Set the selected item to the "real" first item on the list, as the list might be filtered or sorted by default.
selected = filteredItems[0];
if (selected) goto(`#${encodeURI(selected.title)}`);
if (selected) goto(`#${selected.hash}`);
}
});
Expand All @@ -106,8 +111,9 @@
}
function hashChange(event: HashChangeEvent & { currentTarget: EventTarget & Window }) {
const hash = decodeURI(event.newURL.split('#')[1]).replace('#', '');
const found = items.find((item) => item.title === hash);
const hash = event.newURL.split('#')[1]?.replace('#', '');
if (!hash) return;
const found = findByHash(hash);
if (found) selected = found;
}
Expand Down Expand Up @@ -179,7 +185,7 @@
class:active={selected === item}
on:click={() => {
if (selected !== item) selected = item;
goto(`#${encodeURI(item.title)}`);
goto(`#${item.hash}`);
}}
>
{#each columns
Expand Down
4 changes: 4 additions & 0 deletions src/lib/data/classes/documentClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Document<T extends dataTypesWithoutSource> {
return this.name.primary + (this.name.specifier ? `; ${this.name.specifier}` : '');
}

get hash(): string {
return encodeURI(this.title + '_' + this.source.ID);
}

get sourceData() {
return (
contentManager._source.find((src) => src.ID === this.source.ID) ?? {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/data/classes/homebrewSourceClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class HomebrewSource {
return this.fullTitle;
}

get hash(): string {
return encodeURI(this.title + '_' + this.ID);
}

get official(): boolean {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/data/classes/sourceClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Source {
return this.name.full;
}

get hash(): string {
return encodeURI(this.title + '_' + this.ID);
}

get official(): boolean {
return this.tags?.misc?.Official || this.data.publisher?.includes('Paizo') || false;
}
Expand Down

0 comments on commit d1c7fde

Please sign in to comment.