Skip to content

Commit

Permalink
Merge pull request #257 from AtlasOfLivingAustralia/feature/issue256
Browse files Browse the repository at this point in the history
Feature/issue256
  • Loading branch information
salomon-j authored Jul 12, 2024
2 parents 34ffb7c + 7490d64 commit 5045e20
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
15 changes: 15 additions & 0 deletions grails-app/assets/javascripts/forms-knockout-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,21 @@
ecodata.forms.OutputListSupport.apply(target, [options.metadata, options.constructorFunction, options.context, options.userAddedRows, options.config]);
};

/**
* The role of this extender is to provide a function the view model can use to render a list of
* values selected using a multi select component (select2Many / selectMany) that have also used a
* label/value configuration for the options.
* @param target the observable.
* @param options unused
*/
ko.extenders.toReadOnlyString = function(target, options) {
target.toReadOnlyString = function() {
var values = ko.utils.unwrapObservable(target);
var labels = target.constraints && _.isFunction(target.constraints.label) ? _.map(values, target.constraints.label) : values;
return labels.join(', ');
}
}

/**
* This is kind of a hack to make the closure config object available to the any components that use the model.
*/
Expand Down
2 changes: 1 addition & 1 deletion grails-app/assets/javascripts/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ function orEmptyArray(v) {
if (match) {
return self.constraints.text(match);
}
return '';
return value || '';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,8 @@ class ModelJSTagLib {
}

def stringListViewModel(JSModelRenderContext ctx) {
observableArray(ctx)
String extender = '{toReadOnlyString:true}'
observableArray(ctx, [extender])
}

def setViewModel(JSModelRenderContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,23 @@ class ViewModelWidgetRenderer implements ModelWidgetRenderer {
context.writer << "<span ${context.attributes.toString()} data-bind='${context.databindAttrs.toString()}'></span>"
}

/**
* The binding looks for the toReadOnlyString function because there exist some misconfigurations that
* use a "text" data model item with a selectMany/select2Many view. This is a workaround to prevent exceptions.
*/
private static String selectManyBindingString(WidgetRenderContext context) {
'_.isFunction(('+context.source+' || []).toReadOnlyString) ? '+ context.source+'.toReadOnlyString() : ('+context.source+'() || []).join(", ")'
}

@Override
void renderSelectMany(WidgetRenderContext context) {
context.databindAttrs.add 'text', '('+context.source+'() || []).join(", ")'
context.databindAttrs.add 'text',selectManyBindingString(context)
context.writer << "<span ${context.attributes.toString()} data-bind='${context.databindAttrs.toString()}'></span>"
}

@Override
void renderSelect2Many(WidgetRenderContext context) {
context.databindAttrs.add 'text', selectManyBindingString(context)
context.writer << "<span ${context.attributes.toString()} data-bind='${context.databindAttrs.toString()}'></span>"
}

Expand Down Expand Up @@ -195,12 +209,6 @@ class ViewModelWidgetRenderer implements ModelWidgetRenderer {
context.writer << """\$<span data-bind='${context.databindAttrs.toString()}'></span>.00"""
}

@Override
void renderSelect2Many(WidgetRenderContext context) {
context.databindAttrs.add 'text', '('+context.source+'() || []).join(", ")'
context.writer << "<span ${context.attributes.toString()} data-bind='${context.databindAttrs.toString()}'></span>"
}

@Override
void renderMultiInput(WidgetRenderContext context) {
context.databindAttrs.add 'text', '('+context.source+'() || []).join(", ")'
Expand Down
5 changes: 2 additions & 3 deletions src/test/js/spec/DataModelItemSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("DataModelItem Spec", function () {
dataItem('2');
expect(dataItem.constraints.label()).toEqual('2')
expect(dataItem.constraints.label('3')).toEqual('3')
expect(dataItem.constraints.label("does not exist")).toEqual('');
expect(dataItem.constraints.label("does not exist")).toEqual('does not exist'); // Support for tags and historical constraints that have been removed.

var objectConstraints = [{text:'label 1', value:'1'}, {text:'label 2', value:'2'}, {text:'label 3', value:'3'}];
metadata.constraints = {
Expand All @@ -104,7 +104,7 @@ describe("DataModelItem Spec", function () {
dataItem('2');
expect(dataItem.constraints.label()).toEqual('label 2')
expect(dataItem.constraints.label('3')).toEqual('label 3')
expect(dataItem.constraints.label("does not exist")).toEqual('');
expect(dataItem.constraints.label("does not exist")).toEqual('does not exist'); // Support for tags and historical constraints that have been removed.

metadata.constraints = {
type:"pre-populated",
Expand All @@ -128,7 +128,6 @@ describe("DataModelItem Spec", function () {
deferred.resolve(objectConstraints).then(function() {
expect(dataItem.constraints.label()).toEqual('label 2')
expect(dataItem.constraints.label('3')).toEqual('label 3')
expect(dataItem.constraints.label("does not exist")).toEqual('');

done();
});
Expand Down

0 comments on commit 5045e20

Please sign in to comment.