-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.js
More file actions
145 lines (123 loc) · 3.96 KB
/
Copy pathinterface.js
File metadata and controls
145 lines (123 loc) · 3.96 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
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
/**
* 2019 by Pim Meulensteen
*
* This file contains a class to handle the interface between the model and
* the DOM.
*/
class DOMInterface {
constructor() {
this.loopElement = document.getElementById('loopDiv')
this.setupElement = document.getElementById('setupDiv')
this.graphElement = document.getElementById('graph_settings')
this.plusElement = document.getElementById('plus')
this.minusElement = document.getElementById('minus')
}
/**
* Reads value from the node and set this as the runTimes in the model.
* @param {node} node to read the value from.
*/
set runTimes(node) {
model.runTimes = node.value
}
/**
* Get the content from the loop-textarea.
* @returns array of sanatized lines.
*/
get loopContent() {
/* Get the value from the textarea */
let lines = this.loopElement.innerText
/* Split it on each new line */
lines = lines.split('\n')
for (const i in lines) {
lines[i] = this.sanitize(lines[i])
}
/* Filter out all the empty lines. */
lines = lines.filter(function (element) {
return element !== ''
})
return lines
}
/**
* This function get the content from the setup-textarea.
* @returns array of sanatized lines.
*/
get setupContent () {
let lines = this.setupElement.innerText.split('\n')
/* Run the `sanitize` function for every line. */
for (const i in lines) {
lines[i] = this.sanitize(lines[i])
}
/* Filter out all the empty lines. */
lines = lines.filter(function (element) {
return element !== ''
})
return lines
}
/**
* This function removes all unwanted characters from a string of text
* @param {string} text unsanitized text
* @returns sanatized text.
*/
sanitize(text) {
/* Remove spaces and newlines. */
text = text.replace(/[ ]|<br>/g, '')
/* Remove comments. */
text = text.split(COMMENT_CHARACTERS)[0]
/* Replace `,` with `.` (decimal seperator). */
text = text.replace(/[,]/g, '.')
return text
}
/**
* This function makes sure the buttons to add and remove graphs are displayed
* correctly.
*/
setButtonTitles() {
const aantalGrafiek = this.graphElement.childElementCount
if (aantalGrafiek === MAX_GRAPHS) {
this.plusElement.title = `Er kunnen maximaal ${MAX_GRAPHS} grafieken zijn.`
this.plusElement.disabled = true
} else {
this.plusElement.title = 'Voeg een grafiek toe.'
this.plusElement.disabled = false
}
if (aantalGrafiek === 1) {
this.minusElement.title = 'Er moet ten minste één grafiek zijn.'
this.minusElement.disabled = true
} else {
this.minusElement.title = 'Verwijder een grafiek.'
this.minusElement.disabled = false
}
}
/**
* This function add a extra Graph-options-box to the DOM.
*/
addGraph() {
const aantalGrafiek = this.graphElement.childElementCount
if (aantalGrafiek < MAX_GRAPHS) {
const newDiv = document.createElement('div')
for (let i = 0; i < 6; i++) {
/* Add six value input boxes to the div. */
const newInput = document.createElement('input')
newInput.setAttribute('type', 'text')
newDiv.appendChild(newInput)
}
document.getElementById('graph_settings').appendChild(newDiv)
document.getElementById('aantalGrafieken').innerHTML = aantalGrafiek + 1
}
this.setButtonTitles()
}
/**
* This function add a remove Graph-options-box to the DOM.
*/
removeGraph() {
/* Make sure to keep at least one graph alive. */
document.getElementById('plus').title = 'Voeg een grafiek toe.'
const aantalGrafiek = this.graphElement.childElementCount
if (aantalGrafiek > 1) {
const lastChild = this.graphElement.childNodes[this.graphElement.childElementCount + 1]
this.graphElement.removeChild(lastChild)
document.getElementById('aantalGrafieken').innerHTML = aantalGrafiek - 1
}
this.setButtonTitles()
}
}