Skip to content

Commit a91bc29

Browse files
authored
Merge pull request #346 from AtlasOfLivingAustralia/feature/biocollect1895
commit updates #1895
2 parents 434fdb9 + 8ad7588 commit a91bc29

File tree

4 files changed

+61
-39
lines changed

4 files changed

+61
-39
lines changed

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

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,58 +81,61 @@
8181
ko.extenders.simpleDate = function (target, options) {
8282
var includeTime = false;
8383
var isReadOnly = false;
84+
var showInUserTimeZone = false;
85+
8486
if (_.isObject(options)) {
8587
includeTime = options.includeTime || false;
8688
isReadOnly = options.readOnly || false;
89+
showInUserTimeZone = options.showInUserTimeZone || false;
8790
}
8891
else {
8992
includeTime = options || false;
9093
}
9194

92-
target.date = ko.computed({
93-
read: function () {
94-
return Date.fromISO(target());
95-
},
95+
target.date = ko.computed({
96+
read: function () {
97+
return Date.fromISO(target());
98+
},
9699

97-
write: function (newValue) {
100+
write: function (newValue) {
98101
if (isReadOnly) {
99102
return;
100103
}
101-
if (newValue) {
102-
var current = target(),
103-
valueToWrite = convertToIsoDate(newValue);
104-
105-
if (valueToWrite !== current) {
106-
target(valueToWrite);
107-
}
108-
} else {
109-
// date has been cleared
110-
target("");
104+
if (newValue) {
105+
var current = target(),
106+
valueToWrite = convertToIsoDate(newValue);
107+
108+
if (valueToWrite !== current) {
109+
target(valueToWrite);
111110
}
111+
} else {
112+
target("");
112113
}
113-
});
114-
target.formattedDate = ko.computed({
115-
read: function () {
116-
return convertToSimpleDate(target(), includeTime);
117-
},
114+
}
115+
});
116+
target.formattedDate = ko.computed({
117+
read: function () {
118+
return convertToSimpleDate(target(), includeTime, showInUserTimeZone);
119+
},
118120

119-
write: function (newValue) {
121+
write: function (newValue) {
120122
if (isReadOnly) {
121123
return;
122124
}
123-
if (newValue) {
124-
var current = target(),
125-
valueToWrite = convertToIsoDate(newValue);
125+
if (newValue) {
126+
var current = target(),
127+
valueToWrite = convertToIsoDate(newValue);
126128

127-
if (valueToWrite !== current) {
128-
target(valueToWrite);
129-
}
129+
if (valueToWrite !== current) {
130+
target(valueToWrite);
130131
}
131132
}
132-
});
133-
target.date(target());
134-
target.formattedDate(target());
133+
}
134+
});
135135

136-
return target;
137-
};
136+
target.date(target());
137+
target.formattedDate(target());
138+
139+
return target;
140+
};
138141
}());

grails-app/assets/javascripts/utils.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,25 @@ function isValidDate(d) {
8181
return !isNaN(d.getTime());
8282
}
8383

84-
function convertToSimpleDate(isoDate, includeTime) {
84+
function convertToSimpleDate(isoDate, includeTime, showInUserTimeZone) {
8585
if (!isoDate) { return ''}
8686
if (typeof isoDate === 'object') {
8787
// assume a date object
8888
if (!isValidDate(isoDate)) {
8989
return '';
9090
}
9191
}
92-
// Format the stage labels using Melbourne/Sydney/Canberra time to avoid problems where the date starts
93-
// at midnight and displays as the previous day in other timezones.
94-
var date = moment.tz(isoDate, "Australia/Sydney");
92+
93+
var date;
94+
if (showInUserTimeZone === true) {
95+
// default to user's local timezone
96+
date = moment(isoDate);
97+
}
98+
else {
99+
// use existing behaviour
100+
date = moment.tz(isoDate, "Australia/Sydney");
101+
}
102+
95103
var format = includeTime ? "DD-MM-YYYY HH:mm" : "DD-MM-YYYY";
96104
return date.format(format);
97105
}

grails-app/taglib/au/org/ala/ecodata/forms/ModelJSTagLib.groovy

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,20 @@ class ModelJSTagLib {
626626
}
627627

628628
def dateViewModel(JSModelRenderContext ctx) {
629-
List extenders = ["{simpleDate: {includeTime:false}}"]
629+
def options = ["includeTime:false"]
630+
631+
if (ctx.dataModel.computed) {
632+
options << "readOnly:true"
633+
}
634+
635+
if (ctx.dataModel.showInUserTimeZone) {
636+
options << "showInUserTimeZone:true"
637+
}
638+
639+
def extender = "{simpleDate: {${options.join(', ')}}}"
640+
List extenders = [extender]
641+
630642
if (ctx.dataModel.computed) {
631-
extenders = ["{simpleDate: {includeTime:false, readOnly:true}}"]
632643
computedModel(ctx, extenders)
633644
}
634645
else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@atlasoflivingaustralia/ecodata-client-plugin",
3-
"version": "8.5.0-snapshot",
3+
"version": "8.5.1-snapshot",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/AtlasOfLivingAustralia/ecodata-client-plugin.git"

0 commit comments

Comments
 (0)