From ebc77e8bf4ac9d8ec99f8a7aac71a01b25d22ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20H=C3=A1la?= Date: Fri, 15 Sep 2023 20:42:40 +0200 Subject: [PATCH 1/2] Create Options js class to simplify setting cookies-based options --- backend/static/js/Options.js | 63 ++++++++++++++++++++++++++++++ backend/templates/songs/index.html | 44 ++++++++++++++------- 2 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 backend/static/js/Options.js diff --git a/backend/static/js/Options.js b/backend/static/js/Options.js new file mode 100644 index 0000000..2f8baae --- /dev/null +++ b/backend/static/js/Options.js @@ -0,0 +1,63 @@ +import Cookies from "./js.cookie.js"; + +export class BooleanOption { + constructor(checkbox, callable, defaultValue) { + this.checkbox = checkbox; + this.callable = callable; + this.defaultValue = defaultValue || false; + } + + get() { + return this.checkbox.is(':checked') + } + + set(value) { + this.checkbox.prop('checked', value); + } + + call(value) { + this.callable(value) + } + + selector() { + return this.checkbox + } + + default() { + return this.defaultValue + } +} + +export class Options { + constructor(options) { + this.options = options + for (let [name, option] of options) { + option.selector().change(function () { + const value = option.get(); + Cookies.set(name, value, {SameSite: "Strict"}) + option.call(value) + }) + let value = Cookies.get(name) + if (value === undefined) { + value = option.default() + Cookies.set(name, value, {SameSite: "Strict"}) + } + option.set(value) + option.call(value) + } + } + + get(name) { + options[name].get() + } + set(name, value) { + options[name].set(value) + } + + call(name, value) { + options[name].call(value) + } +} +// +// export class Options; +// export class BooleanOption; diff --git a/backend/templates/songs/index.html b/backend/templates/songs/index.html index 75be260..a5821f8 100644 --- a/backend/templates/songs/index.html +++ b/backend/templates/songs/index.html @@ -14,7 +14,7 @@ defer> - + {% endblock %} {% block extra_options %} @@ -81,6 +81,27 @@
<%:author%>