-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpodcast7nav.js
More file actions
171 lines (170 loc) · 6.04 KB
/
Copy pathpodcast7nav.js
File metadata and controls
171 lines (170 loc) · 6.04 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
let activeEpisode, title, audio;
let currentTime;
let Programme = {};
let Page = {};
let chapters = {};
let prog = 'NONE';
let pageTitle = 'ALL';
const querystring = location.search;
if (querystring != '') {
const params = (new URL(document.location)).searchParams;
const pgProg = params.get('prog');
prog = pgProg ? pgProg : prog;
const pgPage = params.get('page');
pageTitle = pgPage ? pgPage : pageTitle;
}
const li_podcasts = (podcasts, pid) => podcasts
.map(podcast => ` <li><a href="podcast.html?prog=${pid}&page=${podcast.title}">${podcast.title} (${podcast.episodes})</a></li>`).join('\n');
const li_years = (years, pid) => years.map(year => ` <li>
<a href="#">${year.name}</a>
<ul class="nav-dropdown">
${li_podcasts(year.podcasts, pid)}
</ul>
</li>`).join('\n');
const indexpage = (programme, pid) => `
<div class="nav-container">
<div class="brand">
<img src="https://podcast.rthk.hk/podcast/upload_photo/item_photo/1400x1400_${pid}.jpg" width="auto" height="70" />
<div class="subtitle">${programme.name}<br />收聽優化版</div>
</div>
<nav>
<div class="nav-mobile">
<a class="active" id="nav-toggle" href="#!"><span></span></a>
</div>
<ul class="nav-list">
${li_years(programme.years, pid)}
</ul>
</nav>
</div>
`;
document.addEventListener("DOMContentLoaded", function(event) {
if (pageTitle === 'ALL') {
myInitIndexPage();
} else { myInitChapters();
}
});
function myInitIndexPage() {
fetch(`${prog}.json`)
.then(response => response.json())
.then(data => {
Programme = data;
if (Programme.pages.length === 1) {
window.location = `podcast.html?prog=${prog}&page=${Programme.pages[0].title}`;
}
document.title = Programme.name;
document.getElementById('navigation').innerHTML = indexpage(Programme, prog);
ProcessMenu();
document.getElementById('nav-toggle').onclick = function () {
this.classList.toggle('active');
};
document.getElementById('nav-toggle').addEventListener('click', function () {
document.querySelectorAll('nav ul').forEach(el => toggle(el));
});
});
}
const media = {};
media['mp4'] = `<video id="audio" width="640" height="480" controls>
<source src="" type="video/mp4">
Your browser does not support the video tag.
</video>
`;
media['mp3'] = `<audio id="audio" preload="auto" tabindex="0" controls="">
<source type="audio/mp3" src="">
Sorry, your browser does not support HTML5 audio.
</audio>
`;
media['m4a'] = `<audio id="audio" preload="auto" tabindex="0" controls="">
<source type="audio/x-m4a" src="">
Sorry, your browser does not support HTML5 audio.
</audio>
`;
function myInitChapters() {
const optChapter = (chapter, index) => `<li><a href="javascript:gotoChapter(${index})">${chapter.caption}</a></li>`;
const optIndexHtml = `<li><a href="podcast.html?prog=${prog}">返 回 前 目 錄</a></li>`;
fetch(`${prog}.json`)
.then(response => response.json())
.then(data => {
Programme = data;
Page = Programme.pages.filter(page => page.title === pageTitle)[0];
chapters = Page.podcasts;
document.title = Page.title;
document.getElementById('myChapterList').innerHTML=`${optIndexHtml}\n${chapters.map((c,i) => optChapter(c,i)).join('\n')}`;
document.getElementById('media').innerHTML = `${media[chapters[0].url.slice(-3)]}`;
ProcessMenu();
document.getElementById('nav-toggle').onclick = function () {
this.classList.toggle('active');
};
document.getElementById('nav-toggle').addEventListener('click', function () {
document.querySelectorAll('nav ul').forEach(el => toggle(el));
});
audio = document.getElementById('audio');
audio.onpause = function (e) {
localStorage.setItem('currentTime'+pageTitle, audio.currentTime);
};
audio.onended = nextChapter;
const chapter = getLastChapter();
gotoChapter(chapter);
});
}
function prevChapter() {
currentTime = 0.0;
const idx = activeEpisode - 1;
let i = idx - 1;
i = (i === -1) ? (chapters.length - 1) : i;
const chapter = i;
gotoChapter(chapter);
}
function nextChapter() {
currentTime = 0.0;
const idx = activeEpisode - 1;
let i = idx + 1;
i = (i === chapters.length) ? 0 : i;
const chapter = i;
gotoChapter(chapter);
}
function gotoChapter(chapter) {
audio.firstElementChild.setAttribute('src', chapters[chapter].url);
audio.load();
audio.play();
activeEpisode = chapter + 1;
localStorage.setItem('activeEpisode'+pageTitle, activeEpisode);
document.getElementById('myBook').innerHTML=`
${pageTitle}
<a href="javascript:prevChapter()" style="color:cyan;">⇐</a>
${chapters[chapter].caption}
<a href="javascript:nextChapter()" style="color:cyan;">⇒</a>
`;
document.title = `${pageTitle} ${chapters[chapter].caption}`;
audio.play();
audio.currentTime = currentTime;
}
function toggle(elem) {
elem.style.display = (elem.style.display === 'none') ? 'block' : 'none';
}
function ProcessMenu() {
document.querySelectorAll('nav ul li > a:not(:only-child)')
.forEach(el => el.onclick = function (e) {
const nd = this.nextElementSibling;
document.querySelectorAll('.nav-dropdown')
.forEach(function(elem) {
if (elem === nd) {
toggle(nd);
} else {
elem.style.display = 'none';
}});
e.stopPropagation();
});
document.documentElement.onclick = function () {
document.querySelectorAll('.nav-dropdown').forEach(el => el.style.display = 'none');
};
}
function getLastChapter() {
if (!localStorage.getItem('activeEpisode'+pageTitle)) {
const start_episode = 1;
localStorage.setItem('activeEpisode'+pageTitle,start_episode);
localStorage.setItem('currentTime'+pageTitle, 0.0);
}
activeEpisode = parseInt(localStorage.getItem('activeEpisode'+pageTitle));
currentTime = localStorage.getItem('currentTime'+pageTitle);
return (activeEpisode - 1);
}