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

Commit 98e2bdf

Browse files
kbaltrinicwesleycho
authored andcommitted
fix(datepicker): fix initDate implementation in datepicker
Fixes use of initDate across datepickerConfig, datepickerOptions, and the initDate attribute
1 parent c19b887 commit 98e2bdf

File tree

2 files changed

+117
-20
lines changed

2 files changed

+117
-20
lines changed

src/datepicker/datepicker.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
4444

4545
$scope.datepickerMode = $scope.datepickerMode || datepickerConfig.datepickerMode;
4646
$scope.uniqueId = 'datepicker-' + $scope.$id + '-' + Math.floor(Math.random() * 10000);
47-
this.activeDate = angular.isDefined($attrs.initDate) ? $scope.$parent.$eval($attrs.initDate) : new Date();
47+
48+
if(angular.isDefined($attrs.initDate)) {
49+
this.activeDate = $scope.$parent.$eval($attrs.initDate) || new Date();
50+
$scope.$parent.$watch($attrs.initDate, function(initDate){
51+
if(initDate && (ngModelCtrl.$isEmpty(ngModelCtrl.$modelValue) || ngModelCtrl.$invalid)){
52+
self.activeDate = initDate;
53+
self.refreshView();
54+
}
55+
});
56+
} else {
57+
this.activeDate = new Date();
58+
}
4859

4960
$scope.isActive = function(dateObject) {
5061
if (self.compare(dateObject.date, self.activeDate) === 0) {
@@ -479,13 +490,19 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
479490
// datepicker element
480491
var datepickerEl = angular.element(popupEl.children()[0]);
481492
if ( attrs.datepickerOptions ) {
482-
angular.forEach(scope.$parent.$eval(attrs.datepickerOptions), function( value, option ) {
493+
var options = scope.$parent.$eval(attrs.datepickerOptions);
494+
if(options.initDate) {
495+
scope.initDate = options.initDate;
496+
datepickerEl.attr( 'init-date', 'initDate' );
497+
delete options.initDate;
498+
}
499+
angular.forEach(options, function( value, option ) {
483500
datepickerEl.attr( cameltoDash(option), value );
484501
});
485502
}
486503

487504
scope.watchData = {};
488-
angular.forEach(['minDate', 'maxDate', 'datepickerMode'], function( key ) {
505+
angular.forEach(['minDate', 'maxDate', 'datepickerMode', 'initDate'], function( key ) {
489506
if ( attrs[key] ) {
490507
var getAttribute = $parse(attrs[key]);
491508
scope.$parent.$watch(getAttribute, function(value){
@@ -510,10 +527,6 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
510527
if (attrs.customClass){
511528
datepickerEl.attr('custom-class', 'customClass({ date: date, mode: mode })');
512529
}
513-
if (attrs.initDate) {
514-
scope.initDate = scope.$parent.$eval(attrs.initDate);
515-
datepickerEl.attr('init-date', 'initDate');
516-
}
517530

518531
function parseDate(viewValue) {
519532
if (angular.isNumber(viewValue)) {

src/datepicker/test/datepicker.spec.js

+97-13
Original file line numberDiff line numberDiff line change
@@ -1337,23 +1337,107 @@ describe('datepicker directive', function () {
13371337
});
13381338

13391339
describe('attribute `datepickerOptions`', function () {
1340-
var weekHeader, weekElement;
1340+
1341+
describe('show-weeks', function(){
1342+
var weekHeader, weekElement;
1343+
beforeEach(function() {
1344+
$rootScope.opts = {
1345+
'show-weeks': false
1346+
};
1347+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1348+
$rootScope.$digest();
1349+
assignElements(wrapElement);
1350+
1351+
weekHeader = getLabelsRow().find('th').eq(0);
1352+
weekElement = element.find('tbody').find('tr').eq(1).find('td').eq(0);
1353+
});
1354+
1355+
it('hides week numbers based on variable', function() {
1356+
expect(weekHeader.text()).toEqual('');
1357+
expect(weekHeader).toBeHidden();
1358+
expect(weekElement).toBeHidden();
1359+
});
1360+
});
1361+
1362+
describe('init-date', function(){
1363+
beforeEach(function() {
1364+
$rootScope.date = null;
1365+
$rootScope.opts = {
1366+
'initDate': new Date('November 9, 1980')
1367+
};
1368+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1369+
$rootScope.$digest();
1370+
assignElements(wrapElement);
1371+
});
1372+
1373+
it('does not alter the model', function() {
1374+
expect($rootScope.date).toBe(null);
1375+
});
1376+
1377+
it('shows the correct title', function() {
1378+
expect(getTitle()).toBe('November 1980');
1379+
});
1380+
});
1381+
});
1382+
1383+
describe('attribute `init-date`', function(){
13411384
beforeEach(function() {
1342-
$rootScope.opts = {
1343-
'show-weeks': false
1344-
};
1345-
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup datepicker-options="opts" is-open="true"></div>')($rootScope);
1346-
$rootScope.$digest();
1347-
assignElements(wrapElement);
1385+
$rootScope.date = null;
1386+
$rootScope.initDate = new Date('November 9, 1980');
1387+
});
1388+
1389+
describe('when initially set', function(){
1390+
beforeEach(function() {
1391+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup init-date="initDate" is-open="true"></div>')($rootScope);
1392+
$rootScope.$digest();
1393+
assignElements(wrapElement);
1394+
});
1395+
1396+
it('does not alter the model', function() {
1397+
expect($rootScope.date).toBe(null);
1398+
});
1399+
1400+
it('shows the correct title', function() {
1401+
expect(getTitle()).toBe('November 1980');
1402+
});
1403+
});
1404+
1405+
describe('when modified before date selected.', function(){
1406+
beforeEach(function() {
1407+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup init-date="initDate" is-open="true"></div>')($rootScope);
1408+
$rootScope.$digest();
1409+
assignElements(wrapElement);
1410+
1411+
$rootScope.initDate = new Date('December 20, 1981');
1412+
$rootScope.$digest();
1413+
});
1414+
1415+
it('does not alter the model', function() {
1416+
expect($rootScope.date).toBe(null);
1417+
});
13481418

1349-
weekHeader = getLabelsRow().find('th').eq(0);
1350-
weekElement = element.find('tbody').find('tr').eq(1).find('td').eq(0);
1419+
it('shows the correct title', function() {
1420+
expect(getTitle()).toBe('December 1981');
1421+
});
13511422
});
13521423

1353-
it('hides week numbers based on variable', function() {
1354-
expect(weekHeader.text()).toEqual('');
1355-
expect(weekHeader).toBeHidden();
1356-
expect(weekElement).toBeHidden();
1424+
describe('when modified after date selected.', function(){
1425+
beforeEach(function() {
1426+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup init-date="initDate" is-open="true"></div>')($rootScope);
1427+
$rootScope.$digest();
1428+
assignElements(wrapElement);
1429+
$rootScope.date = new Date('April 1, 1982');
1430+
$rootScope.initDate = new Date('December 20, 1981');
1431+
$rootScope.$digest();
1432+
});
1433+
1434+
it('does not alter the model', function() {
1435+
expect($rootScope.date).toEqual(new Date('April 1, 1982'));
1436+
});
1437+
1438+
it('shows the correct title', function() {
1439+
expect(getTitle()).toBe('April 1982');
1440+
});
13571441
});
13581442
});
13591443

0 commit comments

Comments
 (0)