-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtabs.js
297 lines (245 loc) · 7.53 KB
/
tabs.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Code for loading the left-side theorems pane -- specifically
* the tabs and appending new theorems in the list of given
* theorems..
*/
var selectedThm = null; // the currently selected theorem (DOM element)
var selectedSave = null;
var box = null; // the selection box
var proof;
/*
* Load a saved proof.
*/
function loadProof(save) {
givens = save.givens;
goals = save.goals;
steps = save.steps;
points = save.points;
lineSegments = save.lineSegments;
angles = save.angles;
triangles = save.triangles;
congruences = save.congruences;
triangleCongruences = save.triangleCongruences;
proof = save;
addAllSteps(steps);
refreshGivens();
addGoal();
setImage(save.image);
}
function setImage(img) {
var frame = sel('#results-content');
if (frame && img) {
img.style.marginTop = '10px';
frame.appendChild(img);
}
}
/*
* Loads the theorems into the theorem sidebar
*/
function loadTheorems() {
for (var i = 0; i < theoremList.length; i++) {
addTheorem(theoremList[i]);
};
// loadProof(makeExercise2());
};
// for adding a theorem to the 'current theorems' tab
function addTheorem(thm) {
var currThms = sel("#curr-theorems-content");
var newThm = document.createElement("div");
// pass in theorem info
newThm.innerHTML = thm.shortName;
// change the selected theorem to the one clicked
newThm.onclick = function() {
// reset selected divs
if (selectedThm) sel("#sel-thm").id = "";
// apply selected div id to the one clicked on
selectedThm = this.innerHTML;
this.id = "sel-thm";
}
// set class to apply CSS, add to the div
newThm.setAttribute("class","theorem-li");
currThms.appendChild(newThm);
}
function checkInputs(inputs, numInputs, objList, theorem) {
console.log(inputs);
filteredInputs = [];
for (var i = 0; i < inputs.length; i += 1) {
if (inputs[i])
filteredInputs.push(i);
};
console.log(filteredInputs);
if (!(filteredInputs.length === numInputs)) {
alert("Not the right number of inputs for this theorem!");
} else {
var objs = [];
for (var i = 0; i < numInputs; i += 1) {
objs.push(objList[filteredInputs[i]]);
}
if (!(theorem.checkConditions(objs))) {
alert("The theorem doesn't apply here!");
} else {
theorem.applyResults(objs);
addStep(theorem.contents());
refreshGivens();
}
};
box.remove();
}
/*
* Applies the theorem
*/
function applyTheorem(theorem) {
// Find theorem
var thm;
for (var i = 0; i < theoremList.length; i += 1) {
var th = theoremList[i];
if (th.shortName === theorem) {
thm = th;
}
}
var options = thm.getInput()[0];
var numOptions = thm.getInput()[1];
box = new CheckDialog('selectionBox', options,
function(inputs) { checkInputs(inputs, numOptions, options, thm) });
box.open();
}
/*
* Adds a step to the proof bench
*/
function addStep(step) {
var currSteps = sel("#curr-steps");
var newStep = document.createElement('div');
newStep.innerHTML = step;
newStep.setAttribute('class', 'step-li');
currSteps.appendChild(newStep);
}
function addAllSteps(steps) {
sel("#curr-steps").innerHTML = '';
for (var i = 0; i < steps.length; i++) {
addStep(steps[i]);
}
}
/*
* Adds the goal
*/
function addGoal() {
var goalPane = sel('#results-content');
goalPane.innerHTML = '';
for (var i = 0; i < goals.length; i += 1) {
goalPane.innerHTML += goals[i].toString() + '<br>';
};
};
/*
* Refreshes the Given pane if there are new things proven
*/
function refreshGivens() {
var givenPane = sel('#given-content');
givenPane.innerHTML = '';
for (var i = 0; i < givens.length; i += 1) {
givenPane.innerHTML += givens[i].toString() + '<br>';
};
if (!proof.complete && proof.proofComplete()) {
alert("Congratulations, you have proved the theorem!");
proof.complete = true;
};
};
/*
* sets handlers to the theorems so that people can see what they
* selected, and so that the program knows what is selected
*/
function setHandlers() {
// ASSIGN HANDLERS:
var currThmsButton = sel("button#curr-theorems");
var currThms = sel("#curr-theorems-content");
// if clicking current thms button, toggle
currThmsButton.onclick = function() {
currThms.style.display = "block";
builtThms.style.display = "none";
}
var builtThmsButton = sel("button#built-theorems");
var builtThms = sel("#built-theorems-content");
// if clicking built thms button, toggle
builtThmsButton.onclick = function() {
currThms.style.display = "none";
builtThms.style.display = "block";
}
var proofArea = sel("#proof-bench");
// clicking on the build area will trigger the
// applyTheorem() method of the selected theorem
proofArea.onclick = function() {
applyTheorem(selectedThm);
}
setProveTheoremHandlers();
}
setHandlers();
// PROVE THEOREMS TAB -- SAVES //
var saves = new Array(); // idk?
// load the initial exercises into the prove theorems tab
function initSaves() {
console.log(makeExercise1);
console.log(makeExercise2);
var ex1 = makeExercise1();
var ex2 = makeExercise2();
saves.push(ex1);
saves.push(ex2);
refreshSaves();
}
function saveCurrentProof() {
if (!proof) // if no proof to save, do nothing
return; // TODO maybe prompt the user to "load a proof before saving"
// or disable the save button visually
var currSteps = sel('#curr-steps').childNodes;
var steps = new Array(); // to hold the steps
// load all steps into steps Array
for (var i = 0; i < currSteps.length; i++) {
steps.push(currSteps[i].innerHTML);
}
var newSave = new Save(givens, goals, steps, points, lineSegments,
angles, triangles, congruences, triangleCongruences);
newSave.name = proof.name;
newSave.image = sel('#results-content').childNodes[2] || undefined;
newSave.proofComplete = proof.proofComplete;
if (proof.hasOwnProperty('complete')) {
newSave.complete = true;
}
var flag = false;
for (var k = 0; k < saves.length; k += 1) {
if (saves[k].name === proof.name) {
saves[k] = newSave;
flag = true;
};
};
if (!flag) {
saves.push(newSave);
}
refreshSaves();
}
function refreshSaves() {
var list = sel('#built-theorems-content');
list.innerHTML = '';
selectedSave = null;
for (var i = 0; i < saves.length; i++) {
var save = saves[i];
var pfSave = document.createElement('div');
pfSave.innerHTML = save.name;
pfSave.save = save; // save inside the document object
if (save.hasOwnProperty('complete')) {
pfSave.innerHTML = '\u2713 ' + pfSave.innerHTML;
}
pfSave.setAttribute("class","save-li");
// when we click on the save, load the save data
pfSave.onclick = function() {
// reset selected divs
if (selectedSave) sel("#sel-save").id = "";
// apply selected div id to the one clicked on
selectedSave = this.innerHTML;
this.id = "sel-save";
loadProof(this.save);
}
list.appendChild(pfSave);
}
}
function setProveTheoremHandlers() {
var saveButton = sel('button#save')
saveButton.onclick = saveCurrentProof;
}