-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
179 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
<script lang="ts"> | ||
import type { Tag } from "$data/types"; | ||
import type { Subject, Tag } from "$data/types"; | ||
import Modal from "../ui/Modal.svelte"; | ||
import EditBook from "./EditBook.svelte"; | ||
export let book: any; | ||
export let isOpen = false; | ||
export let onHide: any; | ||
export let tags: Tag[]; | ||
export let subjects: Subject[]; | ||
let closeModal: any; | ||
</script> | ||
|
||
<Modal headerCaption={`Edit: ${book.title}`} deferStateChangeOnClose={true} {isOpen} {onHide} standardFooter={false} bind:closeModal> | ||
<EditBook {book} {tags} cancel={closeModal} /> | ||
<EditBook {book} {subjects} {tags} cancel={closeModal} /> | ||
</Modal> |
3 changes: 2 additions & 1 deletion
3
svelte-kit/src/lib/components/subjectsAndTags/GenericLabelDisplayItem.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 19 additions & 8 deletions
27
svelte-kit/src/lib/components/subjectsAndTags/subjects/SelectAvailableSubjects.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,35 @@ | ||
<script lang="ts"> | ||
import { stackedSubjects, filterSubjects, subjectsState } from "app/state/subjectsState"; | ||
import type { Subject } from "$data/types"; | ||
import { filterSubjects, subjectState } from "$lib/state/subjectsState"; | ||
//import { stackedSubjects, filterSubjects, subjectsState } from "app/state/subjectsState"; | ||
import GenericLabelSelect from "../GenericLabelSelect.svelte"; | ||
import type { Label } from "../types"; | ||
export let onSelect; | ||
export let onSelect: (item: Label) => void; | ||
export let placeholder = "Subjects"; | ||
export let currentlySelected = []; | ||
export let currentlySelected: string[] = []; | ||
export let subjects: Subject[]; | ||
$: ({ subjectsUnwound } = $stackedSubjects); | ||
$: ({ subjectHash } = $subjectsState); | ||
console.log(subjects); | ||
//$: ({ subjectsUnwound } = $stackedSubjects); | ||
//$: ({ subjectHash } = $subjectsState); | ||
let search = ""; | ||
const doSelect = item => { | ||
const doSelect = (item: Label) => { | ||
onSelect(item); | ||
search = ""; | ||
}; | ||
$: itemHash = currentlySelected.reduce((hash, _idOrObj) => ((hash[_idOrObj] = true), hash), {}); | ||
type LookupHash = { [_id: string]: true }; | ||
$: itemHash = currentlySelected.reduce<LookupHash>((hash, _idOrObj) => ((hash[_idOrObj] = true), hash), {}); | ||
$: subjectsPacket = subjectState(subjects); | ||
$: eligible = filterSubjects(subjectsUnwound, search, subjectHash, itemHash); | ||
$: eligible = filterSubjects(subjectsPacket.subjectsUnwound, search, subjectsPacket.subjectHash, itemHash); | ||
</script> | ||
|
||
<GenericLabelSelect {placeholder} bind:search options={eligible} onItemSelected={doSelect} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type Label = { | ||
name: string; | ||
textColor: string; | ||
backgroundColor: string; | ||
}; | ||
|
||
export type HierarchicalLabel = Label & { | ||
childLevel: number; | ||
prepend: Label[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type { FullSubject, Subject, SubjectHash, SubjectWithPrepends } from "$data/types"; | ||
|
||
export const subjectState = (allSubjectsSorted: Subject[] = []) => { | ||
const subjects = stackAndGetTopLevelSubjects(allSubjectsSorted); | ||
const subjectsUnwound = unwindSubjects(subjects); | ||
const subjectHash = toHash(allSubjectsSorted); | ||
|
||
return { | ||
subjects, | ||
subjectsUnwound, | ||
subjectHash | ||
}; | ||
}; | ||
|
||
export const stackAndGetTopLevelSubjects = (allSubjects: Subject[]): FullSubject[] => { | ||
//let subjects = Object.keys(subjectsHash).map(_id => ({ ...subjectsHash[_id] })); | ||
const subjects: FullSubject[] = allSubjects.map(s => ({ | ||
...s, | ||
childLevel: 0, | ||
children: [] | ||
})); | ||
|
||
subjects.forEach(parent => { | ||
parent.children.push(...subjects.filter(child => new RegExp(`,${parent._id},$`).test(child.path))); | ||
parent.childLevel = !parent.path ? 0 : (parent.path.match(/\,/g) || []).length - 1; | ||
}); | ||
|
||
return subjects.filter(s => s.path == null); | ||
}; | ||
|
||
export const unwindSubjects = (subjects: FullSubject[]): FullSubject[] => { | ||
let result: FullSubject[] = []; | ||
subjects.forEach(s => result.push(s, ...unwindSubjects(s.children || []))); | ||
return result; | ||
}; | ||
|
||
export const toHash = (subjects: Subject[]): SubjectHash => { | ||
return subjects.reduce<SubjectHash>((hash, tag) => { | ||
hash[tag._id] = tag; | ||
return hash; | ||
}, {}); | ||
}; | ||
|
||
type LookupHash = { [_id: string]: true }; | ||
type SearchFn = (s: Subject) => boolean; | ||
|
||
export const filterSubjects = (subjects: Subject[], search?: string, lookupMap: SubjectHash = {}, alreadySelected: LookupHash = {}) => { | ||
let searchFn: SearchFn; | ||
if (!search) { | ||
searchFn = s => !alreadySelected[s._id]; | ||
} else { | ||
let regex = new RegExp(search, "i"); | ||
searchFn = s => regex.test(s.name) && !alreadySelected[s._id]; | ||
} | ||
return subjects.reduce<SubjectWithPrepends[]>((result, s) => { | ||
if (searchFn(s)) { | ||
const entry: SubjectWithPrepends = { ...s, prepend: [] }; | ||
|
||
let currentSubject = s; | ||
let parentId; | ||
let ancestorsInactive = 0; | ||
while ((parentId = computeSubjectParentId(currentSubject.path))) { | ||
if (!parentId) { | ||
break; | ||
} | ||
let parent = lookupMap[parentId]; | ||
if (!parent) { | ||
break; | ||
} | ||
|
||
if (alreadySelected[parent._id] || !searchFn(parent)) { | ||
ancestorsInactive++; | ||
} | ||
|
||
entry.prepend.unshift(parent); | ||
currentSubject = parent; | ||
} | ||
|
||
if (!ancestorsInactive) { | ||
entry.prepend = []; | ||
} | ||
|
||
result.push(entry); | ||
} | ||
return result; | ||
}, []); | ||
}; | ||
|
||
const computeSubjectParentId = (path: string) => { | ||
if (path) { | ||
let pathParts = path.split(","); | ||
return pathParts[pathParts.length - 2]; | ||
} else { | ||
return ""; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
be81359
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
booklist-svelte-kit – ./
booklist-svelte-kit.vercel.app
booklist-svelte-kit-arackaf.vercel.app
booklist-svelte-kit-git-master-arackaf.vercel.app