-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.js
123 lines (106 loc) · 4.02 KB
/
dialog.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
'use strict'
export function dialog(x, y, defaults = {}) {
return new Promise((resolve, reject) => {
const div = document.createElement('div')
div.classList.add('dialog')
const h1 = document.createElement('h1')
h1.textContent = defaults.title || 'Edit Ball'
const form = document.createElement('form')
form.appendChild(h1)
const staticLabel = document.createElement('label')
staticLabel.textContent = 'Static'
const staticInput = document.createElement('input')
staticInput.type = 'checkbox'
staticInput.checked = defaults.isStatic || false
staticLabel.appendChild(staticInput)
form.appendChild(staticLabel)
//pos x
const posXLabel = document.createElement('label')
posXLabel.innerText = 'X'
form.appendChild(posXLabel)
const posXInput = document.createElement('input')
posXInput.type = 'number'
posXInput.value = defaults.posX ?? x
form.appendChild(posXInput)
//pos y
const posYLabel = document.createElement('label')
posYLabel.innerText = 'Y'
form.appendChild(posYLabel)
const posYInput = document.createElement('input')
posYInput.type = 'number'
posYInput.value = defaults.posY ?? y
form.appendChild(posYInput)
//vel x
const velXLabel = document.createElement('label')
velXLabel.innerText = 'Initial velocity X'
form.appendChild(velXLabel)
const velXInput = document.createElement('input')
velXInput.type = 'number'
velXInput.value = defaults.velX ?? 0
velXInput.step = 0.1
form.appendChild(velXInput)
//vel y
const velYLabel = document.createElement('label')
velYLabel.innerText = 'Initial velocity Y'
form.appendChild(velYLabel)
const velYInput = document.createElement('input')
velYInput.type = 'number'
velYInput.value = defaults.velY ?? 0
velYInput.step = 0.1
form.appendChild(velYInput)
//radius
const radiusLabel = document.createElement('label')
radiusLabel.innerText = 'Radius'
form.appendChild(radiusLabel)
const radiusInput = document.createElement('input')
radiusInput.type = 'number'
radiusInput.value = defaults.radius ?? 15
form.appendChild(radiusInput)
//mass
const massLabel = document.createElement('label')
massLabel.innerText = 'Mass (tons)'
form.appendChild(massLabel)
const massInput = document.createElement('input')
massInput.type = 'number'
massInput.value = defaults.mass ?? 5000
massInput.step = 100
form.appendChild(massInput)
const button = document.createElement('button')
button.innerText = 'Save'
form.appendChild(button)
const deleteButton = document.createElement('div')
deleteButton.classList.add('delete')
deleteButton.innerText = 'Delete ball'
if (defaults.posX !== undefined) form.appendChild(deleteButton)
deleteButton.onclick = () => {
div.remove()
resolve({
delete: true,
})
}
const cancelButton = document.createElement('div')
cancelButton.classList.add('cancel')
cancelButton.innerText = 'Cancel'
form.appendChild(cancelButton)
cancelButton.onclick = () => {
div.remove()
resolve(null)
}
div.appendChild(form)
form.onsubmit = (e) => {
e.preventDefault()
const values = {
posX: +posXInput.value,
posY: +posYInput.value,
velX: +velXInput.value / 10,
velY: +velYInput.value / 10,
radius: +radiusInput.value,
mass: +massInput.value * 1000,
isStatic: staticInput.checked,
}
div.remove()
resolve(values)
}
document.body.appendChild(div)
})
}