jellyfin-web/dashboard-ui/scripts/livetvguide.js

353 lines
9.5 KiB
JavaScript
Raw Normal View History

2014-01-05 18:59:21 -07:00
(function ($, document, apiClient) {
2014-01-06 06:33:14 -07:00
// 30 mins
2014-01-06 09:48:43 -07:00
var cellCurationMinutes = 30;
var cellDurationMs = cellCurationMinutes * 60 * 1000;
var gridLocalStartDateMs;
var gridLocalEndDateMs;
2014-01-06 06:33:14 -07:00
2014-01-05 18:59:21 -07:00
function formatDigit(i) {
return i < 10 ? "0" + i : i;
}
function getDateFormat(date) {
// yyyyMMddHHmmss
// Convert to UTC
// http://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc/14610512#14610512
var d = new Date(date.getTime());
return "" + d.getFullYear() + formatDigit(d.getMonth() + 1) + formatDigit(d.getDate()) + formatDigit(d.getHours()) + formatDigit(d.getMinutes()) + formatDigit(d.getSeconds());
}
function normalizeDateToTimeslot(date) {
2014-01-06 09:48:43 -07:00
var minutesOffset = date.getMinutes() - cellCurationMinutes;
2014-01-05 18:59:21 -07:00
if (minutesOffset >= 0) {
2014-01-06 09:48:43 -07:00
date.setHours(date.getHours(), cellCurationMinutes, 0, 0);
2014-01-05 18:59:21 -07:00
} else {
2014-01-06 09:48:43 -07:00
date.setHours(date.getHours(), 0, 0, 0);
2014-01-05 18:59:21 -07:00
}
return date;
}
var currentDate;
var channelsPromise;
function reloadGuide(page) {
channelsPromise = channelsPromise || apiClient.getLiveTvChannels({
userId: Dashboard.getCurrentUserId()
});;
var date = currentDate;
var nextDay = new Date(date.getTime());
2014-01-06 06:33:14 -07:00
nextDay.setDate(nextDay.getDate() + 2);
2014-01-05 18:59:21 -07:00
nextDay.setHours(1, 0, 0, 0);
var promise1 = channelsPromise;
var promise2 = apiClient.getLiveTvPrograms({
UserId: Dashboard.getCurrentUserId(),
2014-01-06 06:33:14 -07:00
MaxStartDate: getDateFormat(nextDay)
2014-01-05 18:59:21 -07:00
});
$.when(promise1, promise2).done(function (response1, response2) {
var channels = response1[0].Items;
var programs = response2[0].Items;
renderGuide(page, date, channels, programs);
});
}
function getTimeslotHeadersHtml(date) {
var html = '';
date = new Date(date.getTime());
var dateNumber = date.getDate();
while (date.getDate() == dateNumber) {
html += '<div class="timeslotHeader">';
2014-01-06 06:33:14 -07:00
html += '<div class="timeslotHeaderInner">';
2014-01-05 18:59:21 -07:00
html += LiveTvHelpers.getDisplayTime(date);
html += '</div>';
2014-01-06 06:33:14 -07:00
html += '</div>';
2014-01-05 18:59:21 -07:00
// Add 30 mins
2014-01-06 06:33:14 -07:00
date.setTime(date.getTime() + cellDurationMs);
2014-01-05 18:59:21 -07:00
}
return html;
}
2014-01-06 06:33:14 -07:00
function findProgramStartingInCell(programs, startIndex, cellStart, cellEnd, cellIndex) {
for (var i = startIndex, length = programs.length; i < length; i++) {
var program = programs[i];
if (!program.StartDateLocal) {
try {
program.StartDateLocal = parseISO8601Date(program.StartDate, { toLocal: true });
} catch (err) {
}
}
if (!program.EndDateLocal) {
try {
program.EndDateLocal = parseISO8601Date(program.EndDate, { toLocal: true });
} catch (err) {
}
}
var localTime = program.StartDateLocal.getTime();
if ((localTime >= cellStart || cellIndex == 0) && localTime < cellEnd && program.EndDateLocal > cellStart) {
return {
index: i,
program: program
};
}
}
return null;
}
2014-01-06 09:48:43 -07:00
function getProgramWidth(program) {
var end = Math.min(gridLocalEndDateMs, program.EndDateLocal.getTime());
var start = Math.max(gridLocalStartDateMs, program.StartDateLocal.getTime());
var ms = end - start;
var width = 100 * ms / cellDurationMs;
// Round to the nearest cell
var overlap = width % 100;
if (overlap) {
width = width - overlap + 100;
}
if (width > 300) {
width += (width / 100) - 3;
}
return width;
}
2014-01-06 06:33:14 -07:00
function getChannelProgramsHtml(page, date, channel, programs) {
2014-01-05 18:59:21 -07:00
var html = '';
2014-01-06 06:33:14 -07:00
var dateNumber = date.getDate();
programs = programs.filter(function (curr) {
return curr.ChannelId == channel.Id;
});
2014-01-05 18:59:21 -07:00
2014-01-06 06:33:14 -07:00
html += '<div class="channelPrograms">';
2014-01-05 18:59:21 -07:00
2014-01-06 06:33:14 -07:00
var programIndex = 0;
var cellIndex = 0;
2014-01-06 09:48:43 -07:00
2014-01-06 06:33:14 -07:00
while (date.getDate() == dateNumber) {
// Add 30 mins
var cellEndDate = new Date(date.getTime() + cellDurationMs);
var program = findProgramStartingInCell(programs, programIndex, date, cellEndDate, cellIndex);
if (program) {
programIndex = program.index + 1;
program = program.program;
}
html += '<div class="timeslotCell">';
2014-01-06 09:48:43 -07:00
var cellTagName;
var href;
2014-01-06 06:33:14 -07:00
var cssClass = "timeslotCellInner";
2014-01-06 09:48:43 -07:00
var style;
2014-01-06 06:33:14 -07:00
if (program) {
if (program.IsKids) {
cssClass += " childProgramInfo";
2014-01-06 09:48:43 -07:00
} else if (program.IsSports) {
2014-01-06 06:33:14 -07:00
cssClass += " sportsProgramInfo";
2014-01-06 09:48:43 -07:00
} else if (program.IsNews) {
2014-01-06 06:33:14 -07:00
cssClass += " newsProgramInfo";
2014-01-06 09:48:43 -07:00
} else if (program.IsMovie) {
2014-01-06 06:33:14 -07:00
cssClass += " movieProgramInfo";
}
2014-01-06 09:48:43 -07:00
else {
cssClass += " plainProgramInfo";
}
cssClass += " timeslotCellInnerWithProgram";
cellTagName = "a";
href = ' href="livetvprogram.html?id=' + program.Id + '"';
var width = getProgramWidth(program);
if (width && width != 100) {
style = ' style="width:' + width + '%;"';
} else {
style = '';
}
} else {
cellTagName = "div";
href = '';
style = '';
2014-01-06 06:33:14 -07:00
}
2014-01-06 09:48:43 -07:00
html += '<' + cellTagName + ' class="' + cssClass + '"' + href + style + '>';
2014-01-06 06:33:14 -07:00
if (program) {
html += '<div class="guideProgramName">';
html += program.Name;
html += '</div>';
2014-01-06 09:48:43 -07:00
2014-01-06 06:33:14 -07:00
html += '<div class="guideProgramTime">';
if (program.IsLive) {
html += '<span class="liveTvProgram">LIVE&nbsp;&nbsp;</span>';
}
else if (program.IsPremiere) {
html += '<span class="premiereTvProgram">PREMIERE&nbsp;&nbsp;</span>';
}
else if (program.IsSeries && !program.IsRepeat) {
html += '<span class="newTvProgram">NEW&nbsp;&nbsp;</span>';
}
html += LiveTvHelpers.getDisplayTime(program.StartDateLocal);
html += ' - ';
html += LiveTvHelpers.getDisplayTime(program.EndDateLocal);
2014-01-06 09:48:43 -07:00
if (program.SeriesTimerId) {
html += '<div class="timerCircle seriesTimerCircle"></div>';
html += '<div class="timerCircle seriesTimerCircle"></div>';
html += '<div class="timerCircle seriesTimerCircle"></div>';
}
else if (program.TimerId) {
html += '<div class="timerCircle"></div>';
}
2014-01-06 06:33:14 -07:00
html += '</div>';
} else {
html += '&nbsp;';
}
2014-01-06 09:48:43 -07:00
html += '</' + cellTagName + '>';
2014-01-06 06:33:14 -07:00
html += '</div>';
date = cellEndDate;
cellIndex++;
}
2014-01-05 18:59:21 -07:00
html += '</div>';
return html;
}
2014-01-06 06:33:14 -07:00
function renderPrograms(page, date, channels, programs) {
2014-01-05 18:59:21 -07:00
var html = [];
for (var i = 0, length = channels.length; i < length; i++) {
2014-01-06 06:33:14 -07:00
html.push(getChannelProgramsHtml(page, date, channels[i], programs));
2014-01-05 18:59:21 -07:00
}
2014-01-06 06:33:14 -07:00
$('.programGrid', page).html(html.join(''));
}
function renderChannelHeaders(page, channels) {
var html = '';
for (var i = 0, length = channels.length; i < length; i++) {
var channel = channels[i];
html += '<div class="channelHeaderCellContainer">';
2014-01-06 09:48:43 -07:00
html += '<div class="channelHeaderCell">';
html += '<a class="channelHeaderCellInner" href="livetvchannel.html?id=' + channel.Id + '">';
2014-01-06 06:33:14 -07:00
html += channel.Name + '<br/>' + channel.Number;
2014-01-06 09:48:43 -07:00
html += '</a>';
2014-01-06 06:33:14 -07:00
html += '</div>';
html += '</div>';
}
$('.channelList', page).html(html);
2014-01-05 18:59:21 -07:00
}
function renderGuide(page, date, channels, programs) {
2014-01-06 06:33:14 -07:00
renderChannelHeaders(page, channels);
$('.timeslotHeaders', page).html(getTimeslotHeadersHtml(date));
renderPrograms(page, date, channels, programs);
}
function onProgramGridScroll(page, elem) {
var grid = $(elem);
grid.prev().scrollTop(grid.scrollTop());
$('.timeslotHeaders', page).scrollLeft(grid.scrollLeft());
2014-01-05 18:59:21 -07:00
}
2014-01-06 06:33:14 -07:00
$(document).on('pageinit', "#liveTvGuidePage", function () {
var page = this;
$('.programGrid', page).on('scroll', function () {
onProgramGridScroll(page, this);
});
}).on('pagebeforeshow', "#liveTvGuidePage", function () {
2014-01-05 18:59:21 -07:00
var page = this;
currentDate = normalizeDateToTimeslot(new Date());
2014-01-06 09:48:43 -07:00
gridLocalStartDateMs = currentDate.getTime();
var clone = new Date(gridLocalStartDateMs);
clone.setHours(0, 0, 0, 0);
clone.setDate(clone.getDate() + 1);
gridLocalEndDateMs = clone.getTime() - 1;
2014-01-05 18:59:21 -07:00
reloadGuide(page);
});
})(jQuery, document, ApiClient);