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

381 lines
12 KiB
JavaScript
Raw Normal View History

2016-08-20 14:58:28 -07:00
define(['libraryBrowser', 'cardBuilder', 'dom', 'scrollStyles', 'emby-itemscontainer', 'emby-tabs', 'emby-button'], function (libraryBrowser, cardBuilder, dom) {
2013-04-25 16:28:01 -07:00
2015-04-12 22:12:02 -07:00
function itemsPerRow() {
2015-05-16 12:09:02 -07:00
2016-08-20 14:58:28 -07:00
var screenWidth = dom.getWindowSize().innerWidth;
2015-04-12 22:12:02 -07:00
2015-04-29 10:39:23 -07:00
return screenWidth >= 1920 ? 9 : (screenWidth >= 1200 ? 12 : (screenWidth >= 1000 ? 10 : 8));
2015-04-12 22:12:02 -07:00
}
2015-05-16 12:09:02 -07:00
function enableScrollX() {
2015-12-14 08:43:03 -07:00
return browserInfo.mobile && AppInfo.enableAppLayouts;
2015-05-16 12:09:02 -07:00
}
function getSquareShape() {
return enableScrollX() ? 'overflowSquare' : 'square';
}
2015-04-11 18:38:38 -07:00
function loadLatest(page, parentId) {
2013-04-25 16:28:01 -07:00
2015-06-17 08:39:46 -07:00
Dashboard.showLoadingMsg();
var userId = Dashboard.getCurrentUserId();
2013-04-25 16:28:01 -07:00
var options = {
IncludeItemTypes: "Audio",
2015-04-12 22:12:02 -07:00
Limit: itemsPerRow(),
2016-08-15 23:13:54 -07:00
Fields: "PrimaryImageAspectRatio,BasicSyncInfo",
2014-11-30 12:01:33 -07:00
ParentId: parentId,
ImageTypeLimit: 1,
2016-05-08 20:13:38 -07:00
EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
EnableTotalRecordCount: false
2013-04-25 16:28:01 -07:00
};
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(ApiClient.getUrl('Users/' + userId + '/Items/Latest', options)).then(function (items) {
2013-04-25 16:28:01 -07:00
2015-06-28 08:43:49 -07:00
var elem = page.querySelector('#recentlyAddedSongs');
2016-07-29 21:53:19 -07:00
elem.innerHTML = cardBuilder.getCardsHtml({
items: items,
2013-12-27 14:20:27 -07:00
showUnplayedIndicator: false,
2015-05-11 12:59:59 -07:00
showLatestItemsPopup: false,
2015-05-16 12:09:02 -07:00
shape: getSquareShape(),
2013-04-25 17:52:55 -07:00
showTitle: true,
2015-01-22 23:15:15 -07:00
showParentTitle: true,
2015-04-11 18:38:38 -07:00
lazy: true,
2015-07-06 00:06:09 -07:00
centerText: true,
2016-08-12 08:54:37 -07:00
overlayPlayButton: true,
allowBottomPadding: !enableScrollX()
2015-04-11 18:38:38 -07:00
2015-06-28 08:43:49 -07:00
});
ImageLoader.lazyChildren(elem);
2013-04-25 17:52:55 -07:00
2015-06-17 08:39:46 -07:00
Dashboard.hideLoadingMsg();
2013-04-25 17:52:55 -07:00
});
2015-04-11 18:38:38 -07:00
}
2013-04-25 17:52:55 -07:00
2015-04-11 18:38:38 -07:00
function loadRecentlyPlayed(page, parentId) {
var options = {
2013-04-25 17:52:55 -07:00
SortBy: "DatePlayed",
SortOrder: "Descending",
IncludeItemTypes: "Audio",
2015-04-12 22:12:02 -07:00
Limit: itemsPerRow(),
2013-04-25 17:52:55 -07:00
Recursive: true,
2016-08-01 22:55:52 -07:00
Fields: "PrimaryImageAspectRatio,AudioInfo",
2014-05-01 19:54:33 -07:00
Filters: "IsPlayed",
2014-11-30 12:01:33 -07:00
ParentId: parentId,
ImageTypeLimit: 1,
2016-05-08 20:13:38 -07:00
EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
EnableTotalRecordCount: false
2013-04-25 17:52:55 -07:00
};
2015-12-14 08:43:03 -07:00
ApiClient.getItems(Dashboard.getCurrentUserId(), options).then(function (result) {
2013-04-25 17:52:55 -07:00
2016-06-17 12:10:05 -07:00
var elem = page.querySelector('#recentlyPlayed');
2015-06-28 08:43:49 -07:00
2013-04-25 17:52:55 -07:00
if (result.Items.length) {
2016-06-17 12:10:05 -07:00
elem.classList.remove('hide');
2013-04-25 17:52:55 -07:00
} else {
2016-06-17 12:10:05 -07:00
elem.classList.add('hide');
2013-04-25 17:52:55 -07:00
}
2015-06-28 08:43:49 -07:00
var itemsContainer = elem.querySelector('.itemsContainer');
2016-07-29 21:53:19 -07:00
itemsContainer.innerHTML = cardBuilder.getCardsHtml({
2013-04-25 17:52:55 -07:00
items: result.Items,
2013-12-27 14:20:27 -07:00
showUnplayedIndicator: false,
2015-05-16 12:09:02 -07:00
shape: getSquareShape(),
2013-04-25 17:52:55 -07:00
showTitle: true,
showParentTitle: true,
2016-07-29 22:25:07 -07:00
action: 'instantmix',
2015-04-11 18:38:38 -07:00
lazy: true,
2015-07-06 00:06:09 -07:00
centerText: true,
2016-08-12 08:54:37 -07:00
overlayMoreButton: true,
allowBottomPadding: !enableScrollX()
2015-06-28 08:43:49 -07:00
});
ImageLoader.lazyChildren(itemsContainer);
2013-04-25 17:52:55 -07:00
});
2015-04-11 18:38:38 -07:00
}
function loadFrequentlyPlayed(page, parentId) {
var options = {
2013-04-25 17:52:55 -07:00
SortBy: "PlayCount",
SortOrder: "Descending",
IncludeItemTypes: "Audio",
2015-04-12 22:12:02 -07:00
Limit: itemsPerRow(),
2013-04-25 17:52:55 -07:00
Recursive: true,
2016-08-01 22:55:52 -07:00
Fields: "PrimaryImageAspectRatio,AudioInfo",
2014-05-01 19:54:33 -07:00
Filters: "IsPlayed",
2014-11-30 12:01:33 -07:00
ParentId: parentId,
ImageTypeLimit: 1,
2016-05-08 20:13:38 -07:00
EnableImageTypes: "Primary,Backdrop,Banner,Thumb",
EnableTotalRecordCount: false
2013-04-25 17:52:55 -07:00
};
2015-12-14 08:43:03 -07:00
ApiClient.getItems(Dashboard.getCurrentUserId(), options).then(function (result) {
2013-04-25 17:52:55 -07:00
2016-06-17 12:10:05 -07:00
var elem = page.querySelector('#topPlayed');
2015-06-28 08:43:49 -07:00
2013-04-25 17:52:55 -07:00
if (result.Items.length) {
2016-06-17 12:10:05 -07:00
elem.classList.remove('hide');
2013-04-25 17:52:55 -07:00
} else {
2016-06-17 12:10:05 -07:00
elem.classList.add('hide');
2013-04-25 17:52:55 -07:00
}
2015-06-28 08:43:49 -07:00
var itemsContainer = elem.querySelector('.itemsContainer');
2016-07-29 21:53:19 -07:00
itemsContainer.innerHTML = cardBuilder.getCardsHtml({
2013-04-25 17:52:55 -07:00
items: result.Items,
2013-12-27 14:20:27 -07:00
showUnplayedIndicator: false,
2015-05-16 12:09:02 -07:00
shape: getSquareShape(),
2013-04-25 17:52:55 -07:00
showTitle: true,
showParentTitle: true,
2016-07-29 22:25:07 -07:00
action: 'instantmix',
2015-04-11 18:38:38 -07:00
lazy: true,
2015-07-06 00:06:09 -07:00
centerText: true,
2016-08-12 08:54:37 -07:00
overlayMoreButton: true,
allowBottomPadding: !enableScrollX()
2015-06-28 08:43:49 -07:00
});
ImageLoader.lazyChildren(itemsContainer);
2013-04-25 16:28:01 -07:00
});
2015-04-11 18:38:38 -07:00
}
function loadPlaylists(page, parentId) {
var options = {
SortBy: "SortName",
SortOrder: "Ascending",
IncludeItemTypes: "Playlist",
Recursive: true,
2016-08-01 22:55:52 -07:00
Fields: "PrimaryImageAspectRatio,SortName,CumulativeRunTimeTicks,CanDelete",
2015-04-11 18:38:38 -07:00
StartIndex: 0,
2016-05-08 20:13:38 -07:00
Limit: itemsPerRow(),
EnableTotalRecordCount: false
2015-04-11 18:38:38 -07:00
};
2015-12-14 08:43:03 -07:00
ApiClient.getItems(Dashboard.getCurrentUserId(), options).then(function (result) {
2015-04-11 18:38:38 -07:00
2016-06-17 12:10:05 -07:00
var elem = page.querySelector('#playlists');
2015-04-11 18:38:38 -07:00
if (result.Items.length) {
2016-06-17 12:10:05 -07:00
elem.classList.remove('hide');
2015-04-11 18:38:38 -07:00
} else {
2016-06-17 12:10:05 -07:00
elem.classList.add('hide');
2015-04-11 18:38:38 -07:00
}
2015-06-28 08:43:49 -07:00
var itemsContainer = elem.querySelector('.itemsContainer');
2016-07-29 21:53:19 -07:00
itemsContainer.innerHTML = cardBuilder.getCardsHtml({
2015-04-11 18:38:38 -07:00
items: result.Items,
2015-05-16 12:09:02 -07:00
shape: getSquareShape(),
2015-04-11 18:38:38 -07:00
showTitle: true,
lazy: true,
coverImage: true,
showItemCounts: true,
2015-07-06 00:06:09 -07:00
centerText: true,
2016-08-12 08:54:37 -07:00
overlayPlayButton: true,
allowBottomPadding: !enableScrollX()
2015-04-11 18:38:38 -07:00
2015-06-28 08:43:49 -07:00
});
ImageLoader.lazyChildren(itemsContainer);
2015-04-11 18:38:38 -07:00
});
}
2015-09-28 20:35:50 -07:00
function loadSuggestionsTab(page, tabContent) {
var parentId = LibraryMenu.getTopParentId();
2016-05-12 12:21:43 -07:00
console.log('loadSuggestionsTab');
loadLatest(tabContent, parentId);
loadPlaylists(tabContent, parentId);
loadRecentlyPlayed(tabContent, parentId);
loadFrequentlyPlayed(tabContent, parentId);
2015-12-14 08:43:03 -07:00
2016-05-12 12:21:43 -07:00
require(['components/favoriteitems'], function (favoriteItems) {
2015-12-14 08:43:03 -07:00
2016-05-12 12:21:43 -07:00
favoriteItems.render(tabContent, Dashboard.getCurrentUserId(), parentId, ['favoriteArtists', 'favoriteAlbums', 'favoriteSongs']);
2015-12-14 08:43:03 -07:00
2016-05-12 12:21:43 -07:00
});
2015-09-03 09:39:24 -07:00
}
2016-05-15 11:52:36 -07:00
pageIdOn('pagebeforeshow', "musicRecommendedPage", function () {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
var page = this;
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
if (!page.getAttribute('data-title')) {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
var parentId = LibraryMenu.getTopParentId();
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
if (parentId) {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), parentId).then(function (item) {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
page.setAttribute('data-title', item.Name);
LibraryMenu.setTitle(item.Name);
});
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
} else {
page.setAttribute('data-title', Globalize.translate('TabMusic'));
LibraryMenu.setTitle(Globalize.translate('TabMusic'));
}
}
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
});
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
return function (view, params) {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
var self = this;
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
function reload() {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
Dashboard.showLoadingMsg();
2015-12-14 08:43:03 -07:00
2016-05-15 11:52:36 -07:00
var tabContent = view.querySelector('.pageTabContent[data-index=\'' + 0 + '\']');
loadSuggestionsTab(view, tabContent);
}
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
function enableScrollX() {
return browserInfo.mobile && AppInfo.enableAppLayouts;
}
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
self.initTab = function () {
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
var tabContent = view.querySelector('.pageTabContent[data-index=\'' + 0 + '\']');
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
var containers = tabContent.querySelectorAll('.itemsContainer');
2016-06-01 23:08:47 -07:00
for (var i = 0, length = containers.length; i < length; i++) {
2016-06-17 12:10:05 -07:00
if (enableScrollX()) {
containers[i].classList.add('hiddenScrollX');
2016-07-29 22:25:07 -07:00
containers[i].classList.remove('vertical-wrap');
2016-06-17 12:10:05 -07:00
} else {
containers[i].classList.remove('hiddenScrollX');
2016-07-29 22:25:07 -07:00
containers[i].classList.add('vertical-wrap');
2016-06-17 12:10:05 -07:00
}
2016-06-01 23:08:47 -07:00
}
2016-05-15 11:52:36 -07:00
};
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
self.renderTab = function () {
reload();
};
2015-09-03 09:39:24 -07:00
2016-05-15 11:52:36 -07:00
var tabControllers = [];
var renderedTabs = [];
2016-06-29 10:43:34 -07:00
function getTabController(page, index, callback) {
2016-05-15 11:52:36 -07:00
var depends = [];
switch (index) {
case 0:
break;
case 1:
depends.push('scripts/musicalbums');
break;
case 2:
2016-06-17 12:32:48 -07:00
depends.push('scripts/musicartists');
2016-05-15 11:52:36 -07:00
break;
case 3:
depends.push('scripts/musicartists');
break;
case 4:
depends.push('scripts/songs');
break;
case 5:
depends.push('scripts/musicgenres');
break;
case 6:
depends.push('scripts/musicfolders');
break;
default:
break;
2015-09-03 09:39:24 -07:00
}
2016-05-15 11:52:36 -07:00
require(depends, function (controllerFactory) {
2016-06-29 10:43:34 -07:00
var tabContent;
2016-05-15 11:52:36 -07:00
if (index == 0) {
2016-06-29 10:43:34 -07:00
tabContent = view.querySelector('.pageTabContent[data-index=\'' + index + '\']');
2016-05-15 11:52:36 -07:00
self.tabContent = tabContent;
}
var controller = tabControllers[index];
if (!controller) {
2016-06-29 10:43:34 -07:00
tabContent = view.querySelector('.pageTabContent[data-index=\'' + index + '\']');
2016-05-15 11:52:36 -07:00
controller = index ? new controllerFactory(view, params, tabContent) : self;
2016-06-29 19:38:20 -07:00
if (index == 2) {
controller.mode = 'albumartists';
} else if (index == 3) {
controller.mode = 'artists';
}
2016-05-15 11:52:36 -07:00
tabControllers[index] = controller;
if (controller.initTab) {
controller.initTab();
}
}
2016-06-29 10:43:34 -07:00
callback(controller);
});
}
function preLoadTab(page, index) {
getTabController(page, index, function (controller) {
if (renderedTabs.indexOf(index) == -1) {
if (controller.preRender) {
controller.preRender();
}
}
});
}
function loadTab(page, index) {
getTabController(page, index, function (controller) {
2016-05-15 11:52:36 -07:00
if (renderedTabs.indexOf(index) == -1) {
renderedTabs.push(index);
controller.renderTab();
}
});
2015-06-29 19:52:23 -07:00
}
2015-09-03 09:39:24 -07:00
2016-08-15 19:40:29 -07:00
var viewTabs = view.querySelector('.libraryViewNav');
2016-05-15 11:52:36 -07:00
2016-08-15 19:40:29 -07:00
libraryBrowser.configurePaperLibraryTabs(view, viewTabs, view.querySelectorAll('.pageTabContent'), [0, 4, 5, 6]);
2016-05-15 11:52:36 -07:00
2016-08-15 19:40:29 -07:00
viewTabs.addEventListener('beforetabchange', function (e) {
2016-06-29 10:43:34 -07:00
preLoadTab(view, parseInt(e.detail.selectedTabIndex));
});
2016-08-15 19:40:29 -07:00
viewTabs.addEventListener('tabchange', function (e) {
2016-05-15 11:52:36 -07:00
loadTab(view, parseInt(e.detail.selectedTabIndex));
});
2016-09-01 09:36:11 -07:00
require(["headroom-window"], function (headroom) {
headroom.add(viewTabs);
self.headroom = headroom;
});
2016-08-15 19:40:29 -07:00
2016-05-15 11:52:36 -07:00
view.addEventListener('viewdestroy', function (e) {
2013-04-25 16:28:01 -07:00
2016-08-15 19:40:29 -07:00
if (self.headroom) {
self.headroom.remove(viewTabs);
}
2016-05-15 11:52:36 -07:00
tabControllers.forEach(function (t) {
if (t.destroy) {
t.destroy();
}
});
});
};
2013-04-25 16:28:01 -07:00
});