From 290b13e99b21673ff9275f0f15400937b3fe2620 Mon Sep 17 00:00:00 2001 From: AlexChan Date: Mon, 27 Oct 2014 22:07:31 +0100 Subject: [PATCH 1/4] Fixes to work with 1.3 --- src/date.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/date.js b/src/date.js index 38c6467..217694a 100644 --- a/src/date.js +++ b/src/date.js @@ -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() { @@ -56,7 +56,7 @@ 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'); } @@ -88,7 +88,7 @@ angular.module('ui.date', []) var dateFormat = attrs.uiDateFormat || uiDateFormatConfig; if ( dateFormat ) { // Use the datepicker with the attribute value as the dateFormat string to convert to and from a string - modelCtrl.$formatters.push(function(value) { + modelCtrl.$formatters.unshift(function(value) { if (angular.isString(value) ) { return jQuery.datepicker.parseDate(dateFormat, value); } @@ -102,9 +102,10 @@ angular.module('ui.date', []) }); } else { // Default to ISO formatting - modelCtrl.$formatters.push(function(value) { + modelCtrl.$formatters.unshift(function(value) { if (angular.isString(value) ) { - return new Date(value); + var isoDate = new Date(value); + return isNaN(isoDate.getTime()) ? null : isoDate; } return null; }); From c390da47a6c0e74d094202f358d980532fd950ae Mon Sep 17 00:00:00 2001 From: AlexChan Date: Sat, 8 Nov 2014 23:52:16 +0100 Subject: [PATCH 2/4] 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 https://github.com/angular/angular.js/pull/9808 is merged --- src/date.js | 73 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/src/date.js b/src/date.js index 217694a..9fc4330 100644 --- a/src/date.js +++ b/src/date.js @@ -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 = {}; @@ -58,7 +58,11 @@ angular.module('ui.date', []) controller.$render = function () { 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); }; @@ -78,45 +82,54 @@ angular.module('ui.date', []) }; } ]) +.service('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){ -.constant('uiDateFormatConfig', '') + return { + stringToDate: stringToDate, + dateToString: dateToString + }; + + function dateToString(dateFormat, value){ + if (value) { + if ( dateFormat || uiDateFormatConfig) { + return jQuery.datepicker.formatDate(dateFormat, value); + } + return value.toISOString(); + } else { + return null; + } + } -.directive('uiDateFormat', ['uiDateFormatConfig', function(uiDateFormatConfig) { + 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; + } +}]) +.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.unshift(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.unshift(function(value) { - if (angular.isString(value) ) { - var isoDate = new Date(value); - return isNaN(isoDate.getTime()) ? null : isoDate; - } - return null; + return uiDateConverter.stringToDate(dateFormat, value); }); + modelCtrl.$parsers.push(function(value){ - if (value) { - return value.toISOString(); - } - return null; + return uiDateConverter.dateToString(dateFormat, value) }); - } + } }; + return directive; }]); From 16314a534680546df164451e601eae9b80330637 Mon Sep 17 00:00:00 2001 From: AlexChan Date: Sun, 9 Nov 2014 00:05:41 +0100 Subject: [PATCH 3/4] jshint fixes --- src/date.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/date.js b/src/date.js index 9fc4330..ecf67ba 100644 --- a/src/date.js +++ b/src/date.js @@ -59,7 +59,7 @@ angular.module('ui.date', []) var date = controller.$modelValue; if ( angular.isDefined(date) && date !== null && !angular.isDate(date) ) { if ( angular.isString(controller.$modelValue) ) { - date = uiDateConverter.stringToDate(attrs.uiDateFormat, 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'); } @@ -84,11 +84,6 @@ angular.module('ui.date', []) ]) .service('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){ - return { - stringToDate: stringToDate, - dateToString: dateToString - }; - function dateToString(dateFormat, value){ if (value) { if ( dateFormat || uiDateFormatConfig) { @@ -111,6 +106,12 @@ angular.module('ui.date', []) } return null; } + + return { + stringToDate: stringToDate, + dateToString: dateToString + }; + }]) .constant('uiDateFormatConfig', '') .directive('uiDateFormat', ['uiDateConverter', function(uiDateConverter) { @@ -125,7 +126,7 @@ angular.module('ui.date', []) }); modelCtrl.$parsers.push(function(value){ - return uiDateConverter.dateToString(dateFormat, value) + return uiDateConverter.dateToString(dateFormat, value); }); } From 01185d73e21b6e23038fc090ff4efe5c4d3a6633 Mon Sep 17 00:00:00 2001 From: AlexChan Date: Sun, 9 Nov 2014 21:25:30 +0100 Subject: [PATCH 4/4] fix(date.js) service can be a factory --- src/date.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/date.js b/src/date.js index ecf67ba..1cc02c7 100644 --- a/src/date.js +++ b/src/date.js @@ -82,7 +82,7 @@ angular.module('ui.date', []) }; } ]) -.service('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){ +.factory('uiDateConverter', ['uiDateFormatConfig', function(uiDateFormatConfig){ function dateToString(dateFormat, value){ if (value) {