-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathalpha-input.gts
More file actions
97 lines (83 loc) · 2.36 KB
/
alpha-input.gts
File metadata and controls
97 lines (83 loc) · 2.36 KB
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
import { hash } from '@ember/helper';
import { on } from '@ember/modifier';
import { action, set } from '@ember/object';
import Component from '@glimmer/component';
import OneWayInputMask from 'ember-inputmask/components/one-way-input-mask';
import { rgbaToHex } from '../data-models/color.ts';
import type { SelectedColorModel } from './rgb-input';
interface AlphaInputSignature {
Element: HTMLInputElement;
Args: {
selectedColor: SelectedColorModel;
update: (value: string | number) => void;
updateColor: () => void;
value?: number;
};
}
export default class AlphaInputComponent extends Component<AlphaInputSignature> {
<template>
<span class="input-prefix">
A:
</span>
<OneWayInputMask
...attributes
maxlength={{4}}
@mask="9[.9[9]]"
@options={{hash
greedy=false
isComplete=this.isComplete
min=0
max=1
oncomplete=this.onComplete
onincomplete=this.onIncomplete
regex=this.alphaRegex
showMaskOnFocus=false
showMaskOnHover=false
unmaskAsNumber=false
}}
@update={{@update}}
@value={{@value}}
{{on "keypress" this.enterPress}}
/>
</template>
alphaRegex = /^[1]$|^[0]$|^(0\.[0-9]{1,2})$/;
@action
enterPress(event: KeyboardEvent): void {
if (event.keyCode === 13) {
(<HTMLInputElement>event.target).blur();
}
}
@action
isComplete(buffer: Array<string>, opts: { regex?: string }): boolean {
const value = buffer.join('');
return (
Boolean(value.length) && new RegExp(opts.regex as string).test(value)
);
}
/**
* When the rgb input value passes the regex, set the value, and update the hex values and color.
* @param {Event} event
*/
@action
onComplete(event: InputEvent): void {
const { selectedColor } = this.args;
let value = parseFloat((<HTMLInputElement>event.target).value);
if (value > 1) {
value = 1;
}
set(selectedColor, 'a', value);
set(selectedColor, `_a`, value);
const { r, g, b, a } = selectedColor;
const hex = rgbaToHex(r, g, b, a);
set(selectedColor, '_hex', hex);
set(selectedColor, 'hex', hex);
this.args.updateColor();
}
/**
* Resets the alpha input value if you navigate away
*/
@action
onIncomplete(): void {
set(this.args.selectedColor, `_a`, this.args.selectedColor.a);
}
}