Files
eigen-iptv/app/js/views.js
T

424 lines
21 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Schermen: zenderinfo (OSD), zenderlijst, tv-gids, instellingen en zenders kiezen. */
(function (App) {
'use strict';
var $ = App.$, K = App.KEY;
/* ---------------- meldingen ---------------- */
var msgTimer = null;
App.showMessage = function (text, ms) {
var m = $('message');
m.textContent = text; m.classList.remove('hidden');
clearTimeout(msgTimer);
if (ms) msgTimer = setTimeout(App.hideMessage, ms);
};
App.hideMessage = function () { clearTimeout(msgTimer); $('message').classList.add('hidden'); };
/* ---------------- OSD ---------------- */
var osdTimer = null;
App.OSD = {
visible: function () { return !$('osd').classList.contains('hidden'); },
show: function (ms) {
var st = App.state, ch = st.channels[st.idx];
if (!ch) return;
$('osd-num').textContent = st.idx + 1;
$('osd-name').textContent = ch.name;
var logo = $('osd-logo');
if (ch.logo) { logo.src = ch.logo; logo.classList.remove('hidden'); } else logo.classList.add('hidden');
logo.onerror = function () { logo.classList.add('hidden'); };
$('osd-clock').textContent = App.fmtTime(Date.now());
var nn = App.Epg.nowNext(ch), now = Date.now();
if (nn.now) {
$('osd-now-time').textContent = App.fmtTime(nn.now[0]) + ' – ' + App.fmtTime(nn.now[1]);
$('osd-now-title').textContent = nn.now[2];
$('osd-progress').style.width = Math.min(100, Math.max(0, (now - nn.now[0]) / (nn.now[1] - nn.now[0]) * 100)) + '%';
} else {
$('osd-now-time').textContent = '';
$('osd-now-title').textContent = App.Epg.at ? 'Geen gidsinformatie' : 'Gids wordt geladen…';
$('osd-progress').style.width = '0';
}
$('osd-next-time').textContent = nn.next ? App.fmtTime(nn.next[0]) : '';
$('osd-next-title').textContent = nn.next ? nn.next[2] : '';
$('osd').classList.remove('hidden');
clearTimeout(osdTimer);
osdTimer = setTimeout(App.OSD.hide, ms || 5000);
},
hide: function () { clearTimeout(osdTimer); $('osd').classList.add('hidden'); }
};
/* ---------------- zenderlijst ---------------- */
var ROWS_LIST = 10;
App.List = {
sel: 0,
open: function () { App.OSD.hide(); this.sel = App.state.idx; $('list').classList.remove('hidden'); App.state.view = 'list'; this.render(); },
close: function () { $('list').classList.add('hidden'); App.state.view = 'tv'; },
render: function () {
var chs = App.state.channels, top = Math.max(0, Math.min(this.sel - 4, chs.length - ROWS_LIST)), html = '';
for (var i = top; i < Math.min(chs.length, top + ROWS_LIST); i++) {
var nn = App.Epg.nowNext(chs[i]);
html += '<div class="li' + (i === this.sel ? ' focus' : '') + (i === App.state.idx ? ' playing' : '') + '">' +
'<span class="n">' + (i + 1) + '</span><div class="t"><div>' + App.esc(chs[i].name) + '</div>' +
'<div class="sub">' + (nn.now ? App.fmtTime(nn.now[0]) + ' ' + App.esc(nn.now[2]) : '&nbsp;') + '</div></div></div>';
}
$('list-items').innerHTML = html;
},
key: function (k) {
var n = App.state.channels.length;
if (k === K.UP) this.sel = (this.sel - 1 + n) % n;
else if (k === K.DOWN) this.sel = (this.sel + 1) % n;
else if (k === K.CH_UP) this.sel = Math.max(0, this.sel - ROWS_LIST);
else if (k === K.CH_DOWN) this.sel = Math.min(n - 1, this.sel + ROWS_LIST);
else if (k === K.OK) { this.close(); App.tune(this.sel); return; }
else if (App.isBack(k) || k === K.LEFT) { this.close(); return; }
else if (k === K.RED || k === K.GUIDE) { this.close(); App.Guide.open(); return; }
else if (k === K.BLUE) { this.close(); App.Settings.open(); return; }
this.render();
}
};
/* ---------------- tv-gids ---------------- */
var HALF = 30 * 60000, WIN = 2 * 3600e3, ROWS_GUIDE = 8;
function floorHalf(t) { return Math.floor(t / HALF) * HALF; }
App.Guide = {
row: 0, top: 0, winStart: 0, focusTime: 0,
open: function () {
App.OSD.hide();
this.row = App.state.idx;
this.top = Math.max(0, this.row - 2);
this.winStart = floorHalf(Date.now());
this.focusTime = Date.now();
$('guide').classList.remove('hidden');
App.state.view = 'guide';
this.render();
},
close: function () { $('guide').classList.add('hidden'); App.state.view = 'tv'; },
/* programma's van een zender binnen het venster; lege stukken worden opgevuld */
blocks: function (ch) {
var l = App.Epg.list(ch), ws = this.winStart, we = ws + WIN, out = [];
for (var i = 0; i < l.length; i++) if (l[i][1] > ws && l[i][0] < we) out.push({ s: l[i][0], e: l[i][1], t: l[i][2], d: l[i][3] });
if (!out.length) out.push({ s: ws, e: we, t: 'Geen gidsinformatie', d: '', empty: true });
return out;
},
focused: function () {
var b = this.blocks(App.state.channels[this.row]), ft = this.focusTime;
for (var i = 0; i < b.length; i++) if (b[i].s <= ft && b[i].e > ft) return { list: b, i: i };
for (i = 0; i < b.length; i++) if (b[i].s > ft) return { list: b, i: i };
return { list: b, i: b.length - 1 };
},
key: function (k) {
var n = App.state.channels.length, f, p, minStart = floorHalf(Date.now()) - 2 * HALF, maxStart = Date.now() + 40 * 3600e3;
if (k === K.UP) this.row = Math.max(0, this.row - 1);
else if (k === K.DOWN) this.row = Math.min(n - 1, this.row + 1);
else if (k === K.CH_UP) this.row = Math.max(0, this.row - ROWS_GUIDE);
else if (k === K.CH_DOWN) this.row = Math.min(n - 1, this.row + ROWS_GUIDE);
else if (k === K.RIGHT) {
f = this.focused(); p = f.list[f.i + 1];
if (p && !p.empty) {
this.focusTime = p.s;
if (p.s >= this.winStart + WIN - HALF) this.winStart = Math.min(maxStart, floorHalf(p.s) - HALF);
} else if (this.winStart < maxStart) {
// geen volgend programma in beeld: venster een half uur opschuiven
this.winStart += HALF;
var nxt = this.firstAfter(f.list[f.i].e - 1);
this.focusTime = nxt ? nxt.s : Math.max(this.focusTime, this.winStart);
}
} else if (k === K.LEFT) {
f = this.focused(); p = f.i > 0 ? f.list[f.i - 1] : null;
if (p && !p.empty) {
this.focusTime = Math.max(p.s, this.winStart);
} else if (this.winStart > minStart) {
this.winStart -= HALF;
var prev = this.lastBefore(f.list[f.i].s);
this.focusTime = prev ? Math.max(prev.s, this.winStart) : this.winStart;
}
} else if (k === K.OK) { this.close(); App.tune(this.row); return; }
else if (App.isBack(k) || k === K.RED || k === K.GUIDE) { this.close(); return; }
else if (k === K.BLUE) { this.close(); App.Settings.open(); return; }
if (this.row < this.top) this.top = this.row;
if (this.row >= this.top + ROWS_GUIDE) this.top = this.row - ROWS_GUIDE + 1;
this.render();
},
firstAfter: function (t) {
var l = App.Epg.list(App.state.channels[this.row]);
for (var i = 0; i < l.length; i++) if (l[i][0] > t) return { s: l[i][0] };
return null;
},
lastBefore: function (t) {
var l = App.Epg.list(App.state.channels[this.row]);
for (var i = l.length - 1; i >= 0; i--) if (l[i][0] < t) return { s: l[i][0] };
return null;
},
render: function () {
var chs = App.state.channels, ws = this.winStart, now = Date.now();
var width = 1920 - 100 - 390, px = width / WIN, html = '', i;
$('guide-day').textContent = App.fmtDay(Math.max(ws, now));
var times = '';
for (i = 0; i < 4; i++) times += '<span style="left:' + Math.round(i * HALF * px) + 'px">' + App.fmtTime(ws + i * HALF) + '</span>';
$('guide-times').innerHTML = times;
var f = this.focused(), fp = f.list[f.i];
for (var r = this.top; r < Math.min(chs.length, this.top + ROWS_GUIDE); r++) {
var ch = chs[r], b = r === this.row ? f.list : this.blocks(ch);
html += '<div class="g-row"><div class="g-ch"><span class="n">' + (r + 1) + '</span>' +
(ch.logo ? '<img src="' + App.esc(ch.logo) + '" onerror="this.style.display=\'none\'">' : '') +
'<span class="nm">' + App.esc(ch.name) + '</span></div><div class="g-progs">';
for (i = 0; i < b.length; i++) {
var s = Math.max(b[i].s, ws), e = Math.min(b[i].e, ws + WIN);
var cls = 'g-p' + (b[i].empty ? ' empty' : '') + (b[i].s <= now && b[i].e > now ? ' live' : '') +
(r === this.row && i === f.i ? ' focus' : '');
html += '<div class="' + cls + '" style="left:' + Math.round((s - ws) * px) + 'px;width:' +
Math.max(4, Math.round((e - s) * px)) + 'px">' + App.esc(b[i].t) + '</div>';
}
if (now >= ws && now < ws + WIN) html += '<div class="g-now" style="left:' + Math.round((now - ws) * px) + 'px"></div>';
html += '</div></div>';
}
$('guide-rows').innerHTML = html;
$('gd-title').textContent = fp.t;
var meta = fp.empty ? chs[this.row].name : App.fmtTime(fp.s) + ' – ' + App.fmtTime(fp.e) + ' · ' + chs[this.row].name;
if (!fp.empty && fp.s <= now && fp.e > now) meta += ' · nog ' + Math.round((fp.e - now) / 60000) + ' min';
else if (!fp.empty && fp.s > now && new Date(fp.s).getDate() !== new Date(now).getDate()) meta += ' · ' + App.fmtDay(fp.s);
$('gd-meta').textContent = meta;
$('gd-desc').textContent = fp.d || '';
}
};
/* ---------------- instellingen ---------------- */
App.Settings = {
idx: 0,
open: function () {
App.OSD.hide();
var c = App.state.cfg;
$('f-m3u').value = c.m3u || ''; $('f-server').value = c.server || '';
$('f-user').value = c.user || ''; $('f-pass').value = c.pass || ''; $('f-epg').value = c.epg || '';
this.applyType();
$('settings').classList.remove('hidden');
App.state.view = 'settings';
this.idx = 0; this.render();
this.status(App.state.channels.length ? App.state.channels.length + ' zenders gekozen.' + this.epgInfo() : 'Vul je IPTV-gegevens in en kies "Zenders laden en kiezen".');
},
close: function () {
if (document.activeElement && document.activeElement.blur) document.activeElement.blur();
$('settings').classList.add('hidden');
},
epgInfo: function () {
if (!App.Epg.at) return '\nGids: nog niet geladen.';
return '\nGids: bijgewerkt om ' + App.fmtTime(App.Epg.at) + ', ' + Object.keys(App.Epg.map).length + ' van de ' + App.state.channels.length + ' zenders met gids.';
},
status: function (t, err) { var s = $('settings-status'); s.textContent = t; s.className = 'status' + (err ? ' err' : ''); },
applyType: function () {
var c = App.state.cfg, x = c.type === 'xtream';
Array.prototype.forEach.call(document.querySelectorAll('.only-m3u'), function (e) { e.classList.toggle('hidden', x); });
Array.prototype.forEach.call(document.querySelectorAll('.only-xtream'), function (e) { e.classList.toggle('hidden', !x); });
Array.prototype.forEach.call($('src-type').children, function (e) { e.classList.toggle('on', e.getAttribute('data-v') === (c.type || 'm3u')); });
Array.prototype.forEach.call($('f-output').children, function (e) { e.classList.toggle('on', e.getAttribute('data-v') === (c.output || 'm3u8')); });
},
items: function () {
return Array.prototype.filter.call(document.querySelectorAll('#settings [data-nav]'), function (e) { return e.offsetParent !== null; });
},
render: function () {
var it = this.items();
this.idx = Math.max(0, Math.min(this.idx, it.length - 1));
it.forEach(function (e, i) { e.classList.toggle('focus', i === this.idx); }, this);
},
readFields: function () {
var c = App.state.cfg;
c.m3u = $('f-m3u').value.trim(); c.server = $('f-server').value.trim();
c.user = $('f-user').value.trim(); c.pass = $('f-pass').value; c.epg = $('f-epg').value.trim();
App.store.set('cfg', c);
},
key: function (k) {
var it = this.items(), cur = it[this.idx], ae = document.activeElement;
if (ae && ae.tagName === 'INPUT' && ae.closest('#settings')) {
// aan het typen: alleen Terug, OK, omhoog/omlaag onderscheppen
if (App.isBack(k) || k === K.OK) { ae.blur(); this.readFields(); return; }
if (k === K.UP || k === K.DOWN) { ae.blur(); this.readFields(); } else return;
}
var isBtn = cur && cur.tagName === 'BUTTON';
if (k === K.DOWN) {
if (isBtn) return;
this.idx++;
} else if (k === K.UP) {
if (isBtn) { var firstBtn = it.indexOf($('b-load')); this.idx = firstBtn - 1; }
else this.idx--;
} else if (k === K.LEFT || k === K.RIGHT) {
if (isBtn) this.idx += (k === K.RIGHT ? 1 : -1);
else if (cur && cur.classList.contains('toggle')) this.toggle(cur);
if (it[this.idx] && it[this.idx].tagName !== 'BUTTON' && isBtn) this.idx -= (k === K.RIGHT ? 1 : -1);
} else if (k === K.OK) {
if (!cur) return;
if (cur.tagName === 'INPUT') { cur.focus(); return; }
if (cur.classList.contains('toggle')) { this.toggle(cur); return; }
if (cur.id === 'b-load') return this.loadChannels();
if (cur.id === 'b-epg') return this.refreshEpg();
if (cur.id === 'b-back') return this.toTv();
} else if (App.isBack(k)) {
return this.toTv();
}
this.render();
},
toggle: function (el) {
var c = App.state.cfg;
if (el.id === 'src-type') c.type = c.type === 'xtream' ? 'm3u' : 'xtream';
if (el.id === 'f-output') c.output = c.output === 'ts' ? 'm3u8' : 'ts';
App.store.set('cfg', c);
this.applyType(); this.render();
},
toTv: function () {
this.readFields();
if (!App.state.channels.length) { this.status('Kies eerst zenders met "Zenders laden en kiezen".', true); return; }
this.close();
App.state.view = 'tv';
App.tune(Math.min(App.state.idx, App.state.channels.length - 1), true);
},
loadChannels: function () {
var self = this;
this.readFields();
this.status('Zenders ophalen…');
App.loadSource(App.state.cfg).then(function (res) {
App.allChannels = res.channels;
App.store.set('srcEpg', res.epgUrl || '');
// gekozen zenders bijwerken met de nieuwste stream-URL en logo
var byKey = {};
res.channels.forEach(function (c) { byKey[c.key] = c; });
App.state.channels.forEach(function (c) { var n = byKey[c.key]; if (n) { c.url = n.url; c.logo = n.logo; c.tvgId = n.tvgId; } });
App.saveChannels();
self.status(res.channels.length + ' live-zenders gevonden.');
self.close();
App.Picker.open();
}).catch(function (e) { self.status('Ophalen mislukt: ' + e.message, true); });
},
refreshEpg: function () {
var self = this;
this.readFields();
if (!App.state.channels.length) { this.status('Kies eerst zenders, dan kan de gids geladen worden.', true); return; }
this.status('Gids laden…');
App.loadEpg(function (bytes, kept) {
self.status('Gids laden… ' + (bytes / 1048576).toFixed(1) + ' MB gelezen, ' + kept + ' programma\'s bewaard');
}).then(function (r) {
self.status('Gids geladen: ' + r.programmes + ' programma\'s, ' + r.matched + ' van de ' + r.total + ' zenders gevonden in de gids.');
}).catch(function (e) { self.status('Gids laden mislukt: ' + e.message, true); });
}
};
/* ---------------- zenders kiezen ---------------- */
var ROWS_PK = 11;
App.Picker = {
col: 'groups', gi: 0, ci: 0, groups: [], chans: [], query: '',
open: function () {
this.col = 'groups'; this.gi = 0; this.ci = 0; this.query = '';
$('pk-search').value = '';
this.buildGroups();
$('picker').classList.remove('hidden');
App.state.view = 'picker';
var self = this;
$('pk-search').oninput = function () { self.query = this.value.trim().toLowerCase(); self.ci = 0; self.filter(); self.render(); };
this.filter(); this.render();
},
close: function () {
$('pk-search').blur();
$('picker').classList.add('hidden');
App.saveChannels();
App.Settings.open();
App.Settings.status(App.state.channels.length + ' zenders gekozen. De gids wordt nu geladen…');
App.Settings.refreshEpg();
},
buildGroups: function () {
var counts = {}, order = [];
(App.allChannels || []).forEach(function (c) {
if (!counts[c.group]) { counts[c.group] = 0; order.push(c.group); }
counts[c.group]++;
});
this.groups = [{ name: '★ Mijn zenders', id: 'sel' }, { name: 'Alle zenders', id: 'all', n: (App.allChannels || []).length }]
.concat(order.map(function (g) { return { name: g, id: 'g', n: counts[g] }; }));
},
isSel: function (key) {
var l = App.state.channels;
for (var i = 0; i < l.length; i++) if (l[i].key === key) return i;
return -1;
},
filter: function () {
var g = this.groups[this.gi], q = this.query, all = App.allChannels || [];
var list = g.id === 'sel' ? App.state.channels.slice() : (g.id === 'all' || q ? all : all.filter(function (c) { return c.group === g.name; }));
if (q) list = list.filter(function (c) { return c.name.toLowerCase().indexOf(q) >= 0; });
this.chans = list;
this.ci = Math.max(0, Math.min(this.ci, list.length - 1));
},
toggle: function (c) {
var i = this.isSel(c.key);
if (i >= 0) App.state.channels.splice(i, 1);
else App.state.channels.push({ key: c.key, name: c.name, url: c.url, logo: c.logo, group: c.group, tvgId: c.tvgId });
},
key: function (k) {
var ae = document.activeElement;
if (ae === $('pk-search')) {
if (App.isBack(k) || k === K.OK || k === K.DOWN) { ae.blur(); this.col = 'chans'; this.render(); }
return;
}
if (this.col === 'search') {
if (k === K.OK) { $('pk-search').focus(); return; }
if (k === K.DOWN) this.col = 'groups';
else if (App.isBack(k)) return this.close();
this.render(); return;
}
var g = this.groups[this.gi];
if (this.col === 'groups') {
if (k === K.UP) { if (this.gi === 0) this.col = 'search'; else { this.gi--; this.ci = 0; this.filter(); } }
else if (k === K.DOWN) { if (this.gi < this.groups.length - 1) { this.gi++; this.ci = 0; this.filter(); } }
else if (k === K.RIGHT || k === K.OK) { if (this.chans.length) this.col = 'chans'; }
else if (k === K.CH_UP) { this.gi = Math.max(0, this.gi - ROWS_PK); this.ci = 0; this.filter(); }
else if (k === K.CH_DOWN) { this.gi = Math.min(this.groups.length - 1, this.gi + ROWS_PK); this.ci = 0; this.filter(); }
else if (App.isBack(k)) return this.close();
} else {
var c = this.chans[this.ci];
if (k === K.UP) { if (this.ci === 0) this.col = 'search'; else this.ci--; }
else if (k === K.DOWN) this.ci = Math.min(this.chans.length - 1, this.ci + 1);
else if (k === K.CH_UP) this.ci = Math.max(0, this.ci - ROWS_PK);
else if (k === K.CH_DOWN) this.ci = Math.min(this.chans.length - 1, this.ci + ROWS_PK);
else if (k === K.LEFT) this.col = 'groups';
else if (k === K.OK && c) { this.toggle(c); if (g.id === 'sel') this.filter(); }
else if ((k === K.YELLOW || k === K.BLUE) && g.id === 'sel' && !this.query && c) {
// volgorde aanpassen in "Mijn zenders"
var l = App.state.channels, to = this.ci + (k === K.YELLOW ? -1 : 1);
if (to >= 0 && to < l.length) { var tmp = l[to]; l[to] = l[this.ci]; l[this.ci] = tmp; this.ci = to; this.filter(); }
}
else if (App.isBack(k)) return this.close();
}
this.render();
},
render: function () {
var self = this, html = '', i, top;
$('pk-count').textContent = '(' + App.state.channels.length + ' gekozen)';
$('pk-search').classList.toggle('focus', this.col === 'search');
this.groups[0].n = App.state.channels.length;
top = Math.max(0, Math.min(this.gi - 5, this.groups.length - ROWS_PK));
for (i = top; i < Math.min(this.groups.length, top + ROWS_PK); i++) {
var g = this.groups[i];
html += '<div class="pk-item' + (i === this.gi ? ' focus' : '') + '"><span class="nm">' + App.esc(g.name) + '</span><span class="gc">' + (g.n || 0) + '</span></div>';
}
$('pk-groups').innerHTML = html;
$('pk-groups').classList.toggle('active', this.col === 'groups');
html = '';
top = Math.max(0, Math.min(this.ci - 5, this.chans.length - ROWS_PK));
var sel = this.groups[this.gi].id === 'sel';
for (i = top; i < Math.min(this.chans.length, top + ROWS_PK); i++) {
var c = this.chans[i], on = self.isSel(c.key) >= 0;
html += '<div class="pk-item' + (on ? ' sel' : '') + (i === this.ci && this.col === 'chans' ? ' focus' : '') + '">' +
'<span class="box">' + (sel ? (i + 1) : (on ? '☑' : '☐')) + '</span><span class="nm">' + App.esc(c.name) + '</span>' +
'<span class="gc">' + App.esc(this.query || this.groups[this.gi].id !== 'g' ? c.group : '') + '</span></div>';
}
if (!this.chans.length) html = '<div class="pk-item"><span class="nm">' + (sel ? 'Nog geen zenders gekozen. Kies links een groep.' : 'Geen zenders gevonden.') + '</span></div>';
$('pk-chans').innerHTML = html;
$('pk-chans').classList.toggle('active', this.col === 'chans');
$('pk-hint').textContent = sel
? 'OK verwijderen · Geel omhoog · Blauw omlaag · ▲ bovenaan = zoeken · Terug klaar'
: 'OK aan/uit · ◀ ▶ kolom · CH+/− pagina · ▲ bovenaan = zoeken · Terug klaar';
}
};
})(window.App);