|
| 1 | +import { |
| 2 | + Component, |
| 3 | + Input, |
| 4 | + Output, |
| 5 | + ViewChild, |
| 6 | + EventEmitter, |
| 7 | + OnInit |
| 8 | +} from "@angular/core"; |
| 9 | +import { NG_VALUE_ACCESSOR } from "@angular/forms"; |
| 10 | + |
| 11 | +import { I18n } from "../i18n/i18n.module"; |
| 12 | +import { FileItem } from "./file-item.interface"; |
| 13 | + |
| 14 | +const noop = () => {}; |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: "ibm-file-uploader", |
| 18 | + template: ` |
| 19 | + <strong class="bx--label">{{title}}</strong> |
| 20 | + <p class="bx--label-description">{{description}}</p> |
| 21 | + <div class="bx--file"> |
| 22 | + <button |
| 23 | + ibmButton="secondary" |
| 24 | + (click)="fileInput.click()" |
| 25 | + [attr.for]="fileUploaderId"> |
| 26 | + {{buttonText}} |
| 27 | + </button> |
| 28 | + <input |
| 29 | + #fileInput |
| 30 | + type="file" |
| 31 | + class="bx--file-input" |
| 32 | + [accept]="accept" |
| 33 | + [id]="fileUploaderId" |
| 34 | + [multiple]="multiple" |
| 35 | + (change)="onFilesAdded()"/> |
| 36 | + <div class="bx--file-container"> |
| 37 | + <ibm-file *ngFor="let fileItem of files" [fileItem]="fileItem" (remove)="removeFile(fileItem)"></ibm-file> |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + `, |
| 41 | + providers: [ |
| 42 | + { |
| 43 | + provide: NG_VALUE_ACCESSOR, |
| 44 | + useExisting: FileUploader, |
| 45 | + multi: true |
| 46 | + } |
| 47 | + ] |
| 48 | +}) |
| 49 | +export class FileUploader implements OnInit { |
| 50 | + /** |
| 51 | + * Counter used to create unique ids for file-uploader components |
| 52 | + */ |
| 53 | + static fileUploaderCount = 0; |
| 54 | + /** |
| 55 | + * Accessible text for the button that opens the upload window. |
| 56 | + * |
| 57 | + * Defaults to the `FILE_UPLOADER.OPEN` value from the i18n service |
| 58 | + */ |
| 59 | + @Input() buttonText = this.i18n.get().FILE_UPLOADER.OPEN; |
| 60 | + /** |
| 61 | + * Text set to the title |
| 62 | + */ |
| 63 | + @Input() title: string; |
| 64 | + /** |
| 65 | + * Text set to the description |
| 66 | + */ |
| 67 | + @Input() description: string; |
| 68 | + /** |
| 69 | + * Specify the types of files that the input should be able to receive |
| 70 | + */ |
| 71 | + @Input() accept = []; |
| 72 | + /** |
| 73 | + * Set to `false` to tell the component to only accept a single file on upload. |
| 74 | + * |
| 75 | + * Defaults to `true`. Accepts multiple files. |
| 76 | + */ |
| 77 | + @Input() multiple = true; |
| 78 | + /** |
| 79 | + * Provides a unique id for the underlying <input> node |
| 80 | + */ |
| 81 | + @Input() fileUploaderId = `file-uploader-${FileUploader.fileUploaderCount}`; |
| 82 | + /** |
| 83 | + * Maintains a reference to the view DOM element of the underlying <input> node |
| 84 | + */ |
| 85 | + @ViewChild("fileInput") fileInput; |
| 86 | + /** |
| 87 | + * The list of files that have been submitted to be uploaded |
| 88 | + */ |
| 89 | + @Input() files: Set<FileItem>; |
| 90 | + @Output() filesChange = new EventEmitter<any>(); |
| 91 | + |
| 92 | + protected onTouchedCallback: () => void = noop; |
| 93 | + protected onChangeCallback: (_: Set<FileItem>) => void = noop; |
| 94 | + |
| 95 | + constructor(protected i18n: I18n) { |
| 96 | + FileUploader.fileUploaderCount++; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Specifies the property to be used as the return value to `ngModel` |
| 101 | + */ |
| 102 | + get value(): Set<FileItem> { |
| 103 | + return this.files; |
| 104 | + } |
| 105 | + set value(v: Set<FileItem>) { |
| 106 | + if (v !== this.files) { |
| 107 | + this.files = v; |
| 108 | + this.onChangeCallback(v); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + ngOnInit() { |
| 113 | + // overrides the undefined files value set by the user |
| 114 | + if (!this.files) { |
| 115 | + this.files = new Set(); |
| 116 | + this.filesChange.emit(this.files); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + onBlur() { |
| 121 | + this.onTouchedCallback(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Propagates the injected `value`. |
| 126 | + */ |
| 127 | + writeValue(value: Set<FileItem>) { |
| 128 | + if (value !== this.value) { |
| 129 | + this.files = value; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + onFilesAdded() { |
| 134 | + const files = this.fileInput.nativeElement.files; |
| 135 | + for (let file of files) { |
| 136 | + const fileItem: FileItem = { |
| 137 | + uploaded: false, |
| 138 | + state: "edit", |
| 139 | + file: file |
| 140 | + }; |
| 141 | + this.files.add(fileItem); |
| 142 | + this.filesChange.emit(this.files); |
| 143 | + } |
| 144 | + |
| 145 | + this.value = this.files; |
| 146 | + } |
| 147 | + |
| 148 | + removeFile(fileItem) { |
| 149 | + this.files.delete(fileItem); |
| 150 | + this.fileInput.nativeElement.value = ""; |
| 151 | + this.filesChange.emit(this.files); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Registers the injected function to control the touch use of the `FileUploader`. |
| 156 | + */ |
| 157 | + registerOnTouched(fn: any) { |
| 158 | + this.onTouchedCallback = fn; |
| 159 | + } |
| 160 | + /** |
| 161 | + * Sets a method in order to propagate changes back to the form. |
| 162 | + */ |
| 163 | + registerOnChange(fn: any) { |
| 164 | + this.onChangeCallback = fn; |
| 165 | + } |
| 166 | +} |
0 commit comments