diff --git a/packages/uhk-web/src/app/components/device/configuration/device-configuration.component.html b/packages/uhk-web/src/app/components/device/configuration/device-configuration.component.html index d3c0d765..e54ef24d 100644 --- a/packages/uhk-web/src/app/components/device/configuration/device-configuration.component.html +++ b/packages/uhk-web/src/app/components/device/configuration/device-configuration.component.html @@ -14,11 +14,9 @@
  • - + +
  • - +

    diff --git a/packages/uhk-web/src/app/components/device/firmware/device-firmware.component.ts b/packages/uhk-web/src/app/components/device/firmware/device-firmware.component.ts index 67b4dc1c..7e8812e0 100644 --- a/packages/uhk-web/src/app/components/device/firmware/device-firmware.component.ts +++ b/packages/uhk-web/src/app/components/device/firmware/device-firmware.component.ts @@ -15,6 +15,7 @@ import { } from '../../../store'; import { UpdateFirmwareAction, UpdateFirmwareWithAction } from '../../../store/actions/device'; import { XtermLog } from '../../../models/xterm-log'; +import { UploadFileData } from '../../../models/upload-file-data'; @Component({ selector: 'device-firmware', @@ -48,19 +49,8 @@ export class DeviceFirmwareComponent implements OnDestroy { this.store.dispatch(new UpdateFirmwareAction()); } - changeFile(event): void { - const files = event.srcElement.files; - - if (files.length === 0) { - return; - } - - const fileReader = new FileReader(); - fileReader.onloadend = function () { - const arrayBuffer = new Uint8Array(fileReader.result); - this.store.dispatch(new UpdateFirmwareWithAction(Array.prototype.slice.call(arrayBuffer))); - }.bind(this); - fileReader.readAsArrayBuffer(files[0]); + changeFile(data: UploadFileData): void { + this.store.dispatch(new UpdateFirmwareWithAction(data.data)); } openFirmwareGitHubIssuePage(event): void { diff --git a/packages/uhk-web/src/app/components/file-upload/file-upload.component.html b/packages/uhk-web/src/app/components/file-upload/file-upload.component.html new file mode 100644 index 00000000..020de26b --- /dev/null +++ b/packages/uhk-web/src/app/components/file-upload/file-upload.component.html @@ -0,0 +1,9 @@ + diff --git a/packages/uhk-web/src/app/components/file-upload/file-upload.component.ts b/packages/uhk-web/src/app/components/file-upload/file-upload.component.ts new file mode 100644 index 00000000..96a558a1 --- /dev/null +++ b/packages/uhk-web/src/app/components/file-upload/file-upload.component.ts @@ -0,0 +1,36 @@ +import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; + +import { UploadFileData } from '../../models/upload-file-data'; + +@Component({ + selector: 'file-upload', + templateUrl: './file-upload.component.html', + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class FileUploadComponent { + @Input() label = 'Select file'; + @Input() disabled: boolean; + @Input() accept: string; + + @Output() fileChanged = new EventEmitter(); + + changeFile(event): void { + const files = event.srcElement.files; + + if (files.length === 0) { + return; + } + + const fileReader = new FileReader(); + fileReader.onloadend = function () { + const arrayBuffer = new Uint8Array(fileReader.result); + const target = event.target || event.srcElement || event.currentTarget; + target.value = null; + this.fileChanged.emit({ + filename: event.srcElement.value, + data: Array.from(arrayBuffer) + }); + }.bind(this); + fileReader.readAsArrayBuffer(files[0]); + } +} diff --git a/packages/uhk-web/src/app/components/file-upload/index.ts b/packages/uhk-web/src/app/components/file-upload/index.ts new file mode 100644 index 00000000..42532e84 --- /dev/null +++ b/packages/uhk-web/src/app/components/file-upload/index.ts @@ -0,0 +1 @@ +export * from './file-upload.component'; diff --git a/packages/uhk-web/src/app/shared.module.ts b/packages/uhk-web/src/app/shared.module.ts index 88b9a716..67eae660 100644 --- a/packages/uhk-web/src/app/shared.module.ts +++ b/packages/uhk-web/src/app/shared.module.ts @@ -107,6 +107,7 @@ import { SliderWrapperComponent } from './components/slider-wrapper/slider-wrapp import { EditableTextComponent } from './components/editable-text/editable-text.component'; import { Autofocus } from './directives/autofocus/autofocus.directive'; import { UhkDeviceBootloaderNotActiveGuard } from './services/uhk-device-bootloader-not-active.guard'; +import { FileUploadComponent } from './components/file-upload'; @NgModule({ declarations: [ @@ -179,7 +180,8 @@ import { UhkDeviceBootloaderNotActiveGuard } from './services/uhk-device-bootloa EditableTextComponent, Autofocus, RestoreConfigurationComponent, - RecoveryModeComponent + RecoveryModeComponent, + FileUploadComponent ], imports: [ CommonModule,