2021-11-30 15:39:08 +00:00
|
|
|
<!--
|
2021-12-17 09:04:49 +00:00
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
2021-11-30 15:39:08 +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-17 09:04:49 +00:00
|
|
|
//
|
2021-11-30 15:39:08 +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-17 09:04:49 +00:00
|
|
|
//
|
2021-11-30 15:39:08 +00:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
2022-01-11 09:07:29 +00:00
|
|
|
import { Class, Doc, Ref, Space } from '@anticrm/core'
|
2021-12-17 09:04:49 +00:00
|
|
|
import { setPlatformStatus, unknownError } from '@anticrm/platform'
|
2022-01-11 09:07:29 +00:00
|
|
|
import { getClient } from '@anticrm/presentation'
|
2021-11-30 15:39:08 +00:00
|
|
|
import { CircleButton, IconAdd, Label, Spinner } from '@anticrm/ui'
|
|
|
|
import { Table } from '@anticrm/view-resources'
|
2022-01-11 09:07:29 +00:00
|
|
|
import attachment from '../plugin'
|
2021-11-30 15:39:08 +00:00
|
|
|
import { uploadFile } from '../utils'
|
|
|
|
import UploadDuo from './icons/UploadDuo.svelte'
|
|
|
|
|
|
|
|
export let objectId: Ref<Doc>
|
|
|
|
export let space: Ref<Space>
|
|
|
|
export let _class: Ref<Class<Doc>>
|
2022-01-11 09:07:29 +00:00
|
|
|
|
2022-01-12 09:18:50 +00:00
|
|
|
export let attachments: number | undefined = undefined
|
2021-11-30 15:39:08 +00:00
|
|
|
|
|
|
|
let inputFile: HTMLInputElement
|
|
|
|
let loading = 0
|
|
|
|
|
|
|
|
const client = getClient()
|
|
|
|
|
|
|
|
async function createAttachment (file: File) {
|
|
|
|
loading++
|
|
|
|
try {
|
2022-01-20 09:27:41 +00:00
|
|
|
const uuid = await uploadFile(file, { space, attachedTo: objectId })
|
2021-11-30 15:39:08 +00:00
|
|
|
console.log('uploaded file uuid', uuid)
|
2021-12-06 10:07:08 +00:00
|
|
|
client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', {
|
2021-11-30 15:39:08 +00:00
|
|
|
name: file.name,
|
|
|
|
file: uuid,
|
|
|
|
type: file.type,
|
|
|
|
size: file.size,
|
|
|
|
lastModified: file.lastModified
|
|
|
|
})
|
|
|
|
} catch (err: any) {
|
|
|
|
setPlatformStatus(unknownError(err))
|
|
|
|
} finally {
|
|
|
|
loading--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileSelected () {
|
|
|
|
const list = inputFile.files
|
|
|
|
if (list === null || list.length === 0) return
|
|
|
|
for (let index = 0; index < list.length; index++) {
|
|
|
|
const file = list.item(index)
|
|
|
|
if (file !== null) createAttachment(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileDrop (e: DragEvent) {
|
|
|
|
const list = e.dataTransfer?.files
|
|
|
|
if (list === undefined || list.length === 0) return
|
|
|
|
for (let index = 0; index < list.length; index++) {
|
|
|
|
const file = list.item(index)
|
|
|
|
if (file !== null) createAttachment(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dragover = false
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="attachments-container">
|
|
|
|
<div class="flex-row-center">
|
2022-02-23 16:10:11 +00:00
|
|
|
<span class="title"><Label label={attachment.string.Attachments} /></span>
|
2021-11-30 15:39:08 +00:00
|
|
|
{#if loading}
|
2021-12-02 13:12:16 +00:00
|
|
|
<Spinner />
|
2021-11-30 15:39:08 +00:00
|
|
|
{:else}
|
2021-12-02 13:12:16 +00:00
|
|
|
<CircleButton
|
|
|
|
icon={IconAdd}
|
|
|
|
size={'small'}
|
2021-12-08 09:24:24 +00:00
|
|
|
selected
|
2021-12-02 13:12:16 +00:00
|
|
|
on:click={() => {
|
|
|
|
inputFile.click()
|
|
|
|
}}
|
|
|
|
/>
|
2021-11-30 15:39:08 +00:00
|
|
|
{/if}
|
2021-12-02 13:12:16 +00:00
|
|
|
<input
|
|
|
|
bind:this={inputFile}
|
|
|
|
multiple
|
|
|
|
type="file"
|
|
|
|
name="file"
|
|
|
|
id="file"
|
|
|
|
style="display: none"
|
|
|
|
on:change={fileSelected}
|
|
|
|
/>
|
2021-11-30 15:39:08 +00:00
|
|
|
</div>
|
|
|
|
|
2022-01-11 09:07:29 +00:00
|
|
|
{#if (attachments === 0) && !loading}
|
2021-12-02 13:12:16 +00:00
|
|
|
<div
|
|
|
|
class="flex-col-center mt-5 zone-container"
|
|
|
|
class:solid={dragover}
|
|
|
|
on:dragover|preventDefault={() => {
|
|
|
|
dragover = true
|
|
|
|
}}
|
|
|
|
on:dragleave={() => {
|
|
|
|
dragover = false
|
|
|
|
}}
|
2021-11-30 15:39:08 +00:00
|
|
|
on:drop|preventDefault|stopPropagation={fileDrop}
|
|
|
|
>
|
|
|
|
<UploadDuo size={'large'} />
|
2022-02-01 09:13:01 +00:00
|
|
|
<div class="text-sm content-dark-color mt-2">
|
2021-12-17 09:04:49 +00:00
|
|
|
<Label label={attachment.string.NoAttachments} />
|
2021-11-30 15:39:08 +00:00
|
|
|
</div>
|
2022-02-01 09:13:01 +00:00
|
|
|
<div class="text-sm">
|
2022-02-08 09:03:27 +00:00
|
|
|
<div class='over-underline' on:click={() => inputFile.click()}><Label label={attachment.string.UploadDropFilesHere} /></div>
|
2021-11-30 15:39:08 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{:else}
|
2021-12-02 13:12:16 +00:00
|
|
|
<Table
|
2021-12-06 10:07:08 +00:00
|
|
|
_class={attachment.class.Attachment}
|
2021-11-30 15:39:08 +00:00
|
|
|
config={['', 'lastModified']}
|
2021-12-02 13:12:16 +00:00
|
|
|
options={{}}
|
|
|
|
query={{ attachedTo: objectId }}
|
2022-01-24 11:25:10 +00:00
|
|
|
loadingProps={ { length: attachments ?? 0 } }
|
2021-11-30 15:39:08 +00:00
|
|
|
/>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.attachments-container {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
|
|
.title {
|
2021-12-15 09:15:43 +00:00
|
|
|
margin-right: 0.75rem;
|
2021-11-30 15:39:08 +00:00
|
|
|
font-weight: 500;
|
|
|
|
font-size: 1.25rem;
|
|
|
|
color: var(--theme-caption-color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.zone-container {
|
|
|
|
padding: 1rem;
|
|
|
|
color: var(--theme-caption-color);
|
2021-12-08 09:24:24 +00:00
|
|
|
background: var(--theme-bg-accent-color);
|
|
|
|
border: 1px dashed var(--theme-zone-border-lite);
|
2021-12-15 09:15:43 +00:00
|
|
|
border-radius: 0.75rem;
|
2022-03-07 09:12:17 +00:00
|
|
|
&.solid { border-style: solid; }
|
2021-11-30 15:39:08 +00:00
|
|
|
}
|
2021-12-02 13:12:16 +00:00
|
|
|
</style>
|