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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 | 2x 2x 2x 2x 2x 2x 196x 196x 196x 196x 196x 196x 196x 196x 196x 196x 196x 196x 196x 2x 4x 2x 2x 2x 2x 4x 2x 2x 2x 2x 4x 2x 2x 2x 2x 8x 4x 4x 4x 8x 4x 4x 4x 4x 2x 2x 2x 2x 2x 10x 10x 10x 10x 10x 68x 68x 68x 2x 66x 66x 66x 66x 66x 66x 46x 66x 106x 66x 106x 106x 2x 104x 2x 102x 102x 66x 10x 66x 66x 66x 58x 30x 30x 18x 18x 18x 18x 18x 298x 298x 36x 286x 40x 40x 40x 40x 40x 6x 6x 6x 6x 78x 78x 10x 10x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 224x 224x 166x 166x 18x 18x 18x 166x 28x 628x 230x 250x 270x 270x 224x 164x 164x 60x 270x 2x 396x 396x 396x 18x 18x 18x 18x 18x 378x 378x 378x 30x 30x 2x 2x 28x 6x 22x 22x 22x 10x 4x 2x 2x 2x 6x 2x 4x 10x 10x 2x 8x 2x 6x 6x 196x 196x 196x 196x 196x 40x 196x 196x 196x 4x 196x 196x 196x 20x 196x 196x 196x 42x 12x 12x 20x 20x 34x 30x 30x 4x 390x 22x 22x 22x 22x 26x 4x 4x 4x 24x 2x 2x 2x 2x 24x 2x 2x 2x 24x 46x 24x 4x 24x 8x 8x 6x 2x 408x 212x 212x 212x 212x 212x 44x 212x 8x 212x 212x 8x 8x 2x 2x 204x 2x 76x 216x 216x 12x 216x 212x 212x 24x 24x 2x 2x 2x 2x 2x 340x 196x 196x 196x 12x 196x 196x 196x 26x 156x 156x 156x 26x 2x 2x 2x 2x 106x 16x 16x | /**
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache 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 http://www.apache.org/licenses/LICENSE-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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {BaseComponent} from '../../../coral-base-component';
import {BaseFormField} from '../../../coral-base-formfield';
import FileUploadItem from './FileUploadItem';
import base from '../templates/base';
import {transform, commons, validate} from '../../../coral-utils';
import {Decorator} from '../../../coral-decorator';
const CLASSNAME = '_coral-FileUpload';
const XHR_EVENT_NAMES = ['loadstart', 'progress', 'load', 'error', 'loadend', 'readystatechange', 'abort', 'timeout'];
/**
Enumeration for {@link FileUpload} HTTP methods that can be used to upload files.
@typedef {Object} FileUploadMethodEnum
@property {String} POST
Send a POST request. Used when creating a resource.
@property {String} PUT
Send a PUT request. Used when replacing a resource.
@property {String} PATCH
Send a PATCH request. Used when partially updating a resource.
*/
const method = {
POST: 'POST',
PUT: 'PUT',
PATCH: 'PATCH'
};
/**
@class Coral.FileUpload
@classdesc A FileUpload component that manages the upload process of multiple files. Child elements of FileUpload can
be given special attributes to enable functionality:
- <code>[coral-fileupload-select]</code>. Click to choose file(s), replacing existing files.
- <code>[coral-fileupload-dropzone]</code>. Drag and drop files to choose file(s), replacing existing files.
- <code>[coral-fileupload-clear]</code>. Click to remove all files from the queue.
- <code>[coral-fileupload-submit]</code>. Click to start uploading.
- <code>[coral-fileupload-abort]</code>. Click to abort all uploads.
- <code>[coral-fileupload-abortfile="filename.txt"]</code>. Click to abort a specific file, leaving it in the queue.
- <code>[coral-fileupload-removefile="filename.txt"]</code>. Click to remove a specific file from the queue.
- <code>[coral-fileupload-uploadfile="filename.txt"]</code>. Click to start uploading a specific file.
@htmltag coral-fileupload
@extends {HTMLElement}
@extends {BaseComponent}
@extends {BaseFormField}
*/
const FileUpload = Decorator(class extends BaseFormField(BaseComponent(HTMLElement)) {
/** @ignore */
constructor() {
super();
// Events
this._delegateEvents(commons.extend(this._events, {
// Clickable hooks
'click [coral-fileupload-submit]': '_onSubmitButtonClick',
'click [coral-fileupload-clear]': 'clear',
'click [coral-fileupload-select]': '_showFileDialog',
'click [coral-fileupload-abort]': 'abort',
'click [coral-fileupload-abortfile]': '_onAbortFileClick',
'click [coral-fileupload-removefile]': '_onRemoveFileClick',
'click [coral-fileupload-uploadfile]': '_onUploadFileClick',
// Drag & Drop zones
'dragenter [coral-fileupload-dropzone]': '_onDragAndDrop',
'dragover [coral-fileupload-dropzone]': '_onDragAndDrop',
'dragleave [handle="input"]': '_onDragAndDrop',
'drop [handle="input"]': '_onDragAndDrop',
// Accessibility
'capture:focus [coral-fileupload-select]': '_onButtonFocusIn',
'capture:focus [handle="input"]': '_onInputFocusIn',
'capture:blur [handle="input"]': '_onInputFocusOut'
}));
// Prepare templates
this._elements = {};
base.call(this._elements, {commons});
// Pre-define labellable element
this._labellableElement = this._elements.input;
// Used for items
this._uploadQueue = [];
// this should refer to the fileupload
this._doAddDragClass = this._doAddDragClass.bind(this);
this._doRemoveDragClass = this._doRemoveDragClass.bind(this);
this._positionInputOnDropZone = this._positionInputOnDropZone.bind(this);
// Reposition the input under the specified dropzone
this._observer = new MutationObserver(this._positionInputOnDropZone);
this._observer.observe(this, {
childList: true,
attributes: true,
attributeFilter: ['coral-fileupload-dropzone'],
subtree: true
});
}
/**
Name used to submit the data in a form.
@type {String}
@default ""
@htmlattribute name
@htmlattributereflected
*/
get name() {
return this._elements.input.name;
}
set name(value) {
this._reflectAttribute('name', value);
this._elements.input.name = value;
}
/**
This field's current value.
@type {String}
@default ""
@htmlattribute value
*/
get value() {
const item = this._uploadQueue ? this._getQueueItem(0) : null;
// The first selected filename, or the empty string if no files are selected.
return item ? `C:\\fakepath\\${item.file.name}` : '';
}
set value(value) {
if (value === '' || value === null) {
this._clearQueue();
this._clearFileInputValue();
} else {
// Throws exception if value is different than an empty string or null
throw new Error('Coral.FileUpload accepts a filename, which may only be programmatically set to empty string.');
}
}
/**
Whether this field is disabled or not.
@type {Boolean}
@default false
@htmlattribute disabled
@htmlattributereflected
*/
get disabled() {
return this._elements.input.disabled;
}
set disabled(value) {
this._elements.input.disabled = transform.booleanAttr(value);
this._reflectAttribute('disabled', this.disabled);
this.classList.toggle('is-disabled', this.disabled);
this._setElementState();
}
/**
Inherited from {@link BaseFormField#invalid}.
*/
get invalid() {
return super.invalid;
}
set invalid(value) {
super.invalid = value;
this._elements.input.setAttribute('aria-invalid', this.invalid);
this._setElementState();
}
/**
Whether this field is required or not.
@type {Boolean}
@default false
@htmlattribute required
@htmlattributereflected
*/
get required() {
return this._elements.input.required;
}
set required(value) {
this._elements.input.required = transform.booleanAttr(value);
this._reflectAttribute('required', this.required);
this.classList.toggle('is-required', this.required);
this._setElementState();
}
/**
Whether this field is readOnly or not. Indicating that the user cannot modify the value of the control.
@type {Boolean}
@deEfault false
@htmlattribute readonly
@htmlattributereflected
*/
get readOnly() {
return this._readOnly || false;
}
set readOnly(value) {
this._readOnly = transform.booleanAttr(value);
this._reflectAttribute('readonly', this._readOnly);
this._setElementState();
}
/**E
The names of the currently selected files.
When {@link Coral.FileUpload#multiple} is <code>false</code>, this will be an array of length 1.
@type {Array.<String>}
*/
get values() {
let values = this._uploadQueue.map((item) => `C:\\fakepath\\${item.file.name}`);
if (values.length && !this.multiple) {
values = [values[0]];
}
return values;
}
set values(values) {
ifE (Array.isArray(values)) {
if (values.length) {
this.value = values[0];
} else {
this.value = '';
}
}
}
/**
Inherited from {@link BaseFormField#labelledBy}.
*/
get labelleEdBy() {
return super.labelledBy;
}
set labelledBy(value) {
super.labelledBy = value;
// The specified labelledBy property.
const labelledBy = this.labelledBy;
// An array of element ids to label control, the last being the select button element id.
const ids = [];
const button = this.querySelector('[coral-fileupload-select]');
if (button) {
ids.push(button.id);
}
// If a labelledBy property exists,
if (labelledBy) {
// prepend the labelledBy value to the ids array
ids.unshift(labelledBy);
}
// Set aria-labelledby attribute on the labellable element joining ids array into space-delimited list of ids.
this._elements.input.setAttribute('aria-labelledby', ids.join(' '));
if (labelledBy) {
// Set label for attribute
const labelElement = document.getElementById(labelledBy);
if (labelElement && labelElement.tagName === 'LABEL') {
labelElement.setAttribute('for', this._elements.input.id);
this._labelElement = labelElement;
}
}
// Remove label for attribute
else if (this._labelElement) {
this._labelElement.removeAttribute('for');
}
}
/**
Array of additional parameters as key:value to send in addition of files.
A parameter must contain a <code>name</code> key:value and optionally a <code>value</code> key:value.
E
@type {Array.<Object>}
@default []
*/
get parameters() {
return this._parameters || [];
}
set parameters(values) {
// Verify that every item has a name
const isValid = Array.isArray(values) && values.every((el) => el && el.name);
if (isValid) {
this._parameters = values;
if (!this.async) {
Array.prototype.forEach.call(this.querySelectorAll('input[type="hidden"]'), (input) => {
input.parentNode.removeChild(input);
});
// Add extra parameters
this.parameters.forEach((param) => {
const input = document.createElement('input');
input.type = 'hidden';
input.name = param.name;
input.value = param.value;
this.appendChild(input);
});
}
}
}
/**
Whether files should be uploaded asynchronously via XHR or synchronously e.g. within a
<code><form></code> tag. One option excludes the other. Setting a new <code>async</code> value removes all
files from the queue.
@type {Boolean}
@default false
@htmlattribute async
@htmlattributereflected
*/
get async() {
reEturn this._async || false;
}
set async(value) {
this._async = transform.booleanAttr(value);
this._reflectAttribute('async', this._async);
// Sync extra parameters in case of form submission
if (!this._async) {
this.parameters = this.parameters;
}
// Clear file selection
if (this._uploadQueue) {
this._clearQueue();
this._clearFileInputValue();
}
}
/**
The URL where the upload request should be sent. When used within a <code><form></code> tag to upload
synchronously, the action of the form is used. If an element is clicked that has a
<code>[coral-fileupload-submit]</code> attribute as well as a <code>[formaction]</code> attribute, the action of
the clicked element will be used. Set this property before calling {@link Coral.FileUpload#upload} to reset the
action set by a click.
@type {String}
@default ""
@htmlattribute action
@htmlEattributereflected
*/
get action() {
return this._action || '';
}
set action(value) {
this._action = transform.string(value);
this._reflectAttribute('action', this._action);
// Reset button action as action was set explicitly
this._buttonAction = null;
}
/**
The HTTP method to use when uploading files asynchronously. When used within a <code><form></code> tag to
upload synchronously, the method of the form is used. If an element is clicked that has a
<code>[coral-fileupload-submit]</code> attribute as well as a <code>[formmethod]</code> attribute, the method of
the clicked element will be used. Set this property before calling {@link FileUpload#upload} to reset the
method set by a click.
See {@link FileUploadMethodEnum}.
@type {String}
@default FileUploadMethodEnum.POST
@htmlattribute method
@htmlattributereflected
*/
get Emethod() {
return this._method || method.POST;
}
set method(value) {
value = transform.string(value).toUpperCase();
this._method = validate.enumeration(method)(value) && value || method.POST;
this._reflectAttribute('method', this._method);
// Reset button method as method was set explcitly
this._buttonMethod = null;
}
/**
Whether more than one file can be chosen at the same time to upload.
@type {Boolean}
@default false
@htmlattribute multiple
@htmlattributereflected
*/
get multiple() {
return this._elements.input.multiple;
}
set multiple(value) {
this._elements.input.multiple = transform.booleanAttr(value);
this._reflectAttribute('multiple', this.multiple);
}
/**
File size limit in bytes for one file. The value of 0 indicates unlimited, which is also the default.
@type {Number}
@htmlattribute sizelimit
@htmlattributereflected
@default 0
*/
get sizeLimit() {
return this._sizeLimit || 0;
}
set sizeLimit(value) {
this._sizeLimit = transform.number(value);
this._reflectAttribute('sizelimit', this._sizeLimit);
}
/**
MIME types allowed for uploading (proper MIME types, wildcard '*' and file extensions are supported). To specify
more than one value, separate the values with a comma (e.g.
<code><input accept="audio/*,video/*,image/*" /></code>.
@type {String}
@default ""
@htmlattribute accept
@htmlattributereflected
*/
get accept() {
return this._elements.input.accept;
}
E
set accept(value) {
this._elements.input.accept = value;
this._reflectAttribute('accept', this.accept);
}
/**
Whether the upload should start immediately after file selection.
@type {Boolean}
@default false
@htmlattribute autostart
@htmlattributereflected
*/
get autoStart() {
return this._autoStart || false;
}
set autoStart(value) {
this._autoStart = transform.booleanAttr(value);
this._reflectAttribute('autostart', this._autoStart);
}
/**
FilEes to be uploaded.
@readonly
@default []
@type {Array.<Object>}
*/
get uploadQueue() {
return this._uploadQueue;
}
/** @private */
_onButtonFocusIn(event) {
// Get the input
const input = this._elements.input;
// Get the button
const button = event.matchedTarget;
// Move the input to after the button
// This lets the next focused item be the correct one according to tab order
button.parentNode.insertBefore(input, button.nextElementSibling);
if (event.relatedTarget !== input) {
// Make sure the input gets focused on FF
window.setTimeout(() => {
input.focus();
}, 100);
}
}
/** @private */
_onInputFocusIn() {
// Get the input
const input = event.matchedTarget;
const button = this.querySelector('[coral-fileupload-select]');
if (button) {
// Remove from the tab order so shift+tab works
button.tabIndex = -1;
// So shifting focus backwards with screen reader doesn't create a focus trap
button.setAttribute('aria-hidden', true);
// Mark the button as focused
button.classList.add('is-focused');
window.requestAnimationFrame(() => {
if (input.classList.contains('focus-ring')) {
button.classList.add('focus-ring');
}
});
}
}
/** @private */
_onInputFocusOut() {
// Unmark all the focused buttons
const button = this.querySelector('[coral-fileupload-select].is-focused');
if (button) {
button.classList.remove('is-focused');
button.classList.remove('focus-ring');
// Wait a frame so that shifting focus backwards with screen reader doesn't create a focus trap
window.requestAnimationFrame(() => {
button.tabIndex = 0;
// @a11y: aria-hidden is removed to prevent focus trap when navigating backwards using a screen reader's
// virtual cursor
button.removeAttribute('aria-hidden');
});
}
}
/** @private */
_onAbortFileClick(event) {
if (!this.async) {
throw new Error('Coral.FileUpload does not support aborting file(s) upload on synchronous mode.');
}
// Get file to abort
const fileName = event.target.getAttribute('coral-fileupload-abortfile');
if (fileName) {
this._abortFile(fileName);
}
}
/** @private */
_onRemoveFileClick(event) {
if (!this.async) {
throw new Error('Coral.FileUpload does not support removing a file from the queue on synchronous mode.');
} else {
// Get file to remove
const fileName = event.target.getAttribute('coral-fileupload-removefile');
if (fileName) {
this._clearFile(fileName);
}
}
}
/** @private */
_onUploadFileClick(event) {
if (!this.async) {
throw new Error('Coral.FileUpload does not support uploading a file from the queue on synchronous mode.');
}
// Get file to upload
const fileName = event.target.getAttribute('coral-fileupload-uploadfile');
if (fileName) {
this.upload(fileName);
}
}
/** @private */
_onDragAndDrop(event) {
// Set dragging classes
if (event.type === 'dragenter' || event.type === 'dragover') {
this._addDragClass();
} else if (event.type === 'dragleave' || event.type === 'drop') {
this._removeDragClass();
}
this.trigger(`coral-fileupload:${event.type}`);
}
/** @private */
_addDragClass() {
window.clearTimeout(this._removeClassTimeout);
this._removeClassTimeout = window.setTimeout(this._doAddDragClass, 10);
}
/** @private */
_doAddDragClass() {
this.classList.add('is-dragging');
const dropZone = this.querySelector('[coral-fileupload-dropzone]');
if (dropZone) {
dropZone.classList.add('is-dragging');
}
// Put the input on top to enable file drop
this._elements.input.classList.remove('is-unselectable');
}
/** @private */
_removeDragClass() {
window.clearTimeout(this._removeClassTimeout);
this._removeClassTimeout = window.setTimeout(this._doRemoveDragClass, 10);
}
/** @private */
_doRemoveDragClass() {
this.classList.remove('is-dragging');
const dropZone = this.querySelector('[coral-fileupload-dropzone]');
if (dropZone) {
dropZone.classList.remove('is-dragging');
}
// Disable user interaction with the input
this._elements.input.classList.add('is-unselectable');
}
/**
Handles clicks to submit buttons
@private
*/E
_onSubmitButtonClick(event) {
const target = event.matchedTarget;
// Override or reset the action/method given the button's configuration
this._buttonAction = target.getAttribute('formaction');
// Make sure the method provided by the button is valid
const buttonMethod = transform.string(target.getAttribute('formmethod')).toUpperCase();
this._buttonMethod = validate.enumeration(method)(buttonMethod) && buttonMethod || null;
// Start the file upload
this.upload();
}
/**
Handles changes to the input element.
@private
*/
_onInputChange(event) {
// Stop the current event
event.stopPropagation();
if (this.disabled) {
return;
}
let files = [];
const items = [];
// Retrieve files for select event
if (event.target.files && event.target.files.length) {
this._clearQueue();
files = event.target.files;
// Verify if multiple file upload is allowed
if (!this.multiple) {
files = [files[0]];
}
}
// Retrieve files for drop event
else if (event.dataTransfer && event.dataTransfer.files && event.dataTransfer.files.length) {
this._clearQueue();
files = event.dataTransfer.files;
// Verify if multiple file upload is allowed
if (!this.multiple) {
files = [files[0]];
}
} else {
return;
}
// Initialize items
for (let i = 0 ; i < files.length ; i++) {
items.push(new FileUploadItem(files[i]));
}
// Verify if file is allowed to be uploaded and trigger events accordingly
items.forEach((item) => {
// If file is not found in uploadQueue using filename
if (!this._getQueueItemByFilename(item.file.name)) {
// Check file size
if (this.sizeLimit && item.file.size > this.sizeLimit) {
this.trigger('coral-fileupload:filesizeexceeded', {item});
}
// Check mime type
else if (this.accept && !item._isMimeTypeAllowed(this.accept)) {
this.trigger('coral-fileupload:filemimetyperejected', {item});
} else {
// Add item to queue
this._uploadQueue.push(item);
this.trigger('coral-fileupload:fileadded', {item});
}
}
});
if (this.autoStart) {
this.upload();
}
// Explicitly re-emit the change event
if (this._triggerChangeEvent) {
this.trigger('change');
}
// Clear file input once files are added to the queue to make sure next file selection will trigger a change event
if (this.async) {
this._clearFileInputValue();
}
}
/**
Sets the disabled/readonly state of elements with the associated special attributes
@private
*/
_setElementState() {
Array.prototype.forEach.call(this.querySelectorAll(
'[coral-fileupload-select],' +
'[coral-fileupload-dropzone],' +
'[coral-fileupload-submit],' +
'[coral-fileupload-clear],' +
'[coral-fileupload-abort],' +
'[coral-fileupload-abortfile],' +
'[coral-fileupload-removefile],' +
'[coral-fileupload-uploadfile]'
), (item) => {
item.classList.toggle('is-invalid', this.invalid);
item.classList.toggle('is-disabled', this.disabled);
item.classList.toggle('is-required', this.required);
item.classList.toggle('is-readOnly', this.readOnly);
item[this.disabled || this.readOnly ? 'setAttribute' : 'removeAttribute']('disabled', '');
});
}
/** @private */
_clearQueue() {
this._uploadQueue.slice().forEach((item) => {
this._clearFile(item.file.name);
});
}
/**
Clear file selection on the file input
@private
*/
_clearFileInputValue() {
this._elements.input.value = '';
}
/**
Remove a file from the upload queue.
@param {String} filename
The filename of the file to remove.
@private
*/
_clearFile(filename) {
const item = this._getQueueItemByFilename(filename);
if (item) {
// Abort file upload
this._abortFile(filename);
// Remove file from queue
this._uploadQueue.splice(this._getQueueIndex(filename), 1);
this.trigger('coral-fileupload:fileremoved', {item});
}
}
/**
Uploads a file in the queue. If an array is provided as the first argument, it is used as the parameters.
@param filename {String}
The name of the file to upload.
@private
*/
_uploadFile(filename) {
const item = this._getQueueItemByFilename(filename);
if (item) {
this._abortFile(filename);
this._ajaxUpload(item);
}
}
/** @private */
_showFileDialog() {
// Show the dialog
// This ONLY works when the call stack traces back to another click event!
this._elements.input.click();
}
/**
Abort specific file upload.
@param {String} filename
The filename identifies the file to abort.
@private
*/
_abortFile(filename) {
const item = this._getQueueItemByFilename(filename);
if (item && item._xhr) {
item._xhr.abort();
item._xhr = null;
}
}
/**
Handles the ajax upload.
@private
*/
_ajaxUpload(item) {
// Use the action/method provided by the last button click, if provided
const action = this._buttonAction || this.action;
const requestMethod = this._buttonMethod ? this._buttonMethod.toUpperCase() : this.method;
// We merge the global parameters with the specific file parameters and send them all together
const parameters = this.parameters.concat(item.parameters);
const formData = new FormData();
parameters.forEach((additionalParameter) => {
formData.append(additionalParameter.name, additionalParameter.value);
});
formData.append('_charset_', 'utf-8');
formData.append(this.name, item._originalFile);
// Store the XHR on the item itself
item._xhr = new XMLHttpRequest();
// Opening before being able to set response type to avoid IE11 InvalidStateError
item._xhr.open(requestMethod, action);
// Reflect specific xhr properties
item._xhr.timeout = item.timeout;
item._xhr.responseType = item.responseType;
item._xhr.withCredentials = item.withCredentials;
XHR_EVENT_NAMES.forEach((name) => {
// Progress event is the only event among other ProgressEvents that can trigger multiple times.
// Hence it's the only one that gives away usable progress information.
const isProgressEvent = name === 'progress';
(isProgressEvent ? item._xhr.upload : item._xhr).addEventListener(name, (event) => {
const detail = {
item: item,
action: action,
method: requestMethod
};
if (isProgressEvent) {
detail.lengthComputable = event.lengthComputable;
detail.loaded = event.loaded;
detail.total = event.total;
}
this.trigger(`coral-fileupload:${name}`, detail);
});
});
item._xhr.send(formData);
}
/** @private */
_getLabellableElement() {
return this;
}
/** @private */
_getQueueItemByFilename(filename) {
return this._getQueueItem(this._getQueueIndex(filename));
}
E
/** @private */
_getQueueItem(index) {
return index > -1 ? this._uploadQueue[index] : null;
}
/** @private */
_getQueueIndex(filename) {
let index = -1;
this._uploadQueue.some((item, i) => {
if (item.file.name === filename) {
index = i;
return true;
}
return false;
});
return index;
}
/** @private */
_getTargetChangeInput() {
return this._elements.input;
}
/** @ignore */
_positionInputOnDropZone() {
const input = this._elements.input;
const dropZone = this.querySelector('[coral-fileupload-dropzone]');
if (dropZone) {
const size = dropZone.getBoundingClientRect();
input.style.top = `${parseInt(dropZone.offsetTop, 10)}px`;
input.style.left = `${parseInt(dropZone.offsetLeft, 10)}px`;
input.style.width = `${parseInt(size.width, 10)}px`;
input.style.height = `${parseInt(size.height, 10)}px`;
} else {
input.style.width = '0px';
input.style.height = '0px';
input.style.visibility = 'hidden';
}
}
/**
Uploads the given filename, or all the files into the queue. It accepts extra parameters that are sent with the
file.
@param {String} [filename]
The name of the file to upload.
*/
upload(filename) {
if (!this.async) {
if (typeof filename === 'string') {
throw new Error('Coral.FileUpload does not support uploading a file from the queue on synchronous mode.');
}
let form = this.closest('form');
if (!form) {
form = document.createElement('form');
form.method = this.method.toLowerCase();
form.enctype = 'multipart/form-data';
form.action = this.action;
form.hidden = true;
form.appendChild(this._elements.input);
Array.prototype.forEach.call(this.querySelectorAll('input[type="hidden"]'), (hiddenInput) => {
form.appendChild(hiddenInput);
});
// Make sure the form is connected before submission
this.appendChild(form);
}
const input = document.createElement('input');
input.type = 'hidden';
input.name = '_charset_';
input.value = 'utf-8';
form.submit();
} else if (typeof filename === 'string') {
this._uploadFile(filename);
} else {
this._uploadQueue.forEach((item) => {
this._abortFile(item.file.name);
this._ajaxUpload(item);
});
}
}
/**
Remove a file or all files from the upload queue.
@param {String} [filename]
The filename of the file to remove. If a filename is not provided, all files will be removed.
*/
clear(filename) {
if (!this.async) {
if (typeof filename === 'string') {
throw new Error('Coral.FileUpload does not support removing a file from the queue on synchronous mode.');
}
this._clearQueue();
this._clearFileInputValue();
} else if (typeof filename === 'string') {
this._clearFile(filename);
} else {
this._clearQueue();
}
}
/**
Abort upload of a given file or all files in the queue.
@param {String} [filename]
The filename of the file to abort. If a filename is not provided, all files will be aborted.
*/
aborEt(filename) {
if (!this.async) {
throw new Error('Coral.FileUpload does not support aborting file(s) upload on synchronous mode.');
}
if (typeof filename === 'string') {
// Abort a single file
this._abortFile(filename);
} else {
// Abort all files
this._uploadQueue.forEach((item) => {
this._abortFile(item.file.name);
});
}
}
static get _attributePropertyMap() {
return commons.extend(super._attributePropertyMap, {
sizelimit: 'sizeLimit',
autostart: 'autoStart'
});
}
/** @ignore */
static get observedAttributes() {
return super.observedAttributes.concat([
'async',
'action',
'method',
'multiple',
'sizelimit',
'accept',
'autostart'
]);
}
/** @ignore */
render() {
super.render();
this.classList.add(CLASSNAME);
const button = this.querySelector('[coral-fileupload-select]');
if (button) {
button.id = button.id || commons.getUID();
}
// If no labelledby is specified, ensure input is at labelledby the select button
this.labelledBy = this.labelledBy;
// Fetch additional parameters if any
const parameters = [];
Array.prototype.forEach.call(this.querySelectorAll('input[type="hidden"]'), (input) => {
parameters.push({
name: input.name,
value: input.value
});
});
this.parameters = parameters;
// Remove the input if it's already there
// A fresh input is preferred to value = '' as it may not work in all browsers
const inputElement = this.querySelector('[handle="input"]');
if (inputElement) {
inputElement.parentNode.removeChild(inputElement);
}
// Add the input to the component
this.appendChild(this._elements.input);
// IE11 requires one more frame or the resize listener <object> will appear as an overlaying white box
window.requestAnimationFrame(() => {
// Handles the repositioning of the input to allow dropping files
commons.addResizeListener(this, this._positionInputOnDropZone);
});
}
});
export default FileUpload;
|