From 1c96787647240afed3c24698dd2d04db011cd796 Mon Sep 17 00:00:00 2001 From: Dmitry Sharabin Date: Tue, 26 Sep 2023 18:17:29 +0200 Subject: [PATCH 1/3] Make mv-options work with expressions, fixes #777 --- src/primitive.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/primitive.js b/src/primitive.js index 93a39f0d..f9c8c1d8 100644 --- a/src/primitive.js +++ b/src/primitive.js @@ -1316,6 +1316,9 @@ Mavo.observe({id: "primitive"}, function({node, type, attribute, record, element else if (attribute === "mv-options") { node.updateOptions(); + let option = node.options.get(node.value) ?? node.options.keys().next().value; + node.setValue(option, {silent: true}); + if (node.editor) { node.generateDefaultEditor(); } From 70257ee0b6325a5105b8f106ac5d968e9435a23e Mon Sep 17 00:00:00 2001 From: Dmitry Sharabin Date: Fri, 29 Sep 2023 16:59:38 +0200 Subject: [PATCH 2/3] Another try to make expressions work correctly with mv-options --- src/primitive.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/primitive.js b/src/primitive.js index f9c8c1d8..46f029a5 100644 --- a/src/primitive.js +++ b/src/primitive.js @@ -768,6 +768,16 @@ var _ = Mavo.Primitive = class Primitive extends Mavo.Node { let presentational; if (this.options) { + if (!this.options.has(value)) { + // There is no option corresponding to the value. Try the default value. + value = this.default; + + if (!this.options.has(value)) { + // There is no option corresponding to the default value either. Use the first option + value = this.options.keys().next().value; + } + } + presentational = this.options.get(value); } @@ -1316,8 +1326,7 @@ Mavo.observe({id: "primitive"}, function({node, type, attribute, record, element else if (attribute === "mv-options") { node.updateOptions(); - let option = node.options.get(node.value) ?? node.options.keys().next().value; - node.setValue(option, {silent: true}); + node.setValue(node.value, {force: true, silent: true}); if (node.editor) { node.generateDefaultEditor(); From 6f9838e5e9ccd742a11b4a385a6e0d6ec21a51b2 Mon Sep 17 00:00:00 2001 From: Dmitry Sharabin Date: Fri, 29 Sep 2023 17:22:31 +0200 Subject: [PATCH 3/3] A bit shorter code --- src/primitive.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/primitive.js b/src/primitive.js index 46f029a5..780d4497 100644 --- a/src/primitive.js +++ b/src/primitive.js @@ -769,13 +769,9 @@ var _ = Mavo.Primitive = class Primitive extends Mavo.Node { if (this.options) { if (!this.options.has(value)) { - // There is no option corresponding to the value. Try the default value. - value = this.default; - - if (!this.options.has(value)) { - // There is no option corresponding to the default value either. Use the first option - value = this.options.keys().next().value; - } + // There is no option corresponding to the value. Try the default one. + // If there is no option corresponding to the default value either, use the first option + value = this.options.has(this.default)? this.default : this.options.keys().next().value; } presentational = this.options.get(value);