Skip to content

Commit 623d5e6

Browse files
authored
Merge pull request #226 from AtlasOfLivingAustralia/feature/issue225
Implemented readonly support for date and selectOne types #225
2 parents 1f4a668 + ebd3b26 commit 623d5e6

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

grails-app/assets/javascripts/forms-knockout-bindings.js

+18
Original file line numberDiff line numberDiff line change
@@ -1252,5 +1252,23 @@
12521252
}
12531253
};
12541254

1255+
ko.bindingHandlers['disableClick'] = {
1256+
'update': function (element, valueAccessor) {
1257+
var value = ko.utils.unwrapObservable(valueAccessor());
1258+
if (value) {
1259+
this.eventHandler = $(element).on('mousedown.disableClick keydown.disableClick touchstart.disableClick', function(e) {
1260+
e.preventDefault();
1261+
return false;
1262+
});
1263+
}
1264+
else {
1265+
if (this.eventHandler) {
1266+
$(element).off('mousedown.disableClick keydown.disableClick touchstart.disableClick');
1267+
}
1268+
}
1269+
1270+
}
1271+
};
1272+
12551273
})();
12561274

grails-app/assets/javascripts/knockout-dates.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@
1818
$element.data('date', initialDateStr);
1919
}
2020

21-
var defaults = {format: 'dd-mm-yyyy', autoclose: true};
21+
var defaults = {format: 'dd-mm-yyyy', autoclose: true, enableOnReadonly: false};
2222
var options = _.defaults(allBindingsAccessor().datepickerOptions || {}, defaults);
2323

24+
$element.click(function() {
25+
if ($element.prop('disabled') && $element.prop('readonly')) {
26+
e.preventDefault();
27+
}
28+
});
29+
2430
//initialize datepicker with some optional options
2531
$element.datepicker(options);
2632

2733
// if the parent container holds any element with the class 'open-datepicker'
2834
// then add a hook to do so
2935
$element.parent().find('.open-datepicker').click(function () {
30-
if (!$element.prop('disabled')) {
36+
if (!$element.prop('disabled') && !$element.prop('readonly')) {
3137
$element.datepicker('show');
3238
}
3339
});

src/main/groovy/au/org/ala/ecodata/forms/EditModelWidgetRenderer.groovy

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ public class EditModelWidgetRenderer implements ModelWidgetRenderer {
116116

117117
context.databindAttrs.add 'optionsCaption', '"Please select"'
118118
context.attributes.addSpan("form-control form-control-sm")
119+
if (isReadOnly(context)) { // HTML Select elements don't support the readonly attribute so we add disabled. This will break validation though.
120+
context.databindAttrs.add('disableClick', 'true')
121+
}
119122

120123
context.writer << "<select${context.attributes.toString()} class=\"select form-control form-control-sm\" data-bind='${context.databindAttrs.toString()}'${context.validationAttr}></select>"
121124
}

0 commit comments

Comments
 (0)