154 lines
5.0 KiB
JavaScript
154 lines
5.0 KiB
JavaScript
import { createElement, setTooltip, showAlert, createPicture, createAudio, createVideo, createFile } from "../../ui";
|
|
|
|
export function createBox(title, functions) {
|
|
const container = createElement('div', 'comm');
|
|
const header = createElement('div', 'title-bar',
|
|
title,
|
|
createElement('div', 'title-functions', ...functions)
|
|
);
|
|
container.appendChild(header);
|
|
return container;
|
|
};
|
|
|
|
export function appendMedia(container, mimeType, url) {
|
|
switch (mimeType) {
|
|
case 'application/pdf':
|
|
container.appendChild(createFile(url, 'file-pdf'));
|
|
break;
|
|
case 'application/msword':
|
|
case 'application/vnd.ms-word':
|
|
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
|
container.appendChild(createFile(url, 'file-word'));
|
|
break;
|
|
case 'application/vnd.ms-excel':
|
|
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
|
|
container.appendChild(createFile(url, 'file-excel'));
|
|
break;
|
|
case 'application/vnd.ms-powerpoint':
|
|
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
|
|
container.appendChild(createFile(url, 'file-powerpoint'));
|
|
break;
|
|
case 'application/smil':
|
|
// TODO: ignore smil files
|
|
// container.appendChild(createFile(url, 'smile'));
|
|
break;
|
|
case 'audio/aac':
|
|
case 'audio/amr':
|
|
case 'audio/mp3':
|
|
case 'audio/mpeg':
|
|
case 'audio/x-mpeg':
|
|
case 'audio/ogg':
|
|
case 'audio/opus':
|
|
case 'audio/wav':
|
|
case 'audio/webm':
|
|
container.appendChild(createAudio(mimeType, url));
|
|
break;
|
|
case 'text/plain':
|
|
case 'text/x-vcard':
|
|
container.appendChild(createFile(url, 'id-card'));
|
|
break;
|
|
case 'video/3gpp':
|
|
case 'video/mp2t':
|
|
case 'video/mp4':
|
|
case 'video/mpeg':
|
|
case 'video/x-mpeg':
|
|
case 'video/quicktime':
|
|
case 'video/webm':
|
|
container.appendChild(createVideo(url));
|
|
break;
|
|
default:
|
|
if (/^image\//.test(mimeType)) {
|
|
container.appendChild(createPicture(url));
|
|
} else if (/^audio\//.test(mimeType)) {
|
|
container.appendChild(createFile(url, 'music'));
|
|
} else if (/^video\//.test(mimeType)) {
|
|
container.appendChild(createFile(url, 'video'));
|
|
} else {
|
|
container.appendChild(createFile(url));
|
|
}
|
|
break;
|
|
}
|
|
return container;
|
|
};
|
|
|
|
const MaxAttachmentSize = {
|
|
limit: 1_258_291,
|
|
text: '1.2MB'
|
|
};
|
|
|
|
export const fileSupported = [
|
|
'.amr',
|
|
'.ogv',
|
|
'application/msword',
|
|
'application/vnd.ms-word',
|
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'application/vnd.ms-excel',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'application/vnd.ms-powerpoint',
|
|
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
'application/pdf',
|
|
'audio/aac',
|
|
'audio/amr',
|
|
'audio/mp3',
|
|
'audio/mpeg',
|
|
'audio/x-mpeg',
|
|
'audio/ogg',
|
|
'audio/opus',
|
|
'audio/wav',
|
|
'audio/webm',
|
|
'image/bmp',
|
|
'image/gif',
|
|
'image/jpeg',
|
|
'image/jfif',
|
|
'image/png',
|
|
'image/tiff',
|
|
'image/webp',
|
|
'text/plain',
|
|
'text/vcard',
|
|
'text/x-vcard',
|
|
'video/3gpp',
|
|
'video/mp2t',
|
|
'video/mp4',
|
|
'video/mpeg',
|
|
'video/x-mpeg',
|
|
'video/quicktime',
|
|
'video/webm',
|
|
];
|
|
|
|
export function insertFile(container, file, r) {
|
|
const label = container.querySelector('.file-selector>.selector-name');
|
|
if (label != null && file != null) {
|
|
let type = file.type;
|
|
if (type == null || type.length === 0) {
|
|
type = file.name;
|
|
type = type.substring(type.lastIndexOf('.'));
|
|
}
|
|
if (fileSupported.indexOf(type) < 0) {
|
|
showAlert(r('P_WO_ERROR', 'Error'), r('P_CU_TYPENOTSUPPORTED', 'File type "{type}" is now not supported.').replace('{type}', type));
|
|
return;
|
|
}
|
|
const isImage = /^image\//.test(type);
|
|
if (!isImage && file.size > MaxAttachmentSize.limit) {
|
|
showAlert(r('P_WO_ERROR', 'Error'), r('P_WO_ATTACHMENTSIZEEXCEEDSTHEMAXIMUMTIPS', `Attachment size exceeds the maximum allowed to be sent (${MaxAttachmentSize.text})`), 'warn');
|
|
return;
|
|
}
|
|
const fn = file.name;
|
|
label.style.display = '';
|
|
label.innerText = fn;
|
|
if (isImage) {
|
|
const img = new Image();
|
|
const reader = new FileReader();
|
|
reader.onload = e => {
|
|
img.src = e.target.result;
|
|
setTooltip(label, img);
|
|
};
|
|
reader.onerror = () => setTooltip(label, fn);
|
|
reader.readAsDataURL(file);
|
|
// img.src = URL.createObjectURL(file);
|
|
// setTooltip(label, img);
|
|
} else {
|
|
setTooltip(label, fn);
|
|
}
|
|
return file;
|
|
}
|
|
}; |