mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Move boolean and float parsing to shared utils
This commit is contained in:
parent
5238888ecf
commit
f32538ace4
5 changed files with 48 additions and 50 deletions
|
@ -4,6 +4,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Events } from 'jellyfin-apiclient';
|
import { Events } from 'jellyfin-apiclient';
|
||||||
|
import { toBoolean, toFloat } from '../../../scripts/stringUtils';
|
||||||
import * as Helper from './Helper';
|
import * as Helper from './Helper';
|
||||||
import Settings from './Settings';
|
import Settings from './Settings';
|
||||||
|
|
||||||
|
@ -48,25 +49,25 @@ class PlaybackCore {
|
||||||
*/
|
*/
|
||||||
loadPreferences() {
|
loadPreferences() {
|
||||||
// Minimum required delay for SpeedToSync to kick in, in milliseconds.
|
// Minimum required delay for SpeedToSync to kick in, in milliseconds.
|
||||||
this.minDelaySpeedToSync = Settings.getFloat('minDelaySpeedToSync', 60.0);
|
this.minDelaySpeedToSync = toFloat(Settings.get('minDelaySpeedToSync'), 60.0);
|
||||||
|
|
||||||
// Maximum delay after which SkipToSync is used instead of SpeedToSync, in milliseconds.
|
// Maximum delay after which SkipToSync is used instead of SpeedToSync, in milliseconds.
|
||||||
this.maxDelaySpeedToSync = Settings.getFloat('maxDelaySpeedToSync', 3000.0);
|
this.maxDelaySpeedToSync = toFloat(Settings.get('maxDelaySpeedToSync'), 3000.0);
|
||||||
|
|
||||||
// Time during which the playback is sped up, in milliseconds.
|
// Time during which the playback is sped up, in milliseconds.
|
||||||
this.speedToSyncDuration = Settings.getFloat('speedToSyncDuration', 1000.0);
|
this.speedToSyncDuration = toFloat(Settings.get('speedToSyncDuration'), 1000.0);
|
||||||
|
|
||||||
// Minimum required delay for SkipToSync to kick in, in milliseconds.
|
// Minimum required delay for SkipToSync to kick in, in milliseconds.
|
||||||
this.minDelaySkipToSync = Settings.getFloat('minDelaySkipToSync', 400.0);
|
this.minDelaySkipToSync = toFloat(Settings.get('minDelaySkipToSync'), 400.0);
|
||||||
|
|
||||||
// Whether SpeedToSync should be used.
|
// Whether SpeedToSync should be used.
|
||||||
this.useSpeedToSync = Settings.getBool('useSpeedToSync', true);
|
this.useSpeedToSync = toBoolean(Settings.get('useSpeedToSync'), true);
|
||||||
|
|
||||||
// Whether SkipToSync should be used.
|
// Whether SkipToSync should be used.
|
||||||
this.useSkipToSync = Settings.getBool('useSkipToSync', true);
|
this.useSkipToSync = toBoolean(Settings.get('useSkipToSync'), true);
|
||||||
|
|
||||||
// Whether sync correction during playback is active.
|
// Whether sync correction during playback is active.
|
||||||
this.enableSyncCorrection = Settings.getBool('enableSyncCorrection', true);
|
this.enableSyncCorrection = toBoolean(Settings.get('enableSyncCorrection'), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
* Module that manages SyncPlay settings.
|
* Module that manages SyncPlay settings.
|
||||||
* @module components/syncPlay/core/Settings
|
* @module components/syncPlay/core/Settings
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Events, AppStorage } from 'jellyfin-apiclient';
|
import { Events, AppStorage } from 'jellyfin-apiclient';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,38 +46,7 @@ class SyncPlaySettings {
|
||||||
|
|
||||||
console.debug(`SyncPlay Settings set: '${name}' from '${oldValue}' to '${newValue}'.`);
|
console.debug(`SyncPlay Settings set: '${name}' from '${oldValue}' to '${newValue}'.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the value of a setting as boolean.
|
|
||||||
* @param {string} name The name of the setting.
|
|
||||||
* @param {boolean} defaultValue The default value if the setting does not exist.
|
|
||||||
* @returns {boolean} The value.
|
|
||||||
*/
|
|
||||||
getBool(name, defaultValue = false) {
|
|
||||||
const value = this.get(name);
|
|
||||||
if (value !== 'true' && value !== 'false') {
|
|
||||||
return defaultValue;
|
|
||||||
} else {
|
|
||||||
return this.get(name) !== 'false';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the value of a setting as float number.
|
|
||||||
* @param {string} name The name of the setting.
|
|
||||||
* @param {number} defaultValue The default value if the setting does not exist.
|
|
||||||
* @returns {number} The value.
|
|
||||||
*/
|
|
||||||
getFloat(name, defaultValue = 0) {
|
|
||||||
const value = this.get(name);
|
|
||||||
if (value === null || value === '' || isNaN(value)) {
|
|
||||||
return defaultValue;
|
|
||||||
} else {
|
|
||||||
return Number.parseFloat(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** SyncPlaySettings singleton. */
|
/** SyncPlaySettings singleton. */
|
||||||
const Settings = new SyncPlaySettings();
|
export default new SyncPlaySettings();
|
||||||
export default Settings;
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Events } from 'jellyfin-apiclient';
|
import { Events } from 'jellyfin-apiclient';
|
||||||
|
import { toFloat } from '../../../../scripts/stringUtils';
|
||||||
import Settings from '../Settings';
|
import Settings from '../Settings';
|
||||||
import TimeSyncServer from './TimeSyncServer';
|
import TimeSyncServer from './TimeSyncServer';
|
||||||
|
|
||||||
|
@ -26,7 +27,7 @@ class TimeSyncCore {
|
||||||
this.timeSyncServer = null;
|
this.timeSyncServer = null;
|
||||||
|
|
||||||
this.timeSyncDeviceId = Settings.get('timeSyncDevice') || 'server';
|
this.timeSyncDeviceId = Settings.get('timeSyncDevice') || 'server';
|
||||||
this.extraTimeOffset = Settings.getFloat('extraTimeOffset', 0.0);
|
this.extraTimeOffset = toFloat(Settings.get('extraTimeOffset'), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,7 +48,7 @@ class TimeSyncCore {
|
||||||
});
|
});
|
||||||
|
|
||||||
Events.on(Settings, 'extraTimeOffset', () => {
|
Events.on(Settings, 'extraTimeOffset', () => {
|
||||||
this.extraTimeOffset = Settings.getFloat('extraTimeOffset', 0.0);
|
this.extraTimeOffset = toFloat(Settings.get('extraTimeOffset'), 0.0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,7 @@ import '../../../../elements/emby-button/paper-icon-button-light';
|
||||||
import '../../../../elements/emby-checkbox/emby-checkbox';
|
import '../../../../elements/emby-checkbox/emby-checkbox';
|
||||||
import '../../../listview/listview.scss';
|
import '../../../listview/listview.scss';
|
||||||
import '../../../formdialog.scss';
|
import '../../../formdialog.scss';
|
||||||
|
import { toBoolean, toFloat } from '../../../../scripts/stringUtils';
|
||||||
|
|
||||||
function centerFocus(elem, horiz, on) {
|
function centerFocus(elem, horiz, on) {
|
||||||
import('../../../../scripts/scrollHelper').then((scrollHelper) => {
|
import('../../../../scripts/scrollHelper').then((scrollHelper) => {
|
||||||
|
@ -145,14 +146,14 @@ class SettingsEditor {
|
||||||
async initEditor() {
|
async initEditor() {
|
||||||
const { context } = this;
|
const { context } = this;
|
||||||
|
|
||||||
context.querySelector('#txtExtraTimeOffset').value = SyncPlay.Settings.getFloat('extraTimeOffset', 0.0);
|
context.querySelector('#txtExtraTimeOffset').value = toFloat(SyncPlay.Settings.get('extraTimeOffset'), 0.0);
|
||||||
context.querySelector('#chkSyncCorrection').checked = SyncPlay.Settings.getBool('enableSyncCorrection', true);
|
context.querySelector('#chkSyncCorrection').checked = toBoolean(SyncPlay.Settings.get('enableSyncCorrection'), true);
|
||||||
context.querySelector('#txtMinDelaySpeedToSync').value = SyncPlay.Settings.getFloat('minDelaySpeedToSync', 60.0);
|
context.querySelector('#txtMinDelaySpeedToSync').value = toFloat(SyncPlay.Settings.get('minDelaySpeedToSync'), 60.0);
|
||||||
context.querySelector('#txtMaxDelaySpeedToSync').value = SyncPlay.Settings.getFloat('maxDelaySpeedToSync', 3000.0);
|
context.querySelector('#txtMaxDelaySpeedToSync').value = toFloat(SyncPlay.Settings.get('maxDelaySpeedToSync'), 3000.0);
|
||||||
context.querySelector('#txtSpeedToSyncDuration').value = SyncPlay.Settings.getFloat('speedToSyncDuration', 1000.0);
|
context.querySelector('#txtSpeedToSyncDuration').value = toFloat(SyncPlay.Settings.get('speedToSyncDuration'), 1000.0);
|
||||||
context.querySelector('#txtMinDelaySkipToSync').value = SyncPlay.Settings.getFloat('minDelaySkipToSync', 400.0);
|
context.querySelector('#txtMinDelaySkipToSync').value = toFloat(SyncPlay.Settings.get('minDelaySkipToSync'), 400.0);
|
||||||
context.querySelector('#chkSpeedToSync').checked = SyncPlay.Settings.getBool('useSpeedToSync', true);
|
context.querySelector('#chkSpeedToSync').checked = toBoolean(SyncPlay.Settings.get('useSpeedToSync'), true);
|
||||||
context.querySelector('#chkSkipToSync').checked = SyncPlay.Settings.getBool('useSkipToSync', true);
|
context.querySelector('#chkSkipToSync').checked = toBoolean(SyncPlay.Settings.get('useSkipToSync'), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
27
src/scripts/stringUtils.js
Normal file
27
src/scripts/stringUtils.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/**
|
||||||
|
* Gets the value of a string as boolean.
|
||||||
|
* @param {string} name The value as a string.
|
||||||
|
* @param {boolean} defaultValue The default value if the string is invalid.
|
||||||
|
* @returns {boolean} The value.
|
||||||
|
*/
|
||||||
|
export function toBoolean(value, defaultValue = false) {
|
||||||
|
if (value !== 'true' && value !== 'false') {
|
||||||
|
return defaultValue;
|
||||||
|
} else {
|
||||||
|
return value !== 'false';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the value of a string as float number.
|
||||||
|
* @param {string} value The value as a string.
|
||||||
|
* @param {number} defaultValue The default value if the string is invalid.
|
||||||
|
* @returns {number} The value.
|
||||||
|
*/
|
||||||
|
export function toFloat(value, defaultValue = 0) {
|
||||||
|
if (value === null || value === '' || isNaN(value)) {
|
||||||
|
return defaultValue;
|
||||||
|
} else {
|
||||||
|
return parseFloat(value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue