Repository navigation
Expand file tree
/
Copy pathcourse-progress.js
More file actions
141 lines (132 loc) · 6.1 KB
/
Copy pathcourse-progress.js
File metadata and controls
141 lines (132 loc) · 6.1 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
// Reading progress kept in this browser only (localStorage, no account):
// new (not opened), seen (opened), done (marked as read by the reader).
// Keys stay independent of the hosting base and retain existing saved progress.
(() => {
const storageKey = 'course-progress';
const labels = {
en: { new: 'Not opened', seen: 'Opened', done: 'Read' },
ru: { new: 'Не открыта', seen: 'Открыта', done: 'Прочитана' },
};
const language = document.documentElement.lang?.startsWith('ru') ? 'ru' : 'en';
function load() {
try { return JSON.parse(localStorage.getItem(storageKey) ?? '{}') || {}; } catch { return {}; }
}
function save(progress) {
try { localStorage.setItem(storageKey, JSON.stringify(progress)); } catch { /* storage may be off */ }
}
const base = document.querySelector('meta[name="course-base"]')?.content ?? '/';
function articleKey(href) {
try {
const { pathname } = new URL(href, location.href);
if (!pathname.startsWith(base)) return null;
const slug = pathname.slice(base.length).replace(/^(?:en|ru)\//, '').replace(/^course\//, '').replace(/\/$/, '');
// Keep existing browser storage keys even though public URLs are shorter.
return slug ? `/course/${slug}/` : null;
} catch { return null; }
}
const progress = load();
const currentKey = document.querySelector('[data-course-progress-toggle]') ? articleKey(location.href) : null;
if (currentKey && !progress[currentKey]) {
progress[currentKey] = 'seen';
save(progress);
}
const stateOf = (key) => progress[key] ?? 'new';
function decorateSidebar() {
document.querySelectorAll('.sidebar-content ul a[href]').forEach((link) => {
const key = articleKey(link.href);
if (!key) return;
let mark = link.querySelector('.course-progress');
if (!mark) {
mark = document.createElement('span');
mark.className = 'course-progress';
link.prepend(mark);
}
const state = stateOf(key);
mark.dataset.state = state;
mark.title = labels[language][state];
mark.setAttribute('aria-label', labels[language][state]);
mark.setAttribute('role', 'img');
});
}
function setupToggle() {
const toggle = document.querySelector('[data-course-progress-toggle]');
if (!toggle || !currentKey) return;
const render = () => {
const done = stateOf(currentKey) === 'done';
toggle.setAttribute('aria-checked', String(done));
toggle.querySelector('.course-progress').dataset.state = done ? 'done' : 'seen';
};
toggle.addEventListener('click', () => {
progress[currentKey] = stateOf(currentKey) === 'done' ? 'seen' : 'done';
save(progress);
render();
decorateSidebar();
});
toggle.hidden = false;
render();
}
function decorateCoursePath() {
document.querySelectorAll('[data-course-articles]').forEach((card) => {
let articles;
try { articles = JSON.parse(card.dataset.courseArticles); } catch { return; }
if (!Array.isArray(articles)) return;
const done = articles.filter((key) => stateOf(key) === 'done').length;
card.querySelector('[data-course-read-count]').textContent = String(done);
card.querySelector('progress').value = done;
card.querySelector('.course-progress').dataset.state = done === 0 ? 'new' : done === articles.length ? 'done' : 'seen';
});
}
function decorateNextArticle() {
const link = document.querySelector('[data-course-next]');
if (!link) return;
let articles;
try { articles = JSON.parse(link.dataset.courseNext); } catch { return; }
const started = Object.values(progress).some((state) => state === 'seen' || state === 'done');
const startButton = document.querySelector('[data-course-start]');
const total = document.querySelector('[data-course-total]');
if (startButton) startButton.hidden = started;
if (total) total.hidden = !started;
const totalRead = document.querySelector('[data-course-total-read]');
const done = articles.filter((article) => stateOf(articleKey(article.href)) === 'done').length;
if (totalRead) totalRead.textContent = String(done);
const totalProgress = document.querySelector('[data-course-total-progress]');
if (totalProgress) {
totalProgress.setAttribute('aria-valuenow', String(done));
totalProgress.querySelectorAll('[data-course-section-articles]').forEach((segment) => {
let sectionArticles;
try { sectionArticles = JSON.parse(segment.dataset.courseSectionArticles); } catch { return; }
const sectionDone = sectionArticles.filter((key) => stateOf(key) === 'done').length;
segment.firstElementChild.style.width = `${sectionDone / sectionArticles.length * 100}%`;
});
}
const lastDone = articles.findLastIndex((article) => stateOf(articleKey(article.href)) === 'done');
const next = articles[lastDone + 1] ?? articles.find((article) => stateOf(articleKey(article.href)) !== 'done');
link.parentElement.hidden = !started || !next;
if (!next) return;
link.href = next.href;
link.querySelector('.pagination-title').textContent = next.title;
link.querySelector('.pagination-section').textContent = next.section;
}
function setupReset() {
const button = document.querySelector('[data-course-progress-reset]');
if (!button) return;
button.addEventListener('click', () => {
try { localStorage.removeItem(storageKey); } catch { /* storage may be off */ }
Object.keys(progress).forEach((key) => delete progress[key]);
decorateSidebar();
decorateCoursePath();
decorateNextArticle();
});
}
window.addEventListener('storage', (event) => {
if (event.key !== storageKey && event.key !== null) return;
Object.keys(progress).forEach((key) => delete progress[key]);
Object.assign(progress, load());
decorateSidebar();
decorateCoursePath();
decorateNextArticle();
});
const start = () => { decorateSidebar(); decorateCoursePath(); decorateNextArticle(); setupToggle(); setupReset(); };
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start);
else start();
})();