jellyfish-web/src/scripts/site.js

277 lines
8.8 KiB
JavaScript
Raw Normal View History

2020-08-14 06:36:46 +02:00
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import 'jquery';
import 'fast-text-encoding';
import 'intersection-observer';
2020-08-14 08:46:34 +02:00
import 'classlist.js';
2020-08-14 06:36:46 +02:00
import 'whatwg-fetch';
import 'resize-observer-polyfill';
import 'jellyfin-noto';
import '../assets/css/site.css';
2020-10-12 22:17:25 +01:00
import AppInfo from '../components/AppInfo';
import { Events } from 'jellyfin-apiclient';
import ServerConnections from '../components/ServerConnections';
2020-10-18 20:00:39 +01:00
import globalize from './globalize';
import browser from './browser';
import keyboardNavigation from './keyboardNavigation';
import './mouseManager';
import autoFocuser from '../components/autoFocuser';
import { appHost } from '../components/apphost';
import { getPlugins } from './settings/webSettings';
import { pluginManager } from '../components/pluginManager';
import packageManager from '../components/packageManager';
import { appRouter } from '../components/appRouter';
2020-08-14 06:36:46 +02:00
// TODO: Move this elsewhere
2020-08-08 15:26:03 +02:00
window.getWindowLocationSearch = function(win) {
2020-10-07 21:12:14 +09:00
let search = (win || window).location.search;
2018-10-23 01:05:09 +03:00
if (!search) {
2020-10-07 21:12:14 +09:00
const index = window.location.href.indexOf('?');
2020-07-30 16:07:13 +02:00
if (index != -1) {
search = window.location.href.substring(index);
}
2018-10-23 01:05:09 +03:00
}
2020-05-04 12:44:12 +02:00
return search || '';
2020-08-08 15:26:03 +02:00
};
2018-10-23 01:05:09 +03:00
2020-08-14 06:36:46 +02:00
// TODO: Move this elsewhere
2020-08-08 15:26:03 +02:00
window.getParameterByName = function(name, url) {
2020-05-04 12:44:12 +02:00
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
2020-10-07 21:12:14 +09:00
const regexS = '[\\?&]' + name + '=([^&#]*)';
const regex = new RegExp(regexS, 'i');
const results = regex.exec(url || getWindowLocationSearch());
2020-07-30 16:07:13 +02:00
if (results == null) {
2020-05-04 12:44:12 +02:00
return '';
}
2020-05-04 12:44:12 +02:00
return decodeURIComponent(results[1].replace(/\+/g, ' '));
2020-07-19 16:15:11 +02:00
};
2018-10-23 01:05:09 +03:00
2020-08-14 06:36:46 +02:00
// TODO: Move this elsewhere
2020-08-08 15:26:03 +02:00
window.pageClassOn = function(eventName, className, fn) {
2019-03-04 20:38:39 +00:00
document.addEventListener(eventName, function (event) {
2020-10-07 21:12:14 +09:00
const target = event.target;
if (target.classList.contains(className)) {
2019-03-04 20:38:39 +00:00
fn.call(target, event);
}
});
2020-08-08 15:26:03 +02:00
};
2018-10-23 01:05:09 +03:00
2020-08-14 06:36:46 +02:00
// TODO: Move this elsewhere
2020-07-19 16:15:11 +02:00
window.pageIdOn = function(eventName, id, fn) {
2019-03-04 20:38:39 +00:00
document.addEventListener(eventName, function (event) {
2020-10-07 21:12:14 +09:00
const target = event.target;
if (target.id === id) {
2019-03-04 20:38:39 +00:00
fn.call(target, event);
}
});
};
if (window.appMode === 'cordova' || window.appMode === 'android' || window.appMode === 'standalone') {
2020-10-12 22:17:25 +01:00
AppInfo.isNativeApp = true;
}
Object.freeze(AppInfo);
2020-10-18 20:00:39 +01:00
function loadCoreDictionary() {
const languages = ['ar', 'be-by', 'bg-bg', 'ca', 'cs', 'da', 'de', 'el', 'en-gb', 'en-us', 'es', 'es-ar', 'es-mx', 'fa', 'fi', 'fr', 'fr-ca', 'gsw', 'he', 'hi-in', 'hr', 'hu', 'id', 'it', 'ja', 'kk', 'ko', 'lt-lt', 'ms', 'nb', 'nl', 'pl', 'pt-br', 'pt-pt', 'ro', 'ru', 'sk', 'sl-si', 'sv', 'tr', 'uk', 'vi', 'zh-cn', 'zh-hk', 'zh-tw'];
const translations = languages.map(function (language) {
return {
lang: language,
path: language + '.json'
};
});
globalize.defaultModule('core');
return globalize.loadStrings({
name: 'core',
translations: translations
});
}
function init() {
ServerConnections.initApiClient();
console.debug('initAfterDependencies promises resolved');
loadCoreDictionary().then(function () {
onGlobalizeInit();
});
keyboardNavigation.enable();
autoFocuser.enable();
Events.on(ServerConnections, 'localusersignedin', globalize.updateCurrentCulture);
}
function onGlobalizeInit() {
if (window.appMode === 'android') {
if (window.location.href.toString().toLowerCase().indexOf('start=backgroundsync') !== -1) {
return onAppReady(browser);
}
2018-10-23 01:05:09 +03:00
}
2020-10-18 20:00:39 +01:00
document.title = globalize.translateHtml(document.title, 'core');
if (browser.tv && !browser.android) {
console.debug('using system fonts with explicit sizes');
import('../assets/css/fonts.sized.css');
} else {
console.debug('using default fonts');
import('../assets/css/fonts.css');
2018-10-23 01:05:09 +03:00
}
2020-10-18 20:00:39 +01:00
import('../assets/css/librarybrowser.css');
loadPlugins(appHost, browser).then(function () {
onAppReady(browser);
});
}
function loadPlugins(appHost, browser, shell) {
console.groupCollapsed('loading installed plugins');
console.dir(pluginManager);
return getPlugins().then(function (list) {
// these two plugins are dependent on features
if (!appHost.supports('remotecontrol')) {
list.splice(list.indexOf('sessionPlayer'), 1);
if (!browser.chrome && !browser.opera) {
list.splice(list.indexOf('chromecastPlayer', 1));
}
2018-10-23 01:05:09 +03:00
}
2020-10-18 20:00:39 +01:00
// add any native plugins
if (window.NativeShell) {
list = list.concat(window.NativeShell.getPlugins());
}
2020-10-18 20:00:39 +01:00
Promise.all(list.map((plugin) => {
return pluginManager.loadPlugin(import(/* webpackChunkName: "[request]" */ `../plugins/${plugin}`));
}))
.then(function (pluginPromises) {
console.debug('finished loading plugins');
})
.catch(() => console.debug('failed loading plugins')
)
.finally(() => {
console.groupEnd('loading installed plugins');
packageManager.init();
})
;
});
}
2018-10-23 01:05:09 +03:00
2020-10-18 20:00:39 +01:00
function onAppReady(browser) {
console.debug('begin onAppReady');
2020-10-18 20:00:39 +01:00
console.debug('onAppReady: loading dependencies');
2020-10-18 20:00:39 +01:00
if (browser.iOS) {
import('../assets/css/ios.css');
2018-10-23 01:05:09 +03:00
}
2020-10-18 20:00:39 +01:00
Promise.all([
import('../elements/emby-button/emby-button'),
import('./autoThemes'),
import('./libraryMenu'),
import('./routes')
])
.then(() => {
appRouter.start({
click: false,
hashbang: true
});
2020-10-18 20:00:39 +01:00
import('../components/themeMediaPlayer');
import('./autoBackdrops');
2020-08-14 06:36:46 +02:00
2020-10-18 20:00:39 +01:00
if (!browser.tv && !browser.xboxOne && !browser.ps4) {
import('../components/nowPlayingBar/nowPlayingBar');
}
2020-10-18 20:00:39 +01:00
if (appHost.supports('remotecontrol')) {
import('../components/playback/playerSelectionMenu');
import('../components/playback/remotecontrolautoplay');
}
2020-10-18 20:00:39 +01:00
import('../libraries/screensavermanager');
2019-11-23 23:25:10 +03:00
2020-10-18 20:00:39 +01:00
if (!appHost.supports('physicalvolumecontrol') || browser.touch) {
import('../components/playback/volumeosd');
}
2020-10-18 20:00:39 +01:00
/* eslint-disable-next-line compat/compat */
if (navigator.mediaSession || window.NativeShell) {
import('../components/playback/mediasession');
}
2020-10-18 20:00:39 +01:00
import('./serverNotifications');
2020-10-18 20:00:39 +01:00
if (!browser.tv && !browser.xboxOne) {
import('../components/playback/playbackorientation');
registerServiceWorker();
2020-10-18 20:00:39 +01:00
if (window.Notification) {
import('../components/notifications/notifications');
}
}
2020-10-18 20:00:39 +01:00
import('../components/playback/playerSelectionMenu');
2020-08-14 06:36:46 +02:00
2020-10-18 20:00:39 +01:00
const apiClient = ServerConnections.currentApiClient();
if (apiClient) {
fetch(apiClient.getUrl('Branding/Css'))
.then(function(response) {
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText);
2020-08-14 06:36:46 +02:00
}
2020-10-18 20:00:39 +01:00
return response.text();
})
.then(function(css) {
let style = document.querySelector('#cssBranding');
if (!style) {
// Inject the branding css as a dom element in body so it will take
// precedence over other stylesheets
style = document.createElement('style');
style.id = 'cssBranding';
document.body.appendChild(style);
}
style.textContent = css;
})
.catch(function(err) {
console.warn('Error applying custom css', err);
2020-08-14 06:36:46 +02:00
});
}
2020-10-18 20:00:39 +01:00
});
}
function registerServiceWorker() {
/* eslint-disable compat/compat */
if (navigator.serviceWorker && window.appMode !== 'cordova' && window.appMode !== 'android') {
try {
navigator.serviceWorker.register('serviceworker.js');
} catch (err) {
console.error('error registering serviceWorker: ' + err);
2018-10-23 01:05:09 +03:00
}
2020-10-18 20:00:39 +01:00
} else {
console.warn('serviceWorker unsupported');
2018-10-23 01:05:09 +03:00
}
2020-10-18 20:00:39 +01:00
/* eslint-enable compat/compat */
2020-07-19 16:15:11 +02:00
}
2020-10-18 20:00:39 +01:00
init();
2020-05-04 12:44:12 +02:00
pageClassOn('viewshow', 'standalonePage', function () {
document.querySelector('.skinHeader').classList.add('noHeaderRight');
2019-01-21 17:47:10 +09:00
});
2020-05-04 12:44:12 +02:00
pageClassOn('viewhide', 'standalonePage', function () {
document.querySelector('.skinHeader').classList.remove('noHeaderRight');
2018-12-11 00:46:50 -05:00
});