1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Migration of layoutManager and itemsrefresher to ES6 module

This commit is contained in:
Cameron 2020-08-04 13:47:40 +01:00
parent b5fab608c1
commit 73bb781c6a
3 changed files with 189 additions and 188 deletions

View file

@ -124,6 +124,8 @@
"src/components/itemHelper.js", "src/components/itemHelper.js",
"src/components/itemidentifier/itemidentifier.js", "src/components/itemidentifier/itemidentifier.js",
"src/components/itemMediaInfo/itemMediaInfo.js", "src/components/itemMediaInfo/itemMediaInfo.js",
"src/components/itemsrefresher.js",
"src/components/layoutManager.js",
"src/components/lazyLoader/lazyLoaderIntersectionObserver.js", "src/components/lazyLoader/lazyLoaderIntersectionObserver.js",
"src/components/libraryoptionseditor/libraryoptionseditor.js", "src/components/libraryoptionseditor/libraryoptionseditor.js",
"src/components/listview/listview.js", "src/components/listview/listview.js",

View file

@ -1,12 +1,11 @@
define(['playbackManager', 'serverNotifications', 'events'], function (playbackManager, serverNotifications, events) { import playbackManager from 'playbackManager';
'use strict'; import serverNotifications from 'serverNotifications';
import events from 'events';
playbackManager = playbackManager.default || playbackManager;
function onUserDataChanged(e, apiClient, userData) { function onUserDataChanged(e, apiClient, userData) {
var instance = this; const instance = this;
var eventsToMonitor = getEventsToMonitor(instance); const eventsToMonitor = getEventsToMonitor(instance);
// TODO: Check user data change reason? // TODO: Check user data change reason?
if (eventsToMonitor.indexOf('markfavorite') !== -1) { if (eventsToMonitor.indexOf('markfavorite') !== -1) {
@ -17,8 +16,8 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function getEventsToMonitor(instance) { function getEventsToMonitor(instance) {
var options = instance.options; const options = instance.options;
var monitor = options ? options.monitorEvents : null; const monitor = options ? options.monitorEvents : null;
if (monitor) { if (monitor) {
return monitor.split(','); return monitor.split(',');
} }
@ -27,7 +26,7 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function onTimerCreated(e, apiClient, data) { function onTimerCreated(e, apiClient, data) {
var instance = this; const instance = this;
if (getEventsToMonitor(instance).indexOf('timers') !== -1) { if (getEventsToMonitor(instance).indexOf('timers') !== -1) {
instance.notifyRefreshNeeded(); instance.notifyRefreshNeeded();
@ -36,7 +35,7 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function onSeriesTimerCreated(e, apiClient, data) { function onSeriesTimerCreated(e, apiClient, data) {
var instance = this; const instance = this;
if (getEventsToMonitor(instance).indexOf('seriestimers') !== -1) { if (getEventsToMonitor(instance).indexOf('seriestimers') !== -1) {
instance.notifyRefreshNeeded(); instance.notifyRefreshNeeded();
return; return;
@ -44,7 +43,7 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function onTimerCancelled(e, apiClient, data) { function onTimerCancelled(e, apiClient, data) {
var instance = this; const instance = this;
if (getEventsToMonitor(instance).indexOf('timers') !== -1) { if (getEventsToMonitor(instance).indexOf('timers') !== -1) {
instance.notifyRefreshNeeded(); instance.notifyRefreshNeeded();
@ -53,7 +52,7 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function onSeriesTimerCancelled(e, apiClient, data) { function onSeriesTimerCancelled(e, apiClient, data) {
var instance = this; const instance = this;
if (getEventsToMonitor(instance).indexOf('seriestimers') !== -1) { if (getEventsToMonitor(instance).indexOf('seriestimers') !== -1) {
instance.notifyRefreshNeeded(); instance.notifyRefreshNeeded();
return; return;
@ -61,25 +60,25 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function onLibraryChanged(e, apiClient, data) { function onLibraryChanged(e, apiClient, data) {
var instance = this; const instance = this;
var eventsToMonitor = getEventsToMonitor(instance); const eventsToMonitor = getEventsToMonitor(instance);
if (eventsToMonitor.indexOf('seriestimers') !== -1 || eventsToMonitor.indexOf('timers') !== -1) { if (eventsToMonitor.indexOf('seriestimers') !== -1 || eventsToMonitor.indexOf('timers') !== -1) {
// yes this is an assumption // yes this is an assumption
return; return;
} }
var itemsAdded = data.ItemsAdded || []; const itemsAdded = data.ItemsAdded || [];
var itemsRemoved = data.ItemsRemoved || []; const itemsRemoved = data.ItemsRemoved || [];
if (!itemsAdded.length && !itemsRemoved.length) { if (!itemsAdded.length && !itemsRemoved.length) {
return; return;
} }
var options = instance.options || {}; const options = instance.options || {};
var parentId = options.parentId; const parentId = options.parentId;
if (parentId) { if (parentId) {
var foldersAddedTo = data.FoldersAddedTo || []; const foldersAddedTo = data.FoldersAddedTo || [];
var foldersRemovedFrom = data.FoldersRemovedFrom || []; const foldersRemovedFrom = data.FoldersRemovedFrom || [];
var collectionFolders = data.CollectionFolders || []; const collectionFolders = data.CollectionFolders || [];
if (foldersAddedTo.indexOf(parentId) === -1 && foldersRemovedFrom.indexOf(parentId) === -1 && collectionFolders.indexOf(parentId) === -1) { if (foldersAddedTo.indexOf(parentId) === -1 && foldersRemovedFrom.indexOf(parentId) === -1 && collectionFolders.indexOf(parentId) === -1) {
return; return;
@ -90,11 +89,11 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function onPlaybackStopped(e, stopInfo) { function onPlaybackStopped(e, stopInfo) {
var instance = this; const instance = this;
var state = stopInfo.state; const state = stopInfo.state;
var eventsToMonitor = getEventsToMonitor(instance); const eventsToMonitor = getEventsToMonitor(instance);
if (state.NowPlayingItem && state.NowPlayingItem.MediaType === 'Video') { if (state.NowPlayingItem && state.NowPlayingItem.MediaType === 'Video') {
if (eventsToMonitor.indexOf('videoplayback') !== -1) { if (eventsToMonitor.indexOf('videoplayback') !== -1) {
instance.notifyRefreshNeeded(true); instance.notifyRefreshNeeded(true);
@ -109,14 +108,14 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
function addNotificationEvent(instance, name, handler, owner) { function addNotificationEvent(instance, name, handler, owner) {
var localHandler = handler.bind(instance); const localHandler = handler.bind(instance);
owner = owner || serverNotifications; owner = owner || serverNotifications;
events.on(owner, name, localHandler); events.on(owner, name, localHandler);
instance['event_' + name] = localHandler; instance['event_' + name] = localHandler;
} }
function removeNotificationEvent(instance, name, owner) { function removeNotificationEvent(instance, name, owner) {
var handler = instance['event_' + name]; const handler = instance['event_' + name];
if (handler) { if (handler) {
owner = owner || serverNotifications; owner = owner || serverNotifications;
events.off(owner, name, handler); events.off(owner, name, handler);
@ -124,7 +123,8 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
} }
function ItemsRefresher(options) { class ItemsRefresher {
constructor(options) {
this.options = options || {}; this.options = options || {};
addNotificationEvent(this, 'UserDataChanged', onUserDataChanged); addNotificationEvent(this, 'UserDataChanged', onUserDataChanged);
@ -136,18 +136,18 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
addNotificationEvent(this, 'playbackstop', onPlaybackStopped, playbackManager); addNotificationEvent(this, 'playbackstop', onPlaybackStopped, playbackManager);
} }
ItemsRefresher.prototype.pause = function () { pause() {
clearRefreshInterval(this, true); clearRefreshInterval(this, true);
this.paused = true; this.paused = true;
}; }
ItemsRefresher.prototype.resume = function (options) { resume(options) {
this.paused = false; this.paused = false;
var refreshIntervalEndTime = this.refreshIntervalEndTime; const refreshIntervalEndTime = this.refreshIntervalEndTime;
if (refreshIntervalEndTime) { if (refreshIntervalEndTime) {
var remainingMs = refreshIntervalEndTime - new Date().getTime(); const remainingMs = refreshIntervalEndTime - new Date().getTime();
if (remainingMs > 0 && !this.needsRefresh) { if (remainingMs > 0 && !this.needsRefresh) {
resetRefreshInterval(this, remainingMs); resetRefreshInterval(this, remainingMs);
} else { } else {
@ -161,9 +161,9 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
return Promise.resolve(); return Promise.resolve();
}; }
ItemsRefresher.prototype.refreshItems = function () { refreshItems() {
if (!this.fetchData) { if (!this.fetchData) {
return Promise.resolve(); return Promise.resolve();
} }
@ -176,15 +176,15 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
this.needsRefresh = false; this.needsRefresh = false;
return this.fetchData().then(onDataFetched.bind(this)); return this.fetchData().then(onDataFetched.bind(this));
}; }
ItemsRefresher.prototype.notifyRefreshNeeded = function (isInForeground) { notifyRefreshNeeded(isInForeground) {
if (this.paused) { if (this.paused) {
this.needsRefresh = true; this.needsRefresh = true;
return; return;
} }
var timeout = this.refreshTimeout; const timeout = this.refreshTimeout;
if (timeout) { if (timeout) {
clearTimeout(timeout); clearTimeout(timeout);
} }
@ -194,7 +194,23 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} else { } else {
this.refreshTimeout = setTimeout(this.refreshItems.bind(this), 10000); this.refreshTimeout = setTimeout(this.refreshItems.bind(this), 10000);
} }
}; }
destroy() {
clearRefreshInterval(this);
removeNotificationEvent(this, 'UserDataChanged');
removeNotificationEvent(this, 'TimerCreated');
removeNotificationEvent(this, 'SeriesTimerCreated');
removeNotificationEvent(this, 'TimerCancelled');
removeNotificationEvent(this, 'SeriesTimerCancelled');
removeNotificationEvent(this, 'LibraryChanged');
removeNotificationEvent(this, 'playbackstop', playbackManager);
this.fetchData = null;
this.options = null;
}
}
function clearRefreshInterval(instance, isPausing) { function clearRefreshInterval(instance, isPausing) {
if (instance.refreshInterval) { if (instance.refreshInterval) {
@ -211,7 +227,7 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
clearRefreshInterval(instance); clearRefreshInterval(instance);
if (!intervalMs) { if (!intervalMs) {
var options = instance.options; const options = instance.options;
if (options) { if (options) {
intervalMs = options.refreshIntervalMs; intervalMs = options.refreshIntervalMs;
} }
@ -231,20 +247,4 @@ define(['playbackManager', 'serverNotifications', 'events'], function (playbackM
} }
} }
ItemsRefresher.prototype.destroy = function () { export default ItemsRefresher;
clearRefreshInterval(this);
removeNotificationEvent(this, 'UserDataChanged');
removeNotificationEvent(this, 'TimerCreated');
removeNotificationEvent(this, 'SeriesTimerCreated');
removeNotificationEvent(this, 'TimerCancelled');
removeNotificationEvent(this, 'SeriesTimerCancelled');
removeNotificationEvent(this, 'LibraryChanged');
removeNotificationEvent(this, 'playbackstop', playbackManager);
this.fetchData = null;
this.options = null;
};
return ItemsRefresher;
});

View file

@ -1,7 +1,6 @@
define(['browser', 'appSettings', 'events'], function (browser, appSettings, events) { import browser from 'browser';
'use strict'; import appSettings from 'appSettings';
import events from 'events';
browser = browser.default || browser;
function setLayout(instance, layout, selectedLayout) { function setLayout(instance, layout, selectedLayout) {
if (layout === selectedLayout) { if (layout === selectedLayout) {
@ -13,11 +12,11 @@ define(['browser', 'appSettings', 'events'], function (browser, appSettings, eve
} }
} }
function LayoutManager() { class LayoutManager {
constructor() {
} }
LayoutManager.prototype.setLayout = function (layout, save) { setLayout(layout, save) {
if (!layout || layout === 'auto') { if (!layout || layout === 'auto') {
this.autoLayout(); this.autoLayout();
@ -35,13 +34,13 @@ define(['browser', 'appSettings', 'events'], function (browser, appSettings, eve
} }
events.trigger(this, 'modechange'); events.trigger(this, 'modechange');
}; }
LayoutManager.prototype.getSavedLayout = function (layout) { getSavedLayout(layout) {
return appSettings.get('layout'); return appSettings.get('layout');
}; }
LayoutManager.prototype.autoLayout = function () { autoLayout() {
// Take a guess at initial layout. The consuming app can override // Take a guess at initial layout. The consuming app can override
if (browser.mobile) { if (browser.mobile) {
this.setLayout('mobile', false); this.setLayout('mobile', false);
@ -50,16 +49,16 @@ define(['browser', 'appSettings', 'events'], function (browser, appSettings, eve
} else { } else {
this.setLayout(this.defaultLayout || 'tv', false); this.setLayout(this.defaultLayout || 'tv', false);
} }
}; }
LayoutManager.prototype.init = function () { init() {
var saved = this.getSavedLayout(); const saved = this.getSavedLayout();
if (saved) { if (saved) {
this.setLayout(saved, false); this.setLayout(saved, false);
} else { } else {
this.autoLayout(); this.autoLayout();
} }
}; }
}
return new LayoutManager(); export default new LayoutManager();
});