Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | 92x 113x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 226x 226x 226x 226x 226x 226x 226x 222x 222x 226x 226x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 226x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 226x 226x 226x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x | import { TemplateResult, css, html } from 'lit';
import { RapidElement } from '../RapidElement';
import { property } from 'lit/decorators.js';
import { Attachment, CustomEventType } from '../interfaces';
import { Icon } from '../Icons';
import {
DEFAULT_MEDIA_ENDPOINT,
WebResponse,
getClasses,
postFormData
} from '../utils';
const verifyAccept = (type: string, accept: string): boolean => {
if (accept) {
const allowed = accept.split(',').map((x) => x.trim());
return (
allowed.includes(type) || allowed.includes(type.split('/')[0] + '/*')
);
}
return true;
};
export class MediaPicker extends RapidElement {
static get styles() {
return css`
.drop-mask {
border-radius: var(--curvature-widget);
transition: opacity ease-in-out var(--transition-speed);
}
.highlight .drop-mask {
}
.drop-mask > div {
margin: auto;
border-radius: var(--curvature-widget);
font-weight: 400;
color: rgba(0, 0, 0, 0.5);
}
.attachments {
}
.attachments-list {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 0.2em;
align-items: center;
}
.attachment-item {
padding: 0.4em;
padding-top: 1em;
}
.attachment-item.error {
background: #fff;
color: rgba(250, 0, 0, 0.75);
padding: 0.2em;
margin: 0.3em 0.5em;
border-radius: var(--curvature);
display: block;
}
.remove-item {
--icon-color: #ccc;
background: #fff;
border-radius: 99%;
transition: transform 200ms linear;
transform: scale(0);
display: block;
margin-bottom: -24px;
margin-left: 10px;
width: 1em;
height: 1em;
}
.attachment-item:hover .remove-item {
transform: scale(1);
}
.remove-item:hover {
--icon-color: #333;
cursor: pointer;
}
.attachment-name {
align-self: center;
font-size: 12px;
padding: 2px 8px;
}
#upload-input {
display: none;
}
.upload-label {
display: flex;
align-items: center;
}
.upload-icon {
color: rgb(102, 102, 102);
}
.add-attachment {
padding: 1em;
background-color: rgba(0, 0, 0, 0.05);
border-radius: var(--curvature);
color: #aaa;
margin: 0.5em;
}
.add-attachment:hover {
background-color: rgba(0, 0, 0, 0.07);
cursor: pointer;
}
`;
}
@property({ type: String, attribute: false })
endpoint = DEFAULT_MEDIA_ENDPOINT;
@property({ type: Boolean })
pendingDrop: boolean;
@property({ type: Boolean })
ignoreDrops = false;
@property({ type: String })
icon = Icon.add;
@property({ type: String })
accept = ''; //e.g. ".xls,.xlsx"
@property({ type: Number })
max = 3;
@property({ type: Array })
attachments: Attachment[] = [];
@property({ type: Boolean, attribute: false })
uploading: boolean;
public updated(changes: Map<string, any>): void {
super.updated(changes);
if (changes.has('attachments')) {
// wait one cycle to fire change for tests
setTimeout(() => {
this.dispatchEvent(new Event('change'));
}, 0);
}
if (changes.has('uploading')) {
this.dispatchEvent(
new CustomEvent(CustomEventType.Loading, {
detail: { loading: this.uploading }
})
);
}
}
private getAcceptableFiles(evt: DragEvent): File[] {
const dt = evt.dataTransfer;
if (dt) {
const files = [...dt.files];
return files.filter((file) => verifyAccept(file.type, this.accept));
}
}
private handleDragEnter(evt: DragEvent): void {
this.highlight(evt);
}
private handleDragOver(evt: DragEvent): void {
this.highlight(evt);
}
private handleDragLeave(evt: DragEvent): void {
this.unhighlight(evt);
}
private handleDrop(evt: DragEvent): void {
if (this.ignoreDrops) {
return;
}
this.unhighlight(evt);
if (this.canAcceptAttachments()) {
this.uploadFiles(this.getAcceptableFiles(evt));
}
}
public canAcceptAttachments() {
return this.attachments.length < this.max;
}
private highlight(evt: DragEvent): void {
if (this.ignoreDrops) {
return;
}
evt.preventDefault();
evt.stopPropagation();
if (this.canAcceptAttachments()) {
this.pendingDrop = true;
}
}
private unhighlight(evt: DragEvent): void {
if (this.ignoreDrops) {
return;
}
evt.preventDefault();
evt.stopPropagation();
this.pendingDrop = false;
}
private addCurrentAttachment(attachmentToAdd: any) {
this.attachments.push(attachmentToAdd);
this.requestUpdate('attachments');
}
private removeCurrentAttachment(attachmentToRemove: any) {
this.attachments = this.attachments.filter(
(currentAttachment) => currentAttachment !== attachmentToRemove
);
this.requestUpdate('attachments');
}
private handleRemoveFileClicked(evt: Event): void {
const target = evt.target as HTMLDivElement;
const currentAttachmentToRemove = this.attachments.find(
({ url }) => url === target.id
);
if (currentAttachmentToRemove) {
this.removeCurrentAttachment(currentAttachmentToRemove);
}
}
private handleUploadFileInputChanged(evt: Event): void {
const target = evt.target as HTMLInputElement;
const files = target.files;
this.uploadFiles([...files]);
}
public uploadFiles(files: File[]): void {
let filesToUpload = [];
//remove duplicate files that have already been uploaded
filesToUpload = files.filter((file) => {
// check our file type against accepts
if (this.accept) {
if (!verifyAccept(file.type, this.accept)) {
return false;
}
}
const index = this.attachments.findIndex(
(value) => value.filename === file.name && value.size === file.size
);
if (index === -1) {
return file;
}
});
filesToUpload.map((fileToUpload) => {
this.uploadFile(fileToUpload);
});
}
private uploadFile(file: File): void {
this.uploading = true;
const url = this.endpoint;
const payload = new FormData();
payload.append('file', file);
postFormData(url, payload)
.then((response: WebResponse) => {
if (this.attachments.length < this.max) {
const attachment = response.json as Attachment;
if (attachment) {
this.addCurrentAttachment(attachment);
}
}
})
.catch((error: WebResponse) => {
let uploadError = '';
if (error.status === 400) {
uploadError = error.json.file[0];
} else {
uploadError = 'Server failure';
}
console.error(uploadError);
})
.finally(() => {
this.uploading = false;
});
}
private renderUploader(): TemplateResult {
if (this.uploading) {
return html`<temba-loading units="3" size="12"></temba-loading>`;
} else {
return this.attachments.length < this.max
? html`<input
type="file"
id="upload-input"
?multiple=${this.max > 1}
accept="${this.accept}"
@change="${this.handleUploadFileInputChanged}"
/>
<label
id="upload-label"
class="actions-left upload-label"
for="upload-input"
>
<div class="add-attachment">
<temba-icon name="${this.icon}" size="1.5"></temba-icon>
</div>
</label>`
: null;
}
}
public render(): TemplateResult {
return html` <div
class=${getClasses({ container: true, highlight: this.pendingDrop })}
@dragenter="${this.handleDragEnter}"
@dragover="${this.handleDragOver}"
@dragleave="${this.handleDragLeave}"
@drop="${this.handleDrop}"
>
<div class="drop-mask">
<div class="attachments-list">
${this.attachments.map((validAttachment) => {
return html`<div class="attachment-item">
<temba-icon
class="remove-item"
@click="${this.handleRemoveFileClicked}"
id="${validAttachment.url}"
name="${Icon.delete_small}"
></temba-icon>
<temba-thumbnail
attachment="${validAttachment.content_type}:${validAttachment.url}"
></temba-thumbnail>
</div>`;
})}
${this.renderUploader()}
</div>
</div>
</div>`;
}
}
|