define(['require', 'browser', 'globalize', 'connectionManager', 'serverNotifications', 'loading', 'datetime', 'focusManager', 'userSettings', 'imageLoader', 'events', 'layoutManager', 'itemShortcuts', 'registrationServices', 'dom', 'clearButtonStyle', 'css!./guide.css', 'programStyles', 'material-icons', 'scrollStyles', 'emby-button', 'paper-icon-button-light', 'emby-tabs'], function (require, browser, globalize, connectionManager, serverNotifications, loading, datetime, focusManager, userSettings, imageLoader, events, layoutManager, itemShortcuts, registrationServices, dom) { 'use strict'; function showViewSettings(instance) { require(['guide-settings-dialog'], function (guideSettingsDialog) { guideSettingsDialog.show(instance.categoryOptions).then(function () { instance.refresh(); }); }); } function Guide(options) { var self = this; var items = {}; self.options = options; self.categoryOptions = { categories: [] }; // 30 mins var cellCurationMinutes = 30; var cellDurationMs = cellCurationMinutes * 60 * 1000; var msPerDay = 86400000; var totalRendererdMs = msPerDay; var currentDate; var currentStartIndex = 0; var currentChannelLimit = 0; var autoRefreshInterval; self.refresh = function () { currentDate = null; reloadPage(options.element); restartAutoRefresh(); }; self.pause = function () { stopAutoRefresh(); }; self.resume = function (refreshData) { if (refreshData) { self.refresh(); } else { restartAutoRefresh(); } }; self.destroy = function () { stopAutoRefresh(); events.off(serverNotifications, 'TimerCreated', onTimerCreated); events.off(serverNotifications, 'SeriesTimerCreated', onSeriesTimerCreated); events.off(serverNotifications, 'TimerCancelled', onTimerCancelled); events.off(serverNotifications, 'SeriesTimerCancelled', onSeriesTimerCancelled); clearCurrentTimeUpdateInterval(); setScrollEvents(options.element, false); itemShortcuts.off(options.element); items = {}; }; function restartAutoRefresh() { stopAutoRefresh(); var intervalMs = 60000 * 15; // (minutes) autoRefreshInterval = setInterval(function () { self.refresh(); }, intervalMs); } function stopAutoRefresh() { if (autoRefreshInterval) { clearInterval(autoRefreshInterval); autoRefreshInterval = null; } } 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(); } var currentTimeUpdateInterval; var currentTimeIndicatorBar; var currentTimeIndicatorArrow; function startCurrentTimeUpdateInterval() { clearCurrentTimeUpdateInterval(); //currentTimeUpdateInterval = setInterval(updateCurrentTimeIndicator, 1000); currentTimeUpdateInterval = setInterval(updateCurrentTimeIndicator, 60000); updateCurrentTimeIndicator(); } function clearCurrentTimeUpdateInterval() { var interval = currentTimeUpdateInterval; if (interval) { clearInterval(interval); } currentTimeUpdateInterval = null; currentTimeIndicatorBar = null; currentTimeIndicatorArrow = null; } function updateCurrentTimeIndicator() { if (!currentTimeIndicatorBar) { currentTimeIndicatorBar = options.element.querySelector('.currentTimeIndicatorBar'); } if (!currentTimeIndicatorArrow) { currentTimeIndicatorArrow = options.element.querySelector('.currentTimeIndicatorArrowContainer'); } var dateDifference = new Date().getTime() - currentDate.getTime(); var pct = dateDifference > 0 ? (dateDifference / totalRendererdMs) : 0; pct = Math.min(pct, 1); if (pct <= 0 || pct >= 1) { currentTimeIndicatorBar.classList.add('hide'); currentTimeIndicatorArrow.classList.add('hide'); } else { currentTimeIndicatorBar.classList.remove('hide'); currentTimeIndicatorArrow.classList.remove('hide'); //pct *= 100; //pct = 100 - pct; //currentTimeIndicatorElement.style.width = (pct * 100) + '%'; currentTimeIndicatorBar.style.transform = 'scaleX(' + pct + ')'; currentTimeIndicatorArrow.style.transform = 'translateX(' + (pct * 100) + '%)'; } } function getChannelLimit(context) { return registrationServices.validateFeature('livetv').then(function () { var limit = browser.slow ? 100 : 500; context.querySelector('.guideRequiresUnlock').classList.add('hide'); return limit; }, function () { var limit = 5; context.querySelector('.guideRequiresUnlock').classList.remove('hide'); context.querySelector('.unlockText').innerHTML = globalize.translate('sharedcomponents#LiveTvGuideRequiresUnlock', limit); return limit; }); } function reloadGuide(context, newStartDate, focusProgramOnRender) { var apiClient = connectionManager.currentApiClient(); var channelQuery = { StartIndex: 0, EnableFavoriteSorting: userSettings.get('livetv-favoritechannelsattop') !== 'false' }; channelQuery.UserId = apiClient.getCurrentUserId(); getChannelLimit(context).then(function (channelLimit) { currentChannelLimit = channelLimit; showLoading(); channelQuery.StartIndex = currentStartIndex; channelQuery.Limit = channelLimit; channelQuery.AddCurrentProgram = false; channelQuery.EnableUserData = false; channelQuery.EnableImageTypes = "Primary"; var categories = self.categoryOptions.categories || []; var displayMovieContent = !categories.length || categories.indexOf('movies') !== -1; var displaySportsContent = !categories.length || categories.indexOf('sports') !== -1; var displayNewsContent = !categories.length || categories.indexOf('news') !== -1; var displayKidsContent = !categories.length || categories.indexOf('kids') !== -1; var displaySeriesContent = !categories.length || categories.indexOf('series') !== -1; if (displayMovieContent && displaySportsContent && displayNewsContent && displayKidsContent) { channelQuery.IsMovie = null; channelQuery.IsSports = null; channelQuery.IsKids = null; channelQuery.IsNews = null; channelQuery.IsSeries = null; } else { if (displayNewsContent) { channelQuery.IsNews = true; } if (displaySportsContent) { channelQuery.IsSports = true; } if (displayKidsContent) { channelQuery.IsKids = true; } if (displayMovieContent) { channelQuery.IsMovie = true; } if (displaySeriesContent) { channelQuery.IsSeries = true; } } if (userSettings.get('livetv-channelorder') === 'DatePlayed') { channelQuery.SortBy = "DatePlayed"; channelQuery.SortOrder = "Descending"; } else { channelQuery.SortBy = null; channelQuery.SortOrder = null; } 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); apiClient.getLiveTvChannels(channelQuery).then(function (channelsResult) { var btnPreviousPage = context.querySelector('.btnPreviousPage'); var btnNextPage = context.querySelector('.btnNextPage'); if (channelsResult.TotalRecordCount > channelLimit) { context.querySelector('.guideOptions').classList.remove('hide'); btnPreviousPage.classList.remove('hide'); btnNextPage.classList.remove('hide'); if (channelQuery.StartIndex) { context.querySelector('.btnPreviousPage').disabled = false; } else { context.querySelector('.btnPreviousPage').disabled = true; } if ((channelQuery.StartIndex + channelLimit) < channelsResult.TotalRecordCount) { btnNextPage.disabled = false; } else { btnNextPage.disabled = true; } } else { context.querySelector('.guideOptions').classList.add('hide'); } apiClient.getLiveTvPrograms({ UserId: apiClient.getCurrentUserId(), MaxStartDate: nextDay.toISOString(), MinEndDate: date.toISOString(), channelIds: channelsResult.Items.map(function (c) { return c.Id; }).join(','), ImageTypeLimit: 1, EnableImages: false, //EnableImageTypes: layoutManager.tv ? "Primary,Backdrop" : "Primary", SortBy: "StartDate", EnableTotalRecordCount: false, EnableUserData: false }).then(function (programsResult) { renderGuide(context, date, channelsResult.Items, programsResult.Items, apiClient, focusProgramOnRender); 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 += '