@@ -679,6 +679,10 @@ function MdDialogProvider($$interimElementProvider) {
679
679
// Those option changes need to be done, before the compilation has started, because otherwise
680
680
// the option changes will be not available in the $mdCompilers locales.
681
681
detectTheming ( options ) ;
682
+
683
+ if ( options . contentElement ) {
684
+ options . restoreContentElement = installContentElement ( options ) ;
685
+ }
682
686
}
683
687
684
688
function beforeShow ( scope , element , options , controller ) {
@@ -702,29 +706,6 @@ function MdDialogProvider($$interimElementProvider) {
702
706
function onShow ( scope , element , options , controller ) {
703
707
angular . element ( $document [ 0 ] . body ) . addClass ( 'md-dialog-is-showing' ) ;
704
708
705
- if ( options . contentElement ) {
706
- var contentEl = options . contentElement ;
707
-
708
- if ( angular . isString ( contentEl ) ) {
709
- contentEl = document . querySelector ( contentEl ) ;
710
- options . elementInsertionSibling = contentEl . nextElementSibling ;
711
- options . elementInsertionParent = contentEl . parentNode ;
712
- } else {
713
- contentEl = contentEl [ 0 ] || contentEl ;
714
- // When the element is not visible in the DOM, then we can treat is as same
715
- // as a normal dialog would do. Removing it at close etc.
716
- // ---
717
- // When the element is visible in the DOM, then we restore it at close of the dialog.
718
- if ( document . contains ( contentEl ) ) {
719
- options . elementInsertionSibling = contentEl . nextElementSibling ;
720
- options . elementInsertionParent = contentEl . parentNode ;
721
- }
722
- }
723
-
724
- options . elementInsertionEntry = contentEl ;
725
- element = angular . element ( contentEl ) ;
726
- }
727
-
728
709
var dialogElement = element . find ( 'md-dialog' ) ;
729
710
730
711
// Once a dialog has `ng-cloak` applied on his template the dialog animation will not work properly.
@@ -813,26 +794,6 @@ function MdDialogProvider($$interimElementProvider) {
813
794
return dialogPopOut ( element , options ) ;
814
795
}
815
796
816
- function removeContentElement ( ) {
817
- if ( ! options . contentElement ) return ;
818
-
819
- options . reverseContainerStretch ( ) ;
820
-
821
- if ( ! options . elementInsertionParent ) {
822
- // When the contentElement has no parent, then it's a virtual DOM element, which should
823
- // be removed at close, as same as normal templates inside of a dialog.
824
- options . elementInsertionEntry . parentNode . removeChild ( options . elementInsertionEntry ) ;
825
- } else if ( ! options . elementInsertionSibling ) {
826
- // When the contentElement doesn't have any sibling, then it can be simply appended to the
827
- // parent, because it plays no role, which index it had before.
828
- options . elementInsertionParent . appendChild ( options . elementInsertionEntry ) ;
829
- } else {
830
- // When the contentElement has a sibling, which marks the previous position of the contentElement
831
- // in the DOM, we insert it correctly before the sibling, to have the same index as before.
832
- options . elementInsertionParent . insertBefore ( options . elementInsertionEntry , options . elementInsertionSibling ) ;
833
- }
834
- }
835
-
836
797
/**
837
798
* Detach the element
838
799
*/
@@ -842,7 +803,8 @@ function MdDialogProvider($$interimElementProvider) {
842
803
if ( ! options . contentElement ) {
843
804
element . remove ( ) ;
844
805
} else {
845
- removeContentElement ( ) ;
806
+ options . reverseContainerStretch ( ) ;
807
+ options . restoreContentElement ( ) ;
846
808
}
847
809
848
810
if ( ! options . $destroy ) options . origin . focus ( ) ;
@@ -865,6 +827,56 @@ function MdDialogProvider($$interimElementProvider) {
865
827
866
828
}
867
829
830
+ /**
831
+ * Installs a content element to the current $$interimElement provider options.
832
+ * @returns {Function } Function to restore the content element at its old DOM location.
833
+ */
834
+ function installContentElement ( options ) {
835
+ var contentEl = options . contentElement ;
836
+ var restoreFn = null ;
837
+
838
+ if ( angular . isString ( contentEl ) ) {
839
+ contentEl = document . querySelector ( contentEl ) ;
840
+ restoreFn = createRestoreFn ( contentEl ) ;
841
+ } else {
842
+ contentEl = contentEl [ 0 ] || contentEl ;
843
+
844
+ // When the element is visible in the DOM, then we restore it at close of the dialog.
845
+ // Otherwise it will be removed from the DOM after close.
846
+ if ( document . contains ( contentEl ) ) {
847
+ restoreFn = createRestoreFn ( contentEl ) ;
848
+ } else {
849
+ restoreFn = function ( ) {
850
+ contentEl . parentNode . removeChild ( contentEl ) ;
851
+ }
852
+ }
853
+ }
854
+
855
+ // Overwrite the options to use the content element.
856
+ options . element = angular . element ( contentEl ) ;
857
+ options . skipCompile = true ;
858
+
859
+ return restoreFn ;
860
+
861
+ function createRestoreFn ( element ) {
862
+ var parent = element . parentNode ;
863
+ var nextSibling = element . nextElementSibling ;
864
+
865
+ return function ( ) {
866
+ if ( ! nextSibling ) {
867
+ // When the element didn't had any sibling, then it can be simply appended to the
868
+ // parent, because it plays no role, which index it had before.
869
+ parent . appendChild ( element ) ;
870
+ } else {
871
+ // When the element had a sibling, which marks the previous position of the element
872
+ // in the DOM, we insert it correctly before the sibling, to have the same index as
873
+ // before.
874
+ parent . insertBefore ( element , nextSibling ) ;
875
+ }
876
+ }
877
+ }
878
+ }
879
+
868
880
/**
869
881
* Capture originator/trigger/from/to element information (if available)
870
882
* and the parent container for the dialog; defaults to the $rootElement
0 commit comments