From f21c5b74d0efbc1145209d2589227b4c40af4cf6 Mon Sep 17 00:00:00 2001 From: kovacsv Date: Wed, 12 Jan 2022 17:06:02 +0100 Subject: [PATCH] CookieHandler shouldn't be a class. --- source/website/cookiehandler.js | 69 ++++++++++++++++++++++++++++++ source/website/cookies.js | 76 --------------------------------- source/website/settings.js | 34 +++++++-------- source/website/website.js | 28 ++++++------ 4 files changed, 99 insertions(+), 108 deletions(-) create mode 100644 source/website/cookiehandler.js delete mode 100644 source/website/cookies.js diff --git a/source/website/cookiehandler.js b/source/website/cookiehandler.js new file mode 100644 index 0000000..3c04f32 --- /dev/null +++ b/source/website/cookiehandler.js @@ -0,0 +1,69 @@ +import { ParameterConverter } from '../engine/parameters/parameterlist.js'; + +export function CookieSetStringVal (key, value) +{ + let date = new Date (); + let expirationDays = 365; + date.setTime (date.getTime () + (expirationDays * 24 * 60 * 60 * 1000)); + document.cookie = key + '=' + value + '; expires=' + date.toUTCString () + ';'; +} + +export function CookieGetStringVal (key, defVal) +{ + let cookie = decodeURIComponent (document.cookie); + let cookieParts = cookie.split (';'); + for (let i = 0; i < cookieParts.length; i++) { + let currentCookie = cookieParts[i].trim (); + if (currentCookie.startsWith (key + '=')) { + return currentCookie.substr (key.length + 1); + } + } + return defVal; +} + +export function CookieGetBoolVal (key, defVal) +{ + let stringVal = CookieGetStringVal (key, null); + if (stringVal === null) { + return defVal; + } + return stringVal === 'true' ? true : false; +} + +export function CookieSetBoolVal (key, value) +{ + CookieSetStringVal (key, value ? 'true' : 'false'); +} + +export function CookieGetIntVal (key, defVal) +{ + let stringVal = CookieGetStringVal (key, null); + if (stringVal === null) { + return defVal; + } + return parseInt (stringVal, 10); +} + +export function CookieSetIntVal (key, value) +{ + CookieSetStringVal (key, value.toString ()); +} + +export function CookieGetColorVal (key, defVal) +{ + let stringVal = CookieGetStringVal (key, null); + if (stringVal === null) { + return defVal; + } + return ParameterConverter.StringToColor (stringVal); +} + +export function CookieSetColorVal (key, value) +{ + CookieSetStringVal (key, ParameterConverter.ColorToString (value)); +} + +export function CookieClearVal (key) +{ + CookieSetStringVal (key, ''); +} diff --git a/source/website/cookies.js b/source/website/cookies.js deleted file mode 100644 index 6fae11e..0000000 --- a/source/website/cookies.js +++ /dev/null @@ -1,76 +0,0 @@ -import { ParameterConverter } from '../engine/parameters/parameterlist.js'; - -export class CookieHandler -{ - constructor () - { - this.expirationDays = 365; - } - - ClearVal (key) - { - this.SetStringVal (key, ''); - } - - SetStringVal (key, value) - { - let date = new Date (); - date.setTime (date.getTime () + (this.expirationDays * 24 * 60 * 60 * 1000)); - document.cookie = key + '=' + value + '; expires=' + date.toUTCString () + ';'; - } - - GetStringVal (key, defVal) - { - let cookie = decodeURIComponent (document.cookie); - let cookieParts = cookie.split (';'); - for (let i = 0; i < cookieParts.length; i++) { - let currentCookie = cookieParts[i].trim (); - if (currentCookie.startsWith (key + '=')) { - return currentCookie.substr (key.length + 1); - } - } - return defVal; - } - - GetBoolVal (key, defVal) - { - let stringVal = this.GetStringVal (key, null); - if (stringVal === null) { - return defVal; - } - return stringVal === 'true' ? true : false; - } - - SetBoolVal (key, value) - { - this.SetStringVal (key, value ? 'true' : 'false'); - } - - GetIntVal (key, defVal) - { - let stringVal = this.GetStringVal (key, null); - if (stringVal === null) { - return defVal; - } - return parseInt (stringVal, 10); - } - - SetIntVal (key, value) - { - this.SetStringVal (key, value.toString ()); - } - - GetColorVal (key, defVal) - { - let stringVal = this.GetStringVal (key, null); - if (stringVal === null) { - return defVal; - } - return ParameterConverter.StringToColor (stringVal); - } - - SetColorVal (key, value) - { - this.SetStringVal (key, ParameterConverter.ColorToString (value)); - } -} diff --git a/source/website/settings.js b/source/website/settings.js index dc6aa98..21bc868 100644 --- a/source/website/settings.js +++ b/source/website/settings.js @@ -1,4 +1,5 @@ import { Color } from '../engine/model/color.js'; +import { CookieGetBoolVal, CookieGetColorVal, CookieGetIntVal, CookieSetBoolVal, CookieSetColorVal, CookieSetIntVal } from './cookiehandler.js'; export const Theme = { @@ -19,26 +20,25 @@ export class Settings this.themeId = Theme.Light; } - LoadFromCookies (cookieHandler) + LoadFromCookies () { - this.backgroundColor = cookieHandler.GetColorVal ('ov_background_color', new Color (255, 255, 255)); - this.defaultColor = cookieHandler.GetColorVal ('ov_default_color', new Color (200, 200, 200)); - this.showGrid = cookieHandler.GetBoolVal ('ov_show_grid', false); - this.showEdges = cookieHandler.GetBoolVal ('ov_show_edges', false); - this.edgeColor = cookieHandler.GetColorVal ('ov_edge_color', new Color (0, 0, 0)); - this.edgeThreshold = cookieHandler.GetIntVal ('ov_edge_threshold', 1); - this.showEdges = cookieHandler.GetBoolVal ('ov_show_edges', false); - this.themeId = cookieHandler.GetIntVal ('ov_theme_id', Theme.Light); + this.backgroundColor = CookieGetColorVal ('ov_background_color', new Color (255, 255, 255)); + this.defaultColor = CookieGetColorVal ('ov_default_color', new Color (200, 200, 200)); + this.showGrid = CookieGetBoolVal ('ov_show_grid', false); + this.showEdges = CookieGetBoolVal ('ov_show_edges', false); + this.edgeColor = CookieGetColorVal ('ov_edge_color', new Color (0, 0, 0)); + this.edgeThreshold = CookieGetIntVal ('ov_edge_threshold', 1); + this.themeId = CookieGetIntVal ('ov_theme_id', Theme.Light); } - SaveToCookies (cookieHandler) + SaveToCookies () { - cookieHandler.SetColorVal ('ov_background_color', this.backgroundColor); - cookieHandler.SetColorVal ('ov_default_color', this.defaultColor); - cookieHandler.SetBoolVal ('ov_show_grid', this.showGrid); - cookieHandler.SetBoolVal ('ov_show_edges', this.showEdges); - cookieHandler.SetColorVal ('ov_edge_color', this.edgeColor); - cookieHandler.SetIntVal ('ov_edge_threshold', this.edgeThreshold); - cookieHandler.SetIntVal ('ov_theme_id', this.themeId); + CookieSetColorVal ('ov_background_color', this.backgroundColor); + CookieSetColorVal ('ov_default_color', this.defaultColor); + CookieSetBoolVal ('ov_show_grid', this.showGrid); + CookieSetBoolVal ('ov_show_edges', this.showEdges); + CookieSetColorVal ('ov_edge_color', this.edgeColor); + CookieSetIntVal ('ov_edge_threshold', this.edgeThreshold); + CookieSetIntVal ('ov_theme_id', this.themeId); } } diff --git a/source/website/website.js b/source/website/website.js index 43b47d9..1e089b6 100644 --- a/source/website/website.js +++ b/source/website/website.js @@ -3,7 +3,6 @@ import { ImportErrorCode, ImportSettings } from '../engine/import/importer.js'; import { Viewer } from '../engine/viewer/viewer.js'; import { MeasureTool } from '../engine/viewer/measuretool.js'; import { AddDiv, AddDomElement, ShowDomElement, SetDomElementOuterHeight } from '../engine/viewer/domutils.js'; -import { CookieHandler } from './cookies.js'; import { CalculatePopupPositionToScreen, ShowListPopup } from './dialogs.js'; import { EventHandler } from './eventhandler.js'; import { HashHandler } from './hashhandler.js'; @@ -19,6 +18,7 @@ import { ShowOpenUrlDialog } from './openurldialog.js'; import { ShowSharingDialog } from './sharingdialog.js'; import { HasDefaultMaterial, ReplaceDefaultMaterialColor } from '../engine/model/modelutils.js'; import { Direction } from '../engine/geometry/geometry.js'; +import { CookieGetBoolVal, CookieSetBoolVal } from './cookiehandler.js'; export const WebsiteUIState = { @@ -37,7 +37,6 @@ export class Website this.viewer = new Viewer (); this.measureTool = new MeasureTool (); this.hashHandler = new HashHandler (); - this.cookieHandler = new CookieHandler (); this.toolbar = new Toolbar (this.parameters.toolbarDiv); this.navigator = new Navigator (this.parameters.navigatorDiv, this.parameters.navigatorSplitterDiv); this.sidebar = new Sidebar (this.parameters.sidebarDiv, this.parameters.sidebarSplitterDiv, this.settings, this.measureTool); @@ -52,7 +51,7 @@ export class Website Load () { - this.settings.LoadFromCookies (this.cookieHandler); + this.settings.LoadFromCookies (); this.SwitchTheme (this.settings.themeId, false); this.eventHandler.HandleEvent ('theme_on_load', this.settings.themeId === Theme.Light ? 'light' : 'dark'); @@ -426,13 +425,13 @@ export class Website UpdateGridDisplay () { - this.settings.SaveToCookies (this.cookieHandler); + this.settings.SaveToCookies (); this.viewer.SetGridSettings (this.settings.showGrid); } UpdateEdgeDisplay () { - this.settings.SaveToCookies (this.cookieHandler); + this.settings.SaveToCookies (); this.viewer.SetEdgeSettings (this.settings.showEdges, this.settings.edgeColor, this.settings.edgeThreshold); } @@ -440,9 +439,8 @@ export class Website { this.settings.themeId = newThemeId; this.themeHandler.SwitchTheme (this.settings.themeId); - this.settings.SaveToCookies (this.cookieHandler); + this.settings.SaveToCookies (); if (resetColors) { - this.settings.SaveToCookies (this.cookieHandler); this.viewer.SetBackgroundColor (this.settings.backgroundColor); let modelLoader = this.modelLoaderUI.GetModelLoader (); if (modelLoader.GetDefaultMaterial () !== null) { @@ -603,11 +601,11 @@ export class Website { this.sidebar.Init ({ onBackgroundColorChange : () => { - this.settings.SaveToCookies (this.cookieHandler); + this.settings.SaveToCookies (); this.viewer.SetBackgroundColor (this.settings.backgroundColor); }, onDefaultColorChange : () => { - this.settings.SaveToCookies (this.cookieHandler); + this.settings.SaveToCookies (); let modelLoader = this.modelLoaderUI.GetModelLoader (); if (modelLoader.GetDefaultMaterial () !== null) { ReplaceDefaultMaterialColor (this.model, this.settings.defaultColor); @@ -639,7 +637,7 @@ export class Website this.Resize (); }, onShowHidePanels : (show) => { - this.cookieHandler.SetBoolVal ('ov_show_sidebar', show); + CookieSetBoolVal ('ov_show_sidebar', show); } }); } @@ -738,22 +736,22 @@ export class Website this.Resize (); }, onShowHidePanels : (show) => { - this.cookieHandler.SetBoolVal ('ov_show_navigator', show); + CookieSetBoolVal ('ov_show_navigator', show); } }); } UpdatePanelsVisibility () { - let showNavigator = this.cookieHandler.GetBoolVal ('ov_show_navigator', true); - let showSidebar = this.cookieHandler.GetBoolVal ('ov_show_sidebar', true); + let showNavigator = CookieGetBoolVal ('ov_show_navigator', true); + let showSidebar = CookieGetBoolVal ('ov_show_sidebar', true); this.navigator.ShowPanels (showNavigator); this.sidebar.ShowPanels (showSidebar); } InitCookieConsent () { - let accepted = this.cookieHandler.GetBoolVal ('ov_cookie_consent', false); + let accepted = CookieGetBoolVal ('ov_cookie_consent', false); if (accepted) { return; } @@ -763,7 +761,7 @@ export class Website AddDiv (popupDiv, 'ov_floating_panel_text', text); let acceptButton = AddDiv (popupDiv, 'ov_button ov_floating_panel_button', 'Accept'); acceptButton.addEventListener ('click', () => { - this.cookieHandler.SetBoolVal ('ov_cookie_consent', true); + CookieSetBoolVal ('ov_cookie_consent', true); popupDiv.remove (); }); }