-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcolor-wrap.component.ts
98 lines (91 loc) · 2.35 KB
/
color-wrap.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { CommonModule } from '@angular/common';
import {
Component,
EventEmitter,
Input,
NgModule,
OnChanges,
OnDestroy,
OnInit,
Output,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { simpleCheckForValidColor, toState } from './helpers/color';
import { Color, HSLA, HSVA, RGBA } from './helpers/color.interfaces';
export interface ColorEvent {
$event: Event;
color: Color;
}
@Component({
// create seletor base for test override property
selector: 'color-wrap',
template: ``,
})
export class ColorWrap implements OnInit, OnChanges, OnDestroy {
@Input() className?: string;
@Input() color: HSLA | HSVA | RGBA | string = {
h: 250,
s: 0.5,
l: 0.2,
a: 1,
};
@Output() onChange = new EventEmitter<ColorEvent>();
@Output() onChangeComplete = new EventEmitter<ColorEvent>();
@Output() onSwatchHover = new EventEmitter<ColorEvent>();
oldHue: number;
hsl: HSLA;
hsv: HSVA;
rgb: RGBA;
hex: string;
source: string;
currentColor: string;
changes: Subscription;
ngOnInit() {
this.changes = this.onChange
.pipe(debounceTime(100))
.subscribe(x => this.onChangeComplete.emit(x));
this.setState(toState(this.color, 0));
this.currentColor = this.hex;
}
ngOnChanges() {
this.setState(toState(this.color, this.oldHue));
}
ngOnDestroy() {
this.changes.unsubscribe();
}
setState(data) {
this.oldHue = data.oldHue;
this.hsl = data.hsl;
this.hsv = data.hsv;
this.rgb = data.rgb;
this.hex = data.hex;
this.source = data.source;
this.afterValidChange();
}
handleChange(data, $event) {
const isValidColor = simpleCheckForValidColor(data);
if (isValidColor) {
const color = toState(data, data.h || this.oldHue);
this.setState(color);
this.onChange.emit({ color, $event });
this.afterValidChange();
}
}
/** hook for components after a complete change */
afterValidChange() {}
handleSwatchHover(data, $event) {
const isValidColor = simpleCheckForValidColor(data);
if (isValidColor) {
const color = toState(data, data.h || this.oldHue);
this.setState(color);
this.onSwatchHover.emit({ color, $event });
}
}
}
@NgModule({
declarations: [ColorWrap],
exports: [ColorWrap],
imports: [CommonModule],
})
export class ColorWrapModule {}