1
1
/**
2
- * Copyright © 2016 Magento. All rights reserved.
2
+ * Copyright © 2015 Magento. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
5
define ( [
@@ -11,9 +11,60 @@ define([
11
11
12
12
return Abstract . extend ( {
13
13
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
+
14
53
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 : ''
17
68
} ,
18
69
19
70
/**
@@ -23,24 +74,91 @@ define([
23
74
*/
24
75
initConfig : function ( ) {
25
76
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 ( ) ;
27
85
28
86
return this ;
29
87
} ,
30
88
31
89
/**
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.
33
99
*
34
- * @returns {String }
100
+ * @param {String } value
35
101
*/
36
- normalizeData : function ( ) {
37
- var value = this . _super ( ) ;
102
+ onValueChange : function ( value ) {
103
+ var dateFormat ,
104
+ shiftedValue ;
38
105
39
106
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 ;
41
156
}
42
157
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 ) ;
44
162
}
45
163
} ) ;
46
164
} ) ;
0 commit comments