2022-04-20 07:56:45 +00:00
|
|
|
<!--
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 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">
|
2022-04-21 09:38:09 +00:00
|
|
|
import { createEventDispatcher } from 'svelte'
|
2022-04-20 07:56:45 +00:00
|
|
|
import type { Channel, ChannelProvider } from '@anticrm/contact'
|
|
|
|
import contact from '@anticrm/contact'
|
|
|
|
import type { AttachedData, Doc, Ref, Timestamp } from '@anticrm/core'
|
|
|
|
import type { Asset, IntlString } from '@anticrm/platform'
|
2022-04-29 05:27:17 +00:00
|
|
|
import { AnyComponent, showPopup, Button, Menu, closePopup } from '@anticrm/ui'
|
2022-04-20 07:56:45 +00:00
|
|
|
import type { Action, ButtonKind, ButtonSize } from '@anticrm/ui'
|
|
|
|
import presentation from '@anticrm/presentation'
|
|
|
|
import { getChannelProviders } from '../utils'
|
|
|
|
import ChannelEditor from './ChannelEditor.svelte'
|
|
|
|
import { NotificationClientImpl } from '@anticrm/notification-resources'
|
|
|
|
|
|
|
|
export let value: AttachedData<Channel>[] | Channel | null
|
2022-05-03 16:04:21 +00:00
|
|
|
export let editable: boolean = false
|
2022-04-20 07:56:45 +00:00
|
|
|
export let kind: ButtonKind = 'no-border'
|
|
|
|
export let size: ButtonSize = 'small'
|
|
|
|
export let length: 'short' | 'full' = 'full'
|
2022-04-21 09:38:09 +00:00
|
|
|
export let shape: 'circle' | undefined = undefined
|
2022-04-20 07:56:45 +00:00
|
|
|
export let integrations: Set<Ref<Doc>> = new Set<Ref<Doc>>()
|
2022-04-21 09:38:09 +00:00
|
|
|
|
2022-04-20 07:56:45 +00:00
|
|
|
const notificationClient = NotificationClientImpl.getClient()
|
|
|
|
const lastViews = notificationClient.getLastViews()
|
2022-04-21 09:38:09 +00:00
|
|
|
const dispatch = createEventDispatcher()
|
2022-04-20 07:56:45 +00:00
|
|
|
|
|
|
|
interface Item {
|
|
|
|
label: IntlString
|
|
|
|
icon: Asset
|
|
|
|
value: string
|
|
|
|
presenter?: AnyComponent
|
|
|
|
placeholder: IntlString
|
|
|
|
provider: Ref<ChannelProvider>
|
|
|
|
integration: boolean
|
|
|
|
notification: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProvider (
|
|
|
|
item: AttachedData<Channel>,
|
|
|
|
map: Map<Ref<ChannelProvider>, ChannelProvider>,
|
|
|
|
lastViews: Map<Ref<Doc>, Timestamp>
|
|
|
|
): any | undefined {
|
|
|
|
const provider = map.get(item.provider)
|
|
|
|
if (provider) {
|
2022-04-29 05:27:17 +00:00
|
|
|
const notification = (item as Channel)._id !== undefined ? isNew(item as Channel, lastViews) : false
|
2022-04-20 07:56:45 +00:00
|
|
|
return {
|
|
|
|
label: provider.label,
|
|
|
|
icon: provider.icon as Asset,
|
|
|
|
value: item.value,
|
|
|
|
presenter: provider.presenter,
|
|
|
|
placeholder: provider.placeholder,
|
|
|
|
provider: provider._id,
|
|
|
|
notification,
|
|
|
|
integration: provider.integrationType !== undefined ? integrations.has(provider.integrationType) : false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log('provider not found: ', item.provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isNew (item: Channel, lastViews: Map<Ref<Doc>, Timestamp>): boolean {
|
|
|
|
const lastView = (item as Channel)._id !== undefined ? lastViews.get((item as Channel)._id) : undefined
|
|
|
|
return lastView ? lastView < item.modifiedOn : (item.items ?? 0) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
async function update (value: AttachedData<Channel>[] | Channel | null, lastViews: Map<Ref<Doc>, Timestamp>) {
|
|
|
|
if (value === null) {
|
|
|
|
displayItems = []
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const result = []
|
|
|
|
const map = await getChannelProviders()
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
for (const item of value) {
|
|
|
|
const provider = getProvider(item, map, lastViews)
|
|
|
|
if (provider !== undefined) {
|
|
|
|
result.push(provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const provider = getProvider(value, map, lastViews)
|
|
|
|
if (provider !== undefined) {
|
|
|
|
result.push(provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
displayItems = result
|
|
|
|
updateMenu()
|
|
|
|
}
|
|
|
|
|
|
|
|
$: if (value) update(value, $lastViews)
|
|
|
|
|
|
|
|
let providers: Map<Ref<ChannelProvider>, ChannelProvider>
|
|
|
|
let displayItems: Item[] = []
|
|
|
|
let actions: Action[] = []
|
|
|
|
let addBtn: HTMLButtonElement
|
2022-04-29 05:27:17 +00:00
|
|
|
const btns: HTMLButtonElement[] = []
|
2022-04-20 07:56:45 +00:00
|
|
|
|
|
|
|
function filterUndefined (channels: AttachedData<Channel>[]): AttachedData<Channel>[] {
|
|
|
|
return channels.filter((channel) => channel.value !== undefined && channel.value.length > 0)
|
|
|
|
}
|
2022-04-29 05:27:17 +00:00
|
|
|
|
|
|
|
getChannelProviders().then((pr) => (providers = pr))
|
2022-04-20 07:56:45 +00:00
|
|
|
|
|
|
|
const updateMenu = (): void => {
|
|
|
|
actions = []
|
2022-04-29 05:27:17 +00:00
|
|
|
providers.forEach((pr) => {
|
|
|
|
if (displayItems.filter((it) => it.provider === pr._id).length === 0) {
|
2022-04-20 07:56:45 +00:00
|
|
|
actions.push({
|
|
|
|
icon: pr.icon ?? contact.icon.SocialEdit,
|
|
|
|
label: pr.label,
|
|
|
|
action: async () => {
|
|
|
|
const provider = getProvider({ provider: pr._id, value: '' }, providers, $lastViews)
|
|
|
|
if (provider !== undefined) {
|
2022-04-29 05:27:17 +00:00
|
|
|
if (displayItems.filter((it) => it.provider === pr._id).length === 0) {
|
2022-04-20 07:56:45 +00:00
|
|
|
displayItems = [...displayItems, provider]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
$: if (providers) updateMenu()
|
|
|
|
|
2022-05-03 16:04:21 +00:00
|
|
|
const dropItem = (n: number): Item[] => {
|
|
|
|
return displayItems.filter((it, i) => i !== n)
|
2022-04-21 09:38:09 +00:00
|
|
|
}
|
|
|
|
const saveItems = (): void => {
|
|
|
|
value = filterUndefined(displayItems)
|
|
|
|
dispatch('change', value)
|
|
|
|
updateMenu()
|
|
|
|
}
|
|
|
|
|
2022-04-20 07:56:45 +00:00
|
|
|
const editChannel = (channel: Item, n: number, ev: MouseEvent): void => {
|
|
|
|
showPopup(
|
|
|
|
ChannelEditor,
|
|
|
|
{ value: channel.value, placeholder: channel.placeholder },
|
|
|
|
ev.target as HTMLElement,
|
2022-04-29 05:27:17 +00:00
|
|
|
(result) => {
|
2022-04-20 07:56:45 +00:00
|
|
|
if (result !== undefined) {
|
2022-05-03 16:04:21 +00:00
|
|
|
if (result === null || result === '') displayItems = dropItem(n)
|
2022-04-21 09:38:09 +00:00
|
|
|
else displayItems[n].value = result
|
2022-05-03 16:04:21 +00:00
|
|
|
saveItems()
|
|
|
|
if (displayItems.length < providers.size && addBtn) addBtn.click()
|
2022-04-21 09:38:09 +00:00
|
|
|
}
|
|
|
|
},
|
2022-04-29 05:27:17 +00:00
|
|
|
(result) => {
|
|
|
|
if (result !== undefined) {
|
2022-04-21 09:38:09 +00:00
|
|
|
if (result === 'left') {
|
|
|
|
closePopup()
|
|
|
|
if (displayItems[n].value === '') {
|
2022-05-03 16:04:21 +00:00
|
|
|
displayItems = dropItem(n)
|
2022-04-21 09:38:09 +00:00
|
|
|
saveItems()
|
|
|
|
}
|
2022-05-03 16:04:21 +00:00
|
|
|
if (n === 0) {
|
|
|
|
if (addBtn) addBtn.click()
|
|
|
|
else btns[displayItems.length - 1].click()
|
|
|
|
} else btns[n - 1].click()
|
2022-04-21 09:38:09 +00:00
|
|
|
} else if (result === 'right') {
|
|
|
|
closePopup()
|
|
|
|
if (displayItems[n].value === '') {
|
2022-05-03 16:04:21 +00:00
|
|
|
displayItems = dropItem(n)
|
2022-04-21 09:38:09 +00:00
|
|
|
saveItems()
|
|
|
|
}
|
2022-05-03 16:04:21 +00:00
|
|
|
if (n === displayItems.length - 1) {
|
|
|
|
if (addBtn) addBtn.click()
|
|
|
|
else btns[0].click()
|
|
|
|
} else btns[n + 1].click()
|
|
|
|
} else if (result === 'open') {
|
|
|
|
closePopup()
|
|
|
|
dispatch('open', { presenter: channel.presenter })
|
2022-04-20 07:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const showMenu = (ev: MouseEvent): void => {
|
2022-04-29 05:27:17 +00:00
|
|
|
showPopup(
|
|
|
|
Menu,
|
|
|
|
{ actions },
|
|
|
|
ev.target as HTMLElement,
|
2022-05-03 16:04:21 +00:00
|
|
|
() => {},
|
2022-04-29 05:27:17 +00:00
|
|
|
(result) => {
|
|
|
|
if (result !== undefined && displayItems.length > 0) {
|
|
|
|
if (result === 'left') {
|
|
|
|
closePopup()
|
|
|
|
btns[displayItems.length - 1].click()
|
|
|
|
} else if (result === 'right') {
|
|
|
|
closePopup()
|
|
|
|
btns[0].click()
|
|
|
|
}
|
2022-04-21 09:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-29 05:27:17 +00:00
|
|
|
)
|
2022-04-20 07:56:45 +00:00
|
|
|
}
|
2022-04-21 09:38:09 +00:00
|
|
|
let copied: boolean = false
|
2022-04-26 05:42:18 +00:00
|
|
|
</script>
|
2022-04-28 09:53:35 +00:00
|
|
|
|
2022-04-21 09:38:09 +00:00
|
|
|
<div
|
2022-04-29 05:27:17 +00:00
|
|
|
class="{displayItems.length === 0 ? 'clear-mins' : 'buttons-group'} {kind === 'no-border'
|
|
|
|
? 'xsmall-gap'
|
|
|
|
: 'xxsmall-gap'}"
|
2022-04-21 09:38:09 +00:00
|
|
|
class:short={displayItems.length > 4 && length === 'short'}
|
|
|
|
>
|
|
|
|
{#each displayItems as item, i}
|
|
|
|
{#if item.value === ''}
|
2022-04-20 07:56:45 +00:00
|
|
|
<Button
|
2022-04-29 05:27:17 +00:00
|
|
|
icon={item.icon}
|
|
|
|
{kind}
|
|
|
|
{size}
|
|
|
|
{shape}
|
|
|
|
click={item.value === ''}
|
|
|
|
on:click={(ev) => {
|
2022-05-03 16:04:21 +00:00
|
|
|
if (editable) editChannel(item, i, ev)
|
2022-04-29 05:27:17 +00:00
|
|
|
}}
|
2022-04-20 07:56:45 +00:00
|
|
|
/>
|
2022-04-21 09:38:09 +00:00
|
|
|
{:else}
|
|
|
|
<div class="tooltip-container">
|
2022-04-29 05:27:17 +00:00
|
|
|
<div class="tooltip">
|
|
|
|
{item.value}{#if copied}<span class="ml-1 text-sm dark-color">(copied)</span>{/if}
|
|
|
|
</div>
|
2022-04-21 09:38:09 +00:00
|
|
|
<Button
|
|
|
|
bind:input={btns[i]}
|
2022-04-29 05:27:17 +00:00
|
|
|
icon={item.icon}
|
|
|
|
{kind}
|
|
|
|
{size}
|
|
|
|
{shape}
|
2022-05-03 16:04:21 +00:00
|
|
|
highlight={item.integration || item.notification}
|
2022-04-21 09:38:09 +00:00
|
|
|
on:click={(ev) => {
|
2022-05-03 16:04:21 +00:00
|
|
|
if (editable) {
|
2022-04-26 05:42:18 +00:00
|
|
|
editChannel(item, i, ev)
|
|
|
|
} else {
|
2022-05-03 16:04:21 +00:00
|
|
|
dispatch('open', item)
|
2022-04-26 05:42:18 +00:00
|
|
|
if (!copied) {
|
|
|
|
navigator.clipboard.writeText(item.value)
|
|
|
|
copied = true
|
2022-04-29 05:27:17 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
copied = false
|
|
|
|
}, 1000)
|
2022-04-26 05:42:18 +00:00
|
|
|
}
|
2022-04-21 09:38:09 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{/each}
|
|
|
|
{#if actions.length > 0 && editable}
|
|
|
|
<Button
|
|
|
|
bind:input={addBtn}
|
|
|
|
icon={contact.icon.SocialEdit}
|
2022-04-27 05:50:07 +00:00
|
|
|
label={displayItems.length === 0 ? presentation.string.AddSocialLinks : undefined}
|
2022-04-29 05:27:17 +00:00
|
|
|
{kind}
|
|
|
|
{size}
|
|
|
|
{shape}
|
2022-05-03 16:04:21 +00:00
|
|
|
on:click={showMenu}
|
2022-04-21 09:38:09 +00:00
|
|
|
/>
|
2022-04-20 07:56:45 +00:00
|
|
|
{/if}
|
2022-04-21 09:38:09 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.tooltip-container {
|
|
|
|
position: relative;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
min-width: 0;
|
|
|
|
min-height: 0;
|
|
|
|
width: min-content;
|
|
|
|
|
|
|
|
.tooltip {
|
2022-04-23 03:51:42 +00:00
|
|
|
overflow: hidden;
|
2022-04-21 09:38:09 +00:00
|
|
|
position: absolute;
|
2022-04-29 05:27:17 +00:00
|
|
|
padding: 0.25rem 0.5rem;
|
2022-04-21 09:38:09 +00:00
|
|
|
bottom: 100%;
|
|
|
|
left: 50%;
|
|
|
|
width: auto;
|
2022-04-23 03:51:42 +00:00
|
|
|
min-width: 0;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
2022-04-21 09:38:09 +00:00
|
|
|
background-color: var(--accent-bg-color);
|
|
|
|
border: 1px solid var(--button-border-color);
|
2022-04-29 05:27:17 +00:00
|
|
|
border-radius: 0.25rem;
|
2022-04-21 09:38:09 +00:00
|
|
|
transform-origin: center center;
|
2022-04-29 05:27:17 +00:00
|
|
|
transform: translate(-50%, -0.25rem) scale(0.9);
|
2022-04-21 09:38:09 +00:00
|
|
|
opacity: 0;
|
|
|
|
box-shadow: var(--accent-shadow);
|
|
|
|
transition-property: transform, opacity;
|
2022-04-29 05:27:17 +00:00
|
|
|
transition-duration: 0.15s;
|
2022-04-21 09:38:09 +00:00
|
|
|
transition-timing-function: cubic-bezier(0.175, 0.885, 0.32, 1.275);
|
|
|
|
pointer-events: none;
|
2022-05-03 16:04:21 +00:00
|
|
|
z-index: 1000;
|
2022-04-21 09:38:09 +00:00
|
|
|
}
|
|
|
|
&:hover .tooltip {
|
2022-04-29 05:27:17 +00:00
|
|
|
transform: translate(-50%, -0.5rem) scale(1);
|
2022-04-21 09:38:09 +00:00
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|