Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 227e2b8

Browse files
author
Brad Miller
committed
put progress bar handling into its own class.
1 parent 786f73c commit 227e2b8

2 files changed

Lines changed: 43 additions & 27 deletions

File tree

runestone/common/js/bookfuncs.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,53 @@ function timedRefresh() {
104104
$.idleTimer(timeoutPeriod);
105105
}
106106

107-
function calculateProgress() {
108-
let possible = 0;
109-
let total = 1;
110-
for (let k in eBookConfig.activities) {
111-
if (k !== undefined) {
112-
possible++;
113-
if (eBookConfig.activities[k] > 0) {
114-
total++;
107+
class PageProgressBar {
108+
109+
constructor(actDict) {
110+
this.possible = 0;
111+
this.total = 1;
112+
this.activities = actDict;
113+
this.calculateProgress();
114+
this.renderProgress()
115+
116+
}
117+
118+
calculateProgress() {
119+
for (let k in this.activities) {
120+
if (k !== undefined) {
121+
this.possible++;
122+
if (this.activities[k] > 0) {
123+
this.total++;
124+
}
115125
}
116126
}
117127
}
118-
return {possible: possible, total: total}
119-
}
120128

121129

122-
function addProgress() {
123-
let {possible, total} = calculateProgress()
124-
$("#scprogresstotal").text(total);
125-
$("#scprogressposs").text(possible);
126-
$( "#subchapterprogress" ).progressbar({
127-
value: 100 * total/possible
128-
});
129-
}
130+
renderProgress() {
131+
$("#scprogresstotal").text(this.total);
132+
$("#scprogressposs").text(this.possible);
133+
$( "#subchapterprogress" ).progressbar({
134+
value: 100 * this.total/this.possible
135+
});
136+
}
137+
138+
updateProgress(div_id) {
139+
this.activities[div_id]++;
140+
// We only update the progress bar on the first interaction with an object.
141+
if (this.activities[div_id] === 1) {
142+
this.total++;
143+
let val = 100 * this.total / this.possible;
144+
$("#scprogresstotal").text(this.total);
145+
$("#scprogressposs").text(this.possible);
146+
$("#subchapterprogress").progressbar("option","value", val)
147+
}
148+
}
130149

131-
function updateProgress(div_id) {
132-
eBookConfig.activities[div_id]++;
133-
let {possible, total} = calculateProgress();
134-
let val = 100 * total / possible;
135-
$("#scprogresstotal").text(total);
136-
$("#scprogressposs").text(possible);
137-
$("#subchapterprogress").progressbar("option","value", val)
138150
}
139151

152+
pageProgressTracker = {};
153+
140154
function handlePageSetup() {
141155

142156
let data = {timezoneoffset: (new Date()).getTimezoneOffset()/60 }
@@ -156,7 +170,7 @@ function handlePageSetup() {
156170
}
157171
$(".loggedinuser").html(mess);
158172

159-
addProgress();
173+
pageProgressTracker = new PageProgressBar(eBookConfig.activities);
160174
notifyRunestoneComponents();
161175
}
162176

runestone/common/js/runestonebase.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ RunestoneBase.prototype.logBookEvent = function (eventInfo) {
3838
null, 'json');
3939
}
4040
console.log("logging event " + JSON.stringify(eventInfo));
41-
updateProgress(eventInfo.div_id);
41+
pageProgressTracker.updateProgress(eventInfo.div_id);
4242
return post_return;
4343
};
4444

@@ -61,6 +61,8 @@ RunestoneBase.prototype.logRunEvent = function (eventInfo) {
6161
this.forceSave = true; }).bind(this))
6262
}
6363
console.log("running " + JSON.stringify(eventInfo));
64+
pageProgressTracker.updateProgress(eventInfo.div_id);
65+
6466
};
6567

6668
/* Checking/loading from storage */

0 commit comments

Comments
 (0)