-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f790a6
commit 771ad6c
Showing
25 changed files
with
832 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
projects/common/src/utilities/formatters/file-size/display-file-size.pipe.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createPipeFactory } from '@ngneat/spectator/jest'; | ||
import { DisplayFileSizePipe } from './display-file-size.pipe'; | ||
|
||
describe('Display File Size Pipe', () => { | ||
const createPipe = createPipeFactory(DisplayFileSizePipe); | ||
|
||
test('should return correct size in Bytes', () => { | ||
const spectator = createPipe(`{{ 1 | htDisplayFileSize}}`); | ||
expect(spectator.element).toHaveText('1 B'); | ||
}); | ||
|
||
test('should return correct size in KiloBytes', () => { | ||
const spectator = createPipe(`{{ 102.4 | htDisplayFileSize}}`); | ||
expect(spectator.element).toHaveText('0.1 KB'); | ||
}); | ||
|
||
test('should return correct size in MegaBytes', () => { | ||
const spectator = createPipe(`{{ 1024 * 102.4 | htDisplayFileSize}}`); | ||
expect(spectator.element).toHaveText('0.1 MB'); | ||
}); | ||
|
||
test('should return correct size in GigaBytes', () => { | ||
const spectator = createPipe(`{{ 1024 * 1024 * 102.4 | htDisplayFileSize}}`); | ||
expect(spectator.element).toHaveText('0.1 GB'); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
projects/common/src/utilities/formatters/file-size/display-file-size.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
@Pipe({ | ||
name: 'htDisplayFileSize' | ||
}) | ||
export class DisplayFileSizePipe implements PipeTransform { | ||
public transform(sizeInBytes: number): string { | ||
let power = Math.round(Math.log(sizeInBytes) / Math.log(1024)); | ||
power = Math.min(power, FILE_SIZE_UNITS.length - 1); | ||
|
||
const size = sizeInBytes / Math.pow(1024, power); | ||
const formattedSize = Math.round(size * 100) / 100; // Formatting the size to 2 decimals | ||
const unit = FILE_SIZE_UNITS[power]; | ||
|
||
return `${formattedSize} ${unit}`; | ||
} | ||
} | ||
|
||
const FILE_SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
projects/components/src/file-upload/drop-zone/drop-zone.directive.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createDirectiveFactory } from '@ngneat/spectator/jest'; | ||
import { DropZoneDirective } from './drop-zone.directive'; | ||
|
||
describe('Drop Zone Directive', () => { | ||
const createDirective = createDirectiveFactory({ directive: DropZoneDirective }); | ||
|
||
test('should emit events correctly', () => { | ||
const spectator = createDirective(`<div class="content" htDropZone></div>`); | ||
|
||
spyOn(spectator.directive.dragHover, 'emit'); | ||
spyOn(spectator.directive.dropped, 'emit'); | ||
|
||
spectator.dispatchMouseEvent(spectator.element, 'dragover'); | ||
expect(spectator.directive.dropped.emit).toHaveBeenCalledTimes(0); | ||
expect(spectator.directive.dragHover.emit).toHaveBeenCalledWith(true); | ||
|
||
spectator.dispatchMouseEvent(spectator.element, 'dragleave'); | ||
expect(spectator.directive.dropped.emit).toHaveBeenCalledTimes(0); | ||
expect(spectator.directive.dragHover.emit).toHaveBeenCalledWith(false); | ||
|
||
spectator.dispatchMouseEvent(spectator.element, 'drop'); | ||
expect(spectator.directive.dropped.emit).toHaveBeenCalledWith(undefined); | ||
expect(spectator.directive.dragHover.emit).toHaveBeenCalledWith(false); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
projects/components/src/file-upload/drop-zone/drop-zone.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Directive, EventEmitter, HostListener, Output } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[htDropZone]' | ||
}) | ||
export class DropZoneDirective { | ||
@Output() | ||
public readonly dropped: EventEmitter<FileList> = new EventEmitter(); | ||
|
||
@Output() | ||
public readonly dragHover: EventEmitter<boolean> = new EventEmitter(); | ||
|
||
@HostListener('drop', ['$event']) | ||
public onDrop(event: DragEvent): void { | ||
event.preventDefault(); | ||
this.dropped.emit(event.dataTransfer?.files); | ||
this.dragHover.emit(false); | ||
} | ||
|
||
@HostListener('dragover', ['$event']) | ||
public onDragover(event: DragEvent): void { | ||
event.preventDefault(); | ||
this.dragHover.emit(true); | ||
} | ||
|
||
@HostListener('dragleave', ['$event']) | ||
public onDragleave(event: DragEvent): void { | ||
event.preventDefault(); | ||
this.dragHover.emit(false); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
projects/components/src/file-upload/file-display/file-display.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
@import 'mixins'; | ||
|
||
.file-display { | ||
display: flex; | ||
align-items: center; | ||
gap: 20px; | ||
padding: 12px; | ||
border: 1px solid $gray-2; | ||
border-radius: 6px; | ||
position: relative; | ||
z-index: 0; | ||
|
||
.file-info { | ||
min-width: 0; | ||
flex: 1 1 auto; | ||
display: flex; | ||
flex-direction: column; | ||
gap: 6px; | ||
|
||
.basic-detail { | ||
width: 100%; | ||
display: flex; | ||
align-items: center; | ||
|
||
.file-name { | ||
@include body-1-regular($gray-7); | ||
@include ellipsis-overflow; | ||
padding-right: 8px; | ||
border-right: 1px solid $gray-4; | ||
} | ||
|
||
.file-size { | ||
@include body-small($gray-3); | ||
padding-left: 8px; | ||
} | ||
} | ||
} | ||
|
||
.delete-icon { | ||
cursor: pointer; | ||
position: absolute; | ||
right: -4px; | ||
top: -4px; | ||
z-index: 1; | ||
} | ||
|
||
&.error { | ||
border-color: $red-4; | ||
background-color: $red-1; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
projects/components/src/file-upload/file-display/file-display.component.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { FormattingModule } from '@hypertrace/common'; | ||
import { createComponentFactory } from '@ngneat/spectator/jest'; | ||
import { MockComponent } from 'ng-mocks'; | ||
import { IconComponent } from '../../icon/icon.component'; | ||
import { FileUploadState } from './file-display'; | ||
import { FileDisplayComponent } from './file-display.component'; | ||
|
||
describe('File Display Component', () => { | ||
const createComponent = createComponentFactory({ | ||
component: FileDisplayComponent, | ||
shallow: true, | ||
imports: [FormattingModule], | ||
declarations: [MockComponent(IconComponent)] | ||
}); | ||
|
||
test('should render everything correctly', () => { | ||
const file = new File([new Blob(['text'])], 'file.txt'); | ||
const spectator = createComponent(); | ||
expect(spectator.query('.file-display')).not.toExist(); | ||
|
||
// File with no progress bar | ||
spectator.setInput({ file: file }); | ||
expect(spectator.query('.file-display')).toExist(); | ||
expect(spectator.query('.file-name')).toHaveText('file.txt'); | ||
expect(spectator.query('.file-size')).toHaveText('4'); | ||
|
||
// Delete file action | ||
spyOn(spectator.component.deleteClick, 'emit'); | ||
spectator.click(spectator.query('.delete-icon') as Element); | ||
expect(spectator.component.deleteClick.emit).toHaveBeenCalled(); | ||
|
||
// Success state | ||
spectator.setInput({ file: file, state: FileUploadState.Success, showState: true }); | ||
expect(spectator.query('.success-icon')).toExist(); | ||
expect(spectator.query('.delete-icon')).not.toExist(); | ||
|
||
// Failure state | ||
spectator.setInput({ file: file, state: FileUploadState.Failure, showState: true }); | ||
expect(spectator.query('.file-display.error')).toExist(); | ||
expect(spectator.query('.failure-icon')).toExist(); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
projects/components/src/file-upload/file-display/file-display.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core'; | ||
import { IconType } from '@hypertrace/assets-library'; | ||
import { Color } from '@hypertrace/common'; | ||
import { IconSize } from '../../icon/icon-size'; | ||
import { FileUploadState } from './file-display'; | ||
|
||
@Component({ | ||
selector: 'ht-file-display', | ||
styleUrls: ['./file-display.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
template: ` | ||
<div *ngIf="this.file" class="file-display" [ngClass]="{ error: this.state === '${FileUploadState.Failure}' }"> | ||
<ht-icon class="note-icon" icon="${IconType.Note}" size="${IconSize.Medium}" color="${Color.Blue4}"></ht-icon> | ||
<div class="file-info"> | ||
<div class="basic-detail"> | ||
<div class="file-name" [htTooltip]="this.file.name">{{ this.file.name }}</div> | ||
<div class="file-size">{{ this.file.size | htDisplayFileSize }}</div> | ||
</div> | ||
</div> | ||
<ng-container *ngIf="this.showState"> | ||
<ng-container [ngSwitch]="this.state" | ||
><ng-container *ngSwitchCase="'${FileUploadState.Success}'" | ||
><ht-icon | ||
class="success-icon" | ||
icon="${IconType.CheckCircle}" | ||
color="${Color.Green5}" | ||
></ht-icon></ng-container | ||
><ng-container *ngSwitchCase="'${FileUploadState.Failure}'" | ||
><ht-icon | ||
class="failure-icon" | ||
icon="${IconType.Alert}" | ||
color="${Color.Red6}" | ||
[htTooltip]="this.errorStateTooltipText" | ||
></ht-icon></ng-container | ||
></ng-container> | ||
</ng-container> | ||
<ht-icon | ||
*ngIf="this.showDelete" | ||
class="delete-icon" | ||
icon="${IconType.CloseCircleFilled}" | ||
size="${IconSize.Small}" | ||
color="${Color.Gray9}" | ||
(click)="this.onDeleteClick()" | ||
></ht-icon> | ||
</div> | ||
` | ||
}) | ||
export class FileDisplayComponent { | ||
@Input() | ||
public file?: File; | ||
|
||
@Input() | ||
public state: FileUploadState = FileUploadState.NotStarted; | ||
|
||
@Input() | ||
public showState: boolean = false; | ||
|
||
@Input() | ||
public errorStateTooltipText: string = ''; | ||
|
||
@Output() | ||
public readonly deleteClick: EventEmitter<void> = new EventEmitter(); | ||
|
||
// Only show file delete option if file upload has not started or it has resulted in failure | ||
public get showDelete(): boolean { | ||
return this.state === FileUploadState.NotStarted || this.state === FileUploadState.Failure; | ||
} | ||
|
||
public onDeleteClick(): void { | ||
this.deleteClick.emit(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
projects/components/src/file-upload/file-display/file-display.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { FormattingModule } from '@hypertrace/common'; | ||
import { IconModule } from '../../icon/icon.module'; | ||
import { TooltipModule } from '../../tooltip/tooltip.module'; | ||
import { FileDisplayComponent } from './file-display.component'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule, IconModule, FormattingModule, TooltipModule], | ||
declarations: [FileDisplayComponent], | ||
exports: [FileDisplayComponent] | ||
}) | ||
export class FileDisplayModule {} |
6 changes: 6 additions & 0 deletions
6
projects/components/src/file-upload/file-display/file-display.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const enum FileUploadState { | ||
Success = 'success', | ||
Failure = 'failure', | ||
InProgress = 'in-progress', | ||
NotStarted = 'not-started' | ||
} |
Oops, something went wrong.