Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 7 additions & 61 deletions src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ function MdDialogProvider($$interimElementProvider) {
return $$interimElementProvider('$mdDialog')
.setDefaults({
methods: ['disableParentScroll', 'hasBackdrop', 'clickOutsideToClose', 'escapeToClose',
'targetEvent', 'closeTo', 'openFrom', 'parent', 'fullscreen', 'contentElement'],
'targetEvent', 'closeTo', 'openFrom', 'parent', 'fullscreen'],
options: dialogDefaultOptions
})
.addPreset('alert', {
Expand Down Expand Up @@ -647,7 +647,6 @@ function MdDialogProvider($$interimElementProvider) {
clickOutsideToClose: false,
escapeToClose: true,
targetEvent: null,
contentElement: null,
closeTo: null,
openFrom: null,
focusOnOpen: true,
Expand Down Expand Up @@ -679,10 +678,6 @@ function MdDialogProvider($$interimElementProvider) {
// Those option changes need to be done, before the compilation has started, because otherwise
// the option changes will be not available in the $mdCompilers locales.
detectTheming(options);

if (options.contentElement) {
options.restoreContentElement = installContentElement(options);
}
}

function beforeShow(scope, element, options, controller) {
Expand Down Expand Up @@ -799,14 +794,15 @@ function MdDialogProvider($$interimElementProvider) {
*/
function detachAndClean() {
angular.element($document[0].body).removeClass('md-dialog-is-showing');
// Only remove the element, if it's not provided through the contentElement option.
if (!options.contentElement) {
element.remove();
} else {

// Reverse the container stretch if using a content element.
if (options.contentElement) {
options.reverseContainerStretch();
options.restoreContentElement();
}

// Exposed cleanup function from the $mdCompiler.
options.cleanupElement();

if (!options.$destroy) options.origin.focus();
}
}
Expand All @@ -827,56 +823,6 @@ function MdDialogProvider($$interimElementProvider) {

}

/**
* Installs a content element to the current $$interimElement provider options.
* @returns {Function} Function to restore the content element at its old DOM location.
*/
function installContentElement(options) {
var contentEl = options.contentElement;
var restoreFn = null;

if (angular.isString(contentEl)) {
contentEl = document.querySelector(contentEl);
restoreFn = createRestoreFn(contentEl);
} else {
contentEl = contentEl[0] || contentEl;

// When the element is visible in the DOM, then we restore it at close of the dialog.
// Otherwise it will be removed from the DOM after close.
if (document.contains(contentEl)) {
restoreFn = createRestoreFn(contentEl);
} else {
restoreFn = function() {
contentEl.parentNode.removeChild(contentEl);
}
}
}

// Overwrite the options to use the content element.
options.element = angular.element(contentEl);
options.skipCompile = true;

return restoreFn;

function createRestoreFn(element) {
var parent = element.parentNode;
var nextSibling = element.nextElementSibling;

return function() {
if (!nextSibling) {
// When the element didn't had any sibling, then it can be simply appended to the
// parent, because it plays no role, which index it had before.
parent.appendChild(element);
} else {
// When the element had a sibling, which marks the previous position of the element
// in the DOM, we insert it correctly before the sibling, to have the same index as
// before.
parent.insertBefore(element, nextSibling);
}
}
}
}

/**
* Capture originator/trigger/from/to element information (if available)
* and the parent container for the dialog; defaults to the $rootElement
Expand Down
33 changes: 33 additions & 0 deletions src/components/dialog/dialog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,39 @@ describe('$mdDialog', function() {
document.body.removeChild(contentElement[0]);
});

it('should support contentElement as a preset method', function() {
var contentElement = $compile(
'<div class="md-dialog-container">' +
'<md-dialog>Dialog</md-dialog>' +
'</div>'
)($rootScope);

var parentEl = angular.element('<div>');

// Add the contentElement to the DOM.
document.body.appendChild(contentElement[0]);

$mdDialog.show(
$mdDialog
.build()
.contentElement(contentElement)
.parent(parentEl)
.escapeToClose(true)
);

$rootScope.$apply();
runAnimation();

expect(contentElement[0].parentNode).toBe(parentEl[0]);

$mdDialog.hide();
runAnimation();

expect(contentElement[0].parentNode).toBe(document.body);

document.body.removeChild(contentElement[0]);
});

it('should correctly query for a contentElement', function() {
var contentElement = $compile(
'<div class="md-dialog-container" id="myId">' +
Expand Down
Loading