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

fix(ui-date): update to work with 1.3.x #99

Merged
merged 4 commits into from
Nov 10, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 47 additions & 32 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module('ui.date', [])

.constant('uiDateConfig', {})

.directive('uiDate', ['uiDateConfig', function (uiDateConfig) {
.directive('uiDate', ['uiDateConfig', 'uiDateConverter', function (uiDateConfig, uiDateConverter) {
'use strict';
var options;
options = {};
Expand All @@ -36,7 +36,7 @@ angular.module('ui.date', [])
showing = true;
controller.$setViewValue(element.datepicker("getDate"));
_onSelect(value, picker);
element.blur();
//element.blur();
});
};
opts.beforeShow = function() {
Expand All @@ -56,9 +56,13 @@ angular.module('ui.date', [])

// Update the date picker when the model changes
controller.$render = function () {
var date = controller.$viewValue;
var date = controller.$modelValue;
if ( angular.isDefined(date) && date !== null && !angular.isDate(date) ) {
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');
if ( angular.isString(controller.$modelValue) ) {
date = uiDateConverter.stringToDate(attrs.uiDateFormat, controller.$modelValue);
} else {
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');
}
}
element.datepicker("setDate", date);
};
Expand All @@ -78,44 +82,55 @@ angular.module('ui.date', [])
};
}
])
.factory('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){

.constant('uiDateFormatConfig', '')
function dateToString(dateFormat, value){
if (value) {
if ( dateFormat || uiDateFormatConfig) {
return jQuery.datepicker.formatDate(dateFormat, value);
}
return value.toISOString();
} else {
return null;
}
}

function stringToDate(dateFormat, value) {
if ( angular.isString(value) ) {
if ( dateFormat || uiDateFormatConfig) {
return jQuery.datepicker.parseDate(dateFormat, value);
}

var isoDate = new Date(value);
return isNaN(isoDate.getTime()) ? null : isoDate;
}
return null;
}

.directive('uiDateFormat', ['uiDateFormatConfig', function(uiDateFormatConfig) {
return {
stringToDate: stringToDate,
dateToString: dateToString
};

}])
.constant('uiDateFormatConfig', '')
.directive('uiDateFormat', ['uiDateConverter', function(uiDateConverter) {
var directive = {
require:'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var dateFormat = attrs.uiDateFormat || uiDateFormatConfig;
if ( dateFormat ) {
var dateFormat = attrs.uiDateFormat;

// Use the datepicker with the attribute value as the dateFormat string to convert to and from a string
modelCtrl.$formatters.push(function(value) {
if (angular.isString(value) ) {
return jQuery.datepicker.parseDate(dateFormat, value);
}
return null;
});
modelCtrl.$parsers.push(function(value){
if (value) {
return jQuery.datepicker.formatDate(dateFormat, value);
}
return null;
});
} else {
// Default to ISO formatting
modelCtrl.$formatters.push(function(value) {
if (angular.isString(value) ) {
return new Date(value);
}
return null;
modelCtrl.$formatters.unshift(function(value) {
return uiDateConverter.stringToDate(dateFormat, value);
});

modelCtrl.$parsers.push(function(value){
if (value) {
return value.toISOString();
}
return null;
return uiDateConverter.dateToString(dateFormat, value);
});
}

}
};

return directive;
}]);