(function () {
$.ajaxSetup({
crossDomain: true
});
if ($.browser.msie) {
// This is unfortunately 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";
if ($.browser.mobile) {
$.mobile.defaultPageTransition = "none";
} else {
$.mobile.defaultPageTransition = "none";
}
//$.mobile.collapsible.prototype.options.contentTheme = "a";
// Make panels a little larger than the defaults
//$.event.special.swipe.verticalDistanceThreshold = 40;
//$.mobile.page.prototype.options.domCache = true;
$.mobile.hideUrlBar = false;
$.mobile.autoInitializePage = false;
$.mobile.changePage.defaults.showLoadMsg = false;
// These are not needed. Nulling them out can help reduce dom querying when pages are loaded
$.mobile.nojs = null;
$.mobile.degradeInputsWithin = null;
$.mobile.filterHtml = Dashboard.filterHtml;
},
filterHtml: function (html) {
// replace the first instance
html = html.replace('');
if (lastIndex != -1) {
html = html.substring(0, lastIndex) + html.substring(lastIndex + 3);
}
return Globalize.translateDocument(html, 'html');
},
isConnectMode: function () {
if (AppInfo.isNativeApp) {
return true;
}
var url = getWindowUrl().toLowerCase();
return url.indexOf('mediabrowser.tv') != -1 ||
url.indexOf('emby.media') != -1;
},
isRunningInCordova: function () {
return window.appMode == 'cordova';
},
onRequestFail: function (e, data) {
if (data.status == 401) {
var url = data.url.toLowerCase();
// Don't bounce to login on failures to contact our external servers
if (url.indexOf('emby.media') != -1) {
Dashboard.hideLoadingMsg();
return;
}
// Don't bounce if the failure is in a sync service
if (url.indexOf('/sync') != -1) {
Dashboard.hideLoadingMsg();
return;
}
// Bounce to the login screen, but not if a password entry fails, obviously
if (url.indexOf('/password') == -1 &&
url.indexOf('/authenticate') == -1 &&
!$($.mobile.activePage).is('.standalonePage')) {
if (data.errorCode == "ParentalControl") {
Dashboard.alert({
message: Globalize.translate('MessageLoggedOutParentalControl'),
callback: function () {
Dashboard.logout(false);
}
});
} else {
Dashboard.logout(false);
}
}
return;
Dashboard.hideLoadingMsg();
}
},
onPopupOpen: function () {
Dashboard.popupCount = (Dashboard.popupCount || 0) + 1;
document.body.classList.add('bodyWithPopupOpen');
},
onPopupClose: function () {
Dashboard.popupCount = (Dashboard.popupCount || 1) - 1;
if (!Dashboard.popupCount) {
document.body.classList.remove('bodyWithPopupOpen');
}
},
getCurrentUser: function () {
if (!Dashboard.getUserPromise) {
Dashboard.getUserPromise = window.ApiClient.getCurrentUser().fail(function () {
Dashboard.getUserPromise = null;
});
}
return Dashboard.getUserPromise;
},
validateCurrentUser: function () {
Dashboard.getUserPromise = null;
if (Dashboard.getCurrentUserId()) {
Dashboard.getCurrentUser();
}
},
serverAddress: function () {
if (Dashboard.isConnectMode()) {
var apiClient = window.ApiClient;
if (apiClient) {
return apiClient.serverAddress();
}
return null;
}
// Try to get the server address from the browser url
// This will preserve protocol, hostname, port and subdirectory
var urlLower = getWindowUrl().toLowerCase();
var index = urlLower.indexOf('/web');
if (index == -1) {
index = urlLower.indexOf('/dashboard');
}
if (index != -1) {
return urlLower.substring(0, index);
}
// If the above failed, just piece it together manually
var loc = window.location;
var address = loc.protocol + '//' + loc.hostname;
if (loc.port) {
address += ':' + loc.port;
}
return address;
},
getCurrentUserId: function () {
var apiClient = window.ApiClient;
if (apiClient) {
return apiClient.getCurrentUserId();
}
return null;
},
onServerChanged: function (userId, accessToken, apiClient) {
apiClient = apiClient || window.ApiClient;
window.ApiClient = apiClient;
Dashboard.getUserPromise = null;
},
logout: function (logoutWithServer) {
function onLogoutDone() {
var loginPage;
if (Dashboard.isConnectMode()) {
loginPage = 'connectlogin.html';
window.ApiClient = null;
} else {
loginPage = 'login.html';
}
Dashboard.navigate(loginPage);
}
if (logoutWithServer === false) {
onLogoutDone();
} else {
ConnectionManager.logout().done(onLogoutDone);
}
},
importCss: function (url) {
url += "?v=" + window.dashboardVersion;
if (!Dashboard.importedCss) {
Dashboard.importedCss = [];
}
if (Dashboard.importedCss.indexOf(url) != -1) {
return;
}
Dashboard.importedCss.push(url);
if (document.createStyleSheet) {
document.createStyleSheet(url);
} else {
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', url);
document.head.appendChild(link);
}
},
showError: function (message) {
Dashboard.alert(message);
},
updateSystemInfo: function (info) {
Dashboard.lastSystemInfo = info;
Dashboard.ensureWebSocket();
if (!Dashboard.initialServerVersion) {
Dashboard.initialServerVersion = info.Version;
}
if (info.HasPendingRestart) {
Dashboard.hideDashboardVersionWarning();
Dashboard.getCurrentUser().done(function (currentUser) {
if (currentUser.Policy.IsAdministrator) {
Dashboard.showServerRestartWarning(info);
}
});
} else {
Dashboard.hideServerRestartWarning();
if (Dashboard.initialServerVersion != info.Version) {
Dashboard.showDashboardRefreshNotification();
}
}
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,500");
}
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 = '' + Globalize.translate('MessagePleaseRestart') + '';
if (systemInfo.CanSelfRestart) {
html += '' + Globalize.translate('ButtonRestart') + '';
}
Dashboard.showFooterNotification({ id: "serverRestartWarning", html: html, forceShow: true, allowHide: false });
},
hideServerRestartWarning: function () {
var elem = document.getElementById('serverRestartWarning');
if (elem) {
elem.parentNode.removeChild(elem);
}
},
showDashboardRefreshNotification: function () {
var html = '' + Globalize.translate('MessagePleaseRefreshPage') + '';
html += '' + Globalize.translate('ButtonRefresh') + '';
Dashboard.showFooterNotification({ id: "dashboardVersionWarning", html: html, forceShow: true, allowHide: false });
},
reloadPage: function () {
var currentUrl = getWindowUrl().toLowerCase();
var newUrl;
// 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) {
newUrl = "dashboard.html";
} else {
newUrl = getWindowUrl();
}
window.location.href = newUrl;
},
hideDashboardVersionWarning: function () {
var elem = document.getElementById('dashboardVersionWarning');
if (elem) {
elem.parentNode.removeChild(elem);
}
},
showFooterNotification: function (options) {
if (!AppInfo.enableFooterNotifications) {
return;
}
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 ? "jQuery(\"#" + options.id + "\").trigger(\"notification.remove\").remove();" : "jQuery(\"#" + options.id + "\").trigger(\"notification.hide\").hide();";
if (options.allowHide !== false) {
options.html += "" + Globalize.translate('ButtonHide') + "";
}
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, transition) {
if (!url) {
throw new Error('url cannot be null or empty');
}
var queryString = getWindowLocationSearch();
if (preserveQueryString && queryString) {
url += queryString;
}
var options = {};
if (transition) {
options.transition = transition;
}
$.mobile.changePage(url, options);
},
showLoadingMsg: function () {
var elem = document.querySelector('.docspinner');
if (elem) {
// This is just an attempt to prevent the fade-in animation from running repeating and causing flickering
elem.active = true;
} else {
// IE renders it incorrectly
if (!$.browser.msie) {
elem = document.createElement("paper-spinner");
elem.classList.add('docspinner');
document.body.appendChild(elem);
elem.active = true;
}
}
},
hideLoadingMsg: function () {
var elem = document.querySelector('.docspinner');
if (elem) {
elem.active = false;
setTimeout(function () {
elem.active = false;
}, 100);
}
},
getModalLoadingMsg: function () {
var elem = $('.modalLoading');
if (!elem.length) {
elem = $('').appendTo(document.body);
}
return elem;
},
showModalLoadingMsg: function () {
Dashboard.getModalLoadingMsg().show();
Dashboard.showLoadingMsg();
},
hideModalLoadingMsg: function () {
Dashboard.getModalLoadingMsg().hide();
Dashboard.hideLoadingMsg();
},
processPluginConfigurationUpdateResult: function () {
Dashboard.hideLoadingMsg();
Dashboard.alert("Settings saved.");
},
defaultErrorMessage: Globalize.translate('DefaultErrorMessage'),
processServerConfigurationUpdateResult: function (result) {
Dashboard.hideLoadingMsg();
Dashboard.alert(Globalize.translate('MessageSettingsSaved'));
},
alert: function (options) {
if (typeof options == "string") {
require(['paperbuttonstyle'], function () {
var message = options;
Dashboard.toastId = Dashboard.toastId || 0;
var id = 'toast' + (Dashboard.toastId++);
var elem = document.createElement("paper-toast");
elem.setAttribute('text', message);
elem.id = id;
document.body.appendChild(elem);
// This timeout is obviously messy but it's unclear how to determine when the webcomponent is ready for use
// element onload never fires
setTimeout(function () {
elem.show();
setTimeout(function () {
elem.parentNode.removeChild(elem);
}, 5000);
}, 300);
});
return;
}
// Cordova
if (navigator.notification && navigator.notification.alert && options.message.indexOf('<') == -1) {
navigator.notification.alert(options.message, options.callback || function () { }, options.title || Globalize.translate('HeaderAlert'));
} else {
Dashboard.confirmInternal(options.message, options.title || Globalize.translate('HeaderAlert'), false, options.callback);
}
},
dialog: function (options) {
var title = options.title;
var message = options.message;
var buttons = options.buttons;
var callback = options.callback;
// Cordova
if (navigator.notification && navigator.notification.confirm && message.indexOf('<') == -1) {
navigator.notification.confirm(message, function (index) {
callback(index);
}, title, buttons.join(','));
} else {
Dashboard.dialogInternal(message, title, buttons, callback);
}
},
dialogInternal: function (message, title, buttons, callback) {
var id = 'paperdlg' + new Date().getTime();
var html = '';
html += '
' + title + '
';
html += '
' + message + '
';
html += '
';
var index = 0;
html += buttons.map(function (b) {
var dataIndex = ' data-index="' + index + '"';
index++;
return '' + b + '';
}).join('');
html += '
';
html += '';
$(document.body).append(html);
// This timeout is obviously messy but it's unclear how to determine when the webcomponent is ready for use
// element onload never fires
setTimeout(function () {
var dlg = document.getElementById(id);
$('.dialogButton', dlg).on('click', function () {
if (callback) {
callback(parseInt(this.getAttribute('data-index')));
}
});
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-closed', function (e) {
this.parentNode.removeChild(this);
});
dlg.open();
}, 300);
},
confirm: function (message, title, callback) {
// Cordova
if (navigator.notification && navigator.notification.confirm && message.indexOf('<') == -1) {
var buttonLabels = [Globalize.translate('ButtonOk'), Globalize.translate('ButtonCancel')];
navigator.notification.confirm(message, function (index) {
callback(index == 1);
}, title || Globalize.translate('HeaderConfirm'), buttonLabels.join(','));
} else {
Dashboard.confirmInternal(message, title, true, callback);
}
},
confirmInternal: function (message, title, showCancel, callback) {
var id = 'paperdlg' + new Date().getTime();
var html = '';
html += '
' + title + '
';
html += '
' + message + '
';
html += '
';
html += '' + Globalize.translate('ButtonOk') + '';
if (showCancel) {
html += '' + Globalize.translate('ButtonCancel') + '';
}
html += '
';
html += '';
$(document.body).append(html);
// This timeout is obviously messy but it's unclear how to determine when the webcomponent is ready for use
// element onload never fires
setTimeout(function () {
var dlg = document.getElementById(id);
// Has to be assigned a z-index after the call to .open()
$(dlg).on('iron-overlay-closed', function (e) {
var confirmed = this.closingReason.confirmed;
this.parentNode.removeChild(this);
if (callback) {
callback(confirmed);
}
});
dlg.open();
}, 300);
},
refreshSystemInfoFromServer: function () {
var apiClient = ApiClient;
if (apiClient && apiClient.accessToken()) {
if (AppInfo.enableFooterNotifications) {
apiClient.getSystemInfo().done(function (info) {
Dashboard.updateSystemInfo(info);
});
} else {
Dashboard.ensureWebSocket();
}
}
},
restartServer: function () {
Dashboard.suppressAjaxErrors = true;
Dashboard.showLoadingMsg();
ApiClient.restartServer().done(function () {
setTimeout(function () {
Dashboard.reloadPageWhenServerAvailable();
}, 250);
}).fail(function () {
Dashboard.suppressAjaxErrors = false;
});
},
reloadPageWhenServerAvailable: function (retryCount) {
// Don't use apiclient method because we don't want it reporting authentication under the old version
ApiClient.getJSON(ApiClient.getUrl("System/Info")).done(function (info) {
// If this is back to false, the restart completed
if (!info.HasPendingRestart) {
Dashboard.reloadPage();
} else {
Dashboard.retryReload(retryCount);
}
}).fail(function () {
Dashboard.retryReload(retryCount);
});
},
retryReload: function (retryCount) {
setTimeout(function () {
retryCount = retryCount || 0;
retryCount++;
if (retryCount < 10) {
Dashboard.reloadPageWhenServerAvailable(retryCount);
} else {
Dashboard.suppressAjaxErrors = false;
}
}, 500);
},
showUserFlyout: function () {
require(['jqmpanel'], function () {
var html = '
';
html += '
';
html += '
';
html += '';
html += '
';
$(document.body).append(html);
var elem = $('#userFlyout').panel({}).lazyChildren().trigger('create').panel("open").on("panelclose", function () {
$(this).off("panelclose").remove();
});
ConnectionManager.user(window.ApiClient).done(function (user) {
Dashboard.updateUserFlyout(elem, user);
});
});
},
updateUserFlyout: function (elem, user) {
var html = '';
var imgWidth = 48;
if (user.imageUrl && AppInfo.enableUserImage) {
var url = user.imageUrl;
if (user.supportsImageParams) {
url += "&width=" + (imgWidth * Math.max(window.devicePixelRatio || 1, 2));
}
html += '';
}
html += user.name;
$('.userHeader', elem).html(html).lazyChildren();
html = '';
if (user.localUser && user.localUser.Policy.EnableUserPreferenceAccess) {
html += '
' + Globalize.translate('ButtonSettings') + '';
}
$('.preferencesContainer', elem).html(html).trigger('create');
},
getPluginSecurityInfo: function () {
var apiClient = ApiClient;
if (!apiClient) {
var deferred = $.Deferred();
deferred.reject();
return deferred.promise();
}
if (!Dashboard.getPluginSecurityInfoPromise) {
var deferred = $.Deferred();
// Don't let this blow up the dashboard when it fails
apiClient.ajax({
type: "GET",
url: apiClient.getUrl("Plugins/SecurityInfo"),
dataType: 'json',
error: function () {
// Don't show normal dashboard errors
}
}).done(function (result) {
deferred.resolveWith(null, [result]);
});
Dashboard.getPluginSecurityInfoPromise = deferred;
}
return Dashboard.getPluginSecurityInfoPromise;
},
resetPluginSecurityInfo: function () {
Dashboard.getPluginSecurityInfoPromise = null;
},
ensureHeader: function (page) {
if (page.classList.contains('standalonePage') && !page.classList.contains('noHeaderPage')) {
Dashboard.renderHeader(page);
}
},
renderHeader: function (page) {
var header = page.querySelector('.header');
if (!header) {
var headerHtml = '';
headerHtml += '