define(['imageLoader', 'layoutManager', 'jQuery'], function (imageLoader, layoutManager, $) {
var mainDrawerPanel = document.querySelector('.mainDrawerPanel');
function renderHeader() {
var html = '';
html += '
';
var backIcon = browserInfo.safari ? 'chevron-left' : 'arrow-back';
html += '';
html += '';
html += '';
html += '
' + Globalize.translate('ButtonHome') + '
';
html += '
';
html += '';
html += '';
if (AppInfo.enableSearchInTopMenu) {
html += '';
html += '
';
html += '';
html += '
';
}
html += '';
html += '';
html += '';
if (!browserInfo.mobile && !Dashboard.isConnectMode()) {
html += '';
}
html += '
';
html += '
';
html += '
';
html += '
';
var viewMenuBar = document.createElement('div');
viewMenuBar.classList.add('viewMenuBar');
viewMenuBar.innerHTML = html;
document.querySelector('.skinHeader').appendChild(viewMenuBar);
imageLoader.lazyChildren(document.querySelector('.viewMenuBar'));
document.dispatchEvent(new CustomEvent("headercreated", {}));
bindMenuEvents();
}
function onBackClick() {
if (Dashboard.exitOnBack()) {
Dashboard.exit();
}
else {
history.back();
}
}
function updateUserInHeader(user) {
var header = document.querySelector('.viewMenuBar');
if (!header) {
return;
}
var headerUserButton = header.querySelector('.headerUserButton');
var hasImage;
if (user && user.name) {
if (user.imageUrl) {
var userButtonHeight = 26;
var url = user.imageUrl;
if (user.supportsImageParams) {
url += "&height=" + Math.round((userButtonHeight * Math.max(window.devicePixelRatio || 1, 2)));
}
if (headerUserButton) {
updateHeaderUserButton(headerUserButton, url, null);
hasImage = true;
}
}
}
if (headerUserButton && !hasImage) {
updateHeaderUserButton(headerUserButton, null, 'person');
}
if (user) {
updateLocalUser(user.localUser);
}
requiresUserRefresh = false;
}
function updateHeaderUserButton(headerUserButton, src, icon) {
if (src) {
headerUserButton.classList.add('headerUserButtonRound');
headerUserButton.innerHTML = '';
} else if (icon) {
headerUserButton.classList.remove('headerUserButtonRound');
headerUserButton.innerHTML = '';
} else {
headerUserButton.classList.remove('headerUserButtonRound');
}
}
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');
}
}
require(['voice/voice'], function (voice) {
if (voice.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 showVoice() {
require(['voice/voice'], function (voice) {
voice.startListening();
});
}
function onHeaderUserButtonClick(e) {
Dashboard.showUserFlyout(e.target);
}
function onHeaderAppsButtonClick() {
require(['dialogHelper'], function (dialogHelper) {
var dlg = dialogHelper.createDialog({
removeOnClose: true,
modal: false,
autoFocus: false,
entryAnimationDuration: 160,
exitAnimationDuration: 160,
enableHistory: false
});
dlg.classList.add('ui-body-a');
dlg.classList.add('background-theme-a');
dlg.classList.add('adminAppsMenu');
var html = '';
html += '
';
var drawer = mainDrawerPanel.querySelector('.mainDrawer');
drawer.innerHTML = html;
var lnkManageServer = drawer.querySelector('.lnkManageServer');
if (lnkManageServer) {
lnkManageServer.addEventListener('click', onManageServerClicked);
}
require(['imageLoader'], function (imageLoader) {
imageLoader.fillImages(mainDrawerPanel.getElementsByClassName('lazy'));
});
}
function refreshDashboardInfoInDrawer(page, user) {
if (!mainDrawerPanel.querySelector('.adminDrawerLogo')) {
createDashboardMenu(page);
} else {
updateDashboardMenuSelectedItem();
}
}
function parentWithTag(elem, tagName) {
while (elem.tagName != tagName) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function updateDashboardMenuSelectedItem() {
var links = mainDrawerPanel.querySelectorAll('.sidebarLink');
for (var i = 0, length = links.length; i < length; i++) {
var link = links[i];
var selected = false;
var pageIds = link.getAttribute('data-pageids');
if (pageIds) {
selected = pageIds.split(',').indexOf($.mobile.activePage.id) != -1
}
if (selected) {
link.classList.add('selectedSidebarLink');
var title = '';
var secondaryTitle = (link.innerText || link.textContent).trim();
title += secondaryTitle;
var documentTitle = secondaryTitle;
Dashboard.setPageTitle(title, documentTitle);
} else {
link.classList.remove('selectedSidebarLink');
}
}
}
function createDashboardMenu() {
var html = '';
html += ''
html += '';
html += '';
html += Dashboard.getToolsMenuHtml();
html = html.split('href=').join('onclick="return LibraryMenu.onLinkClicked(event, this);" href=');
mainDrawerPanel.querySelector('.mainDrawer').innerHTML = html;
updateDashboardMenuSelectedItem();
}
function onSidebarLinkClick() {
var section = this.getElementsByClassName('sectionName')[0];
var text = section ? section.innerHTML : this.innerHTML;
LibraryMenu.setTitle(text);
}
function getUserViews(apiClient, userId) {
return 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 = Object.assign({}, 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);
}
}
return list;
});
}
function showBySelector(selector, show) {
var elem = document.querySelector(selector);
if (elem) {
if (show) {
elem.classList.remove('hide');
} else {
elem.classList.add('hide');
}
}
}
function updateLibraryMenu(user) {
if (!user) {
showBySelector('.lnkMySync', false);
showBySelector('.userMenuOptions', false);
return;
}
if (user.Policy.EnableSync) {
showBySelector('.lnkMySync', true);
} else {
showBySelector('.lnkMySync', false);
}
var userId = Dashboard.getCurrentUserId();
var apiClient = window.ApiClient;
var libraryMenuOptions = document.querySelector('.libraryMenuOptions');
if (!libraryMenuOptions) {
return;
}
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('');
libraryMenuOptions.innerHTML = html;
var elem = libraryMenuOptions;
var sidebarLinks = elem.querySelectorAll('.sidebarLink');
for (var i = 0, length = sidebarLinks.length; i < length; i++) {
sidebarLinks[i].removeEventListener('click', onSidebarLinkClick);
sidebarLinks[i].addEventListener('click', onSidebarLinkClick);
}
});
}
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 : 200;
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 : 200;
setTimeout(function () {
Dashboard.logout();
}, delay);
}
return false;
},
onHardwareMenuButtonClick: function () {
toggleMainDrawer();
},
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;
},
setTabs: function (type, selectedIndex, builder) {
var viewMenuBarTabs;
if (!type) {
if (LibraryMenu.tabType) {
mainDrawerPanel.classList.remove('withTallToolbar');
viewMenuBarTabs = document.querySelector('.viewMenuBarTabs');
viewMenuBarTabs.innerHTML = '';
viewMenuBarTabs.classList.add('hide');
LibraryMenu.tabType = null;
}
return;
}
viewMenuBarTabs = document.querySelector('.viewMenuBarTabs');
if (!LibraryMenu.tabType) {
viewMenuBarTabs.classList.remove('hide');
}
if (LibraryMenu.tabType != type) {
require(['paper-tabs'], function () {
var noInk = browserInfo.animate ? '' : ' noink';
viewMenuBarTabs.innerHTML = '' + builder().map(function (t) {
return '