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

371 lines
12 KiB
JavaScript
Raw Normal View History

2016-08-14 12:58:13 -07:00
define(['libraryBrowser', 'emby-tabs', 'emby-button'], function (libraryBrowser) {
2016-10-22 22:11:46 -07:00
'use strict';
2013-02-20 18:33:05 -07:00
2015-06-07 14:21:30 -07:00
var defaultFirstSection = 'smalllibrarytiles';
2015-04-14 20:41:29 -07:00
2014-06-23 09:05:19 -07:00
function getDefaultSection(index) {
switch (index) {
case 0:
2015-04-14 20:41:29 -07:00
return defaultFirstSection;
2014-06-23 09:05:19 -07:00
case 1:
return 'resume';
case 2:
2016-10-28 12:20:22 -07:00
return 'nextup';
2014-06-23 09:05:19 -07:00
case 3:
2016-10-28 12:20:22 -07:00
return 'latestmedia';
case 4:
2015-06-25 18:10:56 -07:00
return 'latesttvrecordings';
2014-06-23 09:05:19 -07:00
default:
return '';
}
}
2016-11-15 23:00:28 -07:00
function loadSection(page, user, userSettings, index) {
2015-03-24 18:14:24 -07:00
var userId = user.Id;
2014-05-21 12:33:46 -07:00
2016-11-15 23:00:28 -07:00
var section = userSettings.get('homesection' + index) || getDefaultSection(index);
2014-05-21 12:33:46 -07:00
2015-04-11 14:34:05 -07:00
if (section == 'folders') {
2015-04-14 20:41:29 -07:00
section = defaultFirstSection;
2015-04-11 14:34:05 -07:00
}
2015-06-28 07:45:21 -07:00
var elem = page.querySelector('.section' + index);
2014-06-19 21:50:30 -07:00
2014-05-21 12:33:46 -07:00
if (section == 'latestmedia') {
2015-04-12 09:46:29 -07:00
return Sections.loadRecentlyAdded(elem, user);
2014-05-21 12:33:46 -07:00
}
else if (section == 'librarytiles') {
2016-12-01 10:38:13 -07:00
return Sections.loadLibraryTiles(elem, user, 'backdrop', index);
2014-06-07 12:46:24 -07:00
}
2014-06-24 14:45:21 -07:00
else if (section == 'smalllibrarytiles') {
2016-12-01 10:38:13 -07:00
return Sections.loadLibraryTiles(elem, user, 'smallBackdrop', index);
2014-07-11 19:31:08 -07:00
}
else if (section == 'smalllibrarytiles-automobile') {
2016-12-01 10:38:13 -07:00
return Sections.loadLibraryTiles(elem, user, 'smallBackdrop', index);
2015-03-13 21:50:23 -07:00
}
else if (section == 'librarytiles-automobile') {
2016-12-01 10:38:13 -07:00
return Sections.loadLibraryTiles(elem, user, 'backdrop', index);
2014-05-21 12:33:46 -07:00
}
2014-06-24 14:45:21 -07:00
else if (section == 'librarybuttons') {
2015-04-12 09:46:29 -07:00
return Sections.loadlibraryButtons(elem, userId, index);
2014-06-24 14:45:21 -07:00
}
2014-05-21 12:33:46 -07:00
else if (section == 'resume') {
2015-04-12 09:46:29 -07:00
return Sections.loadResume(elem, userId);
2014-06-23 09:05:19 -07:00
}
2015-09-24 22:15:29 -07:00
else if (section == 'nextup') {
return Sections.loadNextUp(elem, userId);
}
2014-06-23 09:05:19 -07:00
else if (section == 'latesttvrecordings') {
2015-04-12 09:46:29 -07:00
return Sections.loadLatestLiveTvRecordings(elem, userId);
2014-05-21 12:33:46 -07:00
}
2015-04-11 14:34:05 -07:00
else if (section == 'latestchannelmedia') {
2015-04-12 09:46:29 -07:00
return Sections.loadLatestChannelMedia(elem, userId);
2014-05-21 20:35:18 -07:00
} else {
2015-07-06 19:25:23 -07:00
elem.innerHTML = '';
2015-04-12 09:46:29 -07:00
2015-12-30 10:02:11 -07:00
return new Promise(function (resolve, reject) {
resolve();
});
2014-05-21 12:33:46 -07:00
}
}
2016-11-15 23:00:28 -07:00
function loadSections(page, user, userSettings) {
2014-05-21 12:33:46 -07:00
var i, length;
2016-10-28 12:20:22 -07:00
var sectionCount = 5;
2014-05-21 12:33:46 -07:00
2015-06-28 07:45:21 -07:00
var elem = page.querySelector('.sections');
2015-06-28 07:45:21 -07:00
if (!elem.innerHTML.length) {
2014-05-21 20:35:18 -07:00
var html = '';
for (i = 0, length = sectionCount; i < length; i++) {
2014-05-21 12:33:46 -07:00
2014-05-21 20:35:18 -07:00
html += '<div class="homePageSection section' + i + '"></div>';
}
2014-05-21 12:33:46 -07:00
2015-06-28 07:45:21 -07:00
elem.innerHTML = html;
2014-05-21 20:35:18 -07:00
}
2014-05-21 12:33:46 -07:00
2015-04-12 09:46:29 -07:00
var promises = [];
2014-05-21 12:33:46 -07:00
for (i = 0, length = sectionCount; i < length; i++) {
2016-11-15 23:00:28 -07:00
promises.push(loadSection(page, user, userSettings, i));
2014-05-21 12:33:46 -07:00
}
2015-04-12 09:46:29 -07:00
2015-12-14 08:43:03 -07:00
return Promise.all(promises);
2014-05-21 12:33:46 -07:00
}
2015-04-20 11:04:02 -07:00
var homePageDismissValue = '14';
2014-09-25 20:47:46 -07:00
var homePageTourKey = 'homePageTour';
2014-06-07 12:46:24 -07:00
2016-02-29 23:02:03 -07:00
function displayPreferencesKey() {
if (AppInfo.isNativeApp) {
return 'Emby Mobile';
}
return 'webclient';
}
2014-05-30 14:06:57 -07:00
function dismissWelcome(page, userId) {
2015-12-14 08:43:03 -07:00
getDisplayPreferences('home', userId).then(function (result) {
2014-05-30 14:06:57 -07:00
2014-09-25 20:47:46 -07:00
result.CustomPrefs[homePageTourKey] = homePageDismissValue;
2016-02-29 23:02:03 -07:00
ApiClient.updateDisplayPreferences('home', result, userId, displayPreferencesKey());
2014-09-25 20:47:46 -07:00
});
}
2014-06-07 12:46:24 -07:00
2015-01-11 11:36:26 -07:00
function showWelcomeIfNeeded(page, displayPreferences) {
if (displayPreferences.CustomPrefs[homePageTourKey] == homePageDismissValue) {
2016-05-16 13:48:56 -07:00
page.querySelector('.welcomeMessage').classList.add('hide');
2015-01-11 11:36:26 -07:00
} else {
2015-06-29 11:45:42 -07:00
Dashboard.hideLoadingMsg();
2016-05-16 13:48:56 -07:00
var elem = page.querySelector('.welcomeMessage');
elem.classList.remove('hide');
2015-01-11 11:36:26 -07:00
if (displayPreferences.CustomPrefs[homePageTourKey]) {
2016-05-16 13:48:56 -07:00
elem.querySelector('.tourHeader').innerHTML = Globalize.translate('HeaderWelcomeBack');
elem.querySelector('.tourButtonText').innerHTML = Globalize.translate('ButtonTakeTheTourToSeeWhatsNew');
2015-01-11 11:36:26 -07:00
} else {
2016-05-16 13:48:56 -07:00
elem.querySelector('.tourHeader').innerHTML = Globalize.translate('HeaderWelcomeToProjectWebClient');
elem.querySelector('.tourButtonText').innerHTML = Globalize.translate('ButtonTakeTheTour');
2015-01-11 11:36:26 -07:00
}
}
2014-10-04 11:05:24 -07:00
}
2014-09-25 20:47:46 -07:00
function takeTour(page, userId) {
2016-01-30 23:03:40 -07:00
require(['slideshow'], function () {
var slides = [
{ imageUrl: 'css/images/tour/web/tourcontent.jpg', title: Globalize.translate('WebClientTourContent') },
{ imageUrl: 'css/images/tour/web/tourmovies.jpg', title: Globalize.translate('WebClientTourMovies') },
{ imageUrl: 'css/images/tour/web/tourmouseover.jpg', title: Globalize.translate('WebClientTourMouseOver') },
{ imageUrl: 'css/images/tour/web/tourtaphold.jpg', title: Globalize.translate('WebClientTourTapHold') },
{ imageUrl: 'css/images/tour/web/tourmysync.png', title: Globalize.translate('WebClientTourMySync') },
{ imageUrl: 'css/images/tour/web/toureditor.png', title: Globalize.translate('WebClientTourMetadataManager') },
{ imageUrl: 'css/images/tour/web/tourplaylist.png', title: Globalize.translate('WebClientTourPlaylists') },
{ imageUrl: 'css/images/tour/web/tourcollections.jpg', title: Globalize.translate('WebClientTourCollections') },
{ imageUrl: 'css/images/tour/web/tourusersettings1.png', title: Globalize.translate('WebClientTourUserPreferences1') },
{ imageUrl: 'css/images/tour/web/tourusersettings2.png', title: Globalize.translate('WebClientTourUserPreferences2') },
{ imageUrl: 'css/images/tour/web/tourusersettings3.png', title: Globalize.translate('WebClientTourUserPreferences3') },
{ imageUrl: 'css/images/tour/web/tourusersettings4.png', title: Globalize.translate('WebClientTourUserPreferences4') },
{ imageUrl: 'css/images/tour/web/tourmobile1.jpg', title: Globalize.translate('WebClientTourMobile1') },
{ imageUrl: 'css/images/tour/web/tourmobile2.png', title: Globalize.translate('WebClientTourMobile2') },
{ imageUrl: 'css/images/tour/enjoy.jpg', title: Globalize.translate('MessageEnjoyYourStay') }
];
require(['slideshow'], function (slideshow) {
var newSlideShow = new slideshow({
slides: slides,
interactive: true,
loop: false
});
newSlideShow.show();
dismissWelcome(page, userId);
2016-05-16 13:48:56 -07:00
page.querySelector('.welcomeMessage').classList.add('hide');
2015-05-08 20:48:43 -07:00
});
2014-05-30 14:06:57 -07:00
});
}
2016-11-15 23:00:28 -07:00
function getRequirePromise(deps) {
return new Promise(function (resolve, reject) {
require(deps, resolve);
});
}
2015-08-18 08:35:51 -07:00
function loadHomeTab(page, tabContent) {
2014-05-30 14:06:57 -07:00
2016-05-12 12:21:43 -07:00
if (window.ApiClient) {
var userId = Dashboard.getCurrentUserId();
Dashboard.showLoadingMsg();
2015-06-29 11:45:42 -07:00
2016-11-15 23:00:28 -07:00
var promises = [
getDisplayPreferences('home', userId),
Dashboard.getCurrentUser(),
getRequirePromise(['userSettings'])
];
2015-03-24 18:14:24 -07:00
2016-11-15 23:00:28 -07:00
Promise.all(promises).then(function(responses) {
var displayPreferences = responses[0];
var user = responses[1];
var userSettings = responses[2];
2015-04-12 09:46:29 -07:00
2016-11-15 23:00:28 -07:00
loadSections(tabContent, user, userSettings).then(function () {
2016-05-12 12:21:43 -07:00
2016-11-15 23:00:28 -07:00
if (!AppInfo.isNativeApp) {
showWelcomeIfNeeded(page, displayPreferences);
}
Dashboard.hideLoadingMsg();
2015-05-19 12:15:40 -07:00
});
2016-05-12 12:21:43 -07:00
});
2015-05-19 12:15:40 -07:00
}
2015-07-01 08:47:41 -07:00
}
2015-05-12 21:55:19 -07:00
function getDisplayPreferences(key, userId) {
2016-02-29 23:02:03 -07:00
return ApiClient.getDisplayPreferences(key, userId, displayPreferencesKey());
2015-05-12 21:55:19 -07:00
}
2016-03-16 13:38:01 -07:00
return function (view, params) {
var self = this;
2016-03-30 23:33:37 -07:00
self.renderTab = function () {
2016-05-15 09:30:32 -07:00
var tabContent = view.querySelector('.pageTabContent[data-index=\'' + 0 + '\']');
2016-03-30 23:33:37 -07:00
loadHomeTab(view, tabContent);
};
2016-08-15 19:40:29 -07:00
var viewTabs = view.querySelector('.libraryViewNav');
2016-03-16 13:38:01 -07:00
2016-09-01 13:21:55 -07:00
libraryBrowser.configurePaperLibraryTabs(view, viewTabs, view.querySelectorAll('.pageTabContent'), [0, 1, 2, 3], AppInfo.enableHomeTabs);
2016-03-16 13:38:01 -07:00
2016-03-30 23:33:37 -07:00
var tabControllers = [];
var renderedTabs = [];
2016-06-29 09:31:01 -07:00
function getTabController(page, index, callback) {
2016-03-30 23:33:37 -07:00
var depends = [];
switch (index) {
case 0:
depends.push('scripts/sections');
break;
case 1:
depends.push('scripts/homenextup');
break;
case 2:
depends.push('scripts/homefavorites');
break;
case 3:
depends.push('scripts/homeupcoming');
break;
default:
return;
}
require(depends, function (controllerFactory) {
2016-06-29 09:31:01 -07:00
var tabContent;
2016-03-30 23:33:37 -07:00
if (index == 0) {
2016-06-29 09:31:01 -07:00
tabContent = view.querySelector('.pageTabContent[data-index=\'' + index + '\']');
2016-03-30 23:33:37 -07:00
self.tabContent = tabContent;
}
var controller = tabControllers[index];
if (!controller) {
2016-06-29 09:31:01 -07:00
tabContent = view.querySelector('.pageTabContent[data-index=\'' + index + '\']');
2016-03-30 23:33:37 -07:00
controller = index ? new controllerFactory(view, params, tabContent) : self;
tabControllers[index] = controller;
if (controller.initTab) {
controller.initTab();
}
}
2016-06-29 09:31:01 -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-03-30 23:33:37 -07:00
if (renderedTabs.indexOf(index) == -1) {
renderedTabs.push(index);
controller.renderTab();
}
});
}
2016-08-15 19:40:29 -07:00
viewTabs.addEventListener('beforetabchange', function (e) {
2016-06-29 09:31:01 -07:00
preLoadTab(view, parseInt(e.detail.selectedTabIndex));
});
2016-08-15 19:40:29 -07:00
viewTabs.addEventListener('tabchange', function (e) {
2016-03-24 11:11:03 -07:00
loadTab(view, parseInt(e.detail.selectedTabIndex));
2016-03-16 13:38:01 -07:00
});
view.querySelector('.btnTakeTour').addEventListener('click', function () {
takeTour(view, Dashboard.getCurrentUserId());
});
if (AppInfo.enableHomeTabs) {
view.classList.remove('noSecondaryNavPage');
view.querySelector('.libraryViewNav').classList.remove('hide');
} else {
view.classList.add('noSecondaryNavPage');
view.querySelector('.libraryViewNav').classList.add('hide');
}
2016-05-09 12:27:38 -07:00
function onPlaybackStop(e, state) {
if (state.NowPlayingItem && state.NowPlayingItem.MediaType == 'Video') {
2016-08-15 19:40:29 -07:00
viewTabs.triggerTabChange();
2016-05-09 12:27:38 -07:00
}
}
2016-06-29 10:43:34 -07:00
function onWebSocketMessage(e, data) {
var msg = data;
if (msg.MessageType === "UserDataChanged") {
if (msg.Data.UserId == Dashboard.getCurrentUserId()) {
renderedTabs = [];
}
}
}
2016-03-16 13:38:01 -07:00
view.addEventListener('viewshow', function (e) {
Events.on(MediaController, 'playbackstop', onPlaybackStop);
2016-06-29 10:43:34 -07:00
Events.on(ApiClient, "websocketmessage", onWebSocketMessage);
2016-03-16 13:38:01 -07:00
});
view.addEventListener('viewbeforehide', function (e) {
Events.off(MediaController, 'playbackstop', onPlaybackStop);
2016-06-29 10:43:34 -07:00
Events.off(ApiClient, "websocketmessage", onWebSocketMessage);
2016-03-16 13:38:01 -07:00
});
2016-08-15 19:40:29 -07:00
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
view.addEventListener('viewdestroy', function (e) {
if (self.headroom) {
self.headroom.remove(viewTabs);
}
});
2016-03-16 22:04:32 -07:00
};
2016-03-15 22:33:31 -07:00
});