diff --git a/plugins/hr-resources/src/components/DepartmentCard.svelte b/plugins/hr-resources/src/components/DepartmentCard.svelte index fdd0159c23..a23c2c6d3f 100644 --- a/plugins/hr-resources/src/components/DepartmentCard.svelte +++ b/plugins/hr-resources/src/components/DepartmentCard.svelte @@ -18,16 +18,8 @@ import { Ref, WithLookup } from '@hcengineering/core' import { Department, Staff } from '@hcengineering/hr' import { getClient } from '@hcengineering/presentation' - import { - Button, - IconAdd, - Label, - closeTooltip, - eventToHTMLElement, - getEventPositionElement, - showPopup - } from '@hcengineering/ui' - import { Menu, openDoc, showMenu } from '@hcengineering/view-resources' + import { Button, IconAdd, Label, closeTooltip, eventToHTMLElement, showPopup } from '@hcengineering/ui' + import { openDoc, showMenu } from '@hcengineering/view-resources' import hr from '../plugin' import { addMember } from '../utils' import CreateDepartment from './CreateDepartment.svelte' diff --git a/plugins/hr-resources/src/components/DepartmentEditor.svelte b/plugins/hr-resources/src/components/DepartmentEditor.svelte index e4b2cbe1af..43699d6b03 100644 --- a/plugins/hr-resources/src/components/DepartmentEditor.svelte +++ b/plugins/hr-resources/src/components/DepartmentEditor.svelte @@ -19,6 +19,7 @@ import { ButtonKind, ButtonSize } from '@hcengineering/ui' import { ObjectBox } from '@hcengineering/view-resources' import hr from '../plugin' + import { createQuery } from '@hcengineering/presentation' export let value: Ref | undefined export let label: IntlString = hr.string.ParentDepartmentLabel @@ -27,16 +28,47 @@ export let size: ButtonSize = 'small' export let justify: 'left' | 'center' = 'center' export let width: string | undefined = undefined + export let object: Department | undefined + + let excluded: Ref[] = [] + let descendants: Map, Department[]> = new Map, Department[]>() + + $: excluded = + object !== undefined + ? descendants + .get(object._id) + ?.map((p) => p._id) + .concat(object._id) ?? [object._id] + : [] + + const q = createQuery() + $: if (object !== undefined) { + q.query(hr.class.Department, {}, (res) => { + descendants.clear() + for (const doc of res) { + if (doc.parent !== undefined && doc._id !== hr.ids.Head) { + const current = descendants.get(doc.parent) ?? [] + current.push(doc) + descendants.set(doc.parent, current) + } + } + descendants = descendants + }) + } else { + q.unsubscribe() + excluded = [] + }