-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
272 lines (245 loc) · 9.54 KB
/
make.js
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
var root = document.querySelector("#root");
var info = [{type:"none"}];
var uinfo = [];
var lastFocused={id:"e0"};
var pnum=0;
var soltext;
function addEquation(f=true){
n = document.createElement("SPAN");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id));}*/
root.appendChild(n);
n.outerHTML = `<div class="rgba-indigo-strong form-control-sm answerb or" style="display: inline-block"><span id='e${info.length}' class='text-light' style="border:none !important;" onclick='lastFocused=this.parentElement;'> </span><button class="badge btn close" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></div>`;
document.getElementById("e"+info.length).click();
var answerMathField = MQ.MathField(document.querySelector("#e"+info.length), {
handlers: {}});
l =info.length;
info.push({type:"mq",
id: "e"+info.length,
elem: document.querySelector("#e"+info.length),
mathfield: answerMathField
});
uinfo.push({type:"mq",
id: "e"+l,
elem: document.querySelector("#e"+l),
mathfield: answerMathField
});
if(f){answerMathField.focus()}
return answerMathField;
};
function addDisplayEquation(f=true){
n = document.createElement("DIV");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.outerHTML = `<div class="form-control-sm answerb darken-3 cyan or mt-0 text-center" onclick="info[${info.length}].mathfield.focus()" ><span id="e${info.length}" style="border:none !important;" class="answerb form-control-sm text-light or" onclick="lastFocused = this.parentElement.parentElement;"></span><button class="badge btn close float-right" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></div>`;
var answerMathField = MQ.MathField(document.querySelector("#e"+info.length), {
handlers: {}});
l = info.length;
info.push({type:"dmq",
id: "e"+info.length,
elem: document.querySelector("#e"+info.length),
mathfield: answerMathField
});
uinfo.push({type:"dmq",
id: "e"+l,
elem: document.querySelector("#e"+l),
mathfield: answerMathField
});
n.onclick=`info[${info.length}].mathfield.focus()`;
if (f){
answerMathField.focus();}
return answerMathField;
};
function addText(){
n = document.createElement("span");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.outerHTML = `<span class="form-control-sm answerb my-2 input-sm rgba-green-strong or"><span contenteditable="true" id="e`+info.length+`" class=" or text-light" style="font-size:14pt" onfocus="lastFocused=this.parentElement;">  </span><button class="badge btn close" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></span>`;
document.getElementById("e"+info.length).focus();
l = info.length;
info.push({type:"text",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
};
function addProblem(){
pnum+=1;
n = document.createElement("LI");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.className = "card-body card no-def my-3 pop blue-grey text-light";
n.innerHTML = `<p><span id='q${info.length}' class='form-control-sm answerb blue-grey darken-1 or' ></span>$\ =\ $<span id='a${info.length}' class='form-control-sm answerb blue-grey darken-1 or' ></span><button class="badge btn close float-right" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></p>
<div class="form-group ">
<!--<label for="h1${info.length}">Hint 1</label>-->
<textarea class="form-control blue-grey darken-1 or text-light" placeholder="Hint 1" id="h1${info.length}" rows="3"></textarea>
</div>
<div class="form-group">
<!--<label for="h2${info.length}">Hint 2</label>-->
<textarea class="form-control blue-grey darken-1 or text-light " id="h2${info.length}" rows="3"></textarea>
</div>
<div class="form-group">
<!--<label for="s${info.length}">Solution</label>-->
<textarea class="form-control blue-grey darken-1 or text-light" id="s${info.length}" rows="3"></textarea>
</div>`;
n.id = "e"+info.length;
n.onclick='lastFocused=this;'
var answerMathField = MQ.MathField(document.querySelector("#q"+info.length), {
handlers: {}});
var answerMathField2 = MQ.MathField(document.querySelector("#a"+info.length), {
handlers: {}});
l = info.length;
info.push({type:"problem",
id: "e"+l,
elem: document.querySelector("#e"+l),
qfield:answerMathField,
afield: answerMathField2,
pnum:pnum,
h1field:("h1"+l),
h2field:("h2"+l),
sfield:("s"+l),
});
console.log(info);
lastFocused=n;
//added an answer space as well.
MathJax.typeset();
return {type:"problem",
id: "e"+l,
elem: document.querySelector("#e"+l),
qfield:answerMathField,
afield: answerMathField2,
pnum:pnum,
h1field:("h1"+l),
h2field:("h2"+l),
sfield:("s"+l),
};
}
function addBreak(){
n = document.createElement("hr");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.outerHTML=`<div class="row"><hr class="rgba-lime-strong col-11" id="e`+info.length+`" onclick="lastFocused=this.parentElement;"><button class="badge btn close float-right col" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button><span>`;
document.getElementById("e"+info.length).click();
l = info.length;
info.push({type:"break",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
uinfo.push({type:"break",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
};
function makeCourse(data){
var final = "<h2>"+document.querySelector("#ctitle").value+"</h2><p>";
var fscript = ``;
var runscript = [];
for (num in data){
obj = data[num];
if (obj.type == "text"){
e = document.createElement("p");
e.innerHTML = document.querySelector("#"+obj.id).innerHTML;
final += e.innerHTML
}
if (obj.type == "mq"){
e = document.createElement("p");
e.innerHTML = " $"+obj.mathfield.latex()+"$ ";
final += e.innerHTML;
}
if (obj.type == "dmq"){
e = document.createElement("p");
e.innerHTML = " $$"+obj.mathfield.latex()+"$$ ";
final += e.innerHTML;
}
if (obj.type =="break"){
e = document.createElement("BR");
//final+=e.outerHTML;
final += "</p><p>"
}
if (obj.type =="problem"){
final += `</p>
<li class="card card-body no-def my-3 pop">
<p>$\\displaystyle{ ${obj.qfield.latex()}= }$ <span id="answer${obj.pnum}" class="form-control-sm answerb"></span><svg id="tu${obj.pnum}" class="bi bi-hand-thumbs-up" width="2em" height="2em" style="display:none" viewBox="0 0 16 16" fill="var(--success)" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z"/>
</svg></p>
<!--Hints--><details><summary class="btn btn-outline-success btn-sm mt-2">Hint</summary> ${document.getElementById(obj.h1field).value}</details>
<details><summary class="btn btn-outline-success btn-sm mt-2">Hint</summary> ${document.getElementById(obj.h2field).value}</details>
<!--Solution--><details><summary class="btn btn-sm btn-outline-success mt-2">Show/Hide Solution</summary>
${document.getElementById(obj.sfield).value}
</details>
</li><p>`;
//initfield(obj.pnum,"",[obj.afield.latex()]);
fscript += `initfield(${obj.pnum},"",["${obj.afield.latex()}"]);`;
runscript.push({n:obj.pnum, ans:[obj.afield.latex()]});
console.log(runscript);
}
}
final += "</p>"
//fscript = "<script>"+fscript+"</script>"
document.querySelector("#out").innerHTML = final;
MathJax.typeset();
document.querySelector("#out2").innerText = final;
if (fscript!==""){
document.querySelector("#out3").innerHTML = fscript;chk=true} else {chk=false}
Prism.highlightAll();
for (a in runscript){
b = runscript[a];
initfield(b.n,"",b.ans);
}
htt=final;
ct = document.querySelector("#ctitle").value;
scr=fscript;
}
/* function make(data){
var final = "<p>";
for (num in data){
obj = data[num];
if (obj.type == "text"){
e = document.createElement("p");
e.innerHTML = document.querySelector("#"+obj.id).innerHTML;
final += e.innerHTML
}
if (obj.type == "mq"){
e = document.createElement("p");
e.innerHTML = " $"+obj.mathfield.latex()+"$ ";
final += e.innerHTML;
}
if (obj.type == "dmq"){
e = document.createElement("p");
e.innerHTML = " $$"+obj.mathfield.latex()+"$$ ";
final += e.innerHTML;
}
if (obj.type =="break"){
e = document.createElement("BR");
//final+=e.outerHTML;
final += "</p><p>"
}
}
final += "</p>"
return final;
} */
function rmFromInfo(id){
for (n in info){
b= info[n];
if (b.id==id){
info.splice(n,1);
return true
}
}
return false
}
function rmFromDOM(id){
var el = document.getElementById(id);
if (el.parentElement==root){el.remove()}
else if (el.parentElement.parentElement==root){el.parentElement.remove()}
else if (el.parentElement.parentElement.parentElement==root){el.parentElement.parentElement.remove()}
}