platform/plugins/tracker-resources/src/components/issues/StatusEditor.svelte
Alexander Platov 2e6181acf1
TSK-1459: update Panel layout (#3163)
Signed-off-by: Alexander Platov <sas_lord@mail.ru>
2023-05-11 14:18:33 +07:00

156 lines
5.3 KiB
Svelte

<!--
// Copyright © 2022 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
-->
<script lang="ts">
import { AttachedData, Ref, StatusManager, WithLookup } from '@hcengineering/core'
import { getClient } from '@hcengineering/presentation'
import { Issue, IssueDraft, IssueStatus, Project } from '@hcengineering/tracker'
import type { ButtonKind, ButtonSize } from '@hcengineering/ui'
import { Button, eventToHTMLElement, SelectPopup, showPopup, TooltipAlignment } from '@hcengineering/ui'
import { createEventDispatcher } from 'svelte'
import tracker from '../../plugin'
import { statusStore } from '@hcengineering/presentation'
import IssueStatusIcon from './IssueStatusIcon.svelte'
import StatusPresenter from './StatusPresenter.svelte'
type ValueType = Issue | (AttachedData<Issue> & { space: Ref<Project> }) | IssueDraft
export let value: ValueType
let statuses: WithLookup<IssueStatus>[] | undefined = undefined
export let isEditable: boolean = true
export let shouldShowLabel: boolean = false
export let tooltipAlignment: TooltipAlignment | undefined = undefined
export let kind: ButtonKind = 'link'
export let size: ButtonSize = 'large'
export let justify: 'left' | 'center' = 'left'
export let width: string | undefined = undefined
export let defaultIssueStatus: Ref<IssueStatus> | undefined = undefined
export let focusIndex: number | undefined = undefined
const client = getClient()
const dispatch = createEventDispatcher()
const changeStatus = async (newStatus: Ref<IssueStatus> | undefined, refocus: boolean = true) => {
if (!isEditable || newStatus === undefined || value.status === newStatus) {
return
}
dispatch('change', newStatus)
if (refocus) {
dispatch('refocus')
}
if ('_class' in value) {
await client.update(value, { status: newStatus })
}
}
const handleStatusEditorOpened = (event: MouseEvent) => {
if (!isEditable) {
return
}
showPopup(
SelectPopup,
{ value: statusesInfo, placeholder: tracker.string.SetStatus, searchable: true },
eventToHTMLElement(event),
changeStatus
)
}
function getStatuses (statusStore: StatusManager, value: ValueType): WithLookup<IssueStatus>[] {
return statusStore.filter((it) => it.space === value?.space)
}
$: statuses = getStatuses($statusStore, value)
function getSelectedStatus (
statuses: WithLookup<IssueStatus>[] | undefined,
value: ValueType,
defaultStatus: Ref<IssueStatus> | undefined
): WithLookup<IssueStatus> | undefined {
if (value.status !== undefined) {
const current = statuses?.find((status) => status._id === value.status)
if (current) return current
}
if (defaultIssueStatus !== undefined) {
const res = statuses?.find((status) => status._id === defaultStatus)
changeStatus(res?._id, false)
return res
}
}
$: selectedStatus = getSelectedStatus(statuses, value, defaultIssueStatus)
$: selectedStatusLabel = shouldShowLabel ? selectedStatus?.name : undefined
$: statusesInfo = statuses?.map((s) => {
return {
id: s._id,
component: StatusPresenter,
props: { value: s, size: 'small' },
isSelected: selectedStatus?._id === s._id ?? false
}
})
$: smallgap = size === 'inline' || size === 'small'
</script>
{#if value && statuses}
{#if kind === 'list' || kind === 'list-header'}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="flex-row-center flex-no-shrink"
class:fix-margin={kind === 'list'}
class:cursor-pointer={isEditable}
on:click={handleStatusEditorOpened}
>
<div class="flex-center flex-no-shrink square-4">
{#if selectedStatus}<IssueStatusIcon value={selectedStatus} size={kind === 'list' ? 'small' : 'medium'} />{/if}
</div>
{#if selectedStatusLabel}
<span class="{kind === 'list' ? 'ml-1 text-md' : 'ml-2 text-base'} overflow-label disabled content-color">
{selectedStatusLabel}
</span>
{/if}
</div>
{:else}
<Button
showTooltip={isEditable ? { label: tracker.string.SetStatus, direction: tooltipAlignment } : undefined}
disabled={!isEditable}
{justify}
{size}
{kind}
{width}
{focusIndex}
on:click={handleStatusEditorOpened}
>
<span slot="content" class="flex-row-center pointer-events-none">
{#if selectedStatus}
<IssueStatusIcon value={selectedStatus} size="inline" />
{/if}
{#if selectedStatusLabel}
<span
class="overflow-label disabled"
class:ml-1={selectedStatus && smallgap}
class:ml-2={selectedStatus && !smallgap}
>
{selectedStatusLabel}
</span>
{/if}
</span>
</Button>
{/if}
{/if}