(function (window, document, $) {
function renderHeader(user) {
var html = '
';
$(document.body).prepend(html);
$('.viewMenuBar').trigger('create');
$(document).trigger('headercreated');
$('.libraryMenuButton').createHoverTouch().on('hovertouch', showLibraryMenu);
$('.dashboardMenuButton').createHoverTouch().on('hovertouch', showDashboardMenu);
}
function getItemHref(item, context) {
return LibraryBrowser.getHref(item, context);
}
function getViewsHtml() {
var html = '';
html += '';
html += '';
return html;
}
function showLibraryMenu() {
var page = $.mobile.activePage;
var panel;
panel = getLibraryMenu();
updateLibraryNavLinks(page);
$(panel).panel('toggle').off('mouseleave.librarymenu').on('mouseleave.librarymenu', function () {
$(this).panel("close");
});
}
function showDashboardMenu() {
var page = $.mobile.activePage;
var panel = getDashboardMenu(page);
$(panel).panel('toggle').off('mouseleave.librarymenu').on('mouseleave.librarymenu', function () {
$(this).panel("close");
});
}
function updateLibraryMenu(panel) {
var apiClient = ConnectionManager.currentApiClient();
if (!apiClient) {
$('.adminMenuOptions').hide();
return;
}
var userId = Dashboard.getCurrentUserId();
apiClient.getUserViews(userId).done(function (result) {
var items = result.Items;
var html = items.map(function (i) {
var viewMenuCssClass = (i.CollectionType || 'general') + 'ViewMenu';
var itemId = i.Id;
if (i.CollectionType == "channels") {
itemId = "channels";
}
else if (i.CollectionType == "livetv") {
itemId = "livetv";
}
if (i.Type == 'Channel') {
viewMenuCssClass = 'channelsViewMenu';
}
return '';
}).join('');
var elem = $('.libraryMenuOptions').html(html);
$('.viewMenuTextLink', elem).on('click', function () {
$('.libraryMenuButtonText').html(this.innerHTML);
});
});
Dashboard.getCurrentUser().done(function (user) {
if (user.Policy.IsAdministrator) {
$('.adminMenuOptions').show();
} else {
$('.adminMenuOptions').hide();
}
});
}
var requiresLibraryMenuRefresh = false;
function getLibraryMenu(user) {
var panel = $('#libraryPanel');
if (!panel.length) {
var html = '';
html += '';
html += '
';
var homeHref = ConnectionManager.currentApiClient() ? 'index.html' : 'selectserver.html';
html += '';
html += '';
html += getViewsHtml();
html += '
';
html += '
';
$(document.body).append(html);
panel = $('#libraryPanel').panel({}).trigger('create');
updateLibraryMenu();
}
else if (requiresLibraryMenuRefresh) {
updateLibraryMenu();
requiresLibraryMenuRefresh = false;
}
return panel;
}
function getDashboardMenu(page) {
var panel = $('#dashboardPanel', page);
if (!panel.length) {
var html = '';
html += '';
html += '
';
html += '
';
html += '
';
$(document.body).append(html);
panel = $('#dashboardPanel').panel({}).trigger('create');
}
return panel;
}
function setLibraryMenuText(text) {
$('.libraryMenuButtonText').html('' + text + '');
}
function getTopParentId() {
return getParameterByName('topParentId') /*|| sessionStore.getItem('topParentId')*/ || null;
}
window.LibraryMenu = {
showLibraryMenu: showLibraryMenu,
getTopParentId: getTopParentId,
setText: setLibraryMenuText
};
function updateCastIcon() {
var info = MediaController.getPlayerInfo();
if (info.isLocalPlayer) {
$('.btnCast').addClass('btnDefaultCast').removeClass('btnActiveCast');
$('.headerSelectedPlayer').html('');
} else {
$('.btnCast').removeClass('btnDefaultCast').addClass('btnActiveCast');
$('.headerSelectedPlayer').html((info.deviceName || info.name));
}
}
function updateLibraryNavLinks(page) {
page = $(page);
var isLiveTvPage = page.hasClass('liveTvPage');
var isChannelsPage = page.hasClass('channelsPage');
var isEditorPage = page.hasClass('metadataEditorPage');
var isReportsPage = page.hasClass('reportsPage');
var id = isLiveTvPage || isChannelsPage || isEditorPage || isReportsPage || page.hasClass('allLibraryPage') ?
'' :
getTopParentId() || '';
$('.lnkMediaFolder').each(function () {
var itemId = this.getAttribute('data-itemid');
if (isChannelsPage && itemId == 'channels') {
$(this).addClass('selectedMediaFolder');
}
else if (isLiveTvPage && itemId == 'livetv') {
$(this).addClass('selectedMediaFolder');
}
else if (isEditorPage && itemId == 'editor') {
$(this).addClass('selectedMediaFolder');
}
else if (isReportsPage && itemId == 'reports') {
$(this).addClass('selectedMediaFolder');
}
else if (id && itemId == id) {
$(this).addClass('selectedMediaFolder');
}
else {
$(this).removeClass('selectedMediaFolder');
}
});
var context = getParameterByName('context');
if (context !== 'playlists') {
$('.scopedLibraryViewNav a', page).each(function () {
var src = this.href;
if (src.indexOf('#') != -1) {
return;
}
src = replaceQueryString(src, 'topParentId', id);
this.href = src;
});
}
}
function updateContextText(page) {
var name = page.getAttribute('data-contextname');
if (name) {
$('.libraryMenuButtonText').html('' + name + '');
}
//else if ($(page).hasClass('type-interior')) {
// $('.libraryMenuButtonText').html('' + 'Dashboard' + '');
//}
else if ($(page).hasClass('allLibraryPage') || $(page).hasClass('type-interior')) {
$('.libraryMenuButtonText').html('');
}
}
function onWebSocketMessage(e, data) {
var msg = data;
if (msg.MessageType === "UserConfigurationUpdated") {
if (msg.Data.Id == Dashboard.getCurrentUserId()) {
requiresLibraryMenuRefresh = true;
}
}
}
$(document).on('pageinit', ".page", function () {
var page = this;
$('.libraryViewNav', page).wrapInner('');
$('.libraryViewNav a', page).each(function () {
this.innerHTML = '' + this.innerHTML + '';
});
}).on('pagebeforeshow', ".page:not(.standalonePage)", function () {
var page = this;
if (!$('.viewMenuBar').length) {
ConnectionManager.user().done(function (user) {
renderHeader(user);
updateCastIcon();
updateLibraryNavLinks(page);
updateContextText(page);
});
} else {
updateContextText(page);
updateLibraryNavLinks(page);
}
var jpage = $(page);
if (jpage.hasClass('libraryPage')) {
$(document.body).addClass('libraryDocument').removeClass('dashboardDocument');
}
else if (jpage.hasClass('type-interior')) {
$(document.body).addClass('dashboardDocument').removeClass('libraryDocument');
} else {
$(document.body).removeClass('dashboardDocument').removeClass('libraryDocument');
}
}).on('pagebeforeshow', ".page", function () {
var page = this;
if ($(page).hasClass('standalonePage')) {
$('.viewMenuBar').hide();
} else {
$('.viewMenuBar').show();
}
}).on('pageshow', ".libraryPage", function () {
var page = this;
var elem = $('.libraryViewNavInner .ui-btn-active:visible', page);
if (elem.length) {
elem[0].scrollIntoView();
// Scroll back up so in case vertical scroll was messed with
$(document).scrollTop(0);
}
});
function initializeApiClient(apiClient) {
$(apiClient).off('websocketmessage.librarymenu', onWebSocketMessage).on('websocketmessage.librarymenu', onWebSocketMessage);
}
$(ConnectionManager).on('apiclientcreated', function (e, apiClient) {
initializeApiClient(apiClient);
});
$(function () {
$(MediaController).on('playerchange', function () {
updateCastIcon();
});
});
})(window, document, jQuery);
$.fn.createHoverTouch = function () {
var preventHover = false;
var timerId;
function startTimer(elem) {
stopTimer();
timerId = setTimeout(function () {
$(elem).trigger('hovertouch');
}, 300);
}
function stopTimer(elem) {
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
}
return $(this).on('mouseenter', function () {
if (preventHover === true) {
preventHover = false;
return;
}
startTimer(this);
}).on('mouseleave', function () {
stopTimer(this);
}).on('touchstart', function () {
preventHover = true;
}).on('click', function () {
preventHover = true;
if (preventHover) {
$(this).trigger('hovertouch');
stopTimer(this);
preventHover = false;
}
});
};