-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.js
142 lines (118 loc) · 3.65 KB
/
gui.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
/*
Set up
*/
let solver
let workingSolutions
$(setupGui)
function setupGui() {
solver = createSolver()
$("#init").click(initClick)
$("#reinit").click(reinitClick)
$("#byWord").click(byWordClick)
$("#bySample").click(bySampleClick)
$("#reset").click(resetClick)
$("#sampleSolutions").click(sampleSolutionsClick)
$("#filterSolutions").click(filterSolutionsClick)
}
/*
Event handlers
*/
function initClick() {
$("#message").html('Initializing...')
setTimeout(() => {
solver.init()
$("#message").html('Initialized and cached. Now, find either 1) all solutions that include a certain word or 2) some random solutions.')
$("#init-container").addClass('hidden')
$("#reinit-container").removeClass('hidden')
$("#byWord-container").removeClass('hidden')
$("#bySample-container").removeClass('hidden')
}, 100)
}
function reinitClick() {
$("#message").html('Re-initializing...')
setTimeout(() => {
solver.reinit()
$("#message").html('Re-initialized and cached. Now, find either 1) all solutions that include a certain word or 2) some random solutions.')
$("#reset-container").addClass('hidden')
$("#sampleSolutions-container").addClass('hidden')
$("#filterSolutions-container").addClass('hidden')
}, 100)
}
function byWordClick() {
let arg = $("#byWord-arg").val()
$("#message").html('Finding all solutions that contain "' + arg + '"...')
setTimeout(() => {
if (!solver.findSolutionsByWord(arg)) {
$("#message").html('No solutions exist that contain "' + arg + '".')
} else {
workingSolutions = solver.getSolutions()
$("#message").html('Found ' + workingSolutions.length + ' solutions.')
$("#reset-container").addClass('hidden')
$("#sampleSolutions-container").removeClass('hidden')
$("#filterSolutions-container").removeClass('hidden')
}
}, 100)
}
function bySampleClick() {
$("#message").html('Finding some solutions by word sample...')
setTimeout(() => {
let arg = $("#bySample-arg").val()
solver.findSolutionsBySample(arg)
workingSolutions = solver.getSolutions()
$("#message").html('Found ' + workingSolutions.length + ' solutions.')
$("#reset-container").addClass('hidden')
$("#sampleSolutions-container").removeClass('hidden')
$("#filterSolutions-container").removeClass('hidden')
}, 100)
}
function resetClick() {
workingSolutions = solver.getSolutions()
$("#message").html('Reset solutions.')
$("#reset-container").addClass('hidden')
}
function sampleSolutionsClick() {
$("#message").html('Getting some solutions...')
setTimeout(() => {
let arg = $("#sampleSolutions-arg").val()
let sample = sampleWorkingSolutions(arg)
if (sample.length > 0) {
drawSolutions(sample)
$("#message").html('Drew ' + sample.length + ' solutions.')
} else {
$("#message").html('No working solutions to sample.')
}
}, 100)
}
function filterSolutionsClick() {
$("#message").html('Filtering working solutions...')
setTimeout(() => {
let arg = $("#filterSolutions-arg").val()
let filtered = filterWorkingSolutions(arg)
if (filtered.length > 0) {
$("#message").html('Filtered solutions and got ' + filtered.length +' results.')
$("#reset-container").removeClass('hidden')
workingSolutions = filtered
} else {
$("#message").html('No working solutions contain "' + arg + '".')
}
}, 100)
}
/*
Util functions
*/
function drawSolutions(solutions) {
let html = ""
solutions.forEach(solution => {
html += solution + "<br \>"
})
$("#results").html(html)
}
function filterWorkingSolutions(word) {
let regex = new RegExp('\\b' + word + '\\b')
return workingSolutions.filter(solution => {
return regex.test(solution)
})
}
function sampleWorkingSolutions(size) {
return _.sampleSize(workingSolutions, size)
}