-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selector.js
73 lines (70 loc) · 1.89 KB
/
Selector.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
import Component from "./Component.js"
import ObjectHelper from "./Helpers/ObjectHelper.js"
export default class Selector extends Component {
constructor() {
super()
this.options = []
}
static copyConfig = ObjectHelper.merge(Component.copyConfig, {
includeProperties: {
options: true,
},
})
addOption(option) {
for (let i = 0; ; i++) {
if (this.options[i] == undefined) {
this.options[i] = option
break
}
}
if (this.isBuilt) this.addOptionToContainer(option)
return this
}
createContainer() {
this.container = document.createElement("select")
this.container.style.pointerEvents = "all"
this.container.style.position = "absolute"
}
addOptionToContainer(option) {
let optionContainer = document.createElement("option")
optionContainer.value = option.value
optionContainer.innerText = option.text
this.container.appendChild(optionContainer)
return this
}
refrestOptions() {
this.container.innerText = ""
for (let option of this.options) {
if (option != undefined) {
this.addOptionToContainer(option)
}
}
}
removeOptionByValue(value) {
for (let i = 0; i < this.options.length; i++) {
if (this.options[i] != undefined && this.options[i].value == value) {
delete this.options[i]
this.refrestOptions()
break
}
}
}
resize() {
let pixelSize = this.getPixelSize()
this.container.style.fontSize = pixelSize.x * this.size.y
this.container.setSize(this.size.multiply(pixelSize))
this.container.setPosition(this.position.multiply(pixelSize))
}
build() {
super.build()
this.refrestOptions()
this.isBuilt = true
return this
}
}
export class Option {
constructor(value, text) {
this.value = value
this.text = text
}
}