';
var links = Dashboard.getToolsMenuLinks(page);
var i, length, link;
for (i = 0, length = links.length; i < length; i++) {
link = links[i];
if (link.divider) {
html += "";
}
if (link.href) {
if (link.selected) {
html += '' + link.name + '';
} else {
html += '' + link.name + '';
}
}
}
// collapsible
html += '
';
for (i = 0, length = links.length; i < length; i++) {
link = links[i];
if (link.divider) {
html += "";
}
if (link.href) {
if (link.selected) {
html += '' + link.name + '';
} else {
html += '' + link.name + '';
}
}
}
html += '
';
$(page).append(html).trigger('create');
}
},
showDashboardMenu: function () {
var page = $.mobile.activePage;
$("#dashboardPanel", page).panel("open");
},
getToolsMenuLinks: function (page) {
var pageElem = page[0];
return [{
name: "Dashboard",
href: "dashboard.html",
selected: pageElem.id == "dashboardPage"
}, {
name: "Media Library",
divider: true,
href: "library.html",
selected: page.hasClass("mediaLibraryPage")
}, {
name: "Metadata",
href: "metadata.html",
selected: pageElem.id == "metadataConfigurationPage" || pageElem.id == "advancedMetadataConfigurationPage" || pageElem.id == "metadataImagesConfigurationPage"
}, {
name: "Auto-Organize",
href: "autoorganizelog.html",
selected: page.hasClass("organizePage")
}, {
name: "Plugins",
href: "plugins.html",
selected: page.hasClass("pluginConfigurationPage")
}, {
name: "Live TV",
href: "livetvstatus.html",
selected: page.hasClass("liveTvSettingsPage")
}, {
name: "Users",
divider: true,
href: "userprofiles.html",
selected: page.hasClass("userProfilesConfigurationPage") || (pageElem.id == "mediaLibraryPage" && getParameterByName('userId'))
}, {
name: "App Settings",
href: "appsplayback.html",
selected: page.hasClass("appsPage")
}, {
name: "Advanced",
divider: true,
href: "advanced.html",
selected: page.hasClass("advancedConfigurationPage")
}, {
name: "Scheduled Tasks",
href: "scheduledtasks.html",
selected: pageElem.id == "scheduledTasksPage" || pageElem.id == "scheduledTaskPage"
}, {
name: "Help",
divider: true,
href: "support.html",
selected: pageElem.id == "supportPage" || pageElem.id == "logPage" || pageElem.id == "supporterPage" || pageElem.id == "supporterKeyPage" || pageElem.id == "aboutPage"
}];
},
ensureWebSocket: function (systemInfo) {
if (!("WebSocket" in window)) {
// Not supported by the browser
return;
}
if (ApiClient.isWebSocketOpenOrConnecting()) {
return;
}
systemInfo = systemInfo || Dashboard.lastSystemInfo;
var location = window.location;
var webSocketUrl = "ws://" + location.hostname;
if (systemInfo.HttpServerPortNumber == systemInfo.WebSocketPortNumber) {
if (location.port) {
webSocketUrl += ':' + location.port;
}
} else {
webSocketUrl += ':' + systemInfo.WebSocketPortNumber;
}
ApiClient.openWebSocket(webSocketUrl);
},
onWebSocketMessageReceived: function (e, data) {
var msg = data;
if (msg.MessageType === "LibraryChanged") {
Dashboard.processLibraryUpdateNotification(msg.Data);
}
else if (msg.MessageType === "ServerShuttingDown") {
Dashboard.hideServerRestartWarning();
}
else if (msg.MessageType === "ServerRestarting") {
Dashboard.hideServerRestartWarning();
}
else if (msg.MessageType === "UserDeleted") {
Dashboard.validateCurrentUser();
}
else if (msg.MessageType === "SystemInfo") {
Dashboard.updateSystemInfo(msg.Data);
}
else if (msg.MessageType === "RestartRequired") {
Dashboard.updateSystemInfo(msg.Data);
}
else if (msg.MessageType === "UserUpdated") {
Dashboard.validateCurrentUser();
var user = msg.Data;
if (user.Id == Dashboard.getCurrentUserId()) {
$('.currentUsername').html(user.Name);
}
}
else if (msg.MessageType === "PackageInstallationCompleted") {
Dashboard.getCurrentUser().done(function (currentUser) {
if (currentUser.Configuration.IsAdministrator) {
Dashboard.showPackageInstallNotification(msg.Data, "completed");
Dashboard.refreshSystemInfoFromServer();
}
});
}
else if (msg.MessageType === "PackageInstallationFailed") {
Dashboard.getCurrentUser().done(function (currentUser) {
if (currentUser.Configuration.IsAdministrator) {
Dashboard.showPackageInstallNotification(msg.Data, "failed");
Dashboard.refreshSystemInfoFromServer();
}
});
}
else if (msg.MessageType === "PackageInstallationCancelled") {
Dashboard.getCurrentUser().done(function (currentUser) {
if (currentUser.Configuration.IsAdministrator) {
Dashboard.showPackageInstallNotification(msg.Data, "cancelled");
Dashboard.refreshSystemInfoFromServer();
}
});
}
else if (msg.MessageType === "PackageInstalling") {
Dashboard.getCurrentUser().done(function (currentUser) {
if (currentUser.Configuration.IsAdministrator) {
Dashboard.showPackageInstallNotification(msg.Data, "progress");
Dashboard.refreshSystemInfoFromServer();
}
});
}
else if (msg.MessageType === "ScheduledTaskEnded") {
Dashboard.getCurrentUser().done(function (currentUser) {
if (currentUser.Configuration.IsAdministrator) {
Dashboard.showTaskCompletionNotification(msg.Data);
}
});
}
else if (msg.MessageType === "Browse") {
Dashboard.onBrowseCommand(msg.Data);
}
else if (msg.MessageType === "Play") {
MediaPlayer.getItemsForPlayback({
Ids: msg.Data.ItemIds.join(',')
}).done(function (result) {
if (msg.Data.PlayCommand == "PlayNext") {
MediaPlayer.queueItemsNext(result.Items);
}
else if (msg.Data.PlayCommand == "PlayLast") {
MediaPlayer.queueItems(result.Items);
}
else {
MediaPlayer.play(result.Items, msg.Data.StartPositionTicks);
}
});
}
else if (msg.MessageType === "Playstate") {
if (msg.Data.Command === 'Stop') {
MediaPlayer.stop();
}
else if (msg.Data.Command === 'Pause') {
MediaPlayer.pause();
}
else if (msg.Data.Command === 'Unpause') {
MediaPlayer.unpause();
}
else if (msg.Data.Command === 'Seek') {
MediaPlayer.seek(msg.Data.SeekPositionTicks);
}
else if (msg.Data.Command === 'NextTrack') {
MediaPlayer.nextTrack();
}
else if (msg.Data.Command === 'PreviousTrack') {
MediaPlayer.previousTrack();
}
}
else if (msg.MessageType === "SystemCommand") {
if (msg.Data === 'GoHome') {
Dashboard.navigate('index.html');
}
else if (msg.Data === 'GoToSettings') {
Dashboard.navigate('dashboard.html');
}
else if (msg.Data === 'Mute') {
MediaPlayer.mute();
}
else if (msg.Data === 'Unmute') {
MediaPlayer.unmute();
}
else if (msg.Data === 'VolumeUp') {
MediaPlayer.volumeUp();
}
else if (msg.Data === 'VolumeDown') {
MediaPlayer.volumeDown();
}
else if (msg.Data === 'ToggleMute') {
MediaPlayer.toggleMute();
}
}
else if (msg.MessageType === "MessageCommand") {
var cmd = msg.Data;
if (cmd.TimeoutMs && WebNotifications.supported()) {
var notification = {
title: cmd.Header,
body: cmd.Text,
timeout: cmd.TimeoutMs
};
WebNotifications.show(notification);
}
else {
Dashboard.showFooterNotification({ html: "" + cmd.Header + ": " + cmd.Text, timeout: cmd.TimeoutMs });
}
}
},
onBrowseCommand: function (cmd) {
var context = cmd.Context || "";
var url;
var type = (cmd.ItemType || "").toLowerCase();
if (type == "genre") {
url = "itembynamedetails.html?genre=" + ApiClient.encodeName(cmd.ItemName) + "&context=" + context;
}
else if (type == "musicgenre") {
url = "itembynamedetails.html?musicgenre=" + ApiClient.encodeName(cmd.ItemName) + "&context=" + (context || "music");
}
else if (type == "gamegenre") {
url = "itembynamedetails.html?gamegenre=" + ApiClient.encodeName(cmd.ItemName) + "&context=" + (context || "games");
}
else if (type == "studio") {
url = "itembynamedetails.html?studio=" + ApiClient.encodeName(cmd.ItemName) + "&context=" + context;
}
else if (type == "person") {
url = "itembynamedetails.html?person=" + ApiClient.encodeName(cmd.ItemName) + "&context=" + context;
}
else if (type == "musicartist") {
url = "itembynamedetails.html?musicartist=" + ApiClient.encodeName(cmd.ItemName) + "&context=" + (context || "music");
}
if (url) {
Dashboard.navigate(url);
return;
}
ApiClient.getItem(Dashboard.getCurrentUserId(), cmd.ItemId).done(function (item) {
Dashboard.navigate(LibraryBrowser.getHref(item, context));
});
},
showTaskCompletionNotification: function (result) {
var html = '';
if (result.Status == "Completed") {
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;
}
var pageElem = page[0];
if (pageElem.hasPageTitle) {
return;
}
var parent = $('.content-primary', page);
if (!parent.length) {
parent = $('.ui-content', page)[0];
}
$(parent).prepend("
" + (document.title || " ") + "
");
pageElem.hasPageTitle = true;
},
setPageTitle: function (title) {
$('.pageTitle', $.mobile.activePage).html(title);
if (title) {
document.title = title;
}
},
getDisplayTime: function (ticks) {
var ticksPerHour = 36000000000;
var parts = [];
var hours = ticks / ticksPerHour;
hours = Math.floor(hours);
if (hours) {
parts.push(hours);
}
ticks -= (hours * ticksPerHour);
var ticksPerMinute = 600000000;
var minutes = ticks / ticksPerMinute;
minutes = Math.floor(minutes);
ticks -= (minutes * ticksPerMinute);
if (minutes < 10 && hours) {
minutes = '0' + minutes;
}
parts.push(minutes);
var ticksPerSecond = 10000000;
var seconds = ticks / ticksPerSecond;
seconds = Math.round(seconds);
if (seconds < 10) {
seconds = '0' + seconds;
}
parts.push(seconds);
return parts.join(':');
},
ratePackage: function (link) {
var id = link.getAttribute('data-id');
var name = link.getAttribute('data-name');
var rating = link.getAttribute('data-rating');
var dialog = new RatingDialog($.mobile.activePage);
dialog.show({
header: "Rate and review " + name,
id: id,
rating: rating,
callback: function (review) {
console.log(review);
dialog.close();
ApiClient.createPackageReview(review).done(function () {
Dashboard.alert({
message: "Thank you for your review",
title: "Thank You"
});
});
}
});
},
getStoreRatingHtml: function (rating, id, name, noLinks) {
var html = "
";
if (!rating) rating = 0;
for (var i = 1; i <= 5; i++) {
var title = noLinks ? rating + " stars" : "Rate " + i + (i > 1 ? " stars" : " star");
html += noLinks ? "" : "";
if (rating <= i - 1) {
html += "";
} else if (rating < i) {
html += "";
} else {
html += "";
}
html += noLinks ? "" : "";
}
html += "
";
return html;
},
populateLanguages: function(select, languages) {
var html = "";
html += "";
for (var i = 0, length = languages.length; i < length; i++) {
var culture = languages[i];
html += "";
}
$(select).html(html).selectmenu("refresh");
},
populateCountries: function (select, allCountries) {
var html = "";
html += "";
for (var i = 0, length = allCountries.length; i < length; i++) {
var culture = allCountries[i];
html += "";
}
$(select).html(html).selectmenu("refresh");
}
};
if (!window.WebSocket) {
alert("This browser does not support web sockets. For a better experience, try a newer browser such as Chrome, Firefox, IE10+, Safari (iOS) or Opera.");
}
else if (!IsStorageEnabled()) {
alert("This browser does not support local storage or is running in private mode. For a better experience, try a newer browser such as Chrome, Firefox, IE10+, Safari (iOS) or Opera.");
}
var ApiClient = MediaBrowser.ApiClient.create("Dashboard", window.dashboardVersion);
$(ApiClient).on("websocketmessage", Dashboard.onWebSocketMessageReceived);
$(function () {
var footerHtml = '';
$(document.body).append(footerHtml);
var footerElem = $('#footer', document.body);
footerElem.trigger('create');
$(window).on("beforeunload", function () {
// Close the connection gracefully when possible
if (ApiClient.isWebSocketOpen() && !MediaPlayer.isPlaying()) {
console.log('Sending close web socket command');
ApiClient.closeWebSocket();
}
});
});
Dashboard.jQueryMobileInit();
$(document).on('pagebeforeshow', ".page", function () {
var page = $(this);
var userId = Dashboard.getCurrentUserId();
ApiClient.currentUserId(userId);
if (!userId) {
if (this.id !== "loginPage" && !page.hasClass('wizardPage')) {
Dashboard.logout();
return;
}
Dashboard.ensureHeader(page);
Dashboard.ensurePageTitle(page);
}
else {
Dashboard.getCurrentUser().done(function (user) {
if (user.Configuration.IsAdministrator) {
Dashboard.ensureToolsMenu(page);
} else if (page.hasClass('adminPage')) {
window.location.replace("index.html");
}
Dashboard.ensureHeader(page, user);
Dashboard.ensurePageTitle(page);
});
}
if (!ApiClient.isWebSocketOpen()) {
Dashboard.refreshSystemInfoFromServer();
}
});