Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
feat(dialog): disable scrolling on parent while showing dialog
Browse files Browse the repository at this point in the history
closes #987
  • Loading branch information
rschmukler committed Jan 7, 2015
1 parent f46487f commit 993fa2b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ function MdDialogDirective($$rAF, $mdTheming) {
* - `targetEvent` - `{DOMClickEvent=}`: A click's event object. When passed in as an option,
* the location of the click will be used as the starting point for the opening animation
* of the the dialog.
* - `disableParentScroll` - `{boolean=}`: Whether to disable scrolling while the dialog is open.
* Default true.
* - `hasBackdrop` - `{boolean=}`: Whether there should be an opaque backdrop behind the dialog.
* Default true.
* - `clickOutsideToClose` - `{boolean=}`: Whether the user can click outside the dialog to
Expand Down Expand Up @@ -254,7 +256,7 @@ function MdDialogProvider($$interimElementProvider) {

return $$interimElementProvider('$mdDialog')
.setDefaults({
methods: ['hasBackdrop', 'clickOutsideToClose', 'escapeToClose', 'targetEvent'],
methods: ['disableParentScroll', 'hasBackdrop', 'clickOutsideToClose', 'escapeToClose', 'targetEvent'],
options: dialogDefaultOptions
})
.addPreset('alert', {
Expand Down Expand Up @@ -309,11 +311,13 @@ function MdDialogProvider($$interimElementProvider) {
clickOutsideToClose: true,
escapeToClose: true,
targetEvent: null,
disableParentScroll: true,
transformTemplate: function(template) {
return '<div class="md-dialog-container">' + template + '</div>';
}
};


// On show method for dialogs
function onShow(scope, element, options) {
// Incase the user provides a raw dom element, always wrap it in jqLite
Expand All @@ -330,6 +334,11 @@ function MdDialogProvider($$interimElementProvider) {
$animate.enter(options.backdrop, options.parent);
}

if (options.disableParentScroll) {
options.oldOverflowStyle = options.parent.css('overflow');
options.parent.css('overflow', 'hidden');
}

return dialogPopIn(
element,
options.parent,
Expand Down Expand Up @@ -377,6 +386,10 @@ function MdDialogProvider($$interimElementProvider) {
if (options.backdrop) {
$animate.leave(options.backdrop);
}
if (options.disableParentScroll) {
options.parent.css('overflow', options.oldOverflowStyle);
$document[0].removeEventListener('scroll', options.captureScroll, true);
}
if (options.escapeToClose) {
$rootElement.off('keyup', options.rootElementKeyupCallback);
}
Expand Down
13 changes: 13 additions & 0 deletions src/components/dialog/dialog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ describe('$mdDialog', function() {
expect(parent[0].querySelectorAll('md-dialog').length).toBe(1);
}));

it('should disableParentScroll == true', inject(function($mdDialog, $animate, $rootScope) {
var parent = angular.element('<div>');
$mdDialog.show({
template: '<md-dialog>',
parent: parent,
disableParentScroll: true
});
$rootScope.$apply();
$animate.triggerCallbacks();
$rootScope.$apply();
expect(parent.css('overflow')).toBe('hidden');
}));

it('should hasBackdrop == true', inject(function($mdDialog, $animate, $rootScope) {
var parent = angular.element('<div>');
$mdDialog.show({
Expand Down

0 comments on commit 993fa2b

Please sign in to comment.