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'], 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(); }); }); } function showCategoryOptions(instance) { require(['guide-categories-dialog'], function (guideCategoriesDialog) { guideCategoriesDialog.show(instance.categoryOptions).then(function (categoryOptions) { instance.categoryOptions = categoryOptions; 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 channelQuery = { StartIndex: 0, EnableFavoriteSorting: true }; var channelsPromise; 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(); setScrollEvents(options.element, false); itemShortcuts.off(options.element); items = {}; }; 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) { var apiClient = connectionManager.currentApiClient(); 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"; 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) { 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); 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 += '