From bdab729e1c00eca31ccfae7cf1ecd7e2197563d2 Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Fri, 4 Dec 2015 23:24:56 +0000 Subject: [PATCH 1/2] fix(datepicker): alternate input formats: update the datepicker --- src/datepicker/datepicker.js | 1 + src/datepicker/test/datepicker.spec.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index a66465147d..10eb241d73 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -817,6 +817,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi for (var i = 0; i < altInputFormats.length; i++) { date = dateParser.parse(viewValue, altInputFormats[i], scope.date); if (!isNaN(date)) { + ngModel.$setViewValue(date); break; } } diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index 972bb57a8c..b81a47c983 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -2343,10 +2343,10 @@ describe('datepicker', function() { describe('datepickerPopupConfig.altInputFormats', function() { var originalConfig = {}; beforeEach(inject(function(uibDatepickerPopupConfig) { + $rootScope.date = new Date('November 9, 1980'); angular.extend(originalConfig, uibDatepickerPopupConfig); uibDatepickerPopupConfig.datepickerPopup = 'MM-dd-yyyy'; uibDatepickerPopupConfig.altInputFormats = ['M!/d!/yyyy']; - var wrapElement = $compile('
')($rootScope); $rootScope.$digest(); assignElements(wrapElement); @@ -2364,6 +2364,12 @@ describe('datepicker', function() { expect($rootScope.date.getMonth()).toEqual(10); expect($rootScope.date.getDate()).toEqual(8); }); + + it('changes the datepicker', function() { + expect(selectedElementIndex()).toEqual(14); + changeInputValueTo(inputEl, '11/8/1980'); + expect(selectedElementIndex()).toEqual(13); + }); }); describe('attribute `alt-input-formats`', function() { @@ -2381,6 +2387,12 @@ describe('datepicker', function() { expect($rootScope.date.getMonth()).toEqual(10); expect($rootScope.date.getDate()).toEqual(8); }); + + it('changes the datepicker', function() { + expect(selectedElementIndex()).toEqual(14); + changeInputValueTo(inputEl, '11/8/1980'); + expect(selectedElementIndex()).toEqual(13); + }); }); }); From f0f193b4a93fbe2d2b8eb53b7c20281688d3b35a Mon Sep 17 00:00:00 2001 From: Dave Anderson Date: Sat, 5 Dec 2015 10:16:45 -0500 Subject: [PATCH 2/2] fix(datepicker): alternate input formats: update datepicker on change --- src/datepicker/datepicker.js | 37 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index 10eb241d73..e14a8d916a 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -679,7 +679,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi // Detect changes in the view from the text box ngModel.$viewChangeListeners.push(function() { - scope.date = dateParser.parse(ngModel.$viewValue, dateFormat, scope.date); + scope.date = parseDateString(ngModel.$viewValue); }); element.bind('keydown', inputKeydownBind); @@ -797,6 +797,19 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi return string.replace(/([A-Z])/g, function($1) { return '-' + $1.toLowerCase(); }); } + function parseDateString(viewValue) { + var date = dateParser.parse(viewValue, dateFormat, scope.date); + if (isNaN(date)) { + for (var i = 0; i < altInputFormats.length; i++) { + date = dateParser.parse(viewValue, altInputFormats[i], scope.date); + if (!isNaN(date)) { + return date; + } + } + } + return date; + } + function parseDate(viewValue) { if (angular.isNumber(viewValue)) { // presumably timestamp to date object @@ -812,16 +825,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi } if (angular.isString(viewValue)) { - var date = dateParser.parse(viewValue, dateFormat, scope.date); - if (isNaN(date)) { - for (var i = 0; i < altInputFormats.length; i++) { - date = dateParser.parse(viewValue, altInputFormats[i], scope.date); - if (!isNaN(date)) { - ngModel.$setViewValue(date); - break; - } - } - } + var date = parseDateString(viewValue); if (isNaN(date)) { return undefined; } @@ -852,16 +856,7 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi } if (angular.isString(value)) { - var date = dateParser.parse(value, dateFormat); - if (isNaN(date)) { - for (var i = 0; i < altInputFormats.length; i++) { - date = dateParser.parse(value, altInputFormats[i]); - if (!isNaN(date)) { - break; - } - } - } - return !isNaN(date); + return !isNaN(parseDateString(viewValue)); } return false;