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/itemidentifier/itemidentifier.js",
"src/components/itemMediaInfo/itemMediaInfo.js",
"src/components/itemsrefresher.js",
"src/components/layoutManager.js",
"src/components/lazyLoader/lazyLoaderIntersectionObserver.js",
"src/components/libraryoptionseditor/libraryoptionseditor.js",
"src/components/listview/listview.js",

View file

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

View file

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