summaryrefslogtreecommitdiff
path: root/scripts/lesson.js
blob: 5901b3515a2afac690201e33b016560d0fbfd4a9 (plain)
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
// DEPENDENCIES:
// * /scripts/global.js
function loadSubject(id) {
	fetch("/lessons/index.json")
		.then((response) => response.json())
		.then((data) => {
			var title = document.getElementById("title-banner");
			var subtitle = document.getElementById("subtitle");
			var container = document.getElementById("container");
			var banner = document.getElementById("img-dynamic-subject");

			console.log(data.subjects[id]);

			if (isLangIndonesia()) {
				title.innerHTML = data.subjects[id].titleId;
			} else {
				title.innerHTML = data.subjects[id].titleEn;
			}
			subtitle.innerHTML = data.subjects[id].splash;
			container.innerHTML = "";
			var bannerPath = data.subjects[id].bannerOriginal;
			banner.style.setProperty("background-image", `url(${bannerPath})`)

			var chapterLength = data.subjects[id].chapters.length;

			for (var i = 0; i < chapterLength; i++) {
				var chapterContainer = `
					<h1 id="bab${i}">${(isLangIndonesia()) ? "Bab" : "Chapter"} ${i + 1}: ${(isLangIndonesia()) ? data.subjects[id].chapters[i].titleId : data.subjects[id].chapters[i].titleEn}</h1>
					<div class="button-group-container">
				`

				var lessonLength = data.subjects[id].chapters[i].lessons.length;

					if (lessonLength > 1) {
						for (var j = 0; j < lessonLength; j++) {
						var l = data.subjects[id].chapters[i].lessons[j];
						var style = "";

						if (j == 0) {
							style = "button-grouped-top";
						} else if (j == lessonLength - 1) {
							style = "button-grouped-bottom";
						}
							
						chapterContainer += `
								<a class="button-grouped ${style}" href="${(l.type == "lesson") ? 'lesson' : 'quiz' }/?subject=${id}&chapter=${i}&id=${j}">
									<span class="button-char-icon">${(l.type == "lesson") ? '📚' :'✏️' }</span>
									<span>${(isLangIndonesia()) ? l.titleId : l.titleEn}</span>
								</a>
							`
						}
				} else {

				}

				chapterContainer += `\n</div>`

				container.innerHTML += chapterContainer;
			}
			// return data.subjects[id];
		}
	);
}

// DEPENDENCIES:
// * /scripts/toc.js
// * /scripts/global.js
// * markdown-it
function loadLesson(subject, chapter, lesson) {
	fetch("/lessons/index.json")
		.then((response) => response.json())
		.then((data) => {
			var title = document.getElementById("title-banner");
			var subtitle = document.getElementById("subtitle");
			var container = document.getElementById("container")

			var chipStatus = document.getElementById("chip-status");
			var chipAuthor = document.getElementById("chip-author");
			var chipGrade = document.getElementById("chip-grade");

			var lessonData = data.subjects[subject].chapters[chapter].lessons[lesson];

			console.log(lessonData);

			if (isLangIndonesia()) {
				title.innerHTML = lessonData.titleId;
				subtitle.innerHTML = `Bab ${parseInt(chapter) + 1}`;
				container.innerHTML = "";
				chipGrade.innerHTML = "Kelas " + lessonData.grade;
					if (lessonData.authors.length == 1) {
						chipAuthor.innerHTML = "Ditulis oleh " + lessonData.authors[0];
					} else {
						chipAuthor.innerHTML = "Ditulis oleh ";
						for (var i = 0; i < lessonData.authors.length - 1; i++) {
							chipAuthor.innerHTML += lessonData.authors[i] + ", ";
						}
						chipAuthor.innerHTML += "dan " + lessonData.authors[lessonData.authors.length - 1];
					}
			} else {
				title.innerHTML = lessonData.titleEn;
				subtitle.innerHTML = `Chapter ${parseInt(chapter) + 1}`;
				container.innerHTML = "";
				chipGrade.innerHTML = "Grade " + lessonData.grade;
					if (lessonData.authors.length == 1) {
						chipAuthor.innerHTML = "Written by " + lessonData.authors[0];
					} else {
						chipAuthor.innerHTML = "Written by ";
						for (var i = 0; i < lessonData.authors.length - 1; i++) {
							chipAuthor.innerHTML += lessonData.authors[i] + ", ";
						}
						chipAuthor.innerHTML += "and " + lessonData.authors[lessonData.authors.length - 1];
					}
			}
			// why the heck switch case not working
			// guess i'm too stupid for a switch case.
			// no i'm not yandev
			if (isLangIndonesia()) {
				if (lessonData.status == 0) {
					chipStatus.innerHTML = "❓ Belum Terverifikasi";
				} else if (lessonData.status == 1) {
					chipStatus.innerHTML = "✅ Terverifikasi";
				} else if (lessonData.status == 2) {
					chipStatus.innerHTML = "⚠️ Mengandung Informasi Tidak Akurat";
				}
			} else {
				if (lessonData.status == 0) {
					chipStatus.innerHTML = "❓ Not Verified";
				} else if (lessonData.status == 1) {
					chipStatus.innerHTML = "✅ Verified";
				} else if (lessonData.status == 2) {
					chipStatus.innerHTML = "⚠️ Contains Wrong Information";
				}
			}

			fetch((isLangIndonesia()) ? lessonData.pathId : lessonData.pathEn)
				.then((response) => response.text())
				.then((data) => {
					const md = markdownit({
						"html": true
					});
					data = data.replaceAll("!---(", `\n<details>\n`);
					data = data.replaceAll(")---!", `\n</details>\n`);
					data = data.replaceAll("#--(", `\n<summary>\n`);
					data = data.replaceAll(")--#", `\n</summary>\n`);
					container.innerHTML += md.render(data);
				}).then(() => {
					MathJax.typeset();
				}).then(() => {
					toc();
				});
		}
	);
	console.log(subject);
	console.log(chapter);
	console.log(lesson);
}

function loadQuiz(subject, chapter, lesson) {

}

function listSubjects() {
	fetch("/lessons/index.json")
		.then((response) => response.json())
		.then((data) => {
			var container = document.getElementById("subject-container");

			for (var i = 0; i < data.subjects.length; i++) {
				var card = `
					<div class="card">
						<a href="/subject/?id=${i}" class="link-card"></a>
						<img src="${data.subjects[i].banner}" alt="" class="card-img">
						<a href="/subject/?id=${i}">
							<h2>${(isLangIndonesia()) ? data.subjects[i].titleId : data.subjects[i].titleEn}</h2>
						</a>
					</div>
				`;

				container.innerHTML += card;
			}
		}
	);
}