@@ -10,7 +10,7 @@ angular.module('ui.date', [])
10
10
11
11
. constant ( 'uiDateConfig' , { } )
12
12
13
- . directive ( 'uiDate' , [ 'uiDateConfig' , function ( uiDateConfig ) {
13
+ . directive ( 'uiDate' , [ 'uiDateConfig' , 'uiDateConverter' , function ( uiDateConfig , uiDateConverter ) {
14
14
'use strict' ;
15
15
var options ;
16
16
options = { } ;
@@ -36,7 +36,7 @@ angular.module('ui.date', [])
36
36
showing = true ;
37
37
controller . $setViewValue ( element . datepicker ( "getDate" ) ) ;
38
38
_onSelect ( value , picker ) ;
39
- element . blur ( ) ;
39
+ // element.blur();
40
40
} ) ;
41
41
} ;
42
42
opts . beforeShow = function ( ) {
@@ -56,9 +56,13 @@ angular.module('ui.date', [])
56
56
57
57
// Update the date picker when the model changes
58
58
controller . $render = function ( ) {
59
- var date = controller . $viewValue ;
59
+ var date = controller . $modelValue ;
60
60
if ( angular . isDefined ( date ) && date !== null && ! angular . isDate ( date ) ) {
61
- throw new Error ( 'ng-Model value must be a Date object - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string' ) ;
61
+ if ( angular . isString ( controller . $modelValue ) ) {
62
+ date = uiDateConverter . stringToDate ( attrs . uiDateFormat , controller . $modelValue ) ;
63
+ } else {
64
+ throw new Error ( 'ng-Model value must be a Date, or a String object with a date formatter - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string' ) ;
65
+ }
62
66
}
63
67
element . datepicker ( "setDate" , date ) ;
64
68
} ;
@@ -78,44 +82,55 @@ angular.module('ui.date', [])
78
82
} ;
79
83
}
80
84
] )
85
+ . factory ( 'uiDateConverter' , [ 'uiDateFormatConfig' , function ( uiDateFormatConfig ) {
81
86
82
- . constant ( 'uiDateFormatConfig' , '' )
87
+ function dateToString ( dateFormat , value ) {
88
+ if ( value ) {
89
+ if ( dateFormat || uiDateFormatConfig ) {
90
+ return jQuery . datepicker . formatDate ( dateFormat , value ) ;
91
+ }
92
+ return value . toISOString ( ) ;
93
+ } else {
94
+ return null ;
95
+ }
96
+ }
97
+
98
+ function stringToDate ( dateFormat , value ) {
99
+ if ( angular . isString ( value ) ) {
100
+ if ( dateFormat || uiDateFormatConfig ) {
101
+ return jQuery . datepicker . parseDate ( dateFormat , value ) ;
102
+ }
103
+
104
+ var isoDate = new Date ( value ) ;
105
+ return isNaN ( isoDate . getTime ( ) ) ? null : isoDate ;
106
+ }
107
+ return null ;
108
+ }
83
109
84
- . directive ( 'uiDateFormat' , [ 'uiDateFormatConfig' , function ( uiDateFormatConfig ) {
110
+ return {
111
+ stringToDate : stringToDate ,
112
+ dateToString : dateToString
113
+ } ;
114
+
115
+ } ] )
116
+ . constant ( 'uiDateFormatConfig' , '' )
117
+ . directive ( 'uiDateFormat' , [ 'uiDateConverter' , function ( uiDateConverter ) {
85
118
var directive = {
86
119
require :'ngModel' ,
87
120
link : function ( scope , element , attrs , modelCtrl ) {
88
- var dateFormat = attrs . uiDateFormat || uiDateFormatConfig ;
89
- if ( dateFormat ) {
121
+ var dateFormat = attrs . uiDateFormat ;
122
+
90
123
// Use the datepicker with the attribute value as the dateFormat string to convert to and from a string
91
- modelCtrl . $formatters . push ( function ( value ) {
92
- if ( angular . isString ( value ) ) {
93
- return jQuery . datepicker . parseDate ( dateFormat , value ) ;
94
- }
95
- return null ;
96
- } ) ;
97
- modelCtrl . $parsers . push ( function ( value ) {
98
- if ( value ) {
99
- return jQuery . datepicker . formatDate ( dateFormat , value ) ;
100
- }
101
- return null ;
102
- } ) ;
103
- } else {
104
- // Default to ISO formatting
105
- modelCtrl . $formatters . push ( function ( value ) {
106
- if ( angular . isString ( value ) ) {
107
- return new Date ( value ) ;
108
- }
109
- return null ;
124
+ modelCtrl . $formatters . unshift ( function ( value ) {
125
+ return uiDateConverter . stringToDate ( dateFormat , value ) ;
110
126
} ) ;
127
+
111
128
modelCtrl . $parsers . push ( function ( value ) {
112
- if ( value ) {
113
- return value . toISOString ( ) ;
114
- }
115
- return null ;
129
+ return uiDateConverter . dateToString ( dateFormat , value ) ;
116
130
} ) ;
117
- }
131
+
118
132
}
119
133
} ;
134
+
120
135
return directive ;
121
136
} ] ) ;
0 commit comments