forked from pencil-js/pencil.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkbox.js
93 lines (84 loc) · 2.13 KB
/
checkbox.js
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
import Input from "@pencil.js/input";
import Square from "@pencil.js/square";
/**
* Checkbox class
* @class
* @extends Container
*/
export default class Checkbox extends Input {
/**
* Checkbox constructor
* @param {PositionDefinition} positionDefinition - Top-left corner
* @param {CheckboxOptions} options -
*/
constructor (positionDefinition, options) {
super(positionDefinition, options);
this.background.width = this.options.size;
this.background.height = this.options.size;
const margin = this.options.size * Checkbox.MARGIN;
this.fill = new Square([margin, margin], this.options.size - (2 * margin), {
fill: this.options.fill,
shown: this.options.value,
cursor: this.background.options.cursor,
});
this.background.add(this.fill);
}
/**
* @inheritDoc
*/
click () {
this.toggle();
super.click();
}
/**
* Inverse whether it's checked
* @param {Boolean} [newValue] - If defined, will force the value
* @return {Boolean}
*/
toggle (newValue = !this.value) {
this.value = newValue;
return newValue;
}
/**
* Return whether it's checked
* @return {Boolean}
*/
get value () {
return this.fill.options.shown;
}
/**
* Change whether it's checked
* @param {Boolean} value - New value
*/
set value (value) {
if (value) {
this.fill.show();
}
else {
this.fill.hide();
}
}
/**
* @typedef {Object} CheckboxOptions
* @extends InputOptions
* @prop {Number} [size=20] - Width and height of the checkbox
* @prop {Boolean} [value=false] - Whether it's check ot not
*/
/**
* @return {CheckboxOptions}
*/
static get defaultOptions () {
return Object.assign(super.defaultOptions, {
size: 20,
value: false,
});
}
/**
* Margin around the filling square in ratio
* @return {Number}
* @constant
*/
static get MARGIN () {
return 0.2;
}
}