Skip to content

Commit

Permalink
Change subject selection for subjects with already-selected parents
Browse files Browse the repository at this point in the history
  • Loading branch information
arackaf committed Dec 7, 2022
1 parent be81359 commit cf9de3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
6 changes: 3 additions & 3 deletions svelte-kit/src/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type Subject = {
path: string;
};

export type SubjectWithPrepends = Subject & {
prepend: Subject[];
export type DisablableSubject = Subject & {
disabled: boolean;
};

export type FullSubject = Subject & {
Expand All @@ -14,7 +14,7 @@ export type FullSubject = Subject & {
};

export type SubjectHash = {
[_id: string]: Subject;
[_id: string]: FullSubject;
};

export type Tag = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@
</script>

<span style="display: flex; margin-left: {effectiveChildLevel * 15}px">
{#each item.prepend || [] as s}
<LabelDisplay extraStyles="margin-right: 5px; cursor: pointer" item={s} disabled={true} />
<i class="fal fa-level-up" style="transform: rotate(90deg) translateX(2px); margin: 0 7px 0 3px;" />
{/each}
<LabelDisplay extraStyles="cursor: pointer" {item} />
<LabelDisplay extraStyles="cursor: pointer" {item} disabled={item.disabled} />
</span>
1 change: 1 addition & 0 deletions svelte-kit/src/lib/components/subjectsAndTags/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type Label = {
export type HierarchicalLabel = Label & {
childLevel: number;
prepend: Label[];
disabled: boolean;
};
36 changes: 19 additions & 17 deletions svelte-kit/src/lib/state/subjectsState.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { FullSubject, Subject, SubjectHash, SubjectWithPrepends } from "$data/types";
import type { DisablableSubject, FullSubject, Subject, SubjectHash } from "$data/types";

export const subjectState = (allSubjectsSorted: Subject[] = []) => {
const subjects = stackAndGetTopLevelSubjects(allSubjectsSorted);
const subjectsUnwound = unwindSubjects(subjects);
const subjectHash = toHash(allSubjectsSorted);
const subjectHash = toHash(subjectsUnwound);

return {
subjects,
Expand All @@ -13,7 +13,6 @@ export const subjectState = (allSubjectsSorted: Subject[] = []) => {
};

export const stackAndGetTopLevelSubjects = (allSubjects: Subject[]): FullSubject[] => {
//let subjects = Object.keys(subjectsHash).map(_id => ({ ...subjectsHash[_id] }));
const subjects: FullSubject[] = allSubjects.map(s => ({
...s,
childLevel: 0,
Expand All @@ -34,7 +33,7 @@ export const unwindSubjects = (subjects: FullSubject[]): FullSubject[] => {
return result;
};

export const toHash = (subjects: Subject[]): SubjectHash => {
export const toHash = (subjects: FullSubject[]): SubjectHash => {
return subjects.reduce<SubjectHash>((hash, tag) => {
hash[tag._id] = tag;
return hash;
Expand All @@ -52,41 +51,44 @@ export const filterSubjects = (subjects: Subject[], search?: string, lookupMap:
let regex = new RegExp(search, "i");
searchFn = s => regex.test(s.name) && !alreadySelected[s._id];
}
return subjects.reduce<SubjectWithPrepends[]>((result, s) => {
const selectedLookup: Set<string> = new Set([]);
return subjects.reduce<DisablableSubject[]>((result, s) => {
if (searchFn(s)) {
const entry: SubjectWithPrepends = { ...s, prepend: [] };
const entry: DisablableSubject = { ...s, disabled: false };
const toAdd: DisablableSubject[] = [entry];

let currentSubject = s;
let parentId;
let ancestorsInactive = 0;
while ((parentId = computeSubjectParentId(currentSubject.path))) {
if (!parentId) {

while ((parentId = computeParentId(currentSubject.path))) {
if (!parentId || selectedLookup.has(parentId)) {
break;
}
let parent = lookupMap[parentId];
if (!parent) {
break;
}
let parentEntry: DisablableSubject = { ...parent, disabled: false };

if (alreadySelected[parent._id] || !searchFn(parent)) {
ancestorsInactive++;
toAdd.unshift(parentEntry);
selectedLookup.add(parentId);

if (alreadySelected[parent._id]) {
parentEntry.disabled = true;
}
}

entry.prepend.unshift(parent);
currentSubject = parent;
}

if (!ancestorsInactive) {
entry.prepend = [];
}

result.push(entry);
result.push(...toAdd);
}
return result;
}, []);
};

const computeSubjectParentId = (path: string) => {
const computeParentId = (path: string) => {
if (path) {
let pathParts = path.split(",");
return pathParts[pathParts.length - 2];
Expand Down

0 comments on commit cf9de3d

Please sign in to comment.