-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/2318/extract and migrate slider 2 (#2787)
- Loading branch information
1 parent
700dfe5
commit 6190747
Showing
15 changed files
with
212 additions
and
60 deletions.
There are no files selected for viewing
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
33 changes: 7 additions & 26 deletions
33
visualization/app/codeCharta/ui/areaSettingsPanel/areaSettingsPanel.component.html
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
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
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,8 @@ | ||
<div class="cc-slider-container"> | ||
<label>Margin</label> | ||
<mat-slider [step]="this.step" [min]="this.min" [max]="this.max" [value]="this.value" (input)="this.handleSliderOnChange($event)"> | ||
</mat-slider> | ||
<mat-form-field> | ||
<input matInput type="number" [value]="this.value" (input)="this.handleInputOnChange($event)" /> | ||
</mat-form-field> | ||
</div> |
32 changes: 32 additions & 0 deletions
32
visualization/app/codeCharta/ui/slider/slider.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,32 @@ | ||
cc-slider { | ||
width: 100%; | ||
|
||
.cc-slider-container { | ||
position: relative; | ||
display: flex; | ||
|
||
label { | ||
position: absolute; | ||
top: 0; | ||
font-size: 0.8em; | ||
color: grey; | ||
left: 4px; | ||
} | ||
|
||
mat-slider { | ||
flex: 1; | ||
margin-right: 8px; | ||
margin-left: -4px; | ||
} | ||
|
||
mat-form-field { | ||
width: 50px; | ||
height: 48px; | ||
|
||
.mat-form-field-infix { | ||
border-top-width: 0; | ||
padding-top: 16px; | ||
} | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
visualization/app/codeCharta/ui/slider/slider.component.spec.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,56 @@ | ||
import { TestBed } from "@angular/core/testing" | ||
import { render, screen } from "@testing-library/angular" | ||
import userEvent from "@testing-library/user-event" | ||
import { drag } from "../../util/testUtils/drag" | ||
import { SliderComponent } from "./slider.component" | ||
import { SliderModule } from "./slider.module" | ||
|
||
describe("SliderComponent", () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [SliderModule] | ||
}) | ||
}) | ||
|
||
it("should update given value on input changes", async () => { | ||
let value = 21 | ||
await render(SliderComponent, { | ||
excludeComponentDeclaration: true, | ||
componentProperties: { | ||
value, | ||
onChange: (newValue: number) => { | ||
value = newValue | ||
}, | ||
min: 1, | ||
max: 100 | ||
} | ||
}) | ||
|
||
const inputField = screen.getByRole("spinbutton") as HTMLInputElement | ||
userEvent.type(inputField, "42", { initialSelectionStart: 0, initialSelectionEnd: 2 }) | ||
|
||
expect(value).toBe(42) | ||
}) | ||
|
||
it("should update given value on slider changes", async () => { | ||
let value = 21 | ||
const { container } = await render(SliderComponent, { | ||
excludeComponentDeclaration: true, | ||
componentProperties: { | ||
value, | ||
onChange: (newValue: number) => { | ||
value = newValue | ||
}, | ||
min: 1, | ||
max: 100 | ||
} | ||
}) | ||
|
||
const sliderThumb = container.querySelector(".mat-slider-thumb") | ||
await drag(sliderThumb, { | ||
delta: { x: -100, y: 0 } | ||
}) | ||
|
||
expect(value).toBe(1) | ||
}) | ||
}) |
28 changes: 28 additions & 0 deletions
28
visualization/app/codeCharta/ui/slider/slider.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,28 @@ | ||
import "./slider.component.scss" | ||
import { Component, Input } from "@angular/core" | ||
import { MatSliderChange } from "@angular/material/slider" | ||
|
||
@Component({ | ||
selector: "cc-slider", | ||
template: require("./slider.component.html") | ||
}) | ||
export class SliderComponent { | ||
@Input() value?: number | ||
@Input() min: number | ||
@Input() max: number | ||
@Input() step?: number = 1 | ||
@Input() onChange: (number) => void | ||
|
||
handleSliderOnChange($event: MatSliderChange) { | ||
if ($event.value !== this.value) { | ||
this.onChange($event.value) | ||
} | ||
} | ||
|
||
handleInputOnChange($event: InputEvent) { | ||
const newValue = Number.parseInt(($event.target as HTMLInputElement).value) | ||
if (newValue !== this.value) { | ||
this.onChange(newValue) | ||
} | ||
} | ||
} |
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,12 @@ | ||
import { CommonModule } from "@angular/common" | ||
import { NgModule } from "@angular/core" | ||
import { MaterialModule } from "../../../material/material.module" | ||
import { SliderComponent } from "./slider.component" | ||
|
||
@NgModule({ | ||
imports: [MaterialModule, CommonModule], | ||
declarations: [SliderComponent], | ||
exports: [SliderComponent], | ||
entryComponents: [SliderComponent] | ||
}) | ||
export class SliderModule {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Taken from https://testing-library.com/docs/example-drag/ | ||
|
||
import { fireEvent } from "@testing-library/dom" | ||
|
||
function getElementClientCenter(element) { | ||
const { left, top, width, height } = element.getBoundingClientRect() | ||
return { | ||
x: left + width / 2, | ||
y: top + height / 2 | ||
} | ||
} | ||
|
||
const sleep = async ms => | ||
new Promise(resolve => { | ||
setTimeout(resolve, ms) | ||
}) | ||
|
||
type DragOptions = { | ||
delta: { x: number; y: number } | ||
steps?: number | ||
duration?: number | ||
} | ||
export async function drag(element: Element, { delta, steps = 20, duration = 500 }: DragOptions) { | ||
const from = getElementClientCenter(element) | ||
const to = { | ||
x: from.x + delta.x, | ||
y: from.y + delta.y | ||
} | ||
|
||
const step = { | ||
x: (to.x - from.x) / steps, | ||
y: (to.y - from.y) / steps | ||
} | ||
|
||
const current = { | ||
clientX: from.x, | ||
clientY: from.y | ||
} | ||
|
||
fireEvent.mouseEnter(element, current) | ||
fireEvent.mouseOver(element, current) | ||
fireEvent.mouseMove(element, current) | ||
fireEvent.mouseDown(element, current) | ||
for (let index = 0; index < steps; index++) { | ||
current.clientX += step.x | ||
current.clientY += step.y | ||
await sleep(duration / steps) | ||
fireEvent.mouseMove(element, current) | ||
} | ||
fireEvent.mouseUp(element, current) | ||
} |
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