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

Commit 87d6230

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(dialog): Provide option to not autowrap templates.
For convenience, we automatically wrap templates that do not contain a `<md-dialog>` tag within them. However, some users would like the ability to create their own dialog directives (like `<my-fancy-dialog>`) that internally use the `<md-dialog>` tag within their template. This causes two `<md-dialog>` tags to be created. Provide a new `autoWrap` option which can be set to false to disable autowrapping when providing a custom directive. Fixes #4898. Closes #5237.
1 parent 7251684 commit 87d6230

File tree

4 files changed

+67
-27
lines changed

4 files changed

+67
-27
lines changed

src/components/dialog/dialog.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ function MdDialogDirective($$rAF, $mdTheming, $mdDialog) {
333333
* - `templateUrl` - `{string=}`: The url of a template that will be used as the content
334334
* of the dialog.
335335
* - `template` - `{string=}`: Same as templateUrl, except this is an actual template string.
336+
* - `autoWrap` - `{boolean=}`: Whether or not to automatically wrap the template with a
337+
* `<md-dialog>` tag if one is not provided. Defaults to true. Can be disabled if you provide a
338+
* custom dialog directive.
336339
* - `targetEvent` - `{DOMClickEvent=}`: A click's event object. When passed in as an option,
337340
* the location of the click will be used as the starting point for the opening animation
338341
* of the the dialog.
@@ -374,7 +377,7 @@ function MdDialogDirective($$rAF, $mdTheming, $mdDialog) {
374377
* starting.
375378
* - `onComplete` `{function=}`: Callback function used to announce when the show() action is
376379
* finished.
377-
* - `onRemoving` `{function=} Callback function used to announce the close/hide() action is
380+
* - `onRemoving` `{function=}`: Callback function used to announce the close/hide() action is
378381
* starting. This allows developers to run custom animations in parallel the close animations.
379382
*
380383
* @returns {promise} A promise that can be resolved with `$mdDialog.hide()` or
@@ -469,15 +472,19 @@ function MdDialogProvider($$interimElementProvider) {
469472
openFrom: null,
470473
focusOnOpen: true,
471474
disableParentScroll: true,
472-
transformTemplate: function(template) {
475+
autoWrap: true,
476+
transformTemplate: function(template, options) {
473477
return '<div class="md-dialog-container">' + validatedTemplate(template) + '</div>';
474478

475479
/**
476480
* The specified template should contain a <md-dialog> wrapper element....
477481
*/
478482
function validatedTemplate(template) {
479-
template || ""
480-
return /<\/md-dialog>/g.test(template) ? template : "<md-dialog>" + template + "</md-dialog>";
483+
if (options.autoWrap && !/<\/md-dialog>/g.test(template)) {
484+
return '<md-dialog>' + (template || '') + '</md-dialog>';
485+
} else {
486+
return template || '';
487+
}
481488
}
482489
}
483490
};

src/components/dialog/dialog.spec.js

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -545,37 +545,61 @@ describe('$mdDialog', function() {
545545
nodes.remove();
546546
}));
547547

548-
it('should not wrap content with existing md-dialog', inject(function($mdDialog, $rootScope) {
548+
describe('when autoWrap parameter is true (default)', function() {
549549

550-
var template = '<md-dialog><div id="rawContent">Hello</div></md-dialog>';
551-
var parent = angular.element('<div>');
550+
it('should not wrap content with existing md-dialog', inject(function($mdDialog, $rootScope) {
552551

553-
$mdDialog.show({
554-
template: template,
555-
parent: parent
556-
});
552+
var template = '<md-dialog><div id="rawContent">Hello</div></md-dialog>';
553+
var parent = angular.element('<div>');
557554

558-
$rootScope.$apply();
555+
$mdDialog.show({
556+
template: template,
557+
parent: parent
558+
});
559559

560-
var container = parent[0].querySelectorAll('md-dialog');
561-
expect(container.length).toBe(1);
562-
}));
560+
$rootScope.$apply();
563561

564-
it('should wrap raw content with md-dialog', inject(function($mdDialog, $rootScope) {
562+
var container = parent[0].querySelectorAll('md-dialog');
563+
expect(container.length).toBe(1);
564+
}));
565565

566-
var template = '<div id="rawContent">Hello</div>';
567-
var parent = angular.element('<div>');
566+
it('should wrap raw content with md-dialog', inject(function($mdDialog, $rootScope) {
568567

569-
$mdDialog.show({
570-
template: template,
571-
parent: parent
572-
});
568+
var template = '<div id="rawContent">Hello</div>';
569+
var parent = angular.element('<div>');
573570

574-
$rootScope.$apply();
571+
$mdDialog.show({
572+
template: template,
573+
parent: parent
574+
});
575575

576-
var container = parent[0].querySelectorAll('md-dialog');
577-
expect(container.length).toBe(1);
578-
}));
576+
$rootScope.$apply();
577+
578+
var container = parent[0].querySelectorAll('md-dialog');
579+
expect(container.length).toBe(1);
580+
}));
581+
});
582+
583+
584+
describe('when autoWrap parameter is false', function() {
585+
586+
it('should not wrap raw content with md-dialog', inject(function($mdDialog, $rootScope) {
587+
588+
var template = '<div id="rawContent">Hello</div>';
589+
var parent = angular.element('<div>');
590+
591+
$mdDialog.show({
592+
template: template,
593+
parent: parent,
594+
autoWrap: false
595+
});
596+
597+
$rootScope.$apply();
598+
599+
var container = parent[0].querySelectorAll('md-dialog');
600+
expect(container.length).toBe(0);
601+
}));
602+
});
579603

580604
it('should append dialog within a md-dialog-container', inject(function($mdDialog, $rootScope) {
581605

src/core/services/compiler/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function mdCompilerService($q, $http, $injector, $compile, $controller, $templat
101101
return $q.all(resolve).then(function(locals) {
102102

103103
var compiledData;
104-
var template = transformTemplate(locals.$template);
104+
var template = transformTemplate(locals.$template, options);
105105
var element = options.element || angular.element('<div>').html(template.trim()).contents();
106106
var linkFn = $compile(element);
107107

src/core/services/compiler/compiler.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ describe('$mdCompiler service', function() {
5454
expect(data.element.html()).toBe('hello world');
5555
});
5656

57+
it('transformTemplate receives the options', function() {
58+
var data = compile({
59+
template: 'world',
60+
someArg: 'foo',
61+
transformTemplate: function(tpl, options) { return 'hello ' + tpl + ': ' + options.someArg; }
62+
});
63+
expect(data.element.html()).toBe('hello world: foo');
64+
});
65+
5766
describe('with resolve and locals options', function() {
5867
var options;
5968

0 commit comments

Comments
 (0)