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

Commit 0cdc460

Browse files
Caitlin Potterpkozlowski-opensource
Caitlin Potter
authored andcommitted
feat(datepicker): datepicker-append-to-body attribute
Attribute / option specifies where in the DOM to place the datepicker popup elements. If this option evaluates to 'true', the datepicker popup elements are appended to 'body'. Otherwise, they follow the directive element.
1 parent 4540476 commit 0cdc460

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

src/datepicker/datepicker.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.position'])
257257
toggleWeeksText: 'Weeks',
258258
clearText: 'Clear',
259259
closeText: 'Done',
260-
closeOnDateSelection: true
260+
closeOnDateSelection: true,
261+
appendToBody: false
261262
})
262263

263264
.directive('datepickerPopup', ['$compile', '$parse', '$document', '$position', 'dateFilter', 'datepickerPopupConfig',
@@ -266,15 +267,18 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
266267
restrict: 'EA',
267268
require: 'ngModel',
268269
link: function(originalScope, element, attrs, ngModel) {
269-
var closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? originalScope.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection;
270270
var dateFormat;
271271
attrs.$observe('datepickerPopup', function(value) {
272272
dateFormat = value || datepickerPopupConfig.dateFormat;
273273
ngModel.$render();
274274
});
275275

276+
var closeOnDateSelection = angular.isDefined(attrs.closeOnDateSelection) ? originalScope.$eval(attrs.closeOnDateSelection) : datepickerPopupConfig.closeOnDateSelection;
277+
var appendToBody = angular.isDefined(attrs.datepickerAppendToBody) ? originalScope.$eval(attrs.datepickerAppendToBody) : datepickerPopupConfig.appendToBody;
278+
276279
// create a child scope for the datepicker directive so we are not polluting original scope
277280
var scope = originalScope.$new();
281+
278282
originalScope.$on('$destroy', function() {
279283
scope.$destroy();
280284
});
@@ -446,7 +450,12 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
446450
$setModelValue(originalScope, null);
447451
};
448452

449-
element.after($compile(popupEl)(scope));
453+
var $popup = $compile(popupEl)(scope);
454+
if ( appendToBody ) {
455+
$document.find('body').append($popup);
456+
} else {
457+
element.after($popup);
458+
}
450459
}
451460
};
452461
}])

src/datepicker/docs/readme.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ Everything is formatted using the [date filter](http://docs.angularjs.org/api/ng
77

88
### Datepicker Settings ###
99

10-
All settings can be provided as attributes in the `<datepicker>` or globally configured through the `datepickerConfig`.
11-
10+
All settings can be provided as attributes in the `<datepicker>` or globally configured through the `datepickerConfig`. `datepicker-popup` options may be provided as attributes in the `datepicker-popup`'s element, or globally configured through the `datepickerPopupConfig`.
1211
* `ng-model` <i class="icon-eye-open"></i>
1312
:
1413
The date object.
@@ -89,4 +88,8 @@ Specific settings for the `datepicker-popup` are:
8988

9089
* `close-on-date-selection`
9190
_(Default: true)_ :
92-
Whether to close calendar when a date is chosen.
91+
Whether to close calendar when a date is chosen.
92+
93+
* `datepicker-append-to-body`
94+
_(Default: false)_:
95+
Append the datepicker popup element to `body`, rather than inserting after `datepicker-popup`. For global configuration, use `datepickerPopupConfig.appendToBody`.

src/datepicker/test/datepicker.spec.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,25 @@ describe('datepicker directive', function () {
12401240
expect(inputEl.val()).toBe('pizza');
12411241
});
12421242
});
1243+
1244+
describe('with an append-to-body attribute', function() {
1245+
beforeEach(inject(function($rootScope) {
1246+
$rootScope.date = new Date();
1247+
}));
1248+
1249+
it('should append to the body', function() {
1250+
var $body = $document.find('body'),
1251+
bodyLength = $body.children().length,
1252+
elm = angular.element(
1253+
'<div><input datepicker-popup ng-model="date" datepicker-append-to-body="true"></input></div>'
1254+
);
1255+
$compile(elm)($rootScope);
1256+
$rootScope.$digest();
1257+
1258+
expect($body.children().length).toEqual(bodyLength + 1);
1259+
expect(elm.children().length).toEqual(1);
1260+
});
1261+
});
12431262
});
12441263
});
12451264

@@ -1263,4 +1282,4 @@ describe('datepicker directive with empty initial state', function () {
12631282
it('is shows rows with days', function() {
12641283
expect(element.find('tbody').find('tr').length).toBeGreaterThan(3);
12651284
});
1266-
});
1285+
});

0 commit comments

Comments
 (0)