Skip to content

Commit c390da4

Browse files
committed
fix and refactor(date.js) compatibility with angular 1.3
This refactors the date parsing to allow it to be used in the ui-date directive. Breaking Change the blur event isn't fired. This can be added back when angular/angular.js#9808 is merged
1 parent 290b13e commit c390da4

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

src/date.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ angular.module('ui.date', [])
1010

1111
.constant('uiDateConfig', {})
1212

13-
.directive('uiDate', ['uiDateConfig', function (uiDateConfig) {
13+
.directive('uiDate', ['uiDateConfig', 'uiDateConverter', function (uiDateConfig, uiDateConverter) {
1414
'use strict';
1515
var options;
1616
options = {};
@@ -58,7 +58,11 @@ angular.module('ui.date', [])
5858
controller.$render = function () {
5959
var date = controller.$modelValue;
6060
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+
}
6266
}
6367
element.datepicker("setDate", date);
6468
};
@@ -78,45 +82,54 @@ angular.module('ui.date', [])
7882
};
7983
}
8084
])
85+
.service('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){
8186

82-
.constant('uiDateFormatConfig', '')
87+
return {
88+
stringToDate: stringToDate,
89+
dateToString: dateToString
90+
};
91+
92+
function dateToString(dateFormat, value){
93+
if (value) {
94+
if ( dateFormat || uiDateFormatConfig) {
95+
return jQuery.datepicker.formatDate(dateFormat, value);
96+
}
97+
return value.toISOString();
98+
} else {
99+
return null;
100+
}
101+
}
83102

84-
.directive('uiDateFormat', ['uiDateFormatConfig', function(uiDateFormatConfig) {
103+
function stringToDate(dateFormat, value) {
104+
if ( angular.isString(value) ) {
105+
if ( dateFormat || uiDateFormatConfig) {
106+
return jQuery.datepicker.parseDate(dateFormat, value);
107+
}
108+
109+
var isoDate = new Date(value);
110+
return isNaN(isoDate.getTime()) ? null : isoDate;
111+
}
112+
return null;
113+
}
114+
}])
115+
.constant('uiDateFormatConfig', '')
116+
.directive('uiDateFormat', ['uiDateConverter', function(uiDateConverter) {
85117
var directive = {
86118
require:'ngModel',
87119
link: function(scope, element, attrs, modelCtrl) {
88-
var dateFormat = attrs.uiDateFormat || uiDateFormatConfig;
89-
if ( dateFormat ) {
120+
var dateFormat = attrs.uiDateFormat;
121+
90122
// Use the datepicker with the attribute value as the dateFormat string to convert to and from a string
91123
modelCtrl.$formatters.unshift(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.unshift(function(value) {
106-
if (angular.isString(value) ) {
107-
var isoDate = new Date(value);
108-
return isNaN(isoDate.getTime()) ? null : isoDate;
109-
}
110-
return null;
124+
return uiDateConverter.stringToDate(dateFormat, value);
111125
});
126+
112127
modelCtrl.$parsers.push(function(value){
113-
if (value) {
114-
return value.toISOString();
115-
}
116-
return null;
128+
return uiDateConverter.dateToString(dateFormat, value)
117129
});
118-
}
130+
119131
}
120132
};
133+
121134
return directive;
122135
}]);

0 commit comments

Comments
 (0)