Skip to content

Commit

Permalink
Support readonly/computed for dates #207
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Aug 21, 2023
1 parent 346bf02 commit a183be4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
30 changes: 19 additions & 11 deletions grails-app/taglib/au/org/ala/ecodata/forms/ModelJSTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ class ModelJSTagLib {
*/
void renderDataModelItem(JSModelRenderContext ctx) {
Map mod = ctx.dataModel
if (mod.computed) {
if (mod.dataType == 'date') {
dateViewModel(ctx)
}
else if (mod.dataType == 'time') {
timeViewModel(ctx)
}
else if (mod.computed) {
computedModel(ctx)
}
else if (mod.dataType == 'text') {
Expand All @@ -151,12 +157,6 @@ class ModelJSTagLib {
else if (mod.dataType == 'species') {
speciesModel(ctx)
}
else if (mod.dataType == 'date') {
dateViewModel(ctx)
}
else if (mod.dataType == 'time') {
timeViewModel(ctx)
}
else if (mod.dataType == 'document') {
documentViewModel(ctx)
}
Expand Down Expand Up @@ -624,7 +624,14 @@ class ModelJSTagLib {
}

def dateViewModel(JSModelRenderContext ctx) {
observable(ctx, ["{simpleDate: false}"])
List extenders = ["{simpleDate: {includeTime:false}}"]
if (ctx.dataModel.computed) {
extenders = ["{simpleDate: {includeTime:false, readOnly:true}}"]
computedModel(ctx, extenders)
}
else {
observable(ctx, extenders)
}
}

def booleanViewModel(JSModelRenderContext ctx) {
Expand Down Expand Up @@ -670,7 +677,7 @@ class ModelJSTagLib {
observable(ctx, ["{feature:config}"])
}

def computedModel(JSModelRenderContext ctx) {
def computedModel(JSModelRenderContext ctx, List extenders = []) {

// TODO computed values within tables are rendered differently to values outside tables for historical reasons
// This should be tidied up.
Expand All @@ -681,10 +688,11 @@ class ModelJSTagLib {
computedValueRenderer.computedViewModel(ctx.out, ctx.attrs, ctx.dataModel, ctx.propertyPath, ctx.propertyPath)
}

if (requiresMetadataExtender(ctx.dataModel)) {
ctx.out << INDENT*3 << "${ctx.propertyPath}.${ctx.dataModel.name} = ${ctx.propertyPath}.${ctx.dataModel.name}${extenderJS(ctx, [])};\n"
if (extenders || requiresMetadataExtender(ctx.dataModel)) {
ctx.out << INDENT*3 << "${ctx.propertyPath}.${ctx.dataModel.name} = ${ctx.propertyPath}.${ctx.dataModel.name}${extenderJS(ctx, extenders)};\n"
}


}

def audioModel(JSModelRenderContext ctx) {
Expand Down
2 changes: 1 addition & 1 deletion grails-app/views/output/_dateDataTypeEditModelTemplate.gsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div id="${context.source}Date" class="input-group">
<input class="form-control form-control-sm inputDatePicker" data-bind="${context.databindAttrs}" type="text" size="12" ${context.validationAttr}/>
<input class="form-control form-control-sm inputDatePicker" ${context.attributes} data-bind="${context.databindAttrs}" type="text" size="12" ${context.validationAttr}/>
<div class="input-group-append">
<span class="add-on input-group-text open-datepicker"><i class="fa fa-th"></i></span>
</div>
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ class ComputedValueRenderer {
String expression = computed.expression
int decimalPlaces = getNumberOfDecimalPlaces(model, computed)

out << "return ecodata.forms.expressionEvaluator.evaluate('${expression}', ${dependantContext}, ${decimalPlaces});\n";
String expressionType
switch (model.dataType) {
case 'text':
case 'date':
expressionType = 'evaluateString'
break
default:
expressionType = "evaluate"
}

out << "return ecodata.forms.expressionEvaluator.${expressionType}('${expression}', ${dependantContext}, ${decimalPlaces});\n";
}

private int getNumberOfDecimalPlaces(Map model, Map computed) {
Expand Down

0 comments on commit a183be4

Please sign in to comment.