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-05-05 08:29:46 +00:00
|
|
|
import { AnyComponent, showPopup, Button, Menu, showTooltip, closeTooltip, eventToHTMLElement } 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 {
|
2022-05-06 17:45:59 +00:00
|
|
|
if (item.lastMessage === undefined) return false
|
2022-04-20 07:56:45 +00:00
|
|
|
const lastView = (item as Channel)._id !== undefined ? lastViews.get((item as Channel)._id) : undefined
|
2022-05-06 17:45:59 +00:00
|
|
|
return lastView ? lastView < item.lastMessage : (item.items ?? 0) > 0
|
2022-04-20 07:56:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 08:29:46 +00:00
|
|
|
let anchor: HTMLElement
|
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-05-05 08:29:46 +00:00
|
|
|
const showMenu = (ev: MouseEvent): void => {
|
|
|
|
showPopup(Menu, { actions }, ev.target as HTMLElement)
|
|
|
|
}
|
|
|
|
|
|
|
|
const editChannel = (el: HTMLElement, n: number, item: Item): void => {
|
|
|
|
showTooltip(
|
|
|
|
undefined,
|
|
|
|
el,
|
|
|
|
undefined,
|
2022-04-20 07:56:45 +00:00
|
|
|
ChannelEditor,
|
2022-05-05 08:29:46 +00:00
|
|
|
{ value: item.value, placeholder: item.placeholder, editable },
|
|
|
|
anchor,
|
2022-04-29 05:27:17 +00:00
|
|
|
(result) => {
|
2022-05-05 08:29:46 +00:00
|
|
|
if (result.detail !== undefined) {
|
|
|
|
if (result.detail === '') displayItems = dropItem(n)
|
|
|
|
else displayItems[n].value = result.detail
|
2022-05-03 16:04:21 +00:00
|
|
|
saveItems()
|
2022-04-20 07:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2022-05-05 08:29:46 +00:00
|
|
|
const _focus = (ev: Event, n: number, item: Item): void => {
|
|
|
|
const el = ev.target as HTMLButtonElement
|
|
|
|
if (el) editChannel(el, n, item)
|
2022-04-20 07:56:45 +00:00
|
|
|
}
|
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-05-05 08:29:46 +00:00
|
|
|
bind:this={anchor}
|
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}
|
2022-05-05 08:29:46 +00:00
|
|
|
<Button
|
2022-05-10 07:31:46 +00:00
|
|
|
id={item.label}
|
2022-05-05 08:29:46 +00:00
|
|
|
bind:input={btns[i]}
|
|
|
|
icon={item.icon}
|
|
|
|
{kind}
|
|
|
|
{size}
|
|
|
|
{shape}
|
|
|
|
highlight={item.integration || item.notification}
|
|
|
|
on:mousemove={(ev) => {
|
|
|
|
_focus(ev, i, item)
|
|
|
|
}}
|
|
|
|
on:focus={(ev) => {
|
|
|
|
_focus(ev, i, item)
|
|
|
|
}}
|
|
|
|
on:click={(ev) => {
|
|
|
|
if (editable) editChannel(eventToHTMLElement(ev), i, item)
|
|
|
|
else closeTooltip()
|
|
|
|
dispatch('open', item)
|
|
|
|
}}
|
|
|
|
/>
|
2022-04-21 09:38:09 +00:00
|
|
|
{/each}
|
|
|
|
{#if actions.length > 0 && editable}
|
|
|
|
<Button
|
2022-05-10 07:31:46 +00:00
|
|
|
id={presentation.string.AddSocialLinks}
|
2022-04-21 09:38:09 +00:00
|
|
|
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>
|