-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathselect.js
202 lines (182 loc) · 6.13 KB
/
select.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Component from "@pencil.js/component";
import Input from "@pencil.js/input";
import MouseEvent from "@pencil.js/mouse-event";
import BaseEvent from "@pencil.js/base-event";
import Rectangle from "@pencil.js/rectangle";
import Text from "@pencil.js/text";
import { constrain } from "@pencil.js/math";
import Triangle from "@pencil.js/triangle";
/**
* @module Select
*/
const selectedKey = Symbol("_selected");
/**
* Select class
* <br><img src="./media/examples/select.png" alt="select demo"/>
* @class
* @extends {module:Input}
*/
export default class Select extends Input {
/**
* Select constructor
* @param {PositionDefinition} positionDefinition - Any position
* @param {Array<String>} optionsList - List of options to display
* @param {InputOptions} [options] - Drawing options
*/
constructor (positionDefinition, optionsList, options) {
if (!optionsList.length) {
throw new RangeError("Options list should have at least one item.");
}
super(positionDefinition, Rectangle, options);
this[selectedKey] = 0;
const textOptions = {
cursor: Component.cursors.pointer,
fill: this.options.foreground,
font: this.options.font,
fontSize: this.options.fontSize,
align: this.options.align,
bold: this.options.bold,
italic: this.options.italic,
underscore: this.options.underscore,
lineHeight: this.options.lineHeight,
};
const margin = Text.measure("M", textOptions).height * Select.MARGIN;
this.display = new Text(undefined, "", textOptions);
this.optionsList = optionsList.map(option => new Text([(margin * 2) - 1, margin], option || "", textOptions));
const maxWidth = Math.max(...this.optionsList.map(text => text.width));
textOptions.origin = [maxWidth * this.display.getAlignOffset(), 0];
this.optionsContainer = new Rectangle(undefined, this.width, 0, {
fill: this.options.fill,
stroke: this.options.stroke,
strokeWidth: this.options.strokeWidth,
});
this.optionsContainer.hide();
let position = 0;
this.optionsList.forEach((text, index) => {
const rect = new Rectangle([1, position + 1], maxWidth + (6 * margin) - 2, text.height + (2 * margin), {
fill: this.options.fill,
cursor: Component.cursors.pointer,
});
position += rect.height;
let fill;
rect
.on(MouseEvent.events.hover, () => {
fill = fill || rect.options.fill;
rect.options.fill = this.options.hover;
})
.on(MouseEvent.events.leave, () => {
rect.options.fill = fill;
fill = null;
})
.on(MouseEvent.events.click, (event) => {
event.stop();
this.value = index;
this.fire(new BaseEvent(Select.events.change, this));
});
rect.add(text);
this.optionsContainer.add(rect);
});
this.optionsContainer.height = position + 2;
this.arrow = new Triangle([maxWidth + (4 * margin), this.height / 2], margin, {
fill: this.options.foreground,
rotation: 0.5,
cursor: Component.cursors.pointer,
});
this.add(this.display, this.optionsContainer, this.arrow);
}
/**
* Computer button size
* @return {{width: Number, height: Number}}
*/
get size () {
const measures = this.optionsList[this.value].getMeasures();
const maxWidth = Math.max(...this.optionsList.map(text => text.width));
const margin = Text.measure("M", this.optionsList[this.value].options).height * Select.MARGIN;
return {
width: maxWidth + (margin * 6),
height: measures.height + (margin * 2),
};
}
/**
* Get this button's width
* @return {Number}
*/
get width () {
return this.size.width;
}
/**
* Get this button's height
* @return {Number}
*/
get height () {
return this.size.height;
}
/**
* @return {Number}
*/
get value () {
return this[selectedKey];
}
/**
* @param {Number} value - Index of the selected option
*/
set value (value) {
this[selectedKey] = constrain(value, 0, this.optionsList.length - 1);
this.display.text = this.optionsList[this.value].text;
const margin = this.optionsList[0].height * Select.MARGIN;
const origin = this.getOrigin();
this.display.options.origin.set(origin.clone().add(margin * 2, margin));
this.arrow.options.origin.set(origin.clone().multiply(-1));
this.optionsContainer.hide();
if (this.isHovered) {
this.fire(new MouseEvent(MouseEvent.events.leave, this));
}
}
/**
* @inheritDoc
*/
click () {
const { position } = this.optionsList[this.value].parent;
this.optionsContainer.position.set(this.getOrigin()).subtract(position).add(1, 0);
this.optionsContainer.show();
}
/**
* @inheritDoc
*/
toJSON () {
const json = super.toJSON();
json.values = this.optionsList.map(component => component.text);
return json;
}
/**
* @inheritDoc
* @param {Object} definition - Select definition
* @return {Select}
*/
static from (definition) {
return new Select(definition.position, definition.values, definition.options);
}
/**
* @typedef {Object} SelectOptions
* @extends TextOptions
* @extends InputOptions
* @prop {String} [value=0] - Selected index of the select
*/
/**
* @type {SelectOptions}
*/
static get defaultOptions () {
return {
...Text.defaultOptions,
...super.defaultOptions,
value: 0,
};
}
/**
* Margin around the text
* @type {Number}
*/
static get MARGIN () {
return 0.2;
}
}