jellyfin-web/dashboard-ui/scripts/librarybrowser.js

273 lines
9.3 KiB
JavaScript
Raw Normal View History

2013-04-01 22:14:37 -07:00
var LibraryBrowser = {
2013-04-03 05:03:13 -07:00
getPosterViewHtml: function (options) {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var items = options.items;
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var primaryImageAspectRatio = options.useAverageAspectRatio ? LibraryBrowser.getAveragePrimaryImageAspectRatio(items) : null;
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var html = "";
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i];
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var hasPrimaryImage = item.ImageTags && item.ImageTags.Primary;
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var href = item.url || (item.IsFolder ? (item.Id ? "itemList.html?parentId=" + item.Id : "#") : "itemdetails.html?id=" + item.Id);
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var showText = options.showTitle || !hasPrimaryImage || (item.Type !== 'Movie' && item.Type !== 'Series' && item.Type !== 'Season' && item.Type !== 'Trailer');
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var cssClass = showText ? "posterViewItem" : "posterViewItem posterViewItemWithNoText";
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
html += "<div class='" + cssClass + "'><a href='" + href + "'>";
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
if (options.preferBackdrop && item.BackdropImageTags && item.BackdropImageTags.length) {
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 198,
width: 352,
tag: item.BackdropImageTags[0]
}) + "' />";
} else if (hasPrimaryImage) {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var height = 300;
var width = primaryImageAspectRatio ? parseInt(height * primaryImageAspectRatio) : null;
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Primary",
height: height,
width: width,
tag: item.ImageTags.Primary
}) + "' />";
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
} else if (item.BackdropImageTags && item.BackdropImageTags.length) {
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 198,
width: 352,
tag: item.BackdropImageTags[0]
}) + "' />";
2013-04-04 10:27:36 -07:00
}
else if (item.MediaType == "Audio" || item.Type == "MusicAlbum" || item.Type == "MusicArtist") {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
html += "<img style='background:" + LibraryBrowser.getMetroColor(item.Id) + ";' src='css/images/items/list/audio.png' />";
2013-04-04 10:27:36 -07:00
}
else if (item.MediaType == "Video") {
html += "<img style='background:" + LibraryBrowser.getMetroColor(item.Id) + ";' src='css/images/items/list/video.png' />";
}
else {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
html += "<img style='background:" + LibraryBrowser.getMetroColor(item.Id) + ";' src='css/images/items/list/collection.png' />";
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
if (showText) {
html += "<div class='posterViewItemText'>";
html += item.Name;
html += "</div>";
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
html += "</a></div>";
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
return html;
},
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
getEpisodePosterViewHtml: function (options) {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var items = options.items;
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var primaryImageAspectRatio = options.useAverageAspectRatio ? LibraryBrowser.getAveragePrimaryImageAspectRatio(items) : null;
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var html = "";
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i];
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var hasPrimaryImage = item.ImageTags && item.ImageTags.Primary;
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var href = item.url || (item.IsFolder ? (item.Id ? "itemList.html?parentId=" + item.Id : "#") : "itemdetails.html?id=" + item.Id);
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var showText = options.showTitle || !hasPrimaryImage || (item.Type !== 'Movie' && item.Type !== 'Series' && item.Type !== 'Season' && item.Type !== 'Trailer');
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var cssClass = showText ? "posterViewItem posterViewItemWithDualText" : "posterViewItem posterViewItemWithNoText";
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
html += "<div class='" + cssClass + "'><a href='" + href + "'>";
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
if (options.preferBackdrop && item.BackdropImageTags && item.BackdropImageTags.length) {
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 198,
width: 352,
tag: item.BackdropImageTags[0]
}) + "' />";
} else if (hasPrimaryImage) {
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var height = 300;
var width = primaryImageAspectRatio ? parseInt(height * primaryImageAspectRatio) : null;
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Primary",
height: height,
width: width,
tag: item.ImageTags.Primary
}) + "' />";
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
} else if (item.BackdropImageTags && item.BackdropImageTags.length) {
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 198,
width: 352,
tag: item.BackdropImageTags[0]
}) + "' />";
} else {
html += "<img style='background:" + LibraryBrowser.getMetroColor(item.Id) + ";' src='css/images/items/list/collection.png' />";
}
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
if (showText) {
html += "<div class='posterViewItemText posterViewItemPrimaryText'>";
if (item.SeriesName != null) {
html += item.SeriesName;
html += "</div>";
html += "<div class='posterViewItemText'>";
}
if (item.ParentIndexNumber != null) {
html += item.ParentIndexNumber + ".";
}
if (item.IndexNumber != null) {
html += item.IndexNumber + " -";
}
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
html += " " + item.Name;
html += "</div>";
}
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
html += "</a></div>";
}
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
return html;
},
2013-04-02 15:53:31 -07:00
2013-04-03 21:21:46 -07:00
getBoxsetPosterViewHtml: function (options) {
var items = options.items;
var primaryImageAspectRatio = options.useAverageAspectRatio ? LibraryBrowser.getAveragePrimaryImageAspectRatio(items) : null;
var html = "";
for (var i = 0, length = items.length; i < length; i++) {
var item = items[i];
var hasPrimaryImage = item.ImageTags && item.ImageTags.Primary;
2013-04-04 21:13:43 -07:00
var href = item.url || ("boxset.html?id=" + item.Id);
2013-04-03 21:21:46 -07:00
var showText = options.showTitle || !hasPrimaryImage || (item.Type !== 'Movie' && item.Type !== 'Series' && item.Type !== 'Season' && item.Type !== 'Trailer');
var cssClass = showText ? "posterViewItem posterViewItemWithDualText" : "posterViewItem posterViewItemWithNoText";
html += "<div class='" + cssClass + "'><a href='" + href + "'>";
if (options.preferBackdrop && item.BackdropImageTags && item.BackdropImageTags.length) {
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 198,
width: 352,
tag: item.BackdropImageTags[0]
}) + "' />";
} else if (hasPrimaryImage) {
var height = 300;
var width = primaryImageAspectRatio ? parseInt(height * primaryImageAspectRatio) : null;
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Primary",
height: height,
width: width,
tag: item.ImageTags.Primary
}) + "' />";
} else if (item.BackdropImageTags && item.BackdropImageTags.length) {
html += "<img src='" + ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 198,
width: 352,
tag: item.BackdropImageTags[0]
}) + "' />";
} else {
2013-04-04 10:27:36 -07:00
html += "<img style='background:" + LibraryBrowser.getMetroColor(item.Id) + ";' src='css/images/items/list/video.png' />";
2013-04-03 21:21:46 -07:00
}
if (showText) {
html += "<div class='posterViewItemText posterViewItemPrimaryText'>";
html += item.Name;
html += "</div>";
html += "<div class='posterViewItemText'>";
html += item.ChildCount+" Movie";
if (item.ChildCount > 1) html += "s";
html += "</div>";
}
html += "</a></div>";
}
return html;
},
2013-04-03 05:03:13 -07:00
getAveragePrimaryImageAspectRatio: function (items) {
2013-04-02 15:53:31 -07:00
2013-04-03 05:03:13 -07:00
var values = [];
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
for (var i = 0, length = items.length; i < length; i++) {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var ratio = items[i].PrimaryImageAspectRatio || 0;
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
if (!ratio) {
continue;
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
values[values.length] = ratio;
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
if (!values.length) {
return null;
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
// Use the median
values.sort(function (a, b) { return a - b; });
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var half = Math.floor(values.length / 2);
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
if (values.length % 2)
return values[half];
else
return (values[half - 1] + values[half]) / 2.0;
},
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
metroColors: ["#6FBD45", "#4BB3DD", "#4164A5", "#E12026", "#800080", "#E1B222", "#008040", "#0094FF", "#FF00C7", "#FF870F", "#7F0037"],
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
getRandomMetroColor: function () {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
var index = Math.floor(Math.random() * (LibraryBrowser.metroColors.length - 1));
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
return LibraryBrowser.metroColors[index];
},
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
getMetroColor: function (str) {
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
if (str) {
2013-04-04 13:36:25 -07:00
var char = String(str.substr(0, 1).charCodeAt());
var sum = 0;
for (var i = 0; i < char.length; i++) {
sum += parseInt(char.charAt(i));
}
var index = String(sum).substr(-1);
2013-04-03 05:03:13 -07:00
return LibraryBrowser.metroColors[index];
} else {
return LibraryBrowser.getRandomMetroColor();
}
2013-04-01 22:14:37 -07:00
2013-04-03 05:03:13 -07:00
}
};