Skip to content
This repository was archived by the owner on May 28, 2019. It is now read-only.

Commit fb52c44

Browse files
author
Alex
committed
Merge pull request #99 from alexanderchan/master
fix(ui-date): update to work with 1.3.x fixes #95 fixes #96
2 parents 51a8e36 + 01185d7 commit fb52c44

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

src/date.js

Lines changed: 47 additions & 32 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 = {};
@@ -36,7 +36,7 @@ angular.module('ui.date', [])
3636
showing = true;
3737
controller.$setViewValue(element.datepicker("getDate"));
3838
_onSelect(value, picker);
39-
element.blur();
39+
//element.blur();
4040
});
4141
};
4242
opts.beforeShow = function() {
@@ -56,9 +56,13 @@ angular.module('ui.date', [])
5656

5757
// Update the date picker when the model changes
5858
controller.$render = function () {
59-
var date = controller.$viewValue;
59+
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,44 +82,55 @@ angular.module('ui.date', [])
7882
};
7983
}
8084
])
85+
.factory('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){
8186

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+
}
83109

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) {
85118
var directive = {
86119
require:'ngModel',
87120
link: function(scope, element, attrs, modelCtrl) {
88-
var dateFormat = attrs.uiDateFormat || uiDateFormatConfig;
89-
if ( dateFormat ) {
121+
var dateFormat = attrs.uiDateFormat;
122+
90123
// 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);
110126
});
127+
111128
modelCtrl.$parsers.push(function(value){
112-
if (value) {
113-
return value.toISOString();
114-
}
115-
return null;
129+
return uiDateConverter.dateToString(dateFormat, value);
116130
});
117-
}
131+
118132
}
119133
};
134+
120135
return directive;
121136
}]);

0 commit comments

Comments
 (0)