From 1c6551a6e06d1bc345ed703350fd6dba8d82eef2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 29 May 2024 16:05:48 +0200 Subject: [PATCH] MOBILE-4470 database: Don't display values that are no longer valid --- .../addon-mod-data-field-checkbox.html | 2 +- .../fields/checkbox/component/checkbox.ts | 32 ++++++++++--------- .../addon-mod-data-field-multimenu.html | 2 +- .../fields/multimenu/component/multimenu.ts | 32 ++++++++++--------- 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/addons/mod/data/fields/checkbox/component/addon-mod-data-field-checkbox.html b/src/addons/mod/data/fields/checkbox/component/addon-mod-data-field-checkbox.html index 76533f6a0..6b0110b6f 100644 --- a/src/addons/mod/data/fields/checkbox/component/addon-mod-data-field-checkbox.html +++ b/src/addons/mod/data/fields/checkbox/component/addon-mod-data-field-checkbox.html @@ -17,4 +17,4 @@ - + diff --git a/src/addons/mod/data/fields/checkbox/component/checkbox.ts b/src/addons/mod/data/fields/checkbox/component/checkbox.ts index 3a1268c10..7b9ef0a64 100644 --- a/src/addons/mod/data/fields/checkbox/component/checkbox.ts +++ b/src/addons/mod/data/fields/checkbox/component/checkbox.ts @@ -25,6 +25,7 @@ import { AddonModDataFieldPluginBaseComponent } from '../../../classes/base-fiel }) export class AddonModDataFieldCheckboxComponent extends AddonModDataFieldPluginBaseComponent { + content?: string; options: { key: string; value: string; @@ -40,31 +41,32 @@ export class AddonModDataFieldCheckboxComponent extends AddonModDataFieldPluginB return; } - this.options = this.field.param1.split(/\r?\n/).map((option) => ({ key: option, value: option })); - - const values: string[] = []; - if (this.editMode && this.value && this.value.content) { - this.value.content.split('##').forEach((value) => { - const x = this.options.findIndex((option) => value == option.key); - if (x >= 0) { - values.push(value); - } - }); - } - if (this.searchMode) { this.addControl('f_' + this.field.id + '_allreq'); } - this.addControl('f_' + this.field.id, values); + this.addControl('f_' + this.field.id, this.getValidValues(this.value)); } /** * @inheritdoc */ protected updateValue(value?: Partial): void { - this.value = value || {}; - this.value.content = value?.content?.split('##').join('
'); + super.updateValue(value); + + this.content = this.getValidValues(value).join('
'); + } + + /** + * Get the list of valid values from current field value. + * + * @param value Field value. + * @returns List of valid values. + */ + protected getValidValues(value?: Partial): string[] { + this.options = this.field.param1.split(/\r?\n/).map((option) => ({ key: option, value: option })); + + return value?.content?.split('##').filter((value) => this.options.some(option => value === option.key)) ?? []; } } diff --git a/src/addons/mod/data/fields/multimenu/component/addon-mod-data-field-multimenu.html b/src/addons/mod/data/fields/multimenu/component/addon-mod-data-field-multimenu.html index d7b459a7c..b5c82796c 100644 --- a/src/addons/mod/data/fields/multimenu/component/addon-mod-data-field-multimenu.html +++ b/src/addons/mod/data/fields/multimenu/component/addon-mod-data-field-multimenu.html @@ -18,4 +18,4 @@ - + diff --git a/src/addons/mod/data/fields/multimenu/component/multimenu.ts b/src/addons/mod/data/fields/multimenu/component/multimenu.ts index a331fd8bf..46cb8696f 100644 --- a/src/addons/mod/data/fields/multimenu/component/multimenu.ts +++ b/src/addons/mod/data/fields/multimenu/component/multimenu.ts @@ -25,6 +25,7 @@ import { AddonModDataFieldPluginBaseComponent } from '../../../classes/base-fiel }) export class AddonModDataFieldMultimenuComponent extends AddonModDataFieldPluginBaseComponent { + content?: string; options: { key: string; value: string; @@ -40,31 +41,32 @@ export class AddonModDataFieldMultimenuComponent extends AddonModDataFieldPlugin return; } - this.options = this.field.param1.split(/\r?\n/).map((option) => ({ key: option, value: option })); - - const values: string[] = []; - if (this.editMode && this.value?.content) { - this.value.content.split('##').forEach((value) => { - const x = this.options.findIndex((option) => value == option.key); - if (x >= 0) { - values.push(value); - } - }); - } - if (this.searchMode) { this.addControl('f_' + this.field.id + '_allreq'); } - this.addControl('f_' + this.field.id, values); + this.addControl('f_' + this.field.id, this.getValidValues(this.value)); } /** * @inheritdoc */ protected updateValue(value?: Partial): void { - this.value = value || {}; - this.value.content = value?.content && value.content.split('##').join('
'); + super.updateValue(value); + + this.content = this.getValidValues(value).join('
'); + } + + /** + * Get the list of valid values from current field value. + * + * @param value Field value. + * @returns List of valid values. + */ + protected getValidValues(value?: Partial): string[] { + this.options = this.field.param1.split(/\r?\n/).map((option) => ({ key: option, value: option })); + + return value?.content?.split('##').filter((value) => this.options.some(option => value === option.key)) ?? []; } }