(function (window, document, $, devicePixelRatio) { function renderHeader() { var html = ''; var backIcon = browserInfo.safari ? 'chevron-left' : 'arrow-back'; html += ''; if (AppInfo.enableNavDrawer) { html += ''; } html += '
' + Globalize.translate('ButtonHome') + '
'; html += '
'; html += ''; html += ''; if (AppInfo.enableSearchInTopMenu) { html += ''; html += '
'; html += '
'; html += ''; html += ''; html += '
'; html += '
'; } html += ''; html += '
0
'; if (!showUserAtTop()) { html += ''; } if (!browserInfo.mobile && !Dashboard.isConnectMode()) { html += ''; } html += '
'; var viewMenuBar = document.createElement('div'); viewMenuBar.classList.add('viewMenuBar'); viewMenuBar.classList.add('ui-body-b'); viewMenuBar.innerHTML = html; document.body.appendChild(viewMenuBar); ImageLoader.lazyChildren(document.querySelector('.viewMenuBar')); document.dispatchEvent(new CustomEvent("headercreated", {})); bindMenuEvents(); } function onBackClick() { if (Dashboard.exitOnBack()) { Dashboard.exit(); } else { history.back(); } } function addUserToHeader(user) { var header = document.querySelector('.viewMenuBar'); if (user.name) { if (user.imageUrl && AppInfo.enableUserImage) { var userButtonHeight = 26; var url = user.imageUrl; if (user.supportsImageParams) { url += "&height=" + (userButtonHeight * Math.max(devicePixelRatio || 1, 2)); } var headerUserButton = header.querySelector('.headerUserButton'); if (headerUserButton) { headerUserButton.icon = null; headerUserButton.src = url; headerUserButton.classList.add('headerUserButtonRound'); } } } updateLocalUser(user.localUser); requiresUserRefresh = false; } function updateLocalUser(user) { var header = document.querySelector('.viewMenuBar'); var headerSearchButton = header.querySelector('.headerSearchButton'); var btnCast = header.querySelector('.btnCast'); var dashboardEntryHeaderButton = header.querySelector('.dashboardEntryHeaderButton'); if (user) { btnCast.classList.remove('hide'); if (headerSearchButton) { headerSearchButton.classList.remove('hide'); } if (dashboardEntryHeaderButton) { if (user.Policy.IsAdministrator) { dashboardEntryHeaderButton.classList.remove('hide'); } else { dashboardEntryHeaderButton.classList.add('hide'); } } requirejs(['voice/voice'], function () { if (VoiceInputManager.isSupported()) { header.querySelector('.headerVoiceButton').classList.remove('hide'); } else { header.querySelector('.headerVoiceButton').classList.add('hide'); } }); } else { btnCast.classList.add('hide'); header.querySelector('.headerVoiceButton').classList.add('hide'); if (headerSearchButton) { headerSearchButton.classList.add('hide'); } if (dashboardEntryHeaderButton) { dashboardEntryHeaderButton.classList.add('hide'); } } } function removeUserFromHeader() { updateLocalUser(null); } function bindMenuEvents() { var mainDrawerButton = document.querySelector('.mainDrawerButton'); if (mainDrawerButton) { mainDrawerButton.addEventListener('click', openMainDrawer); } var headerBackButton = document.querySelector('.headerBackButton'); if (headerBackButton) { headerBackButton.addEventListener('click', onBackClick); } var viewMenuBar = document.querySelector(".viewMenuBar"); initHeadRoom(viewMenuBar); viewMenuBar.querySelector('.btnNotifications').addEventListener('click', function () { Dashboard.navigate('notificationlist.html'); }); } function getItemHref(item, context) { return LibraryBrowser.getHref(item, context); } var requiresDrawerRefresh = true; var requiresDashboardDrawerRefresh = true; var requiresUserRefresh = true; var lastOpenTime = new Date().getTime(); function openMainDrawer() { var drawerPanel = document.querySelector('.mainDrawerPanel'); drawerPanel.openDrawer(); lastOpenTime = new Date().getTime(); } function onMainDrawerOpened() { if (browserInfo.mobile) { document.body.classList.add('bodyWithPopupOpen'); } var pageElem = $($.mobile.activePage)[0]; if (requiresDrawerRefresh || requiresDashboardDrawerRefresh) { ConnectionManager.user(window.ApiClient).then(function (user) { var drawer = document.querySelector('.mainDrawerPanel .mainDrawer'); if (requiresDrawerRefresh) { ensureDrawerStructure(drawer); refreshUserInfoInDrawer(user, drawer); refreshLibraryInfoInDrawer(user, drawer); refreshBottomUserInfoInDrawer(user, drawer); Events.trigger(document, 'libraryMenuCreated'); updateLibraryMenu(user.localUser); } if (requiresDrawerRefresh || requiresDashboardDrawerRefresh) { refreshDashboardInfoInDrawer(pageElem, user, drawer); requiresDashboardDrawerRefresh = false; } requiresDrawerRefresh = false; }); } updateLibraryNavLinks(pageElem); document.querySelector('.mainDrawerPanel #drawer').classList.add('verticalScrollingDrawer'); } function closeMainDrawer() { document.querySelector('.mainDrawerPanel').closeDrawer(); } function onMainDrawerSelect(e) { var drawer = e.target; if (drawer.selected != 'drawer') { document.body.classList.remove('bodyWithPopupOpen'); document.querySelector('.mainDrawerPanel #drawer').classList.remove('verticalScrollingDrawer'); } else { onMainDrawerOpened(); } } function ensureDrawerStructure(drawer) { if (drawer.querySelector('.mainDrawerContent')) { return; } var html = '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; html += '
'; drawer.innerHTML = html; } function refreshUserInfoInDrawer(user, drawer) { var html = ''; var userAtTop = showUserAtTop(); var homeHref = window.ApiClient ? 'index.html' : 'selectserver.html?showuser=1'; var hasUserImage = user.imageUrl && AppInfo.enableUserImage; if (userAtTop) { html += '
'; html += '
'; html += '
'; var imgWidth = 60; if (hasUserImage) { var url = user.imageUrl; if (user.supportsImageParams) { url += "&width=" + (imgWidth * Math.max(devicePixelRatio || 1, 2)); html += '
'; } } else { html += '
'; } html += '
'; html += user.name; html += '
'; html += '
'; html += '
'; html += '
'; html += '' + Globalize.translate('ButtonHome') + ''; } else { html += '
'; html += ''; html += '
'; html += Globalize.translate('ButtonHome'); html += '
'; } html += '' + Globalize.translate('ButtonRemote') + ''; var userHeader = drawer.querySelector('.userheader'); userHeader.innerHTML = html; ImageLoader.fillImages(userHeader.getElementsByClassName('lazy')); } function refreshLibraryInfoInDrawer(user, drawer) { var html = ''; html += '
'; html += '
'; html += '
'; drawer.querySelector('.libraryDrawerContent').innerHTML = html; } function refreshDashboardInfoInDrawer(page, user, drawer) { var html = ''; html += '
'; html += Dashboard.getToolsMenuHtml(page); html = html.split('href=').join('onclick="return LibraryMenu.onLinkClicked(event, this);" href='); drawer.querySelector('.dashboardDrawerContent').innerHTML = html; } function refreshBottomUserInfoInDrawer(user, drawer) { var html = ''; html += '
'; html += '
'; html += '
'; html += Globalize.translate('HeaderAdmin'); html += '
'; html += '' + Globalize.translate('ButtonManageServer') + ''; html += '' + Globalize.translate('ButtonMetadataManager') + ''; if (!browserInfo.mobile) { html += '' + Globalize.translate('ButtonReports') + ''; } html += '
'; html += '
'; html += '
'; if (user.localUser && showUserAtTop()) { html += '' + Globalize.translate('ButtonSettings') + ''; } html += '' + Globalize.translate('ButtonSync') + ''; if (Dashboard.isConnectMode()) { html += '' + Globalize.translate('ButtonSelectServer') + ''; } if (showUserAtTop()) { html += '' + Globalize.translate('ButtonSignOut') + ''; } html += '
'; drawer.querySelector('.userFooter').innerHTML = html; drawer.querySelector('.lnkManageServer').addEventListener('click', onManageServerClicked); } function onSidebarLinkClick() { var section = this.getElementsByClassName('sectionName')[0]; var text = section ? section.innerHTML : this.innerHTML; LibraryMenu.setTitle(text); } function getUserViews(apiClient, userId) { var deferred = $.Deferred(); apiClient.getUserViews({}, userId).then(function (result) { var items = result.Items; var list = []; for (var i = 0, length = items.length; i < length; i++) { var view = items[i]; list.push(view); if (view.CollectionType == 'livetv') { view.ImageTags = {}; view.icon = 'live-tv'; view.onclick = "LibraryBrowser.showTab('livetv.html', 0);"; var guideView = $.extend({}, view); guideView.Name = Globalize.translate('ButtonGuide'); guideView.ImageTags = {}; guideView.icon = 'dvr'; guideView.url = 'livetv.html?tab=1'; guideView.onclick = "LibraryBrowser.showTab('livetv.html', 1);"; list.push(guideView); var recordedTvView = $.extend({}, view); recordedTvView.Name = Globalize.translate('ButtonRecordedTv'); recordedTvView.ImageTags = {}; recordedTvView.icon = 'video-library'; recordedTvView.url = 'livetv.html?tab=3'; recordedTvView.onclick = "LibraryBrowser.showTab('livetv.html', 3);"; list.push(recordedTvView); } } deferred.resolveWith(null, [list]); }); return deferred.promise(); } function updateLibraryMenu(user) { if (!user) { $('.adminMenuOptions').addClass('hide'); $('.lnkMySync').addClass('hide'); $('.userMenuOptions').addClass('hide'); return; } var userId = Dashboard.getCurrentUserId(); var apiClient = window.ApiClient; getUserViews(apiClient, userId).then(function (result) { var items = result; var html = ''; html += '
'; html += Globalize.translate('HeaderMedia'); html += '
'; html += items.map(function (i) { var icon = 'folder'; var color = 'inherit'; var itemId = i.Id; if (i.CollectionType == "channels") { itemId = "channels"; } else if (i.CollectionType == "livetv") { itemId = "livetv"; } if (i.CollectionType == "photos") { icon = 'photo-library'; color = "#009688"; } else if (i.CollectionType == "music" || i.CollectionType == "musicvideos") { icon = 'library-music'; color = '#FB8521'; } else if (i.CollectionType == "books") { icon = 'library-books'; color = "#1AA1E1"; } else if (i.CollectionType == "playlists") { icon = 'view-list'; color = "#795548"; } else if (i.CollectionType == "games") { icon = 'games'; color = "#F44336"; } else if (i.CollectionType == "movies") { icon = 'video-library'; color = '#CE5043'; } else if (i.CollectionType == "channels" || i.Type == 'Channel') { icon = 'videocam'; color = '#E91E63'; } else if (i.CollectionType == "tvshows") { icon = 'tv'; color = "#4CAF50"; } else if (i.CollectionType == "livetv") { icon = 'live-tv'; color = "#293AAE"; } icon = i.icon || icon; var onclick = i.onclick ? ' function(){' + i.onclick + '}' : 'null'; return '' + i.Name + ''; }).join(''); var libraryMenuOptions = document.querySelector('.libraryMenuOptions'); libraryMenuOptions.innerHTML = html; var elem = libraryMenuOptions; $('.sidebarLink', elem).off('click', onSidebarLinkClick).on('click', onSidebarLinkClick); }); if (user.Policy.IsAdministrator) { $('.adminMenuOptions').removeClass('hide'); } else { $('.adminMenuOptions').addClass('hide'); } if (user.Policy.EnableSync) { $('.lnkMySync').removeClass('hide'); } else { $('.lnkMySync').addClass('hide'); } } function showUserAtTop() { return Dashboard.isConnectMode() || browserInfo.mobile; } var requiresLibraryMenuRefresh = false; function onManageServerClicked() { closeMainDrawer(); Dashboard.navigate('dashboard.html'); } function getTopParentId() { return getParameterByName('topParentId') || null; } window.LibraryMenu = { getTopParentId: getTopParentId, onLinkClicked: function (event, link, action) { if (event.which != 1) { return true; } // There doesn't seem to be a way to detect if the drawer is in the process of opening, so try to handle that here if ((new Date().getTime() - lastOpenTime) > 200) { setTimeout(function () { closeMainDrawer(); // On mobile devices don't navigate until after the closing animation has completed or it may stutter var delay = browserInfo.mobile ? 350 : 150; setTimeout(function () { if (action) { action(); } else { Dashboard.navigate(link.href); } }, delay); }, 50); } event.stopPropagation(); event.preventDefault(); return false; }, onLogoutClicked: function () { // There doesn't seem to be a way to detect if the drawer is in the process of opening, so try to handle that here if ((new Date().getTime() - lastOpenTime) > 200) { closeMainDrawer(); // On mobile devices don't navigate until after the closing animation has completed or it may stutter var delay = browserInfo.mobile ? 350 : 150; setTimeout(function () { Dashboard.logout(); }, delay); } return false; }, onHardwareMenuButtonClick: function () { openMainDrawer(); }, onSettingsClicked: function (event) { if (event.which != 1) { return true; } // There doesn't seem to be a way to detect if the drawer is in the process of opening, so try to handle that here Dashboard.navigate('dashboard.html'); return false; }, setTitle: function (title) { document.querySelector('.libraryMenuButtonText').innerHTML = title; }, setBackButtonVisible: function (visible) { var backButton = document.querySelector('.headerBackButton'); if (backButton) { if (visible) { backButton.classList.remove('hide'); } else { backButton.classList.add('hide'); } } }, setMenuButtonVisible: function (visible) { var mainDrawerButton = document.querySelector('.mainDrawerButton'); if (mainDrawerButton) { if (!visible && browserInfo.mobile) { mainDrawerButton.classList.remove('hide'); } else { mainDrawerButton.classList.remove('hide'); } } }, setTransparentMenu: function (transparent) { var viewMenuBar = document.querySelector('.viewMenuBar'); if (viewMenuBar) { if (transparent) { viewMenuBar.classList.add('semiTransparent'); } else { viewMenuBar.classList.remove('semiTransparent'); } } } }; function updateCastIcon() { var info = MediaController.getPlayerInfo(); if (info.isLocalPlayer) { $('.btnCast').removeClass('btnActiveCast').each(function () { this.icon = 'cast'; }); $('.headerSelectedPlayer').html(''); } else { $('.btnCast').addClass('btnActiveCast').each(function () { this.icon = 'cast-connected'; }); $('.headerSelectedPlayer').html((info.deviceName || info.name)); } } function updateLibraryNavLinks(page) { var isLiveTvPage = page.classList.contains('liveTvPage'); var isChannelsPage = page.classList.contains('channelsPage'); var isEditorPage = page.classList.contains('metadataEditorPage'); var isReportsPage = page.classList.contains('reportsPage'); var isMySyncPage = page.classList.contains('mySyncPage'); var id = isLiveTvPage || isChannelsPage || isEditorPage || isReportsPage || isMySyncPage || page.classList.contains('allLibraryPage') ? '' : getTopParentId() || ''; var i, length; var elems = document.getElementsByClassName('lnkMediaFolder'); for (i = 0, length = elems.length; i < length; i++) { var lnkMediaFolder = elems[i]; var itemId = lnkMediaFolder.getAttribute('data-itemid'); if (isChannelsPage && itemId == 'channels') { lnkMediaFolder.classList.add('selectedMediaFolder'); } else if (isLiveTvPage && itemId == 'livetv') { lnkMediaFolder.classList.add('selectedMediaFolder'); } else if (isEditorPage && itemId == 'editor') { lnkMediaFolder.classList.add('selectedMediaFolder'); } else if (isReportsPage && itemId == 'reports') { lnkMediaFolder.classList.add('selectedMediaFolder'); } else if (isMySyncPage && itemId == 'mysync') { lnkMediaFolder.classList.add('selectedMediaFolder'); } else if (id && itemId == id) { lnkMediaFolder.classList.add('selectedMediaFolder'); } else { lnkMediaFolder.classList.remove('selectedMediaFolder'); } } } function updateTabLinks(page) { var elems = page.querySelectorAll('.scopedLibraryViewNav a'); var id = page.classList.contains('liveTvPage') || page.classList.contains('channelsPage') || page.classList.contains('metadataEditorPage') || page.classList.contains('reportsPage') || page.classList.contains('mySyncPage') || page.classList.contains('allLibraryPage') ? '' : getTopParentId() || ''; if (!id) { return; } for (i = 0, length = elems.length; i < length; i++) { var lnk = elems[i]; var src = lnk.href; if (src.indexOf('#') != -1) { continue; } src = replaceQueryString(src, 'topParentId', id); lnk.href = src; } } function onWebSocketMessage(e, data) { var msg = data; if (msg.MessageType === "UserConfigurationUpdated") { if (msg.Data.Id == Dashboard.getCurrentUserId()) { requiresLibraryMenuRefresh = true; } } } function buildViewMenuBar(page) { var viewMenuBar = document.querySelector('.viewMenuBar'); if (page.classList.contains('standalonePage')) { viewMenuBar.classList.add('hide'); } else { viewMenuBar.classList.remove('hide'); } if (requiresUserRefresh) { ConnectionManager.user(window.ApiClient).then(addUserToHeader); } } pageClassOn('pagebeforeshow', 'page', function () { var page = this; if (page.classList.contains('type-interior')) { requiresDashboardDrawerRefresh = true; } buildViewMenuBar(page); updateTabLinks(page); }); pageClassOn('pageshow', 'page', function () { var page = this; if (!NavHelper.isBack()) { // Scroll back up so in case vertical scroll was messed with window.scrollTo(0, 0); } updateTitle(page); updateBackButton(page); var isLibraryPage = page.classList.contains('libraryPage'); if (isLibraryPage) { document.body.classList.add('libraryDocument'); document.body.classList.remove('dashboardDocument'); document.body.classList.remove('hideMainDrawer'); var navs = page.querySelectorAll('.libraryViewNav'); for (var i = 0, length = navs.length; i < length; i++) { initHeadRoom(navs[i]); } } else if (page.classList.contains('type-interior')) { document.body.classList.remove('libraryDocument'); document.body.classList.add('dashboardDocument'); document.body.classList.remove('hideMainDrawer'); } else { document.body.classList.remove('libraryDocument'); document.body.classList.remove('dashboardDocument'); document.body.classList.add('hideMainDrawer'); } // Set drawer background color var darkDrawer = false; if (!Dashboard.isConnectMode() && !browserInfo.mobile) { darkDrawer = true; } var drawer = document.querySelector('.mainDrawerPanel #drawer'); if (drawer) { if (darkDrawer) { drawer.classList.add('darkDrawer'); } else { drawer.classList.remove('darkDrawer'); } } }); function updateTitle(page) { var title = page.getAttribute('data-title') || page.getAttribute('data-contextname'); if (!title) { var titleKey = getParameterByName('titlekey'); if (titleKey) { title = Globalize.translate(titleKey); } } if (!title) { if (page.classList.contains('type-interior')) { title = Globalize.translate('ButtonHome'); } } if (title) { LibraryMenu.setTitle(title); } } function updateBackButton(page) { var canGoBack = !page.classList.contains('homePage') && history.length > 0; var backButton = document.querySelector('.headerBackButton'); var showBackButton = AppInfo.enableBackButton; if (!showBackButton) { showBackButton = page.getAttribute('data-backbutton') == 'true'; } if (backButton) { if (canGoBack && showBackButton) { backButton.classList.remove('hide'); } else { backButton.classList.add('hide'); } } } function initHeadRoom(elem) { if (!AppInfo.enableHeadRoom) { return; } requirejs(["headroom"], function () { // construct an instance of Headroom, passing the element var headroom = new Headroom(elem, { // or scroll tolerance per direction tolerance: { down: 40, up: 0 } }); // initialise headroom.init(); elem.classList.add('headroomEnabled'); }); } function initializeApiClient(apiClient) { requiresLibraryMenuRefresh = true; Events.off(apiClient, 'websocketmessage', onWebSocketMessage); Events.on(apiClient, 'websocketmessage', onWebSocketMessage); } if (window.ApiClient) { initializeApiClient(window.ApiClient); } var mainDrawerPanel = document.querySelector('.mainDrawerPanel'); mainDrawerPanel.addEventListener('iron-select', onMainDrawerSelect); renderHeader(); Events.on(ConnectionManager, 'apiclientcreated', function (e, apiClient) { initializeApiClient(apiClient); }); Events.on(ConnectionManager, 'localusersignedin', function (e, user) { requiresLibraryMenuRefresh = true; requiresDrawerRefresh = true; ConnectionManager.user(ConnectionManager.getApiClient(user.ServerId)).then(addUserToHeader); }); Events.on(ConnectionManager, 'localusersignedout', function () { requiresLibraryMenuRefresh = true; requiresDrawerRefresh = true; removeUserFromHeader(); }); Events.on(MediaController, 'playerchange', function () { updateCastIcon(); }); })(window, document, jQuery, window.devicePixelRatio); (function () { var isCurrentNavBack = false; window.addEventListener("navigate", function (e) { var data = e.detail.state || {}; var direction = data.direction; isCurrentNavBack = direction == 'back'; }); function isBack() { return isCurrentNavBack; } window.NavHelper = { isBack: isBack }; })();