|
| 1 | +import { Icons } from '../../core'; |
1 | 2 | import { Dom } from '../../core/dom';
|
2 | 3 |
|
3 |
| -const SIZE = 20; |
| 4 | +const SIZE = 22; |
| 5 | +const ICON_SIZE = 12; |
4 | 6 |
|
5 | 7 | export class ValidationErrorView {
|
6 | 8 | public static create(parent: SVGElement, x: number, y: number): ValidationErrorView {
|
7 |
| - const g = Dom.svg('g', { |
8 |
| - class: 'sqd-hidden' |
9 |
| - }); |
10 |
| - Dom.translate(g, x, y); |
11 |
| - |
12 |
| - const circle = Dom.svg('path', { |
13 |
| - class: 'sqd-validation-error', |
14 |
| - d: `M 0 ${-SIZE / 2} l ${SIZE / 2} ${SIZE} l ${-SIZE} 0 Z` |
15 |
| - }); |
16 |
| - |
17 |
| - g.appendChild(circle); |
18 |
| - parent.appendChild(g); |
19 |
| - return new ValidationErrorView(g); |
| 9 | + return new ValidationErrorView(parent, x, y); |
20 | 10 | }
|
21 | 11 |
|
22 |
| - public constructor(private readonly g: SVGElement) {} |
| 12 | + private g?: SVGElement; |
| 13 | + |
| 14 | + public constructor(private readonly parent: SVGElement, private readonly x: number, private readonly y: number) {} |
23 | 15 |
|
24 | 16 | public setIsHidden(isHidden: boolean) {
|
25 |
| - Dom.toggleClass(this.g, isHidden, 'sqd-hidden'); |
| 17 | + if (isHidden) { |
| 18 | + if (this.g) { |
| 19 | + this.parent.removeChild(this.g); |
| 20 | + this.g = undefined; |
| 21 | + } |
| 22 | + } else if (!this.g) { |
| 23 | + this.g = Dom.svg('g'); |
| 24 | + Dom.translate(this.g, this.x, this.y); |
| 25 | + |
| 26 | + const halfOfSize = SIZE / 2; |
| 27 | + const circle = Dom.svg('path', { |
| 28 | + class: 'sqd-validation-error', |
| 29 | + d: `M 0 ${-halfOfSize} l ${halfOfSize} ${SIZE} l ${-SIZE} 0 Z` |
| 30 | + }); |
| 31 | + this.g.appendChild(circle); |
| 32 | + |
| 33 | + const icon = Icons.appendPath(this.g, 'sqd-validation-error-icon-path', Icons.alert, ICON_SIZE); |
| 34 | + const offsetX = (SIZE - ICON_SIZE) * 0.5; |
| 35 | + const offsetY = offsetX * 1.5; // 0.5 * 1.5 = 0.75 |
| 36 | + Dom.translate(icon, -halfOfSize + offsetX, -halfOfSize + offsetY); |
| 37 | + |
| 38 | + this.parent.appendChild(this.g); |
| 39 | + } |
26 | 40 | }
|
27 | 41 | }
|
0 commit comments