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 pathacfactory.js
More file actions
223 lines (210 loc) · 7.83 KB
/
acfactory.js
File metadata and controls
223 lines (210 loc) · 7.83 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
import { ActiveCode } from "./activecode.js";
import JSActiveCode from "./activecode_js.js";
import HTMLActiveCode from "./activecode_html.js";
import SQLActiveCode from "./activecode_sql.js";
import BrythonActiveCode from "./activecode_brython.js";
import LiveCode from "./livecode.js";
import {
TimedActiveCode,
TimedLiveCode,
TimedJSActiveCode,
TimedHTMLActiveCode,
TimedSQLActiveCode,
TimedBrythonActiveCode,
} from "./timed_activecode";
import "../../common/js/jquery.highlight.js";
export default class ACFactory {
constructor() {
this.foo = "bar";
}
static createActiveCode(orig, lang, addopts) {
var opts = {
orig: orig,
useRunestoneServices: eBookConfig.useRunestoneServices,
python3: eBookConfig.python3,
};
if (addopts) {
for (var attrname in addopts) {
opts[attrname] = addopts[attrname];
}
}
if (lang === undefined) {
lang = $(opts.orig).find("[data-lang]").data("lang");
}
var text_area = $(opts.orig).find("textarea")[0]
var python3_interpreter = $(text_area).attr("data-python3_interpreter");
if (opts.timed == true) {
if(python3_interpreter==="brython"){
return new TimedBrythonActiveCode(opts);
}
if (lang === "python") {
return new TimedActiveCode(opts);
} else if (
lang === "java" ||
lang === "cpp" ||
lang === "c" ||
lang === "python3"
) {
return new TimedLiveCode(opts);
} else if (lang === "javascript") {
return new TimedJSActiveCode(opts);
} else if (lang === "htmlmixed") {
return new TimedHTMLActiveCode(opts);
} else if (lang === "sql") {
return new TimedSQLActiveCode(opts);
} else {
return new TimedActiveCode(opts);
}
} else {
if ((lang ==="python3") && (python3_interpreter === "brython")){
return new BrythonActiveCode(opts);
}
else if (lang === "javascript") {
return new JSActiveCode(opts);
} else if (lang === "htmlmixed") {
return new HTMLActiveCode(opts);
} else if (lang === "sql") {
return new SQLActiveCode(opts);
} else if (
["java", "cpp", "c", "python3", "python2", "octave"].indexOf(
lang
) > -1
) {
return new LiveCode(opts);
} else {
// default is python
return new ActiveCode(opts);
}
}
}
// used by web2py controller(s)
static addActiveCodeToDiv(outerdivid, acdivid, sid, initialcode, language) {
var thepre, newac;
var acdiv = document.getElementById(acdivid);
$(acdiv).empty();
thepre = document.createElement("textarea");
thepre["data-component"] = "activecode";
thepre.id = outerdivid;
$(thepre).data("lang", language);
$(acdiv).append(thepre);
var opts = {
orig: thepre,
useRunestoneServices: true,
};
var addopts = {
sid: sid,
graderactive: true,
};
if (language === "htmlmixed") {
addopts["vertical"] = true;
}
newac = ACFactory.createActiveCode(thepre, language, addopts);
var savediv = newac.divid;
newac.divid = savediv;
newac.editor.setSize(500, 300);
setTimeout(function () {
newac.editor.refresh();
}, 500);
}
static createActiveCodeFromOpts(opts) {
return ACFactory.createActiveCode(opts.orig, opts.lang, opts);
}
static createScratchActivecode() {
/* set up the scratch Activecode editor in the search menu */
// use the URL to assign a divid - each page should have a unique Activecode block id.
// Remove everything from the URL but the course and page name
// todo: this could probably be eliminated and simply moved to the template file
if (eBookConfig.enableScratchAC == false) return;
var divid = eBookConfig.course + "_scratch_ac";
divid = divid.replace(/[#.]/g, ""); // in case book title has characters that will mess up our selectors
eBookConfig.scratchDiv = divid;
let stdin = "";
var lang = eBookConfig.acDefaultLanguage
? eBookConfig.acDefaultLanguage
: "python";
if (lang === "java" || lang === "cpp" || lang === "python3") {
stdin = `data-stdin="text for stdin"`;
}
// generate the HTML
var html = `<div id="ac_modal_${divid}" class="modal fade">
<div class="modal-dialog scratch-ac-modal">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Scratch ActiveCode</h4>
</div>
<div class="modal-body">
<div data-component="activecode" id=${divid}>
<div id=${divid}_question class="ac_question"><p>Use this area for writing code or taking notes.</p></div>
<textarea data-codelens="true" data-lang="${lang}" ${stdin}>
</textarea>
</div>
</div>
</div>
</div>
</div>`;
var el = $(html);
$("body").append(el);
el.on("shown.bs.modal show.bs.modal", function () {
el.find(".CodeMirror").each(function (i, e) {
e.CodeMirror.refresh();
e.CodeMirror.focus();
});
});
}
static toggleScratchActivecode() {
var divid = "ac_modal_" + eBookConfig.scratchDiv;
var div = $("#" + divid);
$(`#${eBookConfig.scratchDiv}`).removeClass("ac_section");
div.modal("toggle");
}
}
//
// Page Initialization
//
$(document).bind("runestone:login-complete", function () {
ACFactory.createScratchActivecode();
$("[data-component=activecode]").each(function () {
if ($(this).closest("[data-component=timedAssessment]").length == 0) {
// If this element exists within a timed component, don't render it here
try {
window.edList[this.id] = ACFactory.createActiveCode(
this,
$(this).find("textarea").data("lang")
);
} catch (err) {
console.log(`Error rendering Activecode Problem ${this.id}
Details: ${err}`);
}
}
});
if (loggedout) {
for (let k in window.edList) {
window.edList[k].disableSaveLoad();
}
} else {
for (let k in window.edList) {
window.edList[k].enableSaveLoad();
}
}
});
if (typeof window.component_factory === "undefined") {
window.component_factory = {};
}
window.component_factory.activecode = ACFactory.createActiveCodeFromOpts;
// This is the easiest way to expose this outside the module.
window.ACFactory = ACFactory;
// This seems a bit hacky and possibly brittle, but its hard to know how long it will take to
// figure out the login/logout status of the user. Sometimes its immediate, and sometimes its
// long. So to be safe we'll do it both ways..
var loggedout;
$(document).bind("runestone:logout", function () {
loggedout = true;
});
$(document).bind("runestone:logout", function () {
for (let k in window.edList) {
if (window.edList.hasOwnProperty(k)) {
window.edList[k].disableSaveLoad();
}
}
});