forked from GeenenTijd/CoderDojoJavascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
95 lines (80 loc) · 2.97 KB
/
main.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
include(['questions'], function (questions) {
'use strict';
var App = {
editor: null,
finishedTitle: 'Proficiat',
finishedText: '<p>Je hebt alle oefeningen gemaakt. Je kan met een volgende reeks aan de slag.</p>',
finishedTask: '<a href="index.html">Ga terug naar start</a>'
};
App.showCorrect = function (result) {
var elem = document.getElementById("result");
elem.className = 'correct';
elem.innerHTML = result;
document.getElementById("next").disabled = false;
};
App.showError = function (result) {
var elem = document.getElementById("result");
elem.className = 'wrong';
elem.innerHTML = result;
};
App.goNext = function () {
var elem = document.getElementById("result");
elem.className = '';
elem.innerHTML = '';
App.questions.saveQuestion();
App.startQuestion();
};
App.startQuestion = function () {
var question = App.questions.loadQuestion();
if (question) {
document.getElementById("title").innerHTML = question.title;
document.getElementById("description").innerHTML = question.description;
document.getElementById("task").innerHTML = question.task;
if (question.clearCode) {
if (typeof question.code !== 'undefined') {
App.editor.setValue(question.code);
} else {
App.editor.setValue('');
}
}
document.getElementById("next").disabled = !App.questions.nextAllowed;
App.editor.focus();
} else {
App.showEnd();
}
};
App.validate = function () {
if (questions.question) {
questions.question.validate(App.editor.getValue(), function (error, result) {
if (error) {
App.showError(error);
} else {
App.showCorrect(result);
}
App.editor.focus();
});
} else {
App.showEnd();
}
};
App.showEnd = function () {
App.editor.setValue('');
document.getElementById("next").disabled = true;
document.getElementById("title").innerHTML = App.finishedTitle;
document.getElementById("description").innerHTML = App.finishedText;
document.getElementById("task").innerHTML = App.finishedTask;
};
App.editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: 'javascript '
});
App.questions = questions;
// localStorage.clear();
document.getElementById("next").disabled = true;
document.getElementById("test").addEventListener("click", App.validate);
document.getElementById("next").addEventListener("click", App.goNext);
window.onpopstate = App.startQuestion;
App.startQuestion();
});