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

Commit aa3eaa9

Browse files
bekospkozlowski-opensource
authored andcommittedSep 27, 2013
feat(datepicker): dynamic date format for popup
Closes #1071
1 parent 4738386 commit aa3eaa9

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed
 

‎src/datepicker/datepicker.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,12 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
266266
restrict: 'EA',
267267
require: 'ngModel',
268268
link: function(originalScope, element, attrs, ngModel) {
269-
270-
var closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? scope.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection;
271-
var dateFormat = attrs.datepickerPopup || datepickerPopupConfig.dateFormat;
269+
var closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? originalScope.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection;
270+
var dateFormat;
271+
attrs.$observe('datepickerPopup', function(value) {
272+
dateFormat = value || datepickerPopupConfig.dateFormat;
273+
ngModel.$render();
274+
});
272275

273276
// create a child scope for the datepicker directive so we are not polluting original scope
274277
var scope = originalScope.$new();

‎src/datepicker/docs/demo.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<div ng-controller="DatepickerDemoCtrl">
22
<pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>
33

4-
<div class="well well-small pull-left" ng-model="dt">
4+
<h4>Inline</h4>
5+
<div class="well well-small" ng-model="dt" style="display:inline-block;">
56
<datepicker min="minDate" show-weeks="showWeeks"></datepicker>
67
</div>
78

9+
<h4>Popup</h4>
810
<div class="form-horizontal">
9-
<input type="text" datepicker-popup="dd-MMMM-yyyy" ng-model="dt" is-open="opened" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
10-
<button class="btn" ng-click="open()"><i class="icon-calendar"></i></button>
11+
<p>
12+
<input type="text" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
13+
<button class="btn" ng-click="open()"><i class="icon-calendar"></i></button>
14+
</p>
15+
<p><i>Format options:</i> <select ng-model="format" ng-options="f for f in formats"><option></option></select></p>
1116
</div>
1217

1318
<hr />

‎src/datepicker/docs/demo.js

+3
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ var DatepickerDemoCtrl = function ($scope, $timeout) {
3333
'year-format': "'yy'",
3434
'starting-day': 1
3535
};
36+
37+
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate'];
38+
$scope.format = $scope.formats[0];
3639
};

‎src/datepicker/test/datepicker.spec.js

+45
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,51 @@ describe('datepicker directive', function () {
10911091
});
10921092
});
10931093

1094+
describe('dynamic custom format', function () {
1095+
beforeEach(inject(function() {
1096+
$rootScope.format = 'dd-MMMM-yyyy';
1097+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup="{{format}}"><div>')($rootScope);
1098+
$rootScope.$digest();
1099+
assignElements(wrapElement);
1100+
}));
1101+
1102+
it('to display the correct value in input', function() {
1103+
expect(inputEl.val()).toBe('30-September-2010');
1104+
});
1105+
1106+
it('updates the input when a day is clicked', function() {
1107+
clickOption(2, 3);
1108+
expect(inputEl.val()).toBe('15-September-2010');
1109+
expect($rootScope.date).toEqual(new Date('September 15, 2010 15:30:00'));
1110+
});
1111+
1112+
it('updates the input correctly when model changes', function() {
1113+
$rootScope.date = new Date("August 11, 2013 09:09:00");
1114+
$rootScope.$digest();
1115+
expect(inputEl.val()).toBe('11-August-2013');
1116+
});
1117+
1118+
it('updates the input correctly when format changes', function() {
1119+
$rootScope.format = 'dd/MM/yyyy';
1120+
$rootScope.$digest();
1121+
expect(inputEl.val()).toBe('30/09/2010');
1122+
});
1123+
});
1124+
1125+
describe('`close-on-date-selection` attribute', function () {
1126+
beforeEach(inject(function() {
1127+
$rootScope.close = false;
1128+
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup close-on-date-selection="close" is-open="true"><div>')($rootScope);
1129+
$rootScope.$digest();
1130+
assignElements(wrapElement);
1131+
}));
1132+
1133+
it('dpes not close the dropdown when a day is clicked', function() {
1134+
clickOption(2, 3);
1135+
expect(dropdownEl.css('display')).not.toBe('none');
1136+
});
1137+
});
1138+
10941139
describe('button bar', function() {
10951140
var buttons;
10961141
beforeEach(inject(function() {

0 commit comments

Comments
 (0)