-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-ui.js
337 lines (300 loc) · 10.4 KB
/
core-ui.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const defaultNodeTypeIndex = 1;
const cnNumbers = ['一','二','三','四','五','六','七','八','九','十'];
const exerciseTypes = [
{ label: "全部", abbr: "全部" },
{ label: "課堂練習", abbr: "練習" },
{ label: "課後作業", abbr: "作業" }
];
const exercisesData = [
{
index: 0, cIndex: 1,
course: "OOP 簡介",
scriptSrc: "/public/exercises/exercise-001.js",
type: 1
},
{
index: 1, cIndex: 2,
course: "Object & Constructor",
scriptSrc: "/public/exercises/exercise-002.js",
type: 1
},
{
index: 0, cIndex: 2,
course: "Object & Constructor",
scriptSrc: "/public/homeworks/homework-001.js",
type: 2
},
{
index: 2, cIndex: 4,
course: "ES6:變數的解構賦值",
scriptSrc: "/public/exercises/exercise-003.js",
type: 1
},
{
index: 3, cIndex: 5,
course: "ES6:板模字串",
scriptSrc: "/public/exercises/exercise-004.js",
type: 1
},
{
index: 4, cIndex: 6,
course: "ES6:class 類別",
scriptSrc: "/public/exercises/exercise-005.js",
type: 1
},
{
index: 1, cIndex: 6,
course: "ES6:class 類別",
scriptSrc: "/public/homeworks/homework-002.js",
type: 2
},
{
index: 5, cIndex: 7,
course: "OOP : A - 封裝",
scriptSrc: "/public/exercises/exercise-006.js",
type: 1
},
{
index: 6, cIndex: 8,
course: "OOP : B - 繼承",
scriptSrc: "/public/exercises/exercise-007.js",
type: 1
},
{
index: 7, cIndex: 9,
course: "OOP : C - 多型",
scriptSrc: "/public/exercises/exercise-008.js",
type: 1
},
{
index: 8, cIndex: 10,
course: "OOP : 談談 THIS",
scriptSrc: "/public/exercises/exercise-009.js",
type: 1
}
];
const importJSFile = (_scriptSrc) => {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = _scriptSrc;
const callback = (e) => {
console.log(`///////////\n- 檔案 : ${_scriptSrc} 置入成功!\n///////////`);
}
script.onreadystatechange = callback;
script.onload = callback;
document.getElementsByTagName('head')[0].appendChild(script);
// return await import(_scriptSrc);
}
const fetchJSFileContent = async (_scriptSrc) => {
try {
// Fetch the JavaScript file
const response = await fetch(_scriptSrc);
if (!response.ok) {
throw new Error('Network response was not ok');
}
// Get the file content as text
const fileText = await response.text();
// Display the file text in the <pre> tag
return fileText;
} catch (error) {
console.error('There was a problem fetching the file:', error);
throw new Error(error);
}
}
function queryString(url) {
const str1 = url.split('?')[1];
const params = {};
if (str1) {
const pairs = str1.split('&');
for (const pair of pairs) {
const [key, value] = pair.split('=');
params[key] = decodeURIComponent(value
.replace(/\+/g, ' '));
}
}
return params;
}
function updateHeight(node) {
node.style.overflow = "hidden"; // Hide scrollbar
node.style.height = "auto"; // Reset height to calculate new scrollHeight
let newHeight = node.scrollHeight; // Get the actual content height
newHeight = newHeight;
// Apply smooth height transition
if (node.style.height !== newHeight + "px") {
smoothHeightTransition(node, newHeight);
}
}
function smoothHeightTransition(node, newHeight) {
requestAnimationFrame(() => {
// Set the initial height to start the transition from
node.style.height = node.offsetHeight + "px";
node.offsetHeight; // Trigger reflow to ensure the transition starts from the current height
requestAnimationFrame(() => {
// Set the final height to transition to
node.style.height = newHeight + "px";
});
});
}
function TypeSelectionList({
list, selectedIndex = 0
}) {
this.html = document.createElement("select");
this.list = list;
this.selectedIndex = selectedIndex;
this.insertUI = ()=>{
this.html.innerHTML = "";
this.list.forEach((opt, index)=>{
const option = document.createElement("option");
option.innerHTML = opt.label;
option.value = index;
if (index === this.selectedIndex) {
option.selected = true;
}
this.html.appendChild(option);
});
};
this.insertUI();
}
function Exercise({
index, cIndex,
course, type,
scriptSrc, loaded
}) {
this.loaded = false;
this.loading = false;
this.type = type;
this.html = document.createElement("div");
this.html.setAttribute("class", `exercise-node node-${type === 1? 'exercise':'homework'}`);
this.scriptSrc = scriptSrc;
this.scriptContent = '';
this.insertUI = ()=>{
this.html.innerHTML = "";
this.html.appendChild(
(function NodeLabel({ cIndex, course, index, type }) {
const node = document.createElement("div");
node.setAttribute("class", "node-label");
node.appendChild(
(function NodeP({ index, type }) {
const node = document.createElement("p")
const text = document.createTextNode(`${exerciseTypes[type].abbr}${cnNumbers[index]}`);
node.appendChild(text);
return node;
})({ index, type })
);
node.appendChild(
(function NodeC({ cIndex, course }) {
const node = document.createElement("p")
node.setAttribute("class", "label-desc");
const text = document.createTextNode(`課堂:${cIndex+1}. ${course}`);
node.appendChild(text);
return node;
})({ cIndex, course })
);
return node;
})({ cIndex, course, index, type })
);
// console.log(`this.loaded : ${this.loaded}`);
this.html.appendChild(
(function NodeLink({ scriptSrc, loaded }) {
const node = document.createElement("div");
if (!loaded) {
node.setAttribute("class", "node-link");
const button = document.createElement("a");
const label = document.createTextNode(`點擊讀取`);
button.appendChild(label);
node.appendChild(button);
button.addEventListener("click", (e)=>{
loadIntentHandler(scriptSrc);
});
} else {
node.setAttribute("class", "node-status");
const text = document.createElement("a");
const label = document.createTextNode(`來源檔案 : ${scriptSrc} →`);
text.setAttribute("href", scriptSrc);
text.setAttribute("target", "_blank");
text.appendChild(label);
node.appendChild(text);
}
return node;
})({ scriptSrc, loaded: this.loaded })
);
if (this.loaded) {
this.html.appendChild(
(function NodeScriptContent({ scriptContent }) {
const node = document.createElement("div");
node.setAttribute("class", "node-textarea");
const textArea = document.createElement("textarea");
textArea.value = scriptContent;
textArea.readOnly = true;
node.appendChild(textArea);
setTimeout(()=>{
updateHeight(textArea);
},10);
return node;
})({
scriptContent: this.scriptContent
})
);
}
}
const loadIntentHandler = async (scriptSrc)=>{
console.log(`///////////`);
console.log(`- 檔案 : ${scriptSrc} 開始讀取...`);
// const module = await importJSFile(scriptSrc);
const scriptContent = await fetchJSFileContent(scriptSrc);
setLoadedState({
loaded: true,
scriptContent: scriptContent
});
console.log(`- 檔案 : ${scriptSrc} 讀取完畢!`);
console.log(`///////////`);
importJSFile(scriptSrc);
}
const setLoadedState = ({
loaded, scriptContent
}) => {
this.loaded = loaded;
this.scriptContent = scriptContent;
this.insertUI();
}
if (loaded) {
loadIntentHandler(this.scriptSrc);
} else {
this.insertUI();
}
}
const fillExercisesList = (listOfObj, listTypeObj, target) => {
target.innerHTML = "";
target.appendChild(listTypeObj.html);
listOfObj.forEach((obj, index)=>{
if (parseInt(listTypeObj.selectedIndex) > 0) {
if (obj.type === listTypeObj.selectedIndex)
target.appendChild(obj.html);
} else {
// console.log('All IN');
target.appendChild(obj.html);
}
});
};
const { index: excLoadedIndex, type: typeIndex } = queryString(window.location.href);
const listTypeSelectionObject = new TypeSelectionList({
list: exerciseTypes,
selectedIndex: typeIndex !== undefined ? parseInt(typeIndex) : defaultNodeTypeIndex
});
const exercisesObject = exercisesData.map(({ index, type, ...data })=>{
return new Exercise({
...data,
index, type,
...(
parseInt(excLoadedIndex) === index &&
parseInt(typeIndex) === type ?
{ loaded : true } : null
)
});
});
const pool = document.querySelector("#exercise-list");
fillExercisesList(exercisesObject, listTypeSelectionObject, pool);
listTypeSelectionObject.html.addEventListener("change", ({target})=>{
listTypeSelectionObject.selectedIndex = parseInt(target.value);
fillExercisesList(exercisesObject, listTypeSelectionObject, pool);
});