-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
73 lines (66 loc) · 2.53 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Doenet library demo</title>
<script src="https://unpkg.com/@doenet/beta"></script>
<link rel="stylesheet" href="https://unpkg.com/@doenet/beta/dist/main.css">
<script src="./main.js"></script>
</head>
<body>
<h1>Doenet library demo</h1>
<textarea id="textbox" rows="4" cols="50"></textarea>
<input type="checkbox" id="checkbox" name="checkbox"/>
<p>
<button id="increase" type="button">Increase progress</button>
</p>
<h2>How was this page built?</h2>
<p>In the <code><head></code>, this page includes
<pre>
<script src="https://unpkg.com/@doenet/beta"></script>
<link rel="stylesheet" href="https://unpkg.com/@doenet/beta/dist/main.css">
</pre>
in order to load the Doenet library.</p>
<p>
When the <code>DOMContentLoaded</code> event is fired, we run
<pre>
let worksheet = new window.doenet.Worksheet();
</pre>
to load the Doenet services. This triggers the GDPR consent form.</p>
<h2>What happens when I click the button?</h2>
<p>The handler for the <code>click</code> event on the button is:
<pre>
let p = 0;
if (worksheet.progress > 0.0)
p = worksheet.progress;
worksheet.progress = (1 + p)/2.0;
</pre>
Setting <code>worksheet.progress</code> updates the green bar in the corner, and also sends these updates to the gradebook.
</p>
<h2>What happens when I type in the box or tick the checkbox?</h2>
<p>
When the checkbox state changes, we make the corresponding change to the <code>worksheet.state</code>, which synchronizes with the server.
<pre>
let checkbox = document.getElementById("checkbox");
checkbox.addEventListener("input", function() {
worksheet.state.checked = checkbox.checked;
});
</pre>
Similarly when the <code><textarea></code> changes, we make the corresponding change to the <code>worksheet.state</code>. We do this in an ugly way just to demonstrate that you can also completely replace the <code>worksheet.state</code> and it will still sync with the server.
<pre>
let textbox = document.getElementById("textbox");
textbox.addEventListener("input", function() {
console.log(textbox.value);
worksheet.state = { textbox: textbox.value, checked: checkbox.checked };
});
</pre>
Finally, whenever the state changes on the server side, we make the corresponding change to the page.
<pre>
worksheet.addEventListener( 'state', function(event, state) {
if (state.textbox)
textbox.value = state.textbox;
checkbox.checked = state.checked;
});
<pre>
</p>
</body>
</html>