UNPKG

32.4 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { EventEmitter, Directive, Optional, Host, Input, Output, HostBinding, HostListener, forwardRef, ElementRef, Component, ChangeDetectionStrategy, ViewChild, ContentChild, Injectable, NgModule } from '@angular/core';
3import * as i2 from '@angular/common';
4import { CommonModule } from '@angular/common';
5import * as i1 from '@angular/forms';
6import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
7import * as i5 from '@angular/cdk/portal';
8import { TemplatePortalDirective, PortalModule } from '@angular/cdk/portal';
9import * as i3 from '@angular/material/icon';
10import { MatIconModule } from '@angular/material/icon';
11import * as i4 from '@angular/material/button';
12import { MatButtonModule } from '@angular/material/button';
13import { coerceBooleanProperty } from '@angular/cdk/coercion';
14import { ENTER } from '@angular/cdk/keycodes';
15import { Subject, merge, fromEvent } from 'rxjs';
16import { filter, takeUntil, tap } from 'rxjs/operators';
17import { mixinControlValueAccessor, mixinDisabled } from '@covalent/core/common';
18import * as i1$1 from '@angular/common/http';
19import { HttpRequest, HttpHeaders, HttpParams, HttpEventType } from '@angular/common/http';
20
21class TdFileSelectDirective {
22 model;
23 _multiple = false;
24 /**
25 * multiple?: boolean
26 * Sets whether multiple files can be selected at once in host element, or just a single file.
27 * Can also be 'multiple' native attribute.
28 */
29 set multiple(multiple) {
30 this._multiple = coerceBooleanProperty(multiple);
31 }
32 /**
33 * fileSelect?: function
34 * Event emitted when a file or files are selected in host [HTMLInputElement].
35 * Emits a [FileList | File] object.
36 * Alternative to not use [(ngModel)].
37 */
38 fileSelect = new EventEmitter();
39 /**
40 * Binds native 'multiple' attribute if [multiple] property is 'true'.
41 */
42 get multipleBinding() {
43 return this._multiple ? '' : undefined;
44 }
45 constructor(model) {
46 this.model = model;
47 }
48 /**
49 * Listens to 'change' host event to get [HTMLInputElement] files.
50 * Emits the 'fileSelect' event with a [FileList] or [File] depending if 'multiple' attr exists in host.
51 * Uses [(ngModel)] if declared, instead of emitting 'fileSelect' event.
52 */
53 onChange(event) {
54 if (event.target instanceof HTMLInputElement) {
55 const fileInputEl = event.target;
56 const files = fileInputEl.files || new FileList();
57 if (files.length) {
58 const value = this._multiple
59 ? files.length > 1
60 ? files
61 : files[0]
62 : files[0];
63 this.model
64 ? this.model.update.emit(value)
65 : this.fileSelect.emit(value);
66 }
67 }
68 }
69 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileSelectDirective, deps: [{ token: i1.NgModel, host: true, optional: true }], target: i0.ɵɵFactoryTarget.Directive });
70 static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.1.2", type: TdFileSelectDirective, selector: "[tdFileSelect]", inputs: { multiple: "multiple" }, outputs: { fileSelect: "fileSelect" }, host: { listeners: { "change": "onChange($event)" }, properties: { "attr.multiple": "this.multipleBinding" } }, ngImport: i0 });
71}
72i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileSelectDirective, decorators: [{
73 type: Directive,
74 args: [{
75 selector: '[tdFileSelect]',
76 }]
77 }], ctorParameters: () => [{ type: i1.NgModel, decorators: [{
78 type: Optional
79 }, {
80 type: Host
81 }] }], propDecorators: { multiple: [{
82 type: Input
83 }], fileSelect: [{
84 type: Output
85 }], multipleBinding: [{
86 type: HostBinding,
87 args: ['attr.multiple']
88 }], onChange: [{
89 type: HostListener,
90 args: ['change', ['$event']]
91 }] } });
92
93class TdFileDropBase {
94}
95class TdFileDropDirective {
96 _renderer;
97 _element;
98 _ngZone;
99 _multiple = false;
100 _dragenterListener;
101 _dragleaveListener;
102 _dragoverListener;
103 /**
104 * multiple?: boolean
105 * Sets whether multiple files can be dropped at once in host element, or just a single file.
106 * Can also be 'multiple' native attribute.
107 */
108 set multiple(multiple) {
109 this._multiple = coerceBooleanProperty(multiple);
110 }
111 disabled = false;
112 /**
113 * fileDrop?: function
114 * Event emitted when a file or files are dropped in host element after being validated.
115 * Emits a [FileList | File] object.
116 */
117 fileDrop = new EventEmitter();
118 /**
119 * Binds native 'multiple' attribute if [multiple] property is 'true'.
120 */
121 get multipleBinding() {
122 return this._multiple ? '' : undefined;
123 }
124 /**
125 * Binds native 'disabled' attribute if [disabled] property is 'true'.
126 */
127 get disabledBinding() {
128 return this.disabled ? '' : undefined;
129 }
130 constructor(_renderer, _element, _ngZone) {
131 this._renderer = _renderer;
132 this._element = _element;
133 this._ngZone = _ngZone;
134 }
135 ngOnInit() {
136 this._ngZone.runOutsideAngular(() => {
137 // Listens to 'dragenter' host event to add animation class 'drop-zone' which can be overriden in host.
138 // Stops event propagation and default action from browser for 'dragenter' event.
139 this._dragenterListener = this._renderer.listen(this._element.nativeElement, 'dragenter', (event) => {
140 if (!this.disabled) {
141 this._renderer.addClass(this._element.nativeElement, 'drop-zone');
142 }
143 this._stopEvent(event);
144 });
145 // Listens to 'dragleave' host event to remove animation class 'drop-zone'.
146 // Stops event propagation and default action from browser for 'dragleave' event.
147 this._dragleaveListener = this._renderer.listen(this._element.nativeElement, 'dragleave', (event) => {
148 this._renderer.removeClass(this._element.nativeElement, 'drop-zone');
149 this._stopEvent(event);
150 });
151 // Listens to 'dragover' host event to validate transfer items.
152 // Checks if 'multiple' attr exists in host to allow multiple file drops.
153 // Stops event propagation and default action from browser for 'dragover' event.
154 this._dragoverListener = this._renderer.listen(this._element.nativeElement, 'dragover', (event) => {
155 const transfer = event.dataTransfer || new DataTransfer();
156 transfer.dropEffect = this._typeCheck(transfer.types);
157 if (this.disabled ||
158 (!this._multiple &&
159 ((transfer.items && transfer.items.length > 1) ||
160 transfer.mozItemCount > 1))) {
161 transfer.dropEffect = 'none';
162 }
163 else {
164 transfer.dropEffect = 'copy';
165 }
166 this._stopEvent(event);
167 });
168 });
169 }
170 ngOnDestroy() {
171 this._dragenterListener?.();
172 this._dragleaveListener?.();
173 this._dragoverListener?.();
174 }
175 /**
176 * Listens to 'drop' host event to get validated transfer items.
177 * Emits the 'fileDrop' event with a [FileList] or [File] depending if 'multiple' attr exists in host.
178 * Stops event propagation and default action from browser for 'drop' event.
179 */
180 onDrop(event) {
181 if (!this.disabled) {
182 const transfer = event.dataTransfer ?? new DataTransfer();
183 const files = transfer.files;
184 if (files.length) {
185 const value = this._multiple
186 ? files.length > 1
187 ? files
188 : files[0]
189 : files[0];
190 this.fileDrop.emit(value);
191 }
192 }
193 this._renderer.removeClass(this._element.nativeElement, 'drop-zone');
194 this._stopEvent(event);
195 }
196 /**
197 * Validates if the transfer item types are 'Files'.
198 */
199 _typeCheck(types) {
200 let dropEffect = 'none';
201 if (types &&
202 ((types.contains && types.contains('Files')) ||
203 (types.indexOf && types.indexOf('Files') !== -1))) {
204 dropEffect = 'copy';
205 }
206 return dropEffect;
207 }
208 _stopEvent(event) {
209 event.preventDefault();
210 event.stopPropagation();
211 }
212 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileDropDirective, deps: [{ token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Directive });
213 static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.1.2", type: TdFileDropDirective, selector: "[tdFileDrop]", inputs: { multiple: "multiple", disabled: "disabled" }, outputs: { fileDrop: "fileDrop" }, host: { listeners: { "drop": "onDrop($event)" }, properties: { "attr.multiple": "this.multipleBinding", "attr.disabled": "this.disabledBinding" } }, ngImport: i0 });
214}
215i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileDropDirective, decorators: [{
216 type: Directive,
217 args: [{ selector: '[tdFileDrop]' }]
218 }], ctorParameters: () => [{ type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i0.NgZone }], propDecorators: { multiple: [{
219 type: Input
220 }], disabled: [{
221 type: Input
222 }], fileDrop: [{
223 type: Output
224 }], multipleBinding: [{
225 type: HostBinding,
226 args: ['attr.multiple']
227 }], disabledBinding: [{
228 type: HostBinding,
229 args: ['attr.disabled']
230 }], onDrop: [{
231 type: HostListener,
232 args: ['drop', ['$event']]
233 }] } });
234
235class TdFileInputLabelDirective extends TemplatePortalDirective {
236 constructor(templateRef, viewContainerRef) {
237 super(templateRef, viewContainerRef);
238 }
239 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileInputLabelDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive });
240 static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.1.2", type: TdFileInputLabelDirective, selector: "[tdFileInputLabel]ng-template", usesInheritance: true, ngImport: i0 });
241}
242i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileInputLabelDirective, decorators: [{
243 type: Directive,
244 args: [{
245 selector: '[tdFileInputLabel]ng-template',
246 }]
247 }], ctorParameters: () => [{ type: i0.TemplateRef }, { type: i0.ViewContainerRef }] });
248class TdFileInputBase {
249 _changeDetectorRef;
250 constructor(_changeDetectorRef) {
251 this._changeDetectorRef = _changeDetectorRef;
252 }
253}
254const _TdFileInputMixinBase = mixinControlValueAccessor(mixinDisabled(TdFileInputBase));
255class TdFileInputComponent extends _TdFileInputMixinBase {
256 _ngZone;
257 _renderer;
258 _multiple = false;
259 /** The native `<button class="td-file-input"></button>` element */
260 _inputButton;
261 /** The native `<input type="file"> element */
262 _inputElement;
263 get inputElement() {
264 return this._inputElement.nativeElement;
265 }
266 /**
267 * color?: 'accent' | 'primary' | 'warn'
268 * Sets button color. Uses same color palette accepted as [MatButton].
269 */
270 color;
271 /**
272 * multiple?: boolean
273 * Sets if multiple files can be dropped/selected at once in [TdFileInputComponent].
274 */
275 set multiple(multiple) {
276 this._multiple = coerceBooleanProperty(multiple);
277 }
278 get multiple() {
279 return this._multiple;
280 }
281 /**
282 * accept?: string
283 * Sets files accepted when opening the file browser dialog.
284 * Same as 'accept' attribute in <input/> element.
285 */
286 accept;
287 /**
288 * select?: function
289 * Event emitted a file is selected
290 * Emits a [File | FileList] object.
291 */
292 selectFile = new EventEmitter();
293 _destroy$ = new Subject();
294 constructor(_ngZone, _renderer, _changeDetectorRef) {
295 super(_changeDetectorRef);
296 this._ngZone = _ngZone;
297 this._renderer = _renderer;
298 }
299 ngOnInit() {
300 this._ngZone.runOutsideAngular(() => {
301 merge(fromEvent(this._inputButton.nativeElement, 'click'), fromEvent(this._inputButton.nativeElement, 'keyup').pipe(filter((event) => event.keyCode === ENTER)))
302 .pipe(takeUntil(this._destroy$))
303 .subscribe(() => this._inputElement.nativeElement.click());
304 });
305 }
306 ngOnDestroy() {
307 this._destroy$.next();
308 }
309 /**
310 * Method executed when a file is selected.
311 */
312 handleSelect(files) {
313 this.writeValue(files);
314 this.selectFile.emit(files);
315 }
316 /**
317 * Used to clear the selected files from the [TdFileInputComponent].
318 */
319 clear() {
320 this.writeValue(undefined);
321 this._renderer.setProperty(this.inputElement, 'value', '');
322 }
323 /** Method executed when the disabled value changes */
324 onDisabledChange(v) {
325 if (v) {
326 this.clear();
327 }
328 }
329 /**
330 * Sets disable to the component. Implemented as part of ControlValueAccessor.
331 */
332 setDisabledState(isDisabled) {
333 this.disabled = isDisabled;
334 }
335 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileInputComponent, deps: [{ token: i0.NgZone }, { token: i0.Renderer2 }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
336 static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.1.2", type: TdFileInputComponent, selector: "td-file-input", inputs: { disabled: "disabled", value: "value", color: "color", multiple: "multiple", accept: "accept" }, outputs: { selectFile: "selectFile" }, providers: [
337 {
338 provide: NG_VALUE_ACCESSOR,
339 useExisting: forwardRef(() => TdFileInputComponent),
340 multi: true,
341 },
342 ], viewQueries: [{ propertyName: "_inputButton", first: true, predicate: ["fileInputButton"], descendants: true, read: ElementRef, static: true }, { propertyName: "_inputElement", first: true, predicate: ["fileInput"], descendants: true, static: true }], usesInheritance: true, ngImport: i0, template: "<button\n #fileInputButton\n mat-stroked-button\n class=\"td-file-input\"\n type=\"button\"\n [color]=\"color\"\n [multiple]=\"multiple\"\n [disabled]=\"disabled\"\n (fileDrop)=\"handleSelect($event)\"\n tdFileDrop\n>\n <ng-content></ng-content>\n</button>\n<input\n #fileInput\n class=\"td-file-input-hidden\"\n type=\"file\"\n [attr.accept]=\"accept\"\n (fileSelect)=\"handleSelect($event)\"\n [multiple]=\"multiple\"\n [disabled]=\"disabled\"\n tdFileSelect\n/>\n", styles: [":host .td-file-input{padding-left:8px;padding-right:8px}:host input.td-file-input-hidden{display:none}:host ::ng-deep .mdc-button__label{display:flex;align-items:center}:host .drop-zone{border-radius:3px}:host .drop-zone *{pointer-events:none}\n"], dependencies: [{ kind: "component", type: i4.MatButton, selector: " button[mat-button], button[mat-raised-button], button[mat-flat-button], button[mat-stroked-button] ", exportAs: ["matButton"] }, { kind: "directive", type: TdFileSelectDirective, selector: "[tdFileSelect]", inputs: ["multiple"], outputs: ["fileSelect"] }, { kind: "directive", type: TdFileDropDirective, selector: "[tdFileDrop]", inputs: ["multiple", "disabled"], outputs: ["fileDrop"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
343}
344i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileInputComponent, decorators: [{
345 type: Component,
346 args: [{ changeDetection: ChangeDetectionStrategy.OnPush, providers: [
347 {
348 provide: NG_VALUE_ACCESSOR,
349 useExisting: forwardRef(() => TdFileInputComponent),
350 multi: true,
351 },
352 ], selector: 'td-file-input', inputs: ['disabled', 'value'], template: "<button\n #fileInputButton\n mat-stroked-button\n class=\"td-file-input\"\n type=\"button\"\n [color]=\"color\"\n [multiple]=\"multiple\"\n [disabled]=\"disabled\"\n (fileDrop)=\"handleSelect($event)\"\n tdFileDrop\n>\n <ng-content></ng-content>\n</button>\n<input\n #fileInput\n class=\"td-file-input-hidden\"\n type=\"file\"\n [attr.accept]=\"accept\"\n (fileSelect)=\"handleSelect($event)\"\n [multiple]=\"multiple\"\n [disabled]=\"disabled\"\n tdFileSelect\n/>\n", styles: [":host .td-file-input{padding-left:8px;padding-right:8px}:host input.td-file-input-hidden{display:none}:host ::ng-deep .mdc-button__label{display:flex;align-items:center}:host .drop-zone{border-radius:3px}:host .drop-zone *{pointer-events:none}\n"] }]
353 }], ctorParameters: () => [{ type: i0.NgZone }, { type: i0.Renderer2 }, { type: i0.ChangeDetectorRef }], propDecorators: { _inputButton: [{
354 type: ViewChild,
355 args: ['fileInputButton', { static: true, read: ElementRef }]
356 }], _inputElement: [{
357 type: ViewChild,
358 args: ['fileInput', { static: true }]
359 }], color: [{
360 type: Input
361 }], multiple: [{
362 type: Input
363 }], accept: [{
364 type: Input
365 }], selectFile: [{
366 type: Output
367 }] } });
368
369class TdFileUploadBase {
370 _changeDetectorRef;
371 constructor(_changeDetectorRef) {
372 this._changeDetectorRef = _changeDetectorRef;
373 }
374}
375class TdFileUploadComponent {
376 _changeDetectorRef;
377 _multiple = false;
378 _required = false;
379 _disabled = false;
380 fileInput;
381 inputLabel;
382 /**
383 * defaultColor?: 'accent' | 'primary' | 'warn'
384 * Sets browse button color. Uses same color palette accepted as [MatButton] and defaults to 'primary'.
385 */
386 defaultColor = 'primary';
387 /**
388 * activeColor?: 'accent' | 'primary' | 'warn'
389 * Sets upload button color. Uses same color palette accepted as [MatButton] and defaults to 'accent'.
390 */
391 activeColor = 'accent';
392 /**
393 * cancelColor?: 'accent' | 'primary' | 'warn'
394 * Sets cancel button color. Uses same color palette accepted as [MatButton] and defaults to 'warn'.
395 */
396 cancelColor = 'warn';
397 /**
398 * multiple?: boolean
399 * Sets if multiple files can be dropped/selected at once in [TdFileUploadComponent].
400 */
401 set multiple(multiple) {
402 this._multiple = coerceBooleanProperty(multiple);
403 }
404 get multiple() {
405 return this._multiple;
406 }
407 /**
408 * required?: boolean
409 * Forces at least one file upload.
410 * Defaults to 'false'
411 */
412 set required(required) {
413 this._required = coerceBooleanProperty(required);
414 }
415 get required() {
416 return this._required;
417 }
418 /**
419 * accept?: string
420 * Sets files accepted when opening the file browser dialog.
421 * Same as 'accept' attribute in <input/> element.
422 */
423 accept;
424 set disabled(disabled) {
425 this._disabled = disabled;
426 this.onDisabledChange(disabled);
427 }
428 get disabled() {
429 return this._disabled;
430 }
431 value;
432 /**
433 * select?: function
434 * Event emitted when a file is selected.
435 * Emits a [File | FileList] object.
436 */
437 selectFile = new EventEmitter();
438 /**
439 * upload?: function
440 * Event emitted when upload button is clicked.
441 * Emits a [File | FileList] object.
442 */
443 upload = new EventEmitter();
444 /**
445 * cancel?: function
446 * Event emitted when cancel button is clicked.
447 */
448 cancel = new EventEmitter();
449 constructor(_changeDetectorRef) {
450 this._changeDetectorRef = _changeDetectorRef;
451 }
452 writeValue(value) {
453 this.value = value;
454 this._changeDetectorRef.markForCheck();
455 }
456 registerOnChange() {
457 //
458 }
459 registerOnTouched() {
460 //
461 }
462 /**
463 * Method executed when upload button is clicked.
464 */
465 uploadPressed() {
466 if (this.value) {
467 this.upload.emit(this.value);
468 }
469 }
470 /**
471 * Method executed when a file is selected.
472 */
473 handleSelect(value) {
474 this.value = value;
475 this.selectFile.emit(value);
476 }
477 /**
478 * Methods executed when cancel button is clicked.
479 * Clears files.
480 */
481 _cancel() {
482 this.value = undefined;
483 this.cancel.emit();
484 // check if the file input is rendered before clearing it
485 if (this.fileInput) {
486 this.fileInput.clear();
487 }
488 }
489 /** Method executed when the disabled value changes */
490 onDisabledChange(v) {
491 if (v) {
492 this._cancel();
493 }
494 }
495 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileUploadComponent, deps: [{ token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component });
496 static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.1.2", type: TdFileUploadComponent, selector: "td-file-upload", inputs: { defaultColor: "defaultColor", activeColor: "activeColor", cancelColor: "cancelColor", multiple: "multiple", required: "required", accept: "accept", disabled: "disabled", value: "value" }, outputs: { selectFile: "selectFile", upload: "upload", cancel: "cancel" }, providers: [
497 {
498 provide: NG_VALUE_ACCESSOR,
499 useExisting: forwardRef(() => TdFileUploadComponent),
500 multi: true,
501 },
502 ], queries: [{ propertyName: "inputLabel", first: true, predicate: TdFileInputLabelDirective, descendants: true }], viewQueries: [{ propertyName: "fileInput", first: true, predicate: TdFileInputComponent, descendants: true }], ngImport: i0, template: "<td-file-input\n *ngIf=\"!value\"\n [(ngModel)]=\"value\"\n [multiple]=\"multiple\"\n [disabled]=\"disabled\"\n [accept]=\"accept\"\n [color]=\"defaultColor\"\n (selectFile)=\"(handleSelect)\"\n>\n <ng-template [cdkPortalOutlet]=\"inputLabel\" [ngIf]=\"true\"></ng-template>\n</td-file-input>\n<div *ngIf=\"value\">\n <button\n #fileUpload\n class=\"td-file-upload\"\n mat-raised-button\n type=\"button\"\n [color]=\"activeColor\"\n (keyup.delete)=\"_cancel()\"\n (keyup.backspace)=\"_cancel()\"\n (keyup.escape)=\"_cancel()\"\n (click)=\"uploadPressed()\"\n >\n <ng-content></ng-content>\n </button>\n <button\n mat-icon-button\n type=\"button\"\n class=\"td-file-upload-cancel\"\n [color]=\"cancelColor\"\n (click)=\"_cancel()\"\n >\n <mat-icon>cancel</mat-icon>\n </button>\n</div>\n", styles: [".td-file-upload{padding-left:8px;padding-right:8px}.td-file-upload-cancel{height:24px;width:24px;position:relative;top:24px;left:-12px}::ng-deep [dir=rtl] .td-file-upload-cancel{right:-12px;left:0}.td-file-upload-cancel mat-icon{border-radius:12px;vertical-align:baseline}.drop-zone{border-radius:3px}.drop-zone *{pointer-events:none}\n"], dependencies: [{ kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i3.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "component", type: i4.MatButton, selector: " button[mat-button], button[mat-raised-button], button[mat-flat-button], button[mat-stroked-button] ", exportAs: ["matButton"] }, { kind: "component", type: i4.MatIconButton, selector: "button[mat-icon-button]", exportAs: ["matButton"] }, { kind: "directive", type: i5.CdkPortalOutlet, selector: "[cdkPortalOutlet]", inputs: ["cdkPortalOutlet"], outputs: ["attached"], exportAs: ["cdkPortalOutlet"] }, { kind: "component", type: TdFileInputComponent, selector: "td-file-input", inputs: ["disabled", "value", "color", "multiple", "accept"], outputs: ["selectFile"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
503}
504i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileUploadComponent, decorators: [{
505 type: Component,
506 args: [{ changeDetection: ChangeDetectionStrategy.OnPush, providers: [
507 {
508 provide: NG_VALUE_ACCESSOR,
509 useExisting: forwardRef(() => TdFileUploadComponent),
510 multi: true,
511 },
512 ], selector: 'td-file-upload', template: "<td-file-input\n *ngIf=\"!value\"\n [(ngModel)]=\"value\"\n [multiple]=\"multiple\"\n [disabled]=\"disabled\"\n [accept]=\"accept\"\n [color]=\"defaultColor\"\n (selectFile)=\"(handleSelect)\"\n>\n <ng-template [cdkPortalOutlet]=\"inputLabel\" [ngIf]=\"true\"></ng-template>\n</td-file-input>\n<div *ngIf=\"value\">\n <button\n #fileUpload\n class=\"td-file-upload\"\n mat-raised-button\n type=\"button\"\n [color]=\"activeColor\"\n (keyup.delete)=\"_cancel()\"\n (keyup.backspace)=\"_cancel()\"\n (keyup.escape)=\"_cancel()\"\n (click)=\"uploadPressed()\"\n >\n <ng-content></ng-content>\n </button>\n <button\n mat-icon-button\n type=\"button\"\n class=\"td-file-upload-cancel\"\n [color]=\"cancelColor\"\n (click)=\"_cancel()\"\n >\n <mat-icon>cancel</mat-icon>\n </button>\n</div>\n", styles: [".td-file-upload{padding-left:8px;padding-right:8px}.td-file-upload-cancel{height:24px;width:24px;position:relative;top:24px;left:-12px}::ng-deep [dir=rtl] .td-file-upload-cancel{right:-12px;left:0}.td-file-upload-cancel mat-icon{border-radius:12px;vertical-align:baseline}.drop-zone{border-radius:3px}.drop-zone *{pointer-events:none}\n"] }]
513 }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }], propDecorators: { fileInput: [{
514 type: ViewChild,
515 args: [TdFileInputComponent]
516 }], inputLabel: [{
517 type: ContentChild,
518 args: [TdFileInputLabelDirective]
519 }], defaultColor: [{
520 type: Input
521 }], activeColor: [{
522 type: Input
523 }], cancelColor: [{
524 type: Input
525 }], multiple: [{
526 type: Input
527 }], required: [{
528 type: Input
529 }], accept: [{
530 type: Input
531 }], disabled: [{
532 type: Input
533 }], value: [{
534 type: Input
535 }], selectFile: [{
536 type: Output
537 }], upload: [{
538 type: Output
539 }], cancel: [{
540 type: Output
541 }] } });
542
543class TdFileService {
544 _http;
545 _progressSubject = new Subject();
546 _progressObservable;
547 /**
548 * Gets progress observable to keep track of the files being uploaded.
549 * Needs to be supported by backend.
550 */
551 get progress() {
552 return this._progressObservable;
553 }
554 /**
555 * Creates a new instance
556 * @param _http the http client instance
557 * @breaking-change 3.0.0 remove 'Optional' decorator once the legay upload method is removed
558 */
559 constructor(_http) {
560 this._http = _http;
561 this._progressObservable = this._progressSubject.asObservable();
562 }
563 /**
564 * Uploads a file to a URL.
565 */
566 send(url, method, body, { headers, params } = {}) {
567 if (!this._http) {
568 throw new Error('The HttpClient module needs to be imported at root module level');
569 }
570 const req = new HttpRequest(method.toUpperCase(), url, body, {
571 reportProgress: true,
572 headers: new HttpHeaders(headers || {}),
573 params: new HttpParams({ fromObject: params || {} }),
574 });
575 return this._http
576 .request(req)
577 .pipe(tap((event) => this.handleEvent(event)));
578 }
579 handleEvent(event) {
580 switch (event.type) {
581 case HttpEventType.Sent:
582 this._progressSubject.next(0);
583 break;
584 case HttpEventType.UploadProgress:
585 this._progressSubject.next(Math.round((100 * event.loaded) / (event.total ?? 0)));
586 break;
587 case HttpEventType.Response:
588 this._progressSubject.next(100);
589 break;
590 default:
591 break;
592 }
593 }
594 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileService, deps: [{ token: i1$1.HttpClient, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
595 static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileService });
596}
597i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: TdFileService, decorators: [{
598 type: Injectable
599 }], ctorParameters: () => [{ type: i1$1.HttpClient, decorators: [{
600 type: Optional
601 }] }] });
602
603const TD_FILE = [
604 TdFileSelectDirective,
605 TdFileDropDirective,
606 TdFileUploadComponent,
607 TdFileInputComponent,
608 TdFileInputLabelDirective,
609];
610class CovalentFileModule {
611 static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: CovalentFileModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
612 static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.1.2", ngImport: i0, type: CovalentFileModule, declarations: [TdFileSelectDirective,
613 TdFileDropDirective,
614 TdFileUploadComponent,
615 TdFileInputComponent,
616 TdFileInputLabelDirective], imports: [FormsModule,
617 CommonModule,
618 MatIconModule,
619 MatButtonModule,
620 PortalModule], exports: [TdFileSelectDirective,
621 TdFileDropDirective,
622 TdFileUploadComponent,
623 TdFileInputComponent,
624 TdFileInputLabelDirective] });
625 static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: CovalentFileModule, providers: [TdFileService], imports: [FormsModule,
626 CommonModule,
627 MatIconModule,
628 MatButtonModule,
629 PortalModule] });
630}
631i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.1.2", ngImport: i0, type: CovalentFileModule, decorators: [{
632 type: NgModule,
633 args: [{
634 imports: [
635 FormsModule,
636 CommonModule,
637 MatIconModule,
638 MatButtonModule,
639 PortalModule,
640 ],
641 declarations: [TD_FILE],
642 exports: [TD_FILE],
643 providers: [TdFileService],
644 }]
645 }] });
646
647/**
648 * Generated bundle index. Do not edit.
649 */
650
651export { CovalentFileModule, TdFileDropBase, TdFileDropDirective, TdFileInputBase, TdFileInputComponent, TdFileInputLabelDirective, TdFileSelectDirective, TdFileService, TdFileUploadBase, TdFileUploadComponent, _TdFileInputMixinBase };
652//# sourceMappingURL=covalent-core-file.mjs.map