jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/guide/guide.js

895 lines
31 KiB
JavaScript
Raw Normal View History

2016-09-05 22:02:05 -07:00
define(['require', 'browser', 'globalize', 'connectionManager', 'serverNotifications', 'loading', 'datetime', 'focusManager', 'userSettings', 'imageLoader', 'events', 'layoutManager', 'itemShortcuts', 'registrationservices', 'dom', 'clearButtonStyle', 'css!./guide.css', 'material-icons', 'scrollStyles', 'emby-button', 'paper-icon-button-light'], function (require, browser, globalize, connectionManager, serverNotifications, loading, datetime, focusManager, userSettings, imageLoader, events, layoutManager, itemShortcuts, registrationServices, dom) {
function showViewSettings(instance) {
require(['guide-settings-dialog'], function (guideSettingsDialog) {
guideSettingsDialog.show().then(function () {
instance.refresh();
});
});
}
2016-04-26 11:28:04 -07:00
function Guide(options) {
var self = this;
var items = {};
2016-04-26 19:59:43 -07:00
self.options = options;
2016-04-26 11:28:04 -07:00
// 30 mins
var cellCurationMinutes = 30;
var cellDurationMs = cellCurationMinutes * 60 * 1000;
var msPerDay = 86400000;
2016-06-06 10:33:27 -07:00
var totalRendererdMs = msPerDay;
2016-04-26 11:28:04 -07:00
var currentDate;
2016-08-18 10:03:22 -07:00
var currentStartIndex = 0;
var currentChannelLimit = 0;
2016-04-26 11:28:04 -07:00
var channelQuery = {
StartIndex: 0,
EnableFavoriteSorting: true
};
var channelsPromise;
2016-06-09 09:13:25 -07:00
self.refresh = function () {
currentDate = null;
reloadPage(options.element);
};
self.destroy = function () {
events.off(serverNotifications, 'TimerCreated', onTimerCreated);
events.off(serverNotifications, 'SeriesTimerCreated', onSeriesTimerCreated);
events.off(serverNotifications, 'TimerCancelled', onTimerCancelled);
events.off(serverNotifications, 'SeriesTimerCancelled', onSeriesTimerCancelled);
clearCurrentTimeUpdateInterval();
2016-07-26 22:19:56 -07:00
setScrollEvents(options.element, false);
2016-06-09 09:13:25 -07:00
itemShortcuts.off(options.element);
items = {};
};
2016-04-26 11:28:04 -07:00
function normalizeDateToTimeslot(date) {
var minutesOffset = date.getMinutes() - cellCurationMinutes;
if (minutesOffset >= 0) {
date.setHours(date.getHours(), cellCurationMinutes, 0, 0);
} else {
date.setHours(date.getHours(), 0, 0, 0);
}
return date;
}
function showLoading() {
loading.show();
}
function hideLoading() {
loading.hide();
}
2016-06-06 10:33:27 -07:00
var currentTimeUpdateInterval;
2016-06-06 11:05:22 -07:00
var currentTimeIndicatorBar;
var currentTimeIndicatorArrow;
2016-06-06 10:33:27 -07:00
function startCurrentTimeUpdateInterval() {
clearCurrentTimeUpdateInterval();
//currentTimeUpdateInterval = setInterval(updateCurrentTimeIndicator, 1000);
currentTimeUpdateInterval = setInterval(updateCurrentTimeIndicator, 60000);
updateCurrentTimeIndicator();
}
function clearCurrentTimeUpdateInterval() {
var interval = currentTimeUpdateInterval;
if (interval) {
clearInterval(interval);
}
currentTimeUpdateInterval = null;
2016-06-06 11:05:22 -07:00
currentTimeIndicatorBar = null;
currentTimeIndicatorArrow = null;
2016-06-06 10:33:27 -07:00
}
function updateCurrentTimeIndicator() {
2016-06-06 11:05:22 -07:00
if (!currentTimeIndicatorBar) {
currentTimeIndicatorBar = options.element.querySelector('.currentTimeIndicatorBar');
}
if (!currentTimeIndicatorArrow) {
currentTimeIndicatorArrow = options.element.querySelector('.currentTimeIndicatorArrowContainer');
2016-06-06 10:33:27 -07:00
}
var dateDifference = new Date().getTime() - currentDate.getTime();
var pct = dateDifference > 0 ? (dateDifference / totalRendererdMs) : 0;
pct = Math.min(pct, 1);
if (pct <= 0 || pct >= 1) {
2016-06-06 11:05:22 -07:00
currentTimeIndicatorBar.classList.add('hide');
currentTimeIndicatorArrow.classList.add('hide');
2016-06-06 10:33:27 -07:00
} else {
2016-06-06 11:05:22 -07:00
currentTimeIndicatorBar.classList.remove('hide');
currentTimeIndicatorArrow.classList.remove('hide');
2016-06-06 10:33:27 -07:00
//pct *= 100;
//pct = 100 - pct;
2016-06-06 11:05:22 -07:00
//currentTimeIndicatorElement.style.width = (pct * 100) + '%';
currentTimeIndicatorBar.style.transform = 'scaleX(' + pct + ')';
currentTimeIndicatorArrow.style.transform = 'translateX(' + (pct * 100) + '%)';
2016-06-06 10:33:27 -07:00
}
}
2016-04-26 11:28:04 -07:00
function getChannelLimit(context) {
return registrationServices.validateFeature('livetv').then(function () {
2016-08-18 10:27:22 -07:00
var limit = browser.slow ? 100 : 500;
2016-04-26 11:28:04 -07:00
context.querySelector('.guideRequiresUnlock').classList.add('hide');
return limit;
}, function () {
var limit = 5;
context.querySelector('.guideRequiresUnlock').classList.remove('hide');
2016-06-09 09:13:25 -07:00
context.querySelector('.unlockText').innerHTML = globalize.translate('sharedcomponents#LiveTvGuideRequiresUnlock', limit);
2016-04-26 11:28:04 -07:00
return limit;
});
}
function reloadGuide(context, newStartDate) {
var apiClient = connectionManager.currentApiClient();
channelQuery.UserId = apiClient.getCurrentUserId();
getChannelLimit(context).then(function (channelLimit) {
2016-08-18 10:03:22 -07:00
currentChannelLimit = channelLimit;
2016-04-26 11:28:04 -07:00
showLoading();
2016-08-18 10:03:22 -07:00
channelQuery.StartIndex = currentStartIndex;
2016-04-26 11:28:04 -07:00
channelQuery.Limit = channelLimit;
channelQuery.AddCurrentProgram = false;
2016-08-17 12:28:43 -07:00
channelQuery.EnableUserData = false;
channelQuery.EnableImageTypes = "Primary";
2016-04-26 11:28:04 -07:00
channelsPromise = channelsPromise || apiClient.getLiveTvChannels(channelQuery);
var date = newStartDate;
// Add one second to avoid getting programs that are just ending
date = new Date(date.getTime() + 1000);
// Subtract to avoid getting programs that are starting when the grid ends
var nextDay = new Date(date.getTime() + msPerDay - 2000);
console.log(nextDay);
channelsPromise.then(function (channelsResult) {
2016-09-05 22:02:05 -07:00
var btnPreviousPage = context.querySelector('.btnPreviousPage');
var btnNextPage = context.querySelector('.btnNextPage');
2016-08-18 10:03:22 -07:00
if (channelsResult.TotalRecordCount > channelLimit) {
2016-09-05 22:02:05 -07:00
2016-09-07 00:08:20 -07:00
context.querySelector('.guideOptions').classList.remove('hide');
2016-09-05 22:02:05 -07:00
btnPreviousPage.classList.remove('hide');
btnNextPage.classList.remove('hide');
2016-08-18 10:03:22 -07:00
if (channelQuery.StartIndex) {
context.querySelector('.btnPreviousPage').disabled = false;
} else {
context.querySelector('.btnPreviousPage').disabled = true;
}
if ((channelQuery.StartIndex + channelLimit) < channelsResult.TotalRecordCount) {
2016-09-05 22:02:05 -07:00
btnNextPage.disabled = false;
2016-08-18 10:03:22 -07:00
} else {
2016-09-05 22:02:05 -07:00
btnNextPage.disabled = true;
2016-08-18 10:03:22 -07:00
}
} else {
2016-09-07 00:08:20 -07:00
context.querySelector('.guideOptions').classList.add('hide');
2016-08-18 10:03:22 -07:00
}
2016-04-26 11:28:04 -07:00
apiClient.getLiveTvPrograms({
UserId: apiClient.getCurrentUserId(),
MaxStartDate: nextDay.toISOString(),
MinEndDate: date.toISOString(),
channelIds: channelsResult.Items.map(function (c) {
return c.Id;
}).join(','),
ImageTypeLimit: 1,
2016-08-17 22:56:10 -07:00
EnableImages: false,
//EnableImageTypes: layoutManager.tv ? "Primary,Backdrop" : "Primary",
2016-05-08 20:13:38 -07:00
SortBy: "StartDate",
2016-08-17 12:28:43 -07:00
EnableTotalRecordCount: false,
EnableUserData: false
2016-04-26 11:28:04 -07:00
}).then(function (programsResult) {
renderGuide(context, date, channelsResult.Items, programsResult.Items, apiClient);
hideLoading();
});
});
});
}
function getDisplayTime(date) {
if ((typeof date).toString().toLowerCase() === 'string') {
try {
date = datetime.parseISO8601Date(date, { toLocal: true });
} catch (err) {
return date;
}
}
return datetime.getDisplayTime(date).toLowerCase();
}
function getTimeslotHeadersHtml(startDate, endDateTime) {
var html = '';
// clone
startDate = new Date(startDate.getTime());
html += '<div class="timeslotHeadersInner">';
while (startDate.getTime() < endDateTime) {
html += '<div class="timeslotHeader">';
html += getDisplayTime(startDate);
html += '</div>';
// Add 30 mins
startDate.setTime(startDate.getTime() + cellDurationMs);
}
2016-06-06 10:33:27 -07:00
2016-06-06 11:05:22 -07:00
html += '<div class="currentTimeIndicatorBar hide">';
2016-06-06 10:33:27 -07:00
html += '</div>';
2016-06-06 11:05:22 -07:00
html += '<div class="currentTimeIndicatorArrowContainer hide">';
2016-06-09 23:54:03 -07:00
html += '<i class="currentTimeIndicatorArrow md-icon">arrow_drop_down</i>';
2016-04-26 11:28:04 -07:00
html += '</div>';
return html;
}
function parseDates(program) {
if (!program.StartDateLocal) {
try {
program.StartDateLocal = datetime.parseISO8601Date(program.StartDate, { toLocal: true });
} catch (err) {
}
}
if (!program.EndDateLocal) {
try {
program.EndDateLocal = datetime.parseISO8601Date(program.EndDate, { toLocal: true });
} catch (err) {
}
}
return null;
}
2016-05-20 08:57:07 -07:00
function getChannelProgramsHtml(context, date, channel, programs, options) {
2016-04-26 11:28:04 -07:00
var html = '';
var startMs = date.getTime();
var endMs = startMs + msPerDay - 1;
programs = programs.filter(function (curr) {
return curr.ChannelId == channel.Id;
});
2016-08-24 20:07:31 -07:00
var cssClass = layoutManager.tv ? 'channelPrograms channelPrograms-tv' : 'channelPrograms';
html += '<div class="' + cssClass + '" data-channelid="' + channel.Id + '">';
2016-04-26 11:28:04 -07:00
for (var i = 0, length = programs.length; i < length; i++) {
var program = programs[i];
if (program.ChannelId != channel.Id) {
continue;
}
parseDates(program);
if (program.EndDateLocal.getTime() < startMs) {
continue;
}
if (program.StartDateLocal.getTime() > endMs) {
break;
}
items[program.Id] = program;
var renderStartMs = Math.max(program.StartDateLocal.getTime(), startMs);
var startPercent = (program.StartDateLocal.getTime() - startMs) / msPerDay;
startPercent *= 100;
startPercent = Math.max(startPercent, 0);
var renderEndMs = Math.min(program.EndDateLocal.getTime(), endMs);
var endPercent = (renderEndMs - renderStartMs) / msPerDay;
endPercent *= 100;
var cssClass = "programCell clearButton itemAction";
var addAccent = true;
if (program.IsKids) {
cssClass += " childProgramInfo";
} else if (program.IsSports) {
cssClass += " sportsProgramInfo";
} else if (program.IsNews) {
cssClass += " newsProgramInfo";
} else if (program.IsMovie) {
cssClass += " movieProgramInfo";
}
else {
cssClass += " plainProgramInfo";
addAccent = false;
}
2016-05-28 11:03:38 -07:00
var timerAttributes = '';
if (program.TimerId) {
timerAttributes += ' data-timerid="' + program.TimerId + '"';
}
if (program.SeriesTimerId) {
timerAttributes += ' data-seriestimerid="' + program.SeriesTimerId + '"';
}
2016-09-14 09:20:03 -07:00
html += '<button data-action="programdialog"' + timerAttributes + ' data-isfolder="' + program.IsFolder + '" data-id="' + program.Id + '" data-serverid="' + program.ServerId + '" data-type="' + program.Type + '" class="' + cssClass + '" style="left:' + startPercent + '%;width:' + endPercent + '%;">';
2016-04-26 11:28:04 -07:00
var guideProgramNameClass = "guideProgramName";
html += '<div class="' + guideProgramNameClass + '">';
2016-05-20 08:57:07 -07:00
if (program.IsLive && options.showLiveIndicator) {
2016-09-07 13:11:16 -07:00
html += '<span class="liveTvProgram guideProgramIndicator">' + globalize.translate('sharedcomponents#Live') + '</span>';
2016-04-26 11:28:04 -07:00
}
2016-05-20 08:57:07 -07:00
else if (program.IsPremiere && options.showPremiereIndicator) {
2016-09-07 13:11:16 -07:00
html += '<span class="premiereTvProgram guideProgramIndicator">' + globalize.translate('sharedcomponents#Premiere') + '</span>';
2016-04-26 11:28:04 -07:00
}
2016-05-20 08:57:07 -07:00
else if (program.IsSeries && !program.IsRepeat && options.showNewIndicator) {
2016-09-07 00:08:20 -07:00
html += '<span class="newTvProgram guideProgramIndicator">' + globalize.translate('sharedcomponents#AttributeNew') + '</span>';
2016-04-26 11:28:04 -07:00
}
2016-09-07 13:11:16 -07:00
else if (program.IsSeries && program.IsRepeat && options.showRepeatIndicator) {
html += '<span class="repeatTvProgram guideProgramIndicator">' + globalize.translate('sharedcomponents#Repeat') + '</span>';
}
2016-04-26 11:28:04 -07:00
html += program.Name;
html += '</div>';
2016-05-20 08:57:07 -07:00
if (program.IsHD && options.showHdIcon) {
2016-08-02 18:32:16 -07:00
html += '<i class="guideHdIcon md-icon programIcon">hd</i>';
2016-04-26 11:28:04 -07:00
}
if (program.SeriesTimerId) {
2016-08-02 18:32:16 -07:00
html += '<i class="seriesTimerIcon md-icon programIcon">fiber_smart_record</i>';
2016-04-26 11:28:04 -07:00
}
else if (program.TimerId) {
2016-08-02 18:32:16 -07:00
html += '<i class="timerIcon md-icon programIcon">fiber_manual_record</i>';
2016-04-26 11:28:04 -07:00
}
if (addAccent) {
2016-08-02 18:32:16 -07:00
if (program.IsKids) {
html += '<div class="programAccent childAccent"></div>';
} else if (program.IsSports) {
html += '<div class="programAccent sportsAccent"></div>';
} else if (program.IsNews) {
html += '<div class="programAccent newsAccent"></div>';
} else if (program.IsMovie) {
html += '<div class="programAccent movieAccent"></div>';
}
2016-04-26 11:28:04 -07:00
}
html += '</button>';
}
html += '</div>';
return html;
}
function renderPrograms(context, date, channels, programs) {
var html = [];
2016-05-20 08:57:07 -07:00
// Normally we'd want to just let responsive css handle this,
// but since mobile browsers are often underpowered,
// it can help performance to get them out of the markup
2016-09-07 10:17:26 -07:00
var allowIndicators = dom.getWindowSize().innerWidth >= 600;
2016-05-20 08:57:07 -07:00
var options = {
2016-09-07 10:17:26 -07:00
showHdIcon: allowIndicators && userSettings.get('guide-indicator-hd') == 'true',
2016-09-07 23:15:44 -07:00
showLiveIndicator: allowIndicators && userSettings.get('guide-indicator-live') != 'false',
showPremiereIndicator: allowIndicators && userSettings.get('guide-indicator-premiere') != 'false',
2016-09-07 13:11:16 -07:00
showNewIndicator: allowIndicators && userSettings.get('guide-indicator-new') == 'true',
showRepeatIndicator: allowIndicators && userSettings.get('guide-indicator-repeat') == 'true'
2016-05-20 08:57:07 -07:00
};
2016-04-26 11:28:04 -07:00
for (var i = 0, length = channels.length; i < length; i++) {
2016-05-20 08:57:07 -07:00
html.push(getChannelProgramsHtml(context, date, channels[i], programs, options));
2016-04-26 11:28:04 -07:00
}
var programGrid = context.querySelector('.programGrid');
programGrid.innerHTML = html.join('');
programGrid.scrollTop = 0;
programGrid.scrollLeft = 0;
}
function renderChannelHeaders(context, channels, apiClient) {
var html = '';
for (var i = 0, length = channels.length; i < length; i++) {
var channel = channels[i];
var hasChannelImage = channel.ImageTags.Primary;
var dataSrc = '';
if (hasChannelImage) {
var url = apiClient.getScaledImageUrl(channel.Id, {
maxHeight: 200,
tag: channel.ImageTags.Primary,
type: "Primary"
});
dataSrc = ' data-src="' + url + '"';
}
2016-04-27 18:43:09 -07:00
var cssClass = 'channelHeaderCell clearButton itemAction lazy';
2016-04-26 11:28:04 -07:00
2016-08-24 20:07:31 -07:00
if (layoutManager.tv) {
cssClass += ' channelHeaderCell-tv';
}
2016-04-27 18:43:09 -07:00
html += '<button type="button" class="' + cssClass + '"' + dataSrc + ' data-action="link" data-isfolder="' + channel.IsFolder + '" data-id="' + channel.Id + '" data-serverid="' + channel.ServerId + '" data-type="' + channel.Type + '">';
2016-04-26 11:28:04 -07:00
2016-08-02 18:32:16 -07:00
cssClass = 'guideChannelNumber';
if (hasChannelImage) {
cssClass += ' guideChannelNumberWithImage';
}
html += '<div class="' + cssClass + '">' + channel.Number + '</div>';
2016-04-27 18:43:09 -07:00
if (!hasChannelImage) {
html += '<div class="guideChannelName">' + channel.Name + '</div>';
}
2016-04-26 11:28:04 -07:00
html += '</button>';
}
var channelList = context.querySelector('.channelList');
channelList.innerHTML = html;
imageLoader.lazyChildren(channelList);
}
2016-05-25 09:10:57 -07:00
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
2016-04-26 11:28:04 -07:00
function renderGuide(context, date, channels, programs, apiClient) {
//var list = [];
//channels.forEach(function(i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels.forEach(function (i) {
// list.push(i);
//});
//channels = list;
2016-05-25 09:10:57 -07:00
var activeElement = document.activeElement;
var itemId = activeElement && activeElement.getAttribute ? activeElement.getAttribute('data-id') : null;
var channelRowId = null;
if (activeElement) {
channelRowId = parentWithClass(activeElement, 'channelPrograms');
channelRowId = channelRowId && channelRowId.getAttribute ? channelRowId.getAttribute('data-channelid') : null;
}
2016-04-26 11:28:04 -07:00
renderChannelHeaders(context, channels, apiClient);
var startDate = date;
var endDate = new Date(startDate.getTime() + msPerDay);
context.querySelector('.timeslotHeaders').innerHTML = getTimeslotHeadersHtml(startDate, endDate);
2016-06-06 10:33:27 -07:00
startCurrentTimeUpdateInterval();
2016-04-26 11:28:04 -07:00
items = {};
renderPrograms(context, date, channels, programs);
if (layoutManager.tv) {
2016-05-25 09:10:57 -07:00
var focusElem;
if (itemId) {
focusElem = context.querySelector('[data-id="' + itemId + '"]')
}
if (focusElem) {
focusManager.focus(focusElem);
} else {
var autoFocusParent;
if (channelRowId) {
autoFocusParent = context.querySelector('[data-channelid="' + channelRowId + '"]')
}
if (!autoFocusParent) {
autoFocusParent = context.querySelector('.programGrid');
}
focusManager.autoFocus(autoFocusParent, true);
}
2016-04-26 11:28:04 -07:00
}
}
function nativeScrollTo(container, pos, horizontal) {
if (container.scrollTo) {
if (horizontal) {
container.scrollTo(pos, 0);
} else {
container.scrollTo(0, pos);
}
} else {
if (horizontal) {
container.scrollLeft = Math.round(pos);
} else {
container.scrollTop = Math.round(pos);
}
}
}
var lastGridScroll = 0;
var lastHeaderScroll = 0;
function onProgramGridScroll(context, elem, timeslotHeaders) {
if ((new Date().getTime() - lastHeaderScroll) >= 1000) {
lastGridScroll = new Date().getTime();
nativeScrollTo(timeslotHeaders, elem.scrollLeft, true);
}
}
function onTimeslotHeadersScroll(context, elem, programGrid) {
if ((new Date().getTime() - lastGridScroll) >= 1000) {
lastHeaderScroll = new Date().getTime();
nativeScrollTo(programGrid, elem.scrollLeft, true);
}
}
function changeDate(page, date) {
2016-06-06 10:33:27 -07:00
clearCurrentTimeUpdateInterval();
2016-04-26 11:28:04 -07:00
var newStartDate = normalizeDateToTimeslot(date);
currentDate = newStartDate;
reloadGuide(page, newStartDate);
2016-09-07 13:11:16 -07:00
var dateText = datetime.getLocaleDateStringParts(date);
2016-09-07 00:08:20 -07:00
if (dateText.length == 1) {
dateText = dateText[0];
} else {
dateText = '<div>' + dateText[0] + '</div><div class="guideDateTextDate">' + dateText[1] + '</div>';
}
page.querySelector('.guideDateText').innerHTML = dateText;
2016-04-26 11:28:04 -07:00
}
var dateOptions = [];
function setDateRange(page, guideInfo) {
var today = new Date();
today.setHours(today.getHours(), 0, 0, 0);
var start = datetime.parseISO8601Date(guideInfo.StartDate, { toLocal: true });
var end = datetime.parseISO8601Date(guideInfo.EndDate, { toLocal: true });
start.setHours(0, 0, 0, 0);
end.setHours(0, 0, 0, 0);
if (start.getTime() >= end.getTime()) {
end.setDate(start.getDate() + 1);
}
start = new Date(Math.max(today, start));
dateOptions = [];
while (start <= end) {
dateOptions.push({
2016-09-07 13:11:16 -07:00
name: datetime.getLocaleDateStringParts(start).join(' '),
2016-04-26 11:28:04 -07:00
id: start.getTime()
});
start.setDate(start.getDate() + 1);
start.setHours(0, 0, 0, 0);
}
var date = new Date();
if (currentDate) {
date.setTime(currentDate.getTime());
}
changeDate(page, date);
}
function reloadPage(page) {
showLoading();
var apiClient = connectionManager.currentApiClient();
apiClient.getLiveTvGuideInfo().then(function (guideInfo) {
setDateRange(page, guideInfo);
});
}
function selectDate(page) {
2016-09-05 22:02:05 -07:00
var selectedDate = currentDate || new Date();
dateOptions.forEach(function (d) {
d.selected = new Date(d.id).getDate() == selectedDate.getDate();
});
2016-04-26 11:28:04 -07:00
require(['actionsheet'], function (actionsheet) {
actionsheet.show({
items: dateOptions,
2016-05-12 12:21:43 -07:00
title: globalize.translate('sharedcomponents#HeaderSelectDate'),
2016-04-26 11:28:04 -07:00
callback: function (id) {
var date = new Date();
date.setTime(parseInt(id));
changeDate(page, date);
}
});
});
}
2016-07-26 22:19:56 -07:00
function setScrollEvents(view, enabled) {
2016-04-26 11:28:04 -07:00
if (layoutManager.tv) {
2016-07-26 22:19:56 -07:00
require(['scrollHelper'], function (scrollHelper) {
2016-04-26 11:28:04 -07:00
2016-07-26 22:19:56 -07:00
var fn = enabled ? 'on' : 'off';
scrollHelper.centerFocus[fn](view.querySelector('.smoothScrollY'), false);
scrollHelper.centerFocus[fn](view.querySelector('.programGrid'), true);
});
2016-04-26 11:28:04 -07:00
}
}
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function onProgramGridFocus(e) {
var programCell = parentWithClass(e.target, 'programCell');
if (!programCell) {
return;
}
2016-08-29 11:42:53 -07:00
var focused = e.target;
var id = focused.getAttribute('data-id');
var item = items[id];
2016-04-26 11:28:04 -07:00
2016-08-29 11:42:53 -07:00
if (item) {
events.trigger(self, 'focus', [
{
item: item
}]);
2016-04-26 11:28:04 -07:00
}
}
2016-06-08 10:10:07 -07:00
function onTimerCreated(e, apiClient, data) {
var programId = data.ProgramId;
// This could be null, not supported by all tv providers
var newTimerId = data.Id;
// find guide cells by program id, ensure timer icon
var cells = options.element.querySelectorAll('.programCell[data-id="' + programId + '"]');
for (var i = 0, length = cells.length; i < length; i++) {
var cell = cells[i];
var icon = cell.querySelector('.timerIcon');
if (!icon) {
2016-06-09 23:54:03 -07:00
cell.insertAdjacentHTML('beforeend', '<i class="timerIcon md-icon">fiber_manual_record</i>');
2016-06-08 10:10:07 -07:00
}
if (newTimerId) {
cell.setAttribute('data-timerid', newTimerId);
}
}
}
function onSeriesTimerCreated(e, apiClient, data) {
}
function onTimerCancelled(e, apiClient, data) {
var id = data.Id;
// find guide cells by timer id, remove timer icon
var cells = options.element.querySelectorAll('.programCell[data-timerid="' + id + '"]');
for (var i = 0, length = cells.length; i < length; i++) {
var cell = cells[i];
2016-06-08 10:10:07 -07:00
var icon = cell.querySelector('.timerIcon');
if (icon) {
icon.parentNode.removeChild(icon);
}
cell.removeAttribute('data-timerid');
}
}
function onSeriesTimerCancelled(e, apiClient, data) {
var id = data.Id;
// find guide cells by timer id, remove timer icon
var cells = options.element.querySelectorAll('.programCell[data-seriestimerid="' + id + '"]');
for (var i = 0, length = cells.length; i < length; i++) {
var cell = cells[i];
2016-06-08 10:10:07 -07:00
var icon = cell.querySelector('.seriesTimerIcon');
if (icon) {
icon.parentNode.removeChild(icon);
}
cell.removeAttribute('data-seriestimerid');
}
}
2016-05-12 19:32:12 -07:00
require(['text!./tvguide.template.html'], function (template) {
2016-04-26 11:28:04 -07:00
var context = options.element;
2016-06-09 09:13:25 -07:00
context.innerHTML = globalize.translateDocument(template, 'sharedcomponents');
2016-04-26 11:28:04 -07:00
if (layoutManager.desktop) {
var visibleGuideScrollers = context.querySelectorAll('.guideScroller');
for (var i = 0, length = visibleGuideScrollers.length; i < length; i++) {
visibleGuideScrollers[i].classList.add('visibleGuideScroller');
}
}
2016-04-26 11:28:04 -07:00
var programGrid = context.querySelector('.programGrid');
var timeslotHeaders = context.querySelector('.timeslotHeaders');
programGrid.addEventListener('focus', onProgramGridFocus, true);
2016-08-07 12:43:52 -07:00
dom.addEventListener(programGrid, 'scroll', function (e) {
2016-04-26 11:28:04 -07:00
onProgramGridScroll(context, this, timeslotHeaders);
2016-05-04 21:18:35 -07:00
}, {
passive: true
2016-04-26 11:28:04 -07:00
});
2016-08-07 12:43:52 -07:00
dom.addEventListener(timeslotHeaders, 'scroll', function () {
2016-04-26 11:28:04 -07:00
onTimeslotHeadersScroll(context, this, programGrid);
2016-05-04 21:18:35 -07:00
}, {
passive: true
2016-04-26 11:28:04 -07:00
});
context.querySelector('.btnSelectDate').addEventListener('click', function () {
selectDate(context);
});
context.querySelector('.btnUnlockGuide').addEventListener('click', function () {
2016-08-18 10:03:22 -07:00
currentStartIndex = 0;
channelsPromise = null;
reloadPage(context);
});
context.querySelector('.btnNextPage').addEventListener('click', function () {
currentStartIndex += currentChannelLimit;
channelsPromise = null;
reloadPage(context);
});
context.querySelector('.btnPreviousPage').addEventListener('click', function () {
currentStartIndex = Math.max(currentStartIndex - currentChannelLimit, 0);
channelsPromise = null;
2016-04-26 11:28:04 -07:00
reloadPage(context);
});
2016-09-07 00:08:20 -07:00
context.querySelector('.btnGuideViewSettings').addEventListener('click', function () {
2016-09-05 22:02:05 -07:00
showViewSettings(self);
});
2016-04-26 11:28:04 -07:00
context.classList.add('tvguide');
2016-07-26 22:19:56 -07:00
setScrollEvents(context, true);
2016-04-26 11:28:04 -07:00
itemShortcuts.on(context);
2016-04-26 19:59:43 -07:00
events.trigger(self, 'load');
2016-06-08 10:10:07 -07:00
events.on(serverNotifications, 'TimerCreated', onTimerCreated);
events.on(serverNotifications, 'SeriesTimerCreated', onSeriesTimerCreated);
events.on(serverNotifications, 'TimerCancelled', onTimerCancelled);
events.on(serverNotifications, 'SeriesTimerCancelled', onSeriesTimerCancelled);
2016-04-26 11:28:04 -07:00
self.refresh();
2016-05-12 19:32:12 -07:00
});
2016-04-26 11:28:04 -07:00
};
return Guide;
});