2021-08-06 08:31:17 +00:00
|
|
|
<!--
|
2021-12-07 09:05:52 +00:00
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// 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
|
2021-12-07 09:05:52 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// 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.
|
2021-12-07 09:05:52 +00:00
|
|
|
//
|
2021-08-06 08:31:17 +00:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
-->
|
2021-11-04 11:17:59 +00:00
|
|
|
|
2021-08-06 08:31:17 +00:00
|
|
|
<script lang="ts">
|
2021-09-03 08:34:42 +00:00
|
|
|
import { onMount } from 'svelte'
|
2021-08-06 08:31:17 +00:00
|
|
|
import type { IntlString } from '@anticrm/platform'
|
2021-09-08 15:36:04 +00:00
|
|
|
import { getClient } from '../utils'
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-09-03 08:34:42 +00:00
|
|
|
import { Label, showPopup } from '@anticrm/ui'
|
2021-08-06 08:31:17 +00:00
|
|
|
import Avatar from './Avatar.svelte'
|
2021-09-03 08:34:42 +00:00
|
|
|
import UsersPopup from './UsersPopup.svelte'
|
2021-08-06 08:31:17 +00:00
|
|
|
|
|
|
|
import type { Ref, Class } from '@anticrm/core'
|
2021-12-07 09:05:52 +00:00
|
|
|
import contact, { Contact, formatName } from '@anticrm/contact'
|
2021-11-29 11:32:17 +00:00
|
|
|
import { createEventDispatcher } from 'svelte'
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-12-07 09:05:52 +00:00
|
|
|
export let _class: Ref<Class<Contact>>
|
2021-08-06 08:31:17 +00:00
|
|
|
export let title: IntlString
|
|
|
|
export let caption: IntlString
|
2021-12-07 09:05:52 +00:00
|
|
|
export let value: Ref<Contact> | null | undefined
|
2021-08-06 08:31:17 +00:00
|
|
|
export let show: boolean = false
|
2021-12-03 10:16:16 +00:00
|
|
|
export let allowDeselect = false
|
|
|
|
export let titleDeselect: IntlString | undefined = undefined
|
2021-12-07 09:05:52 +00:00
|
|
|
|
2021-11-29 11:32:17 +00:00
|
|
|
const dispatch = createEventDispatcher()
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-12-07 09:05:52 +00:00
|
|
|
let selected: Contact | undefined
|
2021-09-03 08:34:42 +00:00
|
|
|
let btn: HTMLElement
|
2021-09-21 10:21:41 +00:00
|
|
|
let container: HTMLElement
|
2021-11-04 11:17:59 +00:00
|
|
|
let opened: boolean = false
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-09-08 15:36:04 +00:00
|
|
|
const client = getClient()
|
|
|
|
|
2021-12-07 09:05:52 +00:00
|
|
|
async function updateSelected (value: Ref<Contact>) {
|
2021-09-08 16:16:07 +00:00
|
|
|
selected = await client.findOne(_class, { _id: value })
|
2021-09-08 15:36:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 10:16:16 +00:00
|
|
|
$: if (value != null) {
|
|
|
|
updateSelected(value)
|
|
|
|
}
|
2021-09-08 15:36:04 +00:00
|
|
|
|
2021-09-03 08:34:42 +00:00
|
|
|
onMount(() => {
|
|
|
|
if (btn && show) {
|
|
|
|
btn.click()
|
|
|
|
show = false
|
|
|
|
}
|
|
|
|
})
|
2021-12-07 09:05:52 +00:00
|
|
|
|
|
|
|
function getName (obj: Contact): string {
|
|
|
|
const isPerson = client.getHierarchy().isDerived(obj._class, contact.class.Person)
|
|
|
|
return isPerson ? formatName(obj.name) : obj.name
|
|
|
|
}
|
2021-08-06 08:31:17 +00:00
|
|
|
</script>
|
|
|
|
|
2021-09-21 10:21:41 +00:00
|
|
|
<div class="flex-row-center container" bind:this={container}
|
|
|
|
on:click|preventDefault={() => {
|
|
|
|
btn.focus()
|
2021-11-04 11:17:59 +00:00
|
|
|
if (!opened) {
|
|
|
|
opened = true
|
2021-12-03 10:16:16 +00:00
|
|
|
showPopup(UsersPopup, { _class, title, caption, allowDeselect, selected: value, titleDeselect }, container, (result) => {
|
|
|
|
if (result === undefined) {
|
|
|
|
// Value is not changed.
|
|
|
|
opened = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (result != null) {
|
2021-11-29 11:32:17 +00:00
|
|
|
value = result._id
|
|
|
|
dispatch('change', value)
|
2021-12-03 10:16:16 +00:00
|
|
|
} else {
|
|
|
|
value = null
|
|
|
|
selected = undefined
|
|
|
|
dispatch('change', null)
|
2021-11-29 11:32:17 +00:00
|
|
|
}
|
2021-11-04 11:17:59 +00:00
|
|
|
opened = false
|
|
|
|
})
|
|
|
|
}
|
2021-09-21 10:21:41 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<button class="focused-button btn" class:selected bind:this={btn}>
|
2021-11-15 13:51:24 +00:00
|
|
|
<Avatar avatar={selected ? selected.avatar : undefined} size={'medium'} />
|
2021-09-03 08:34:42 +00:00
|
|
|
</button>
|
2021-08-06 08:31:17 +00:00
|
|
|
|
|
|
|
<div class="selectUser">
|
|
|
|
<div class="title"><Label label={title} /></div>
|
2021-08-17 14:46:06 +00:00
|
|
|
<div class="caption-color">
|
2021-12-07 09:05:52 +00:00
|
|
|
{#if selected}{getName(selected)}{:else}<Label label={'Not selected'} />{/if}
|
2021-08-06 08:31:17 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2021-09-21 10:21:41 +00:00
|
|
|
.container { cursor: pointer; }
|
2021-08-17 14:46:06 +00:00
|
|
|
.btn {
|
|
|
|
width: 2.25rem;
|
|
|
|
height: 2.25rem;
|
2021-09-10 06:41:22 +00:00
|
|
|
background-color: transparent;
|
|
|
|
border: 1px solid var(--theme-card-divider);
|
2021-08-17 14:46:06 +00:00
|
|
|
border-radius: 50%;
|
|
|
|
}
|
2021-09-10 06:41:22 +00:00
|
|
|
.selected { border: none; }
|
2021-08-06 08:31:17 +00:00
|
|
|
|
2021-08-17 14:46:06 +00:00
|
|
|
.selectUser {
|
|
|
|
margin-left: .75rem;
|
|
|
|
.title {
|
|
|
|
font-size: .75rem;
|
|
|
|
font-weight: 500;
|
|
|
|
color: var(--theme-content-accent-color);
|
2021-08-06 08:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|