From b3c0b36ba61bcc728c4c8e7253769c058188f1a9 Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 20 Nov 2022 20:11:27 +0100 Subject: [PATCH] refactor imageoptions to use updateOption() --- .../app/widgets/dialogs/options/images.js | 38 +++++++------------ .../widgets/dialogs/options/options_tab.js | 17 ++++++++- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/public/app/widgets/dialogs/options/images.js b/src/public/app/widgets/dialogs/options/images.js index 5cf5db1dc..4c6691878 100644 --- a/src/public/app/widgets/dialogs/options/images.js +++ b/src/public/app/widgets/dialogs/options/images.js @@ -1,5 +1,3 @@ -import server from "../../../services/server.js"; -import toastService from "../../../services/toast.js"; import OptionsTab from "./options_tab.js"; const TPL = ` @@ -33,7 +31,7 @@ const TPL = ` export default class ImageOptions extends OptionsTab { get tabTitle() { return "Images" } - + lazyRender() { this.$widget = $(TPL); @@ -41,49 +39,39 @@ export default class ImageOptions extends OptionsTab { this.$imageJpegQuality = this.$widget.find("#image-jpeg-quality"); this.$imageMaxWidthHeight.on('change', () => { - const opts = { 'imageMaxWidthHeight': this.$imageMaxWidthHeight.val() }; - server.put('options', opts).then(() => toastService.showMessage("Options change have been saved.")); - - return false; + this.updateOption('imageMaxWidthHeight', this.$imageMaxWidthHeight.val()); }); this.$imageJpegQuality.on('change', () => { - const opts = { 'imageJpegQuality': this.$imageJpegQuality.val() }; - server.put('options', opts).then(() => toastService.showMessage("Options change have been saved.")); - - return false; + this.updateOption('imageJpegQuality', this.$imageJpegQuality.val()); }); this.$downloadImagesAutomatically = this.$widget.find("#download-images-automatically"); this.$downloadImagesAutomatically.on("change", () => { const isChecked = this.$downloadImagesAutomatically.prop("checked"); - const opts = { 'downloadImagesAutomatically': isChecked ? 'true' : 'false' }; - - server.put('options', opts).then(() => toastService.showMessage("Options change have been saved.")); + this.updateOption('downloadImagesAutomatically', isChecked ? 'true' : 'false'); }); this.$enableImageCompression = this.$widget.find("#image-compresion-enabled"); this.$imageCompressionWrapper = this.$widget.find("#image-compression-enabled-wraper"); - this.setImageCompression = (isChecked) => { - if (isChecked) { - this.$imageCompressionWrapper.removeClass("disabled-field"); - } else { - this.$imageCompressionWrapper.addClass("disabled-field"); - } - }; - this.$enableImageCompression.on("change", () => { const isChecked = this.$enableImageCompression.prop("checked"); - const opts = { 'compressImages': isChecked ? 'true' : 'false' }; - - server.put('options', opts).then(() => toastService.showMessage("Options change have been saved.")); + this.updateOption('compressImages', isChecked ? 'true' : 'false'); this.setImageCompression(isChecked); }); } + setImageCompression(isChecked) { + if (isChecked) { + this.$imageCompressionWrapper.removeClass("disabled-field"); + } else { + this.$imageCompressionWrapper.addClass("disabled-field"); + } + } + optionsLoaded(options) { this.$imageMaxWidthHeight.val(options['imageMaxWidthHeight']); this.$imageJpegQuality.val(options['imageJpegQuality']); diff --git a/src/public/app/widgets/dialogs/options/options_tab.js b/src/public/app/widgets/dialogs/options/options_tab.js index fa0104964..914134161 100644 --- a/src/public/app/widgets/dialogs/options/options_tab.js +++ b/src/public/app/widgets/dialogs/options/options_tab.js @@ -1,5 +1,18 @@ import BasicWidget from "../../basic_widget.js"; +import server from "../../../services/server.js"; +import toastService from "../../../services/toast.js"; export default class OptionsTab extends BasicWidget { - -} \ No newline at end of file + async updateOption(name, value) { + const opts = { [name]: value }; + server.put('options', opts).then(() => { + toastService.showPersistent({ + id: "options-change-saved", + title: "Options status", + message: "Options change have been saved.", + icon: "slider", + closeAfter: 2000 + }) + }); + } +}