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

Commit 0cbbcb9

Browse files
committed
Clean: make renderRunestoneComponent async.
1 parent 90d228e commit 0cbbcb9

2 files changed

Lines changed: 36 additions & 37 deletions

File tree

runestone/common/js/renderComponent.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function renderRunestoneComponent(componentSrc, whereDiv, moreOpts) {
1+
export async function renderRunestoneComponent(componentSrc, whereDiv, moreOpts) {
22
/**
33
* The easy part is adding the componentSrc to the existing div.
44
* The tedious part is calling the right functions to turn the
@@ -19,47 +19,46 @@ export function renderRunestoneComponent(componentSrc, whereDiv, moreOpts) {
1919
"component"
2020
);
2121
// Import the JavaScript for this component before proceeding.
22-
runestone_import(componentKind).then(() => {
23-
let opt = {};
24-
opt.orig = jQuery(`#${whereDiv} [data-component]`)[0];
25-
if (opt.orig) {
26-
opt.lang = $(opt.orig).data("lang");
27-
opt.useRunestoneServices = true;
28-
opt.graderactive = false;
29-
opt.python3 = true;
30-
if (typeof moreOpts !== "undefined") {
31-
for (let key in moreOpts) {
32-
opt[key] = moreOpts[key];
33-
}
22+
await runestone_import(componentKind);
23+
let opt = {};
24+
opt.orig = jQuery(`#${whereDiv} [data-component]`)[0];
25+
if (opt.orig) {
26+
opt.lang = $(opt.orig).data("lang");
27+
opt.useRunestoneServices = true;
28+
opt.graderactive = false;
29+
opt.python3 = true;
30+
if (typeof moreOpts !== "undefined") {
31+
for (let key in moreOpts) {
32+
opt[key] = moreOpts[key];
3433
}
3534
}
35+
}
3636

37-
if (typeof component_factory === "undefined") {
38-
alert(
39-
"Error: Missing the component factory! Clear you browser cache."
37+
if (typeof component_factory === "undefined") {
38+
alert(
39+
"Error: Missing the component factory! Clear you browser cache."
40+
);
41+
} else {
42+
if (
43+
!window.component_factory[componentKind] &&
44+
!jQuery(`#${whereDiv}`).html()
45+
) {
46+
jQuery(`#${whereDiv}`).html(
47+
`<p>Preview not available for ${componentKind}</p>`
4048
);
4149
} else {
42-
if (
43-
!window.component_factory[componentKind] &&
44-
!jQuery(`#${whereDiv}`).html()
45-
) {
46-
jQuery(`#${whereDiv}`).html(
47-
`<p>Preview not available for ${componentKind}</p>`
48-
);
49-
} else {
50-
let res = window.component_factory[componentKind](opt);
51-
if (componentKind === "activecode") {
52-
if (moreOpts.multiGrader) {
53-
window.edList[
54-
`${moreOpts.gradingContainer} ${res.divid}`
55-
] = res;
56-
} else {
57-
window.edList[res.divid] = res;
58-
}
50+
let res = window.component_factory[componentKind](opt);
51+
if (componentKind === "activecode") {
52+
if (moreOpts.multiGrader) {
53+
window.edList[
54+
`${moreOpts.gradingContainer} ${res.divid}`
55+
] = res;
56+
} else {
57+
window.edList[res.divid] = res;
5958
}
6059
}
6160
}
62-
});
61+
}
6362
}
6463

6564
export function createTimedComponent(componentSrc, moreOpts) {

runestone/selectquestion/js/selectone.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export default class SelectOne extends RunestoneBase {
193193
}
194194
///////////////////////////
195195
// just render this component on the page in its usual place
196-
renderRunestoneComponent(htmlsrc, selectorId, {
196+
await renderRunestoneComponent(htmlsrc, selectorId, {
197197
selector_id: selectorId,
198198
useRunestoneServices: true,
199199
});
@@ -283,7 +283,7 @@ export default class SelectOne extends RunestoneBase {
283283
async toggleSet(parentID, selectedQuestion, htmlsrc) {
284284
var selectorId = parentID + "-toggleSelectedQuestion";
285285
document.getElementById(selectorId).innerHTML = ""; // need to check whether this is even necessary
286-
let res = renderRunestoneComponent(htmlsrc, selectorId, {
286+
await renderRunestoneComponent(htmlsrc, selectorId, {
287287
selector_id: selectorId,
288288
useRunestoneServices: true,
289289
});
@@ -294,7 +294,7 @@ export default class SelectOne extends RunestoneBase {
294294
selectedQuestion,
295295
{}
296296
);
297-
let response = await fetch(request);
297+
await fetch(request);
298298
$("#component-preview").html("");
299299
$("#" + parentID).data("toggle_current", selectedQuestion);
300300
}

0 commit comments

Comments
 (0)