$.ajaxSetup({ crossDomain: true, error: function (event) { Dashboard.hideLoadingMsg(); if (!Dashboard.suppressAjaxErrors) { setTimeout(function () { var msg = event.getResponseHeader("X-Application-Error-Code") || Dashboard.defaultErrorMessage; Dashboard.showError(msg); }, 500); } } }); if ($.browser.msie) { // This is unfortuantely required due to IE's over-aggressive caching. // https://github.com/MediaBrowser/MediaBrowser/issues/179 $.ajaxSetup({ cache: false }); } $.support.cors = true; $(document).one('click', WebNotifications.requestPermission); var Dashboard = { jQueryMobileInit: function () { // Page //$.mobile.page.prototype.options.theme = "a"; //$.mobile.page.prototype.options.headerTheme = "a"; //$.mobile.page.prototype.options.contentTheme = "a"; //$.mobile.page.prototype.options.footerTheme = "a"; //$.mobile.button.prototype.options.theme = "c"; //$.mobile.listview.prototype.options.dividerTheme = "b"; //$.mobile.popup.prototype.options.theme = "c"; //$.mobile.popup.prototype.options.transition = "none"; $.mobile.defaultPageTransition = "none"; //$.mobile.collapsible.prototype.options.contentTheme = "a"; }, getCurrentUser: function () { if (!Dashboard.getUserPromise) { var userId = Dashboard.getCurrentUserId(); Dashboard.getUserPromise = ApiClient.getUser(userId).fail(Dashboard.logout); } return Dashboard.getUserPromise; }, validateCurrentUser: function (page) { Dashboard.getUserPromise = null; if (Dashboard.getCurrentUserId()) { Dashboard.getCurrentUser(); } page = page || $.mobile.activePage; var header = $('.header', page); if (header.length) { // Re-render the header header.remove(); if (Dashboard.getUserPromise) { Dashboard.getUserPromise.done(function (user) { Dashboard.ensureHeader(page, user); }); } else { Dashboard.ensureHeader(page); } } }, getCurrentUserId: function () { if (!window.localStorage) { return null; } var autoLoginUserId = getParameterByName('u'); var storedUserId = localStorage.getItem("userId"); var userId; if (autoLoginUserId && autoLoginUserId != storedUserId) { localStorage.setItem("userId", autoLoginUserId); ApiClient.currentUserId(autoLoginUserId); } return autoLoginUserId || storedUserId; }, setCurrentUser: function (userId) { if (window.localStorage) { localStorage.setItem("userId", userId); } ApiClient.currentUserId(userId); Dashboard.getUserPromise = null; }, logout: function () { if (window.localStorage) { localStorage.removeItem("userId"); } Dashboard.getUserPromise = null; ApiClient.currentUserId(null); window.location = "login.html"; }, showError: function (message) { $.mobile.loading('show', { text: message, textonly: true, textVisible: true }); setTimeout(function () { $.mobile.loading('hide'); }, 3000); }, alert: function (options) { if (typeof options == "string") { var message = options; $.mobile.loading('show', { text: message, textonly: true, textVisible: true }); setTimeout(function () { $.mobile.loading('hide'); }, 3000); return; } Dashboard.confirmInternal(options.message, options.title || 'Alert', false, options.callback); }, updateSystemInfo: function (info) { Dashboard.lastSystemInfo = info; Dashboard.ensureWebSocket(info); if (!Dashboard.initialServerVersion) { Dashboard.initialServerVersion = info.Version; } if (info.HasPendingRestart) { Dashboard.hideDashboardVersionWarning(); Dashboard.getCurrentUser().done(function (currentUser) { if (currentUser.Configuration.IsAdministrator) { Dashboard.showServerRestartWarning(info); } }); } else { Dashboard.hideServerRestartWarning(); if (Dashboard.initialServerVersion != info.Version) { Dashboard.showDashboardVersionWarning(); } } Dashboard.showInProgressInstallations(info.InProgressInstallations); }, showInProgressInstallations: function (installations) { installations = installations || []; for (var i = 0, length = installations.length; i < length; i++) { var installation = installations[i]; var percent = installation.PercentComplete || 0; if (percent < 100) { Dashboard.showPackageInstallNotification(installation, "progress"); } } if (installations.length) { Dashboard.ensureInstallRefreshInterval(); } else { Dashboard.stopInstallRefreshInterval(); } }, ensureInstallRefreshInterval: function () { if (!Dashboard.installRefreshInterval) { if (ApiClient.isWebSocketOpen()) { ApiClient.sendWebSocketMessage("SystemInfoStart", "0,350"); } Dashboard.installRefreshInterval = 1; } }, stopInstallRefreshInterval: function () { if (Dashboard.installRefreshInterval) { if (ApiClient.isWebSocketOpen()) { ApiClient.sendWebSocketMessage("SystemInfoStop"); } Dashboard.installRefreshInterval = null; } }, cancelInstallation: function (id) { ApiClient.cancelPackageInstallation(id).always(Dashboard.refreshSystemInfoFromServer); }, showServerRestartWarning: function (systemInfo) { var html = 'Please restart to finish updating.'; if (systemInfo.CanSelfRestart) { html += ''; } Dashboard.showFooterNotification({ id: "serverRestartWarning", html: html, forceShow: true, allowHide: false }); }, hideServerRestartWarning: function () { $('#serverRestartWarning').remove(); }, showDashboardVersionWarning: function () { var html = 'Please refresh this page to receive new updates from the server.'; Dashboard.showFooterNotification({ id: "dashboardVersionWarning", html: html, forceShow: true, allowHide: false }); }, reloadPage: function () { var currentUrl = window.location.toString().toLowerCase(); // If they're on a plugin config page just go back to the dashboard // The plugin may not have been loaded yet, or could have been uninstalled if (currentUrl.indexOf('configurationpage') != -1) { window.location.href = "dashboard.html"; } else { window.location.href = window.location.href; } }, hideDashboardVersionWarning: function () { $('#dashboardVersionWarning').remove(); }, showFooterNotification: function (options) { var removeOnHide = !options.id; options.id = options.id || "notification" + new Date().getTime() + parseInt(Math.random()); var footer = $("#footer").css("top", "initial").show(); var parentElem = $('#footerNotifications', footer); var elem = $('#' + options.id, parentElem); if (!elem.length) { elem = $('
').appendTo(parentElem); } var onclick = removeOnHide ? "$(\"#" + options.id + "\").trigger(\"notification.remove\").remove();" : "$(\"#" + options.id + "\").trigger(\"notification.hide\").hide();"; if (options.allowHide !== false) { options.html += ""; } if (options.forceShow) { elem.slideDown(400); } elem.html(options.html).trigger("create"); if (options.timeout) { setTimeout(function () { if (removeOnHide) { elem.trigger("notification.remove").remove(); } else { elem.trigger("notification.hide").hide(); } }, options.timeout); } footer.on("notification.remove notification.hide", function (e) { setTimeout(function () { // give the DOM time to catch up if (!parentElem.html()) { footer.slideUp(); } }, 50); }); }, getConfigurationPageUrl: function (name) { return "ConfigurationPage?name=" + encodeURIComponent(name); }, navigate: function (url, preserveQueryString) { var queryString = window.location.search; if (preserveQueryString && queryString) { url += queryString; } $.mobile.changePage(url); }, showLoadingMsg: function () { $.mobile.loading("show"); }, hideLoadingMsg: function () { $.mobile.loading("hide"); }, processPluginConfigurationUpdateResult: function () { Dashboard.hideLoadingMsg(); Dashboard.alert("Settings saved."); }, defaultErrorMessage: "There was an error processing the request.", processServerConfigurationUpdateResult: function (result) { Dashboard.hideLoadingMsg(); Dashboard.alert("Settings saved."); }, confirmInternal: function (message, title, showCancel, callback) { $('.confirmFlyout').popup("close").remove(); var html = 'Preferences'; html += '
'; html += '
';
}
headerHtml += '';
if (page.hasClass('type-interior')) {
headerHtml += '
';
}
headerHtml += '';
if (user.Configuration.IsAdministrator) {
var href = window.location.toString().toLowerCase().indexOf('dashboard.html') == -1 ? 'dashboard.html' : '#';
headerHtml += 'Tools';
}
}
headerHtml += '
').insertBefore($('.btnTools', header));
} else {
$('
').insertBefore($('.btnTools', header));
}
});
}
$(Dashboard).trigger('interiorheaderrendered', [header, user]);
},
ensureToolsMenu: function (page) {
if (!page.hasClass('type-interior')) {
return;
}
var sidebar = $('.toolsSidebar', page);
if (!sidebar.length) {
var html = '';
html += '
';
return;
}
else if (result.Status == "Cancelled") {
html += '
';
return;
}
else {
html += '
';
}
html += '';
html += result.Name + " " + result.Status;
html += '';
var timeout = 0;
if (result.Status == 'Cancelled') {
timeout = 2000;
}
Dashboard.showFooterNotification({ html: html, id: result.Id, forceShow: true, timeout: timeout });
},
showPackageInstallNotification: function (installation, status) {
var html = '';
if (status == 'completed') {
html += '
';
}
else if (status == 'cancelled') {
html += '
';
}
else if (status == 'failed') {
html += '
';
}
else if (status == 'progress') {
html += '
';
}
html += '';
if (status == 'completed') {
html += installation.Name + ' ' + installation.Version + ' installation completed';
}
else if (status == 'cancelled') {
html += installation.Name + ' ' + installation.Version + ' installation was cancelled';
}
else if (status == 'failed') {
html += installation.Name + ' ' + installation.Version + ' installation failed';
}
else if (status == 'progress') {
html += 'Installing ' + installation.Name + ' ' + installation.Version;
}
html += '';
if (status == 'progress') {
var percentComplete = Math.round(installation.PercentComplete || 0);
html += '';
if (percentComplete < 100) {
var btnId = "btnCancel" + installation.Id;
html += '';
}
}
var timeout = 0;
if (status == 'cancelled') {
timeout = 2000;
}
var forceShow = status != "progress";
var allowHide = status != "progress" && status != 'cancelled';
Dashboard.showFooterNotification({ html: html, id: installation.Id, timeout: timeout, forceShow: forceShow, allowHide: allowHide });
},
processLibraryUpdateNotification: function (data) {
var newItems = data.ItemsAdded;
if (!newItems.length) {
return;
}
ApiClient.getItems(Dashboard.getCurrentUserId(), {
Recursive: true,
Limit: 3,
Filters: "IsNotFolder",
SortBy: "DateCreated",
SortOrder: "Descending",
ImageTypes: "Primary",
Ids: newItems.join(',')
}).done(function (result) {
var items = result.Items;
for (var i = 0, length = Math.min(items.length, 2) ; i < length; i++) {
var item = items[i];
var notification = {
title: "New " + item.Type,
body: item.Name,
timeout: 5000
};
var imageTags = item.ImageTags || {};
if (imageTags.Primary) {
notification.icon = ApiClient.getImageUrl(item.Id, {
width: 100,
tag: imageTags.Primary,
type: "Primary"
});
}
WebNotifications.show(notification);
}
});
},
ensurePageTitle: function (page) {
if (!page.hasClass('type-interior')) {
return;
}
if ($('.pageTitle', page).length) {
return;
}
var parent = $('.content-primary', page);
if (!parent.length) {
parent = $('.ui-content', page)[0];
}
$(parent).prepend("