Skip to content

Commit 523877a

Browse files
committed
Merge remote-tracking branch 'firedrakes/MAGETWO-49314' into BUGS
2 parents 1417646 + 1d84cab commit 523877a

File tree

3 files changed

+148
-15
lines changed
  • app/code/Magento/Ui

3 files changed

+148
-15
lines changed

app/code/Magento/Ui/Component/Form/Element/DataType/Date.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,29 @@ public function __construct(
6060
public function prepare()
6161
{
6262
$config = $this->getData('config');
63+
64+
if (!isset($config['timeOffset'])) {
65+
$config['timeOffset'] = (new \DateTime(
66+
'now',
67+
new \DateTimeZone(
68+
$this->localeDate->getConfigTimezone()
69+
)
70+
))->getOffset();
71+
}
72+
73+
if (!isset($config['timeFormat'])) {
74+
$config['timeFormat'] = $this->localeDate->getTimeFormat(\IntlDateFormatter::SHORT);
75+
}
76+
6377
if (!isset($config['dateFormat'])) {
64-
$config['dateFormat'] = $this->localeDate->getDateTimeFormat(\IntlDateFormatter::MEDIUM);
65-
$this->setData('config', $config);
78+
$config['dateFormat'] = $this->localeDate->getDateFormat(\IntlDateFormatter::MEDIUM);
6679
}
80+
81+
$this->setData('config', $config);
82+
6783
parent::prepare();
6884
}
6985

70-
7186
/**
7287
* Get locale
7388
*
Lines changed: 128 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright © 2016 Magento. All rights reserved.
2+
* Copyright © 2015 Magento. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
55
define([
@@ -11,9 +11,60 @@ define([
1111

1212
return Abstract.extend({
1313
defaults: {
14+
options: {},
15+
16+
timeOffset: 0,
17+
18+
showsTime: false,
19+
20+
dateFormat: 'MM/dd/y', // ICU Date Format
21+
timeFormat: 'HH:mm', // ICU Time Format
22+
23+
/**
24+
* Format of date that comes from the
25+
* server (ICU Date Format).
26+
*
27+
* Used only in date picker mode
28+
* (this.showsTime == false).
29+
*
30+
* @type {String}
31+
*/
32+
inputDateFormat: 'y-MM-dd',
33+
34+
/**
35+
* Format of date that should be sent to the
36+
* server (ICU Date Format).
37+
*
38+
* Used only in date picker mode
39+
* (this.showsTime == false).
40+
*
41+
* @type {String}
42+
*/
43+
outputDateFormat: 'MM/dd/y',
44+
45+
/**
46+
* Date/time format that is used to display date in
47+
* the input field.
48+
*
49+
* @type {String}
50+
*/
51+
datetimeFormat: '',
52+
1453
elementTmpl: 'ui/form/element/date',
15-
dateFormat: 'MM/dd/YYYY',
16-
options: {}
54+
55+
listens: {
56+
'value': 'onValueChange',
57+
'shiftedValue': 'onShiftedValueChange'
58+
},
59+
60+
/**
61+
* Date/time value shifted to corresponding timezone
62+
* according to this.timeOffset property. This value
63+
* will be sent to the server.
64+
*
65+
* @type {String}
66+
*/
67+
shiftedValue: ''
1768
},
1869

1970
/**
@@ -23,24 +74,91 @@ define([
2374
*/
2475
initConfig: function () {
2576
this._super();
26-
this.dateFormat = utils.normalizeDate(this.dateFormat);
77+
78+
utils.extend(this.options, {
79+
showsTime: this.showsTime,
80+
timeFormat: this.timeFormat,
81+
dateFormat: this.dateFormat
82+
});
83+
84+
this.prepareDatetimeFormats();
2785

2886
return this;
2987
},
3088

3189
/**
32-
* Formats provided value according to 'dateFormat' property.
90+
* @inheritdoc
91+
*/
92+
initObservable: function () {
93+
return this._super().observe(['shiftedValue']);
94+
},
95+
96+
/**
97+
* Prepares and sets date/time value that will be displayed
98+
* in the input field.
3399
*
34-
* @returns {String}
100+
* @param {String} value
35101
*/
36-
normalizeData: function () {
37-
var value = this._super();
102+
onValueChange: function (value) {
103+
var dateFormat,
104+
shiftedValue;
38105

39106
if (value) {
40-
value = moment(value).format(this.dateFormat);
107+
if (this.showsTime) {
108+
shiftedValue = moment.utc(value).add(this.timeOffset, 'seconds');
109+
} else {
110+
dateFormat = this.shiftedValue() ? this.outputDateFormat : this.inputDateFormat;
111+
112+
shiftedValue = moment(value, dateFormat);
113+
}
114+
115+
shiftedValue = shiftedValue.format(this.datetimeFormat);
116+
117+
if (shiftedValue !== this.shiftedValue()) {
118+
this.shiftedValue(shiftedValue);
119+
}
120+
}
121+
},
122+
123+
/**
124+
* Prepares and sets date/time value that will be sent
125+
* to the server.
126+
*
127+
* @param {String} shiftedValue
128+
*/
129+
onShiftedValueChange: function (shiftedValue) {
130+
var value;
131+
132+
if (shiftedValue) {
133+
if (this.showsTime) {
134+
value = moment.utc(shiftedValue, this.datetimeFormat);
135+
value = value.subtract(this.timeOffset, 'seconds').toISOString();
136+
} else {
137+
value = moment(shiftedValue, this.datetimeFormat);
138+
value = value.format(this.outputDateFormat);
139+
}
140+
141+
if (value !== this.value()) {
142+
this.value(value);
143+
}
144+
}
145+
},
146+
147+
/**
148+
* Prepares and converts all date/time formats to be compatible
149+
* with moment.js library.
150+
*/
151+
prepareDatetimeFormats: function () {
152+
this.datetimeFormat = this.dateFormat;
153+
154+
if (this.showsTime) {
155+
this.datetimeFormat += ' ' + this.timeFormat;
41156
}
42157

43-
return value;
158+
this.datetimeFormat = utils.normalizeDate(this.datetimeFormat);
159+
160+
this.inputDateFormat = utils.normalizeDate(this.inputDateFormat);
161+
this.outputDateFormat = utils.normalizeDate(this.outputDateFormat);
44162
}
45163
});
46164
});

app/code/Magento/Ui/view/base/web/templates/form/element/date.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
-->
77
<input class="admin__control-text" type="text" data-bind="
88
hasFocus: focused,
9-
datepicker: { storage: value, options: options },
9+
datepicker: { storage: shiftedValue, options: options },
1010
valueUpdate: valueUpdate,
1111
attr: {
12-
value: value,
12+
value: shiftedValue,
1313
name: inputName,
1414
placeholder: placeholder,
1515
'aria-describedby': noticeId,

0 commit comments

Comments
 (0)