-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
463 lines (421 loc) · 12.7 KB
/
render.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// render.js - frontend js for the testYourself flashcard game
//
// author: Tom Ladendorf (tladendo)
/* OBJECTS! */
// Node for use in the Linked List
function Node(value) {
this.value = value;
this.prev = null;
this.next = null;
}
Node.prototype.setNext = function(node) {
if (node == null) {
this.next = null;
return false;
} else {
this.next = node;
node.prev = this;
}
}
Node.prototype.setPrev = function(node) {
if (node == null) {
this.prev = null;
return false;
} else {
this.prev = node;
node.next = this;
}
}
Node.prototype.remove = function() {
if (this.prev != null) {
this.prev.setNext(this.next);
}
if (this.next != null) {
this.next.setPrev(this.prev);
}
}
// Linked List implementation
function LL() {
this.length = 0;
this.head = null;
this.tail = null;
}
function LL(node) {
this.head = node;
this.tail = node;
this.length = 1;
}
LL.prototype.add = function(node) {
if (this.length == 0) {
this.head = node;
this.tail = node;
this.length = 1;
}
else {
this.tail.setNext(node);
this.tail = node;
this.length++;
}
}
LL.prototype.toString = function() {
var ans = "";
var currentNode = this.head;
while (currentNode != null) {
ans += currentNode.value + ", ";
currentNode = currentNode.next;
}
return ans;
}
LL.prototype.shuffle = function() {
var currentNode = this.head;
var arr = [];
while (currentNode != null) {
arr.push(currentNode.value);
currentNode = currentNode.next;
}
for (var i = 0; i < this.length; i++) {
var j = Math.floor(Math.random() * this.length);
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
this.head = new Node(arr[0]);
currentNode = this.head;
for (var i = 1; i < this.length; i++) {
var newNode = new Node(arr[i]);
currentNode.setNext(newNode);
currentNode = newNode;
}
this.tail = currentNode;
}
// Card object that represents flashcards
function Card() {}
function Card(question, answer) {
this.question = question;
this.answer = answer;
this.current = question;
this.other = answer;
this.onQuestion = true;
}
Card.prototype.flip = function() {
if (this.onQuestion == true) {
this.current = this.answer;
this.other = this.question;
this.onQuestion = false;
} else {
this.current = this.question;
this.other = this.answer;
this.onQuestion = true;
}
}
// Master object takes the place of a global state variable
// Default constructor pulls from divs on page
function Master() {
var master = this;
$("div.pair").each(
function() {
var kids = $(this).children();
var question = $(kids[0]).text();
var answer = $(kids[1]).text();
master.add(new Card(question, answer));
});
master.displayCurrent();
master.flipped = false;
}
Master.prototype.init = function(card) {
this.currentCard = card;
this.currentCardNode = new Node(card);
this.cardList = new LL(this.currentCardNode);
this.length = 1;
this.index = 0;
}
Master.prototype.update = function() {
// first, let's clean this object up
this.currentCard = null;
this.currentCardNode = null;
this.cardList = null;
this.length = null;
this.index = null;
// next, let's initialize everything from and bring in the new divs
var master = this;
$("div.pair").each(
function() {
var kids = $(this).children();
var question = $(kids[0]).text();
var answer = $(kids[1]).text();
master.add(new Card(question, answer));
});
master.displayCurrent();
}
Master.prototype.add = function(card) {
if (this.length == 0 || this.length == null) {
this.init(card);
} else {
this.cardList.add(new Node(card));
this.length++;
}
}
Master.prototype.shuffle = function() {
this.cardList.shuffle();
this.currentCardNode = this.cardList.head;
this.currentCard = this.currentCardNode.value;
this.index = 0;
this.displayCurrent();
}
Master.prototype.permanentlyRemove = function() {
if (this.tableName == "DEFAULT_SET") return;
//$.ajax({type: 'POST', url: 'delcard.cgi?tablename=' + this.tableName + '&question=' + this.currentCard.question, async: false, success: function() { }});
$.post("delcard.cgi", "question=" + this.currentCard.question + "&tablename=" + this.tableName, function(data) { });
this.deleteFromSession();
}
Master.prototype.deleteFromSession = function() {
// $.ajax({type: 'POST', url: 'delcard.cgi?tablename=' + this.tableName + '&question=' + this.currentCard.question, async: false, success: function(text) { ans = $(text); }});
if (this.length == 0) return 0;
this.length--;
this.cardList.length--;
var next = this.currentCardNode.next;
var prev = this.currentCardNode.prev;
if (prev != null) {
prev.setNext(next);
this.cardList.head = next;
} else {
next.setPrev(prev);
}
if (next == null) {
this.currentCardNode = prev;
this.cardList.tail = prev;
} else {
this.currentCardNode = next;
}
this.currentCard = this.currentCardNode.value;
this.displayCurrent();
}
Master.prototype.displayCurrent = function() {
if (this.index == this.length) {
this.index--;
}
if (this.flipped) {
$("#display").text(this.currentCard.other);
} else {
$("#display").text(this.currentCard.current);
}
$("#cardNumber").text(this.index + 1);
}
Master.prototype.flip = function() {
this.currentCard.flip();
this.displayCurrent();
}
Master.prototype.advance = function() {
if (this.index < this.length - 1) {
this.index++;
this.currentCardNode = this.currentCardNode.next;
this.currentCard = this.currentCardNode.value;
} else {
// do nothing
}
}
Master.prototype.retreat = function() {
if (this.index > 0) {
this.index--;
this.currentCardNode = this.currentCardNode.prev;
this.currentCard = this.currentCardNode.value;
} else {
// do nothing
}
}
Master.prototype.displayNext = function() {
this.advance();
this.displayCurrent();
}
Master.prototype.displayPrev = function() {
this.retreat();
this.displayCurrent();
}
Master.prototype.flipAll = function() {
if (this.flipped) {
this.flipped = false;
} else {
this.flipped = true;
}
this.displayCurrent();
}
Master.prototype.actionInit = function() {
var master = this;
$("#card").click(function() { master.flip(); });
$("#next").click(function() { master.displayNext(); });
$("#prev").click(function() { master.displayPrev(); });
$("#setAside").click(function() { master.deleteFromSession(); });
$("#delete").click(function() { master.permanentlyRemove(); });
$("#flipAll").click(function() { master.flipAll(); });
$("#shuffle").click(function() { master.shuffle(); });
}
Master.prototype.dismantle = function() {
$("#card").unbind(this.flip);
$("#next").unbind(this.displayNext);
$("#prev").unbind(this.displayPrev);
}
/* END OBJECTS */
/* OTHER IMPORTANT FUNCTIONS */
// this function is tied to the "SELECT ANOTHER SET" button
function displaySelectAnother(ev) {
// FIXME: this hack
global.rightButtonsContainer = $("#rightButtonsContainer");
$("#selectAnotherMenu").css("display", "inline");
$("#rightContainer").children().remove();
$("#selectAnotherMenu").css("display", "inline");
$("#rightContainer").append($("#selectAnotherMenu"));
var selectButton = "<a class='button' id='selectSubmitButton'>SELECT</a>";
var cancelButton = "<a class='button' id='cancelSubmitButton'>CANCEL</a>";
$("#rightContainer").append($("<br />")).append($("<br />")).append($(selectButton));
$("#rightContainer").append($("<br />")).append($(cancelButton));
// add event listener to select button
$("#selectSubmitButton").click(ev.data, selectNewSet);
$("#cancelSubmitButton").click(ev.data, cancelSelectNewSet);
}
function cancelSelectNewSet(ev) {
$("body").append($("#selectAnotherMenu").css("display", "none"));
$("#rightContainer").children().remove();
$("#rightContainer").append(global.rightButtonsContainer);
listenRight(ev.data);
}
// this function is tied to the "SELECT ANOTHER SET" > "SELECT" button
function selectNewSet(ev) {
var master = ev.data
// As a safety/debugging measure, get rid of all the old cards
$("div.pair").remove();
// Make a request for the cards from the desired set and store them in a variable
var select = $("#selectAnotherMenu").val();
master.tableName = select;
// Will return a div jQuery object
var ans = {};
$.ajax({type: 'GET', url: 'dbget.cgi?table=' + select, async: false, success: function(text) { ans = $(text); }});
function add(elt) {
$("body").append(elt);
}
ans.children().each(function() { add($(this)); });
master.update();
global = master;
cancelSelectNewSet(master);
/*
// using "global" is a hack. fix it
global.ans = ansObj;
global.reset();
//var p = $.ajax({type: 'POST', url: 'http://www.tomladendorf.com/flashcards/addcard.cgi', data: "first_name=Tom&last_name=Ladendorf"});
*/
}
// displays input when "ADD A NEW CARD" is clicked
function displayInput(ev) {
if (ev.data.tableName == "DEFAULT_SET") return;
$("#buttons a:last-child").remove();
var form = "<form id='addForm'>Q: "
+ "<input type='text' id='questionField' /><br />" +
"A: <input type='text' id='answerField' /></form>";
$("#buttons").append($("<div id='addContainer'></div>"));
$("#addContainer").append($(form));
$("#addContainer").append($("<a id='newCardSubmit' class='button'>" +
"ADD NEW CARD</a>"));
$("#newCardSubmit").click(ev.data, postInput);
$("#addContainer").append($("<span> </span>"));
$("#addContainer").append($("<a id='cancel' class='button'>CANCEL</a>"));
$("#cancel").click(ev.data, cancel);
}
// posts input for a new card
function postInput(ev) {
//var p = $.ajax({type: 'POST', url: 'http://www.tomladendorf.com/flashcards/addcard.cgi', data: "first_name=Tom&last_name=Ladendorf"});
var question = $("#questionField").val();
var answer = $("#answerField").val();
var master = ev.data;
var back = $.post("addcard.cgi", "question=" + question + "&answer=" + answer + "&id=" + master.length + "&tablename=" + master.tableName, function(data) {
//alert("DATA: " + data);
});
var newPair = "<div class='pair'><div class='question'>" + question + "</div><div class='answer'>" + answer + "</div></div>";
$("body").append($(newPair));
master.add(new Card(question, answer));
$("#addContainer").remove();
$("#buttons").append($("<a id='add' class='button'>ADD A NEW CARD</a>"));
$("#add").click(ev.data, displayInput);
$("body").append($(back));
}
// cancels adding a new card
function cancel(ev) {
$("#addContainer").remove();
}
function createNewSet(ev) {
// FIXME: this hack
global.rightButtonsContainer = $("#rightButtonsContainer");
$("#rightContainer").children().remove();
var form = "<form id='addForm'>NAME: "
+ "<input type='text' id='tableName' /><br />" +
"Q: <input type='text' id='questionField' /><br />" +
"A: <input type='text' id='answerField' /></form>";
$("#rightContainer").append($("<div id='createContainer'></div>"));
$("#createContainer").append($(form));
$("#createContainer").append($("<a id='newSetSubmit' class='button'>" +
"ADD NEW SET</a>"));
$("#newSetSubmit").click(ev.data, postSet);
$("#createContainer").append($("<span>  </span>"));
$("#createContainer").append($("<a id='cancel' class='button'>CANCEL</a>"));
$("#cancel").click(ev.data, cancelCreate);
}
function postSet(ev) {
var tablename = $("#tableName").val();
var question = $("#questionField").val();
var answer = $("#answerField").val();
addOption(tablename);
$.post("createset.cgi", "question=" + question + "&id=1&answer=" + answer + "&tablename=" + tablename, function(data) { });
cancelCreate(ev.data);
}
function cancelCreate(ev) {
$("#rightContainer").children().remove();
$("#rightContainer").append(global.rightButtonsContainer);
listenRight(ev.data);
}
function deleteCurrentSet(ev) {
if (ev.data.tableName == "DEFAULT_SET") {
return;
}
console.log(ev.data);
var master = ev.data;
$.post("delset.cgi", "tablename=" + master.tableName, function(data) { });
var msg = "Set deleted. Please select another!";
var card = new Card(msg, msg);
var node = new Node(card);
node.setNext(node);
ev.data.currentCardNode = node;
ev.data.currentCard = card;
ev.data.displayCurrent();
removeSetOption(ev.data.tableName);
}
function removeSetOption(op) {
$("#selectAnotherMenu").children().each(function() {
if ($(this).val() == op) {
$(this).remove();
}
});
}
function addOption(op) {
var str = '<option value="' + op + '">' + op + '</option>';
$("#selectAnotherMenu").append($(str));
}
function listenRight(master) {
// TODO: fix this hack
if (master == undefined) {
master = global;
}
$("#selectAnotherButton").click(master, displaySelectAnother);
$("#createNewButton").click(master, createNewSet);
$("#deleteSetButton").click(master, deleteCurrentSet);
}
/* END ADDL FUNCTIONS */
// for debugging purposes
var global = {};
// This is where everything happens
$("document").ready(function() {
// Create the master object and pull in all the existing divs
var master = new Master();
master.actionInit();
global = master;
master.tableName = "DEFAULT_SET";
$("#add").click(master, displayInput);
listenRight(master);
});