2017-04-22 13:03:00 +00:00
|
|
|
"use strict";
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-04-22 13:03:00 +00:00
|
|
|
const $ = require("jquery");
|
|
|
|
const storage = require("./localStorage");
|
2017-12-11 19:01:15 +00:00
|
|
|
const socket = require("./socket");
|
2018-07-08 14:57:02 +00:00
|
|
|
const {vueApp} = require("./vue");
|
|
|
|
require("../js/autocompletion");
|
2017-04-22 13:03:00 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
const $theme = $("#theme");
|
|
|
|
const $userStyles = $("#user-specified-css");
|
2017-04-22 13:03:00 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
const noCSSparamReg = /[?&]nocss/;
|
|
|
|
|
|
|
|
// Default settings
|
2018-07-08 14:57:02 +00:00
|
|
|
const settings = vueApp.settings;
|
2017-08-06 04:49:22 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
const noSync = ["syncSettings"];
|
|
|
|
|
2019-01-16 09:23:12 +00:00
|
|
|
// alwaysSync is reserved for settings that should be synced
|
|
|
|
// to the server regardless of the clients sync setting.
|
2019-07-17 09:33:59 +00:00
|
|
|
const alwaysSync = ["highlights"];
|
2017-12-11 19:01:15 +00:00
|
|
|
|
2019-07-22 16:50:04 +00:00
|
|
|
const defaultThemeColor = document.querySelector('meta[name="theme-color"]').content;
|
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
// Process usersettings from localstorage.
|
2018-03-30 07:46:59 +00:00
|
|
|
let userSettings = JSON.parse(storage.get("settings")) || false;
|
2017-12-11 19:01:15 +00:00
|
|
|
|
2018-03-30 07:46:59 +00:00
|
|
|
if (!userSettings) {
|
|
|
|
// Enable sync by default if there are no user defined settings.
|
|
|
|
settings.syncSettings = true;
|
|
|
|
} else {
|
|
|
|
for (const key in settings) {
|
2019-01-16 09:23:12 +00:00
|
|
|
// Older The Lounge versions converted highlights to an array, turn it back into a string
|
|
|
|
if (key === "highlights" && typeof userSettings[key] === "object") {
|
|
|
|
userSettings[key] = userSettings[key].join(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the setting in local storage has the same type that the code expects
|
2019-07-17 09:33:59 +00:00
|
|
|
if (
|
|
|
|
typeof userSettings[key] !== "undefined" &&
|
|
|
|
typeof settings[key] === typeof userSettings[key]
|
|
|
|
) {
|
2018-03-30 07:46:59 +00:00
|
|
|
settings[key] = userSettings[key];
|
|
|
|
}
|
2017-08-06 04:49:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-22 13:03:00 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
// Apply custom CSS and themes on page load
|
|
|
|
// Done here and not on init because on slower devices and connections
|
|
|
|
// it can take up to several seconds before init is called.
|
|
|
|
if (typeof userSettings.userStyles === "string" && !noCSSparamReg.test(window.location.search)) {
|
|
|
|
$userStyles.html(userSettings.userStyles);
|
2017-11-07 20:22:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (
|
|
|
|
typeof userSettings.theme === "string" &&
|
|
|
|
$theme.attr("href") !== `themes/${userSettings.theme}.css`
|
|
|
|
) {
|
2018-03-13 15:38:01 +00:00
|
|
|
$theme.attr("href", `themes/${userSettings.theme}.css`);
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
userSettings = null;
|
2017-08-16 08:01:09 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
module.exports = {
|
2018-03-05 00:59:16 +00:00
|
|
|
alwaysSync,
|
|
|
|
noSync,
|
2017-12-11 19:01:15 +00:00
|
|
|
initialized: false,
|
2018-03-05 00:59:16 +00:00
|
|
|
settings,
|
2018-03-30 07:32:20 +00:00
|
|
|
syncAllSettings,
|
2017-12-11 19:01:15 +00:00
|
|
|
processSetting,
|
|
|
|
initialize,
|
2019-03-03 16:28:57 +00:00
|
|
|
updateSetting,
|
2017-12-11 19:01:15 +00:00
|
|
|
};
|
2017-04-22 13:03:00 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
// Updates the checkbox and warning in settings.
|
|
|
|
// When notifications are not supported, this is never called (because
|
|
|
|
// checkbox state can not be changed).
|
|
|
|
function updateDesktopNotificationStatus() {
|
2019-02-20 09:10:18 +00:00
|
|
|
if (Notification.permission === "granted") {
|
|
|
|
vueApp.desktopNotificationState = "granted";
|
2017-12-11 19:01:15 +00:00
|
|
|
} else {
|
2019-02-20 09:10:18 +00:00
|
|
|
vueApp.desktopNotificationState = "blocked";
|
2017-04-22 13:03:00 +00:00
|
|
|
}
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|
2017-04-22 13:03:00 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
function applySetting(name, value) {
|
|
|
|
if (name === "syncSettings" && value) {
|
|
|
|
$syncWarningOverride.hide();
|
2018-03-30 07:32:20 +00:00
|
|
|
$forceSyncButton.hide();
|
2017-12-11 19:01:15 +00:00
|
|
|
} else if (name === "theme") {
|
2019-07-22 16:50:04 +00:00
|
|
|
const themeUrl = `themes/${value}.css`;
|
|
|
|
|
|
|
|
if ($theme.attr("href") !== themeUrl) {
|
|
|
|
$theme.attr("href", themeUrl);
|
|
|
|
|
|
|
|
const newTheme = $settings.find("#theme-select option[value='" + value + "']");
|
|
|
|
let themeColor = defaultThemeColor;
|
|
|
|
|
|
|
|
if (newTheme.length > 0 && newTheme[0].dataset.themeColor) {
|
|
|
|
themeColor = newTheme[0].dataset.themeColor;
|
|
|
|
}
|
2018-03-13 15:38:01 +00:00
|
|
|
|
2019-07-22 16:50:04 +00:00
|
|
|
document.querySelector('meta[name="theme-color"]').content = themeColor;
|
2018-03-13 15:38:01 +00:00
|
|
|
}
|
2017-12-11 19:01:15 +00:00
|
|
|
} else if (name === "userStyles" && !noCSSparamReg.test(window.location.search)) {
|
|
|
|
$userStyles.html(value);
|
|
|
|
} else if (name === "desktopNotifications") {
|
2019-02-20 09:10:18 +00:00
|
|
|
updateDesktopNotificationStatus();
|
|
|
|
|
|
|
|
if (("Notification" in window) && value && Notification.permission !== "granted") {
|
2017-12-11 19:01:15 +00:00
|
|
|
Notification.requestPermission(updateDesktopNotificationStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 13:55:55 +00:00
|
|
|
|
2017-12-11 19:01:15 +00:00
|
|
|
function settingSetEmit(name, value) {
|
2018-03-05 00:59:16 +00:00
|
|
|
socket.emit("setting:set", {name, value});
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When sync is `true` the setting will also be send to the backend for syncing.
|
|
|
|
function updateSetting(name, value, sync) {
|
2019-03-03 16:28:57 +00:00
|
|
|
settings[name] = value;
|
|
|
|
storage.set("settings", JSON.stringify(settings));
|
|
|
|
applySetting(name, value);
|
2017-12-11 19:01:15 +00:00
|
|
|
|
2019-03-03 16:28:57 +00:00
|
|
|
// Sync is checked, request settings from server.
|
|
|
|
if (name === "syncSettings" && value) {
|
|
|
|
socket.emit("setting:get");
|
|
|
|
}
|
2017-04-22 13:03:00 +00:00
|
|
|
|
2019-03-03 16:28:57 +00:00
|
|
|
if (settings.syncSettings && !noSync.includes(name) && sync) {
|
|
|
|
settingSetEmit(name, value);
|
|
|
|
} else if (alwaysSync.includes(name) && sync) {
|
|
|
|
settingSetEmit(name, value);
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-03-30 07:32:20 +00:00
|
|
|
function syncAllSettings(force = false) {
|
|
|
|
// Sync all settings if sync is enabled or force is true.
|
|
|
|
if (settings.syncSettings || force) {
|
2017-12-11 19:01:15 +00:00
|
|
|
for (const name in settings) {
|
|
|
|
if (!noSync.includes(name)) {
|
|
|
|
settingSetEmit(name, settings[name]);
|
|
|
|
} else if (alwaysSync.includes(name)) {
|
|
|
|
settingSetEmit(name, settings[name]);
|
2017-11-07 20:22:16 +00:00
|
|
|
}
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If `save` is set to true it will pass the setting to `updateSetting()` processSetting
|
|
|
|
function processSetting(name, value, save) {
|
|
|
|
// No need to also call processSetting when `save` is true.
|
|
|
|
// updateSetting does take care of that.
|
|
|
|
if (save) {
|
|
|
|
// Sync is false as applySetting is never called as the result of a user changing the setting.
|
|
|
|
updateSetting(name, value, false);
|
|
|
|
} else {
|
|
|
|
applySetting(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initialize() {
|
|
|
|
module.exports.initialized = true;
|
|
|
|
|
|
|
|
// Settings have now entirely updated, apply settings to the client.
|
|
|
|
for (const name in settings) {
|
|
|
|
processSetting(name, settings[name], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If browser does not support notifications
|
|
|
|
// display proper message in settings.
|
2019-02-20 09:10:18 +00:00
|
|
|
if (("Notification" in window)) {
|
|
|
|
updateDesktopNotificationStatus();
|
2017-12-11 19:01:15 +00:00
|
|
|
} else {
|
2019-02-20 09:10:18 +00:00
|
|
|
vueApp.desktopNotificationState = "unsupported";
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Local init is done, let's sync
|
|
|
|
// We always ask for synced settings even if it is disabled.
|
|
|
|
// Settings can be mandatory to sync and it is used to determine sync base state.
|
2018-03-30 07:49:02 +00:00
|
|
|
socket.emit("setting:get");
|
2017-12-11 19:01:15 +00:00
|
|
|
}
|