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

213 lines
7.5 KiB
JavaScript
Raw Normal View History

2016-10-12 11:23:09 -07:00
define(['listView', 'cardBuilder', 'libraryBrowser', 'apphost', 'emby-itemscontainer'], function (listView, cardBuilder, libraryBrowser, appHost) {
2016-10-22 22:11:46 -07:00
'use strict';
2016-07-30 10:43:55 -07:00
return function (view, params) {
var data = {};
function getPageData(context) {
var key = getSavedQueryKey(context);
var pageData = data[key];
if (!pageData) {
pageData = data[key] = {
query: {
SortBy: "SortName",
SortOrder: "Ascending",
IncludeItemTypes: "Playlist",
Recursive: true,
2016-08-01 22:55:52 -07:00
Fields: "PrimaryImageAspectRatio,SortName,CumulativeRunTimeTicks,CanDelete",
2016-07-30 10:43:55 -07:00
StartIndex: 0,
Limit: LibraryBrowser.getDefaultPageSize()
},
2016-10-12 11:23:09 -07:00
view: LibraryBrowser.getSavedView(key) || appHost.preferVisualCards ? 'PosterCard' : 'Poster'
2016-07-30 10:43:55 -07:00
};
pageData.query.ParentId = LibraryMenu.getTopParentId();
LibraryBrowser.loadSavedQueryValues(key, pageData.query);
}
return pageData;
2015-08-15 17:41:55 -07:00
}
2016-07-30 10:43:55 -07:00
function getQuery(context) {
2016-07-30 10:43:55 -07:00
return getPageData(context).query;
}
2016-07-30 10:43:55 -07:00
function getSavedQueryKey(context) {
2016-07-30 10:43:55 -07:00
if (!context.savedQueryKey) {
context.savedQueryKey = libraryBrowser.getSavedQueryKey();
}
return context.savedQueryKey;
}
2016-07-30 10:43:55 -07:00
function showLoadingMessage() {
2014-08-14 06:24:30 -07:00
2016-07-30 10:43:55 -07:00
Dashboard.showLoadingMsg();
}
2014-08-14 06:24:30 -07:00
2016-07-30 10:43:55 -07:00
function hideLoadingMessage() {
Dashboard.hideLoadingMsg();
}
2014-08-14 06:24:30 -07:00
2016-07-30 10:43:55 -07:00
function onViewStyleChange() {
2016-07-30 10:43:55 -07:00
var viewStyle = getPageData(view).view;
2016-07-30 10:43:55 -07:00
var itemsContainer = view.querySelector('.itemsContainer');
2016-07-30 10:43:55 -07:00
if (viewStyle == "List") {
2016-07-30 10:43:55 -07:00
itemsContainer.classList.add('vertical-list');
itemsContainer.classList.remove('vertical-wrap');
}
else {
2016-07-30 10:43:55 -07:00
itemsContainer.classList.remove('vertical-list');
itemsContainer.classList.add('vertical-wrap');
}
itemsContainer.innerHTML = '';
}
2016-07-30 10:43:55 -07:00
function reloadItems() {
2016-07-30 10:43:55 -07:00
showLoadingMessage();
2015-08-15 17:41:55 -07:00
2016-07-30 10:43:55 -07:00
var query = getQuery(view);
var promise1 = ApiClient.getItems(Dashboard.getCurrentUserId(), query);
var promise2 = Dashboard.getCurrentUser();
2016-07-30 10:43:55 -07:00
Promise.all([promise1, promise2]).then(function (responses) {
2016-07-30 10:43:55 -07:00
var result = responses[0];
var user = responses[1];
2016-07-30 10:43:55 -07:00
// Scroll back up so they can see the results from the beginning
window.scrollTo(0, 0);
var html = '';
var viewStyle = getPageData(view).view;
view.querySelector('.listTopPaging').innerHTML = LibraryBrowser.getQueryPagingHtml({
startIndex: query.StartIndex,
limit: query.Limit,
totalRecordCount: result.TotalRecordCount,
viewButton: false,
showLimit: false,
updatePageSizeSetting: false,
addLayoutButton: true,
layouts: 'List,Poster,PosterCard,Thumb,ThumbCard',
currentLayout: viewStyle
});
if (result.TotalRecordCount) {
if (viewStyle == "List") {
html = listView.getListViewHtml({
items: result.Items,
sortBy: query.SortBy
});
}
else if (viewStyle == "PosterCard") {
html = cardBuilder.getCardsHtml({
items: result.Items,
shape: "square",
showTitle: true,
lazy: true,
coverImage: true,
showItemCounts: true,
2016-10-12 11:23:09 -07:00
cardLayout: true,
vibrant: true
2016-07-30 10:43:55 -07:00
});
}
else if (viewStyle == "Thumb") {
html = cardBuilder.getCardsHtml({
items: result.Items,
shape: "backdrop",
showTitle: true,
centerText: true,
lazy: true,
preferThumb: true,
overlayPlayButton: true
});
}
else if (viewStyle == "ThumbCard") {
html = cardBuilder.getCardsHtml({
items: result.Items,
shape: "backdrop",
showTitle: true,
lazy: true,
preferThumb: true,
cardLayout: true,
2016-10-12 11:23:09 -07:00
showItemCounts: true,
vibrant: true
2016-07-30 10:43:55 -07:00
});
}
else {
// Poster
html = cardBuilder.getCardsHtml({
items: result.Items,
shape: "square",
showTitle: true,
lazy: true,
coverImage: true,
showItemCounts: true,
centerText: true,
overlayPlayButton: true
});
}
view.querySelector('.noItemsMessage').classList.add('hide');
} else {
view.querySelector('.noItemsMessage').classList.remove('hide');
}
2016-07-30 10:43:55 -07:00
var elem = view.querySelector('.itemsContainer');
elem.innerHTML = html;
ImageLoader.lazyChildren(elem);
var btnNextPage = view.querySelector('.btnNextPage');
if (btnNextPage) {
btnNextPage.addEventListener('click', function () {
query.StartIndex += query.Limit;
reloadItems();
2015-08-17 09:52:56 -07:00
});
}
2016-07-30 10:43:55 -07:00
var btnPreviousPage = view.querySelector('.btnPreviousPage');
if (btnPreviousPage) {
btnPreviousPage.addEventListener('click', function () {
query.StartIndex -= query.Limit;
reloadItems();
2015-08-17 09:52:56 -07:00
});
}
2016-07-30 10:43:55 -07:00
var btnChangeLayout = view.querySelector('.btnChangeLayout');
if (btnChangeLayout) {
btnChangeLayout.addEventListener('layoutchange', function (e) {
var layout = e.detail.viewStyle;
getPageData(view).view = layout;
LibraryBrowser.saveViewSetting(getSavedQueryKey(view), layout);
onViewStyleChange();
reloadItems();
2016-05-27 10:18:22 -07:00
});
}
2016-07-30 10:43:55 -07:00
LibraryBrowser.saveQueryValues(getSavedQueryKey(view), query);
2016-07-30 10:43:55 -07:00
hideLoadingMessage();
});
}
2016-07-30 10:43:55 -07:00
view.addEventListener('viewbeforeshow', function() {
reloadItems();
});
2016-07-30 10:43:55 -07:00
onViewStyleChange();
2016-07-30 10:43:55 -07:00
};
});