This repository was archived by the owner on Jun 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathbookfuncs.js
More file actions
252 lines (230 loc) · 8.28 KB
/
bookfuncs.js
File metadata and controls
252 lines (230 loc) · 8.28 KB
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
/**
*
* User: bmiller
* Original: 2011-04-20
* Date: 2019-06-14
* Time: 2:01 PM
* This change marks the beginning of version 4.0 of the runestone components
* Login/logout is no longer handled through javascript but rather server side.
* Many of the components depend on the runestone:login event so we will keep that
* for now to keep the churn fairly minimal.
*/
/*
Copyright (C) 2011 Brad Miller bonelake@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//
// Page decoration functions
//
function addReadingList() {
if (eBookConfig.readings) {
var l, nxt, path_parts, nxt_link;
let cur_path_parts = window.location.pathname.split("/");
let name =
cur_path_parts[cur_path_parts.length - 2] +
"/" +
cur_path_parts[cur_path_parts.length - 1];
let position = eBookConfig.readings.indexOf(name);
let num_readings = eBookConfig.readings.length;
if (position == eBookConfig.readings.length - 1) {
// no more readings
l = $("<div />", {
text: `Finished reading assignment. Page ${num_readings} of ${num_readings}.`,
});
} else if (position >= 0) {
// get next name
nxt = eBookConfig.readings[position + 1];
path_parts = cur_path_parts.slice(0, cur_path_parts.length - 2);
path_parts.push(nxt);
nxt_link = path_parts.join("/");
l = $("<a />", {
name: "link",
class: "btn btn-lg ' + 'buttonConfirmCompletion'",
href: nxt_link,
text: `Continue to page ${
position + 2
} of ${num_readings} in the reading assignment.`,
});
} else {
l = $("<div />", {
text:
"This page is not part of the last reading assignment you visited.",
});
}
$("#main-content").append(l);
}
}
function timedRefresh() {
var timeoutPeriod = 900000; // 75 minutes
$(document).bind("idle.idleTimer", function () {
// After timeout period send the user back to the index. This will force a login
// if needed when they want to go to a particular page. This may not be perfect
// but its an easy way to make sure laptop users are properly logged in when they
// take quizzes and save stuff.
if (location.href.indexOf("index.html") < 0) {
console.log("Idle timer - " + location.pathname);
location.href =
eBookConfig.app +
"/default/user/login?_next=" +
location.pathname +
location.search;
}
});
$.idleTimer(timeoutPeriod);
}
class PageProgressBar {
constructor(actDict) {
this.possible = 0;
this.total = 1;
if (actDict && Object.keys(actDict).length > 0) {
this.activities = actDict;
} else {
let activities = { page: 0 };
$(".runestone").each(function (idx, e) {
activities[e.firstElementChild.id] = 0;
});
this.activities = activities;
}
this.calculateProgress();
if (
window.location.pathname.match(
/.*(index.html|toctree.html|Exercises.html|Glossary.html|search.html)$/i
)
) {
$("#scprogresscontainer").hide();
}
this.renderProgress();
}
calculateProgress() {
for (let k in this.activities) {
if (k !== undefined) {
this.possible++;
if (this.activities[k] > 0) {
this.total++;
}
}
}
}
renderProgress() {
let value = 0;
$("#scprogresstotal").text(this.total);
$("#scprogressposs").text(this.possible);
try {
value = (100 * this.total) / this.possible;
} catch (e) {
value = 0;
}
$("#subchapterprogress").progressbar({
value: value,
});
if (!eBookConfig.isLoggedIn) {
$("#subchapterprogress>div").addClass("loggedout");
}
}
updateProgress(div_id) {
this.activities[div_id]++;
// Only update the progress bar on the first interaction with an object.
if (this.activities[div_id] === 1) {
this.total++;
let val = (100 * this.total) / this.possible;
$("#scprogresstotal").text(this.total);
$("#scprogressposs").text(this.possible);
$("#subchapterprogress").progressbar("option", "value", val);
if (
val == 100.0 &&
$("#completionButton").text().toLowerCase() ===
"mark as completed"
) {
$("#completionButton").click();
}
}
}
}
export var pageProgressTracker = {};
function handlePageSetup() {
var mess;
if (eBookConfig.useRunestoneServices) {
jQuery.get(eBookConfig.ajaxURL + "set_tz_offset", {
timezoneoffset: new Date().getTimezoneOffset() / 60,
});
}
if (eBookConfig.isLoggedIn) {
mess = `username: ${eBookConfig.username}`;
if (!eBookConfig.isInstructor) {
$("#ip_dropdown_link").remove();
}
$(document).trigger("runestone:login");
addReadingList();
// Avoid the timedRefresh on the grading page.
if (window.location.pathname.indexOf("/admin/grading") == -1) {
timedRefresh();
}
} else {
mess = "Not logged in";
$(document).trigger("runestone:logout");
}
$(".loggedinuser").html(mess);
pageProgressTracker = new PageProgressBar(eBookConfig.activities);
notifyRunestoneComponents();
}
function setupNavbarLoggedIn() {
$("#profilelink").show();
$("#passwordlink").show();
$("#registerlink").hide();
$("li.loginout").html(
'<a href="' + eBookConfig.app + '/default/user/logout">Log Out</a>'
);
}
$(document).bind("runestone:login", setupNavbarLoggedIn);
function setupNavbarLoggedOut() {
if (eBookConfig.useRunestoneServices) {
console.log("setup navbar for logged out");
$("#registerlink").show();
$("#profilelink").hide();
$("#passwordlink").hide();
$("#ip_dropdown_link").hide();
$("li.loginout").html(
'<a href="' + eBookConfig.app + '/default/user/login">Login</a>'
);
$(".footer").html("user not logged in");
}
}
$(document).bind("runestone:logout", setupNavbarLoggedOut);
function notifyRunestoneComponents() {
// Runestone components wait until login process is over to load components because of storage issues. This triggers the `dynamic import machinery`, which then sends the login complete signal when this and all dynamic imports are finished.
$(document).trigger("runestone:pre-login-complete");
}
// initialize stuff
$(function () {
if (eBookConfig) {
handlePageSetup();
} else {
if (typeof eBookConfig === "undefined") {
console.log(
"eBookConfig is not defined. This page must not be set up for Runestone"
);
}
}
});
// misc stuff
// todo: This could be further distributed but making a video.js file just for one function seems dumb.
window.addEventListener("load", function () {
// This function is needed to allow the dropdown search bar to work;
// The default behaviour is that the dropdown menu closes when something in
// it (like the search bar) is clicked
$(function () {
// Fix input element click problem
$(".dropdown input, .dropdown label").click(function (e) {
e.stopPropagation();
});
});
});