/*! * devextreme-angular-test * Version: 17.2.8 * Build date: Mon Feb 05 2018 * * Copyright (c) 2012 - 2018 Developer Express Inc. ALL RIGHTS RESERVED * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file in the root of the project for details. * * https://github.com/DevExpress/devextreme-angular */ import { Component, NgModule, ElementRef, NgZone, Input, Output, OnDestroy, Injector, EventEmitter, OnInit, AfterViewInit, ContentChild, forwardRef, HostListener, OnChanges, DoCheck, SimpleChanges } from '@angular/core'; import DxFileUploader from 'devextreme/ui/file_uploader'; import { DxValidatorComponent } from './validator'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { DxComponent } from '../core/component'; import { DxTemplateHost } from '../core/template-host'; import { DxTemplateModule } from '../core/template'; import { EventsRegistrator } from '../core/events-strategy'; import { NestedOptionHost } from '../core/nested-option'; import { WatcherHelper } from '../core/watcher-helper'; import { IterableDifferHelper } from '../core/iterable-differ-helper'; const CUSTOM_VALUE_ACCESSOR_PROVIDER = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => DxFileUploaderComponent), multi: true }; /** * The FileUploader widget enables an end user to upload files to the server. An end user can select files in the file explorer or drag and drop files to the FileUploader area on the page. */ @Component({ selector: 'dx-file-uploader', template: '', providers: [ DxTemplateHost, WatcherHelper, CUSTOM_VALUE_ACCESSOR_PROVIDER, NestedOptionHost, IterableDifferHelper ] }) export class DxFileUploaderComponent extends DxComponent implements OnDestroy, OnInit, AfterViewInit, ControlValueAccessor, OnChanges, DoCheck { instance: DxFileUploader; @ContentChild(DxValidatorComponent) validator: DxValidatorComponent; /** * Specifies a file type or several types accepted by the widget. */ @Input() get accept(): string { return this._getOption('accept'); } set accept(value: string) { this._setOption('accept', value); } /** * Specifies the shortcut key that sets focus on the widget. */ @Input() get accessKey(): string { return this._getOption('accessKey'); } set accessKey(value: string) { this._setOption('accessKey', value); } /** * Specifies whether or not the widget changes its state when interacting with a user. */ @Input() get activeStateEnabled(): boolean { return this._getOption('activeStateEnabled'); } set activeStateEnabled(value: boolean) { this._setOption('activeStateEnabled', value); } /** * Specifies if an end user can remove a file from the selection and interrupt uploading. */ @Input() get allowCanceling(): boolean { return this._getOption('allowCanceling'); } set allowCanceling(value: boolean) { this._setOption('allowCanceling', value); } @Input() get buttonText(): any { return this._getOption('buttonText'); } set buttonText(value: any) { this._setOption('buttonText', value); } /** * Specifies whether the widget responds to user interaction. */ @Input() get disabled(): boolean { return this._getOption('disabled'); } set disabled(value: boolean) { this._setOption('disabled', value); } /** * Specifies the attributes to be attached to the widget's root element. */ @Input() get elementAttr(): any { return this._getOption('elementAttr'); } set elementAttr(value: any) { this._setOption('elementAttr', value); } /** * Specifies whether the widget can be focused using keyboard navigation. */ @Input() get focusStateEnabled(): boolean { return this._getOption('focusStateEnabled'); } set focusStateEnabled(value: boolean) { this._setOption('focusStateEnabled', value); } /** * Specifies the widget's height. */ @Input() get height(): number| Function| string { return this._getOption('height'); } set height(value: number| Function| string) { this._setOption('height', value); } /** * Specifies text for a hint that appears when a user pauses on the widget. */ @Input() get hint(): string { return this._getOption('hint'); } set hint(value: string) { this._setOption('hint', value); } /** * Specifies whether the widget changes its state when a user pauses on it. */ @Input() get hoverStateEnabled(): boolean { return this._getOption('hoverStateEnabled'); } set hoverStateEnabled(value: boolean) { this._setOption('hoverStateEnabled', value); } /** * Specifies whether the editor's value is valid. */ @Input() get isValid(): boolean { return this._getOption('isValid'); } set isValid(value: boolean) { this._setOption('isValid', value); } /** * Specifies the text displayed on the area to which an end-user can drop a file. */ @Input() get labelText(): string { return this._getOption('labelText'); } set labelText(value: string) { this._setOption('labelText', value); } /** * Specifies whether the widget enables an end-user to select a single file or multiple files. */ @Input() get multiple(): boolean { return this._getOption('multiple'); } set multiple(value: boolean) { this._setOption('multiple', value); } /** * Specifies the value passed to the name attribute of the underlying input element. */ @Input() get name(): string { return this._getOption('name'); } set name(value: string) { this._setOption('name', value); } /** * Gets the current progress in percentages. */ @Input() get progress(): number { return this._getOption('progress'); } set progress(value: number) { this._setOption('progress', value); } /** * A Boolean value specifying whether or not the widget is read-only. */ @Input() get readOnly(): boolean { return this._getOption('readOnly'); } set readOnly(value: boolean) { this._setOption('readOnly', value); } /** * The message displayed by the widget when it is ready to upload the specified files. */ @Input() get readyToUploadMessage(): string { return this._getOption('readyToUploadMessage'); } set readyToUploadMessage(value: string) { this._setOption('readyToUploadMessage', value); } /** * Switches the widget to a right-to-left representation. */ @Input() get rtlEnabled(): boolean { return this._getOption('rtlEnabled'); } set rtlEnabled(value: boolean) { this._setOption('rtlEnabled', value); } /** * The text displayed on the button that opens the file browser. */ @Input() get selectButtonText(): string { return this._getOption('selectButtonText'); } set selectButtonText(value: string) { this._setOption('selectButtonText', value); } /** * Specifies whether or not the widget displays the list of selected files. */ @Input() get showFileList(): boolean { return this._getOption('showFileList'); } set showFileList(value: boolean) { this._setOption('showFileList', value); } /** * Specifies the number of the element when the Tab key is used for navigating. */ @Input() get tabIndex(): number { return this._getOption('tabIndex'); } set tabIndex(value: number) { this._setOption('tabIndex', value); } /** * The text displayed on the button that starts uploading. */ @Input() get uploadButtonText(): string { return this._getOption('uploadButtonText'); } set uploadButtonText(value: string) { this._setOption('uploadButtonText', value); } /** * The message displayed by the widget when uploading is finished. */ @Input() get uploadedMessage(): string { return this._getOption('uploadedMessage'); } set uploadedMessage(value: string) { this._setOption('uploadedMessage', value); } /** * The message displayed by the widget on uploading failure. */ @Input() get uploadFailedMessage(): string { return this._getOption('uploadFailedMessage'); } set uploadFailedMessage(value: string) { this._setOption('uploadFailedMessage', value); } /** * Specifies headers for the upload request. */ @Input() get uploadHeaders(): any { return this._getOption('uploadHeaders'); } set uploadHeaders(value: any) { this._setOption('uploadHeaders', value); } /** * Specifies the method for the upload request. */ @Input() get uploadMethod(): string { return this._getOption('uploadMethod'); } set uploadMethod(value: string) { this._setOption('uploadMethod', value); } /** * Specifies how the widget uploads files. */ @Input() get uploadMode(): string { return this._getOption('uploadMode'); } set uploadMode(value: string) { this._setOption('uploadMode', value); } /** * Specifies a target Url for the upload request. */ @Input() get uploadUrl(): string { return this._getOption('uploadUrl'); } set uploadUrl(value: string) { this._setOption('uploadUrl', value); } /** * Specifies information on the validation error when using a custom validation engine. Should be changed at runtime along with the isValid option. */ @Input() get validationError(): any { return this._getOption('validationError'); } set validationError(value: any) { this._setOption('validationError', value); } /** * Specifies a File instance representing the selected file. Read-only when uploadMode is "useForm". */ @Input() get value(): Array { return this._getOption('value'); } set value(value: Array) { this._setOption('value', value); } @Input() get values(): any { return this._getOption('values'); } set values(value: any) { this._setOption('values', value); } /** * Specifies whether the widget is visible. */ @Input() get visible(): boolean { return this._getOption('visible'); } set visible(value: boolean) { this._setOption('visible', value); } /** * Specifies the widget's width. */ @Input() get width(): number| Function| string { return this._getOption('width'); } set width(value: number| Function| string) { this._setOption('width', value); } /** * A handler for the contentReady event. Executed when the widget's content is ready. This handler may be executed multiple times during the widget's lifetime depending on the number of times its content changes. */ @Output() onContentReady: EventEmitter; /** * A handler for the disposing event. Executed when the widget is removed from the DOM using the remove(), empty(), or html() jQuery methods only. */ @Output() onDisposing: EventEmitter; /** * A handler for the initialized event. Executed only once, after the widget is initialized. */ @Output() onInitialized: EventEmitter; /** * A handler for the optionChanged event. Executed after an option of the widget is changed. */ @Output() onOptionChanged: EventEmitter; /** * A handler for the uploaded event. */ @Output() onProgress: EventEmitter; /** * A handler for the uploadAborted event. */ @Output() onUploadAborted: EventEmitter; /** * A handler for the uploaded event. */ @Output() onUploaded: EventEmitter; /** * A handler for the uploadError event. */ @Output() onUploadError: EventEmitter; /** * A handler for the uploadStarted event. */ @Output() onUploadStarted: EventEmitter; /** * A handler for the valueChanged event. */ @Output() onValueChanged: EventEmitter; /** * A handler for the acceptChange event. */ @Output() acceptChange: EventEmitter; /** * A handler for the accessKeyChange event. */ @Output() accessKeyChange: EventEmitter; /** * A handler for the activeStateEnabledChange event. */ @Output() activeStateEnabledChange: EventEmitter; /** * A handler for the allowCancelingChange event. */ @Output() allowCancelingChange: EventEmitter; /** * A handler for the buttonTextChange event. */ @Output() buttonTextChange: EventEmitter; /** * A handler for the disabledChange event. */ @Output() disabledChange: EventEmitter; /** * A handler for the elementAttrChange event. */ @Output() elementAttrChange: EventEmitter; /** * A handler for the focusStateEnabledChange event. */ @Output() focusStateEnabledChange: EventEmitter; /** * A handler for the heightChange event. */ @Output() heightChange: EventEmitter; /** * A handler for the hintChange event. */ @Output() hintChange: EventEmitter; /** * A handler for the hoverStateEnabledChange event. */ @Output() hoverStateEnabledChange: EventEmitter; /** * A handler for the isValidChange event. */ @Output() isValidChange: EventEmitter; /** * A handler for the labelTextChange event. */ @Output() labelTextChange: EventEmitter; /** * A handler for the multipleChange event. */ @Output() multipleChange: EventEmitter; /** * A handler for the nameChange event. */ @Output() nameChange: EventEmitter; /** * A handler for the progressChange event. */ @Output() progressChange: EventEmitter; /** * A handler for the readOnlyChange event. */ @Output() readOnlyChange: EventEmitter; /** * A handler for the readyToUploadMessageChange event. */ @Output() readyToUploadMessageChange: EventEmitter; /** * A handler for the rtlEnabledChange event. */ @Output() rtlEnabledChange: EventEmitter; /** * A handler for the selectButtonTextChange event. */ @Output() selectButtonTextChange: EventEmitter; /** * A handler for the showFileListChange event. */ @Output() showFileListChange: EventEmitter; /** * A handler for the tabIndexChange event. */ @Output() tabIndexChange: EventEmitter; /** * A handler for the uploadButtonTextChange event. */ @Output() uploadButtonTextChange: EventEmitter; /** * A handler for the uploadedMessageChange event. */ @Output() uploadedMessageChange: EventEmitter; /** * A handler for the uploadFailedMessageChange event. */ @Output() uploadFailedMessageChange: EventEmitter; /** * A handler for the uploadHeadersChange event. */ @Output() uploadHeadersChange: EventEmitter; /** * A handler for the uploadMethodChange event. */ @Output() uploadMethodChange: EventEmitter; /** * A handler for the uploadModeChange event. */ @Output() uploadModeChange: EventEmitter; /** * A handler for the uploadUrlChange event. */ @Output() uploadUrlChange: EventEmitter; /** * A handler for the validationErrorChange event. */ @Output() validationErrorChange: EventEmitter; /** * A handler for the valueChange event. */ @Output() valueChange: EventEmitter>; /** * A handler for the valuesChange event. */ @Output() valuesChange: EventEmitter; /** * A handler for the visibleChange event. */ @Output() visibleChange: EventEmitter; /** * A handler for the widthChange event. */ @Output() widthChange: EventEmitter; @Output() onBlur: EventEmitter; @HostListener('valueChange', ['$event']) change(_) { } @HostListener('onBlur', ['$event']) touched = () => {}; constructor(elementRef: ElementRef, ngZone: NgZone, templateHost: DxTemplateHost, injector: Injector, private _watcherHelper: WatcherHelper, private _idh: IterableDifferHelper, optionHost: NestedOptionHost) { super(elementRef, ngZone, templateHost, _watcherHelper); injector.get(EventsRegistrator); this._createEventEmitters([ { subscribe: 'contentReady', emit: 'onContentReady' }, { subscribe: 'disposing', emit: 'onDisposing' }, { subscribe: 'initialized', emit: 'onInitialized' }, { subscribe: 'optionChanged', emit: 'onOptionChanged' }, { subscribe: 'progress', emit: 'onProgress' }, { subscribe: 'uploadAborted', emit: 'onUploadAborted' }, { subscribe: 'uploaded', emit: 'onUploaded' }, { subscribe: 'uploadError', emit: 'onUploadError' }, { subscribe: 'uploadStarted', emit: 'onUploadStarted' }, { subscribe: 'valueChanged', emit: 'onValueChanged' }, { emit: 'acceptChange' }, { emit: 'accessKeyChange' }, { emit: 'activeStateEnabledChange' }, { emit: 'allowCancelingChange' }, { emit: 'buttonTextChange' }, { emit: 'disabledChange' }, { emit: 'elementAttrChange' }, { emit: 'focusStateEnabledChange' }, { emit: 'heightChange' }, { emit: 'hintChange' }, { emit: 'hoverStateEnabledChange' }, { emit: 'isValidChange' }, { emit: 'labelTextChange' }, { emit: 'multipleChange' }, { emit: 'nameChange' }, { emit: 'progressChange' }, { emit: 'readOnlyChange' }, { emit: 'readyToUploadMessageChange' }, { emit: 'rtlEnabledChange' }, { emit: 'selectButtonTextChange' }, { emit: 'showFileListChange' }, { emit: 'tabIndexChange' }, { emit: 'uploadButtonTextChange' }, { emit: 'uploadedMessageChange' }, { emit: 'uploadFailedMessageChange' }, { emit: 'uploadHeadersChange' }, { emit: 'uploadMethodChange' }, { emit: 'uploadModeChange' }, { emit: 'uploadUrlChange' }, { emit: 'validationErrorChange' }, { emit: 'valueChange' }, { emit: 'valuesChange' }, { emit: 'visibleChange' }, { emit: 'widthChange' }, { emit: 'onBlur' } ]); this._idh.setHost(this); optionHost.setHost(this); } protected _createInstance(element, options) { return new DxFileUploader(element, options); } writeValue(value: any): void { this.value = value; } setDisabledState(isDisabled: boolean): void { this.disabled = isDisabled; } registerOnChange(fn: (_: any) => void): void { this.change = fn; } registerOnTouched(fn: () => void): void { this.touched = fn; } _createWidget(element: any) { super._createWidget(element); this.instance.on('focusOut', (e) => { this.eventHelper.fireNgEvent('onBlur', [e]); }); } ngOnDestroy() { this._destroyWidget(); } ngOnChanges(changes: SimpleChanges) { super.ngOnChanges(changes); this.setupChanges('value', changes); } setupChanges(prop: string, changes: SimpleChanges) { if (!(prop in this._optionsToUpdate)) { this._idh.setup(prop, changes); } } ngDoCheck() { this._idh.doCheck('value'); this._watcherHelper.checkWatchers(); super.ngDoCheck(); } _setOption(name: string, value: any) { let isSetup = this._idh.setupSingle(name, value); let isChanged = this._idh.getChanges(name, value) !== null; if (isSetup || isChanged) { super._setOption(name, value); } } ngOnInit() { super.ngOnInit(); if (this.validator) { this.validator.createInstanceOnInit = false; } } ngAfterViewInit() { super.ngAfterViewInit(); if (this.validator) { this.validator.createInstance(this.element.nativeElement); } } } @NgModule({ imports: [ DxTemplateModule ], declarations: [ DxFileUploaderComponent ], exports: [ DxFileUploaderComponent, DxTemplateModule ], providers: [EventsRegistrator] }) export class DxFileUploaderModule { }