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

fix(dialog): apply foreground color and automatically detect theme #8723

Closed
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
3 changes: 3 additions & 0 deletions src/components/dialog/dialog-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ $dialog-border-radius: 4px !default;

md-dialog.md-THEME_NAME-theme {
border-radius: $dialog-border-radius;

background-color: '{{background-hue-1}}';
color: '{{foreground-1}}';


&.md-content-overflow {
.md-actions, md-dialog-actions {
Expand Down
33 changes: 30 additions & 3 deletions src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ function MdDialogProvider($$interimElementProvider) {
});

/* @ngInject */
function advancedDialogOptions($mdDialog, $mdTheming, $mdConstant) {
function advancedDialogOptions($mdDialog, $mdConstant) {
return {
template: [
'<md-dialog md-theme="{{ dialog.theme }}" aria-label="{{ dialog.ariaLabel }}" ng-class="dialog.css">',
Expand Down Expand Up @@ -599,15 +599,17 @@ function MdDialogProvider($$interimElementProvider) {
},
controllerAs: 'dialog',
bindToController: true,
theme: $mdTheming.defaultTheme()
};
}

/* @ngInject */
function dialogDefaultOptions($mdDialog, $mdAria, $mdUtil, $mdConstant, $animate, $document, $window, $rootElement, $log, $injector) {
function dialogDefaultOptions($mdDialog, $mdAria, $mdUtil, $mdConstant, $animate, $document, $window, $rootElement,
$log, $injector, $mdTheming) {

return {
hasBackdrop: true,
isolateScope: true,
onCompiling: beforeCompile,
onShow: onShow,
onShowing: beforeShow,
onRemove: onRemove,
Expand Down Expand Up @@ -641,7 +643,15 @@ function MdDialogProvider($$interimElementProvider) {
}
};

function beforeCompile(options) {
// Automatically apply the theme, if the user didn't specify a theme explicitly.
// 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);
}

function beforeShow(scope, element, options, controller) {

if (controller) {
controller.mdHtmlContent = controller.htmlContent || options.htmlContent || '';
controller.mdTextContent = controller.textContent || options.textContent ||
Expand Down Expand Up @@ -797,6 +807,22 @@ function MdDialogProvider($$interimElementProvider) {
}
}

function detectTheming(options) {
// Only detect the theming, if the developer didn't specify the theme specifically.
if (options.theme) return;

options.theme = $mdTheming.defaultTheme();

if (options.targetEvent && options.targetEvent.target) {
var targetEl = angular.element(options.targetEvent.target);

// Once the user specifies a targetEvent, we will automatically try to find the correct
// nested theme.
options.theme = (targetEl.controller('mdTheme') || {}).$mdTheme || options.theme;
}

}

/**
* Capture originator/trigger/from/to element information (if available)
* and the parent container for the dialog; defaults to the $rootElement
Expand All @@ -817,6 +843,7 @@ function MdDialogProvider($$interimElementProvider) {
options.origin = getBoundingClientRect(options.targetEvent.target, options.origin);
}


/**
* Identify the bounding RECT for the target element
*
Expand Down
43 changes: 43 additions & 0 deletions src/components/dialog/dialog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ describe('$mdDialog', function() {
expect(resolved).toBe(true);
}));

it('should normally use the default theme', inject(function($animate, $rootScope, $mdDialog, $compile) {
var dialogParent = angular.element('<div>');

$mdDialog.show(
$mdDialog
.alert()
.parent(dialogParent)
.title("Title")
.textContent("Themed Dialog")
.ok('Close')
);

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

var mdContainer = angular.element(dialogParent[0].querySelector('.md-dialog-container'));
var mdDialog = mdContainer.find('md-dialog');

expect(mdDialog.attr('md-theme')).toBe('default');
}));

it('should apply the specified theme', inject(function($animate, $rootScope, $mdDialog, $compile) {
var dialogParent = angular.element('<div>');

$mdDialog.show(
$mdDialog
.alert()
.parent(dialogParent)
.title("Title")
.theme('myTheme')
.textContent("Themed Dialog")
.ok('Close')
);

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

var mdContainer = angular.element(dialogParent[0].querySelector('.md-dialog-container'));
var mdDialog = mdContainer.find('md-dialog');

expect(mdDialog.attr('md-theme')).toBe('myTheme');
}));

it('should focus `md-dialog-content` on open', inject(function($mdDialog, $rootScope, $document) {
jasmine.mockElementFocus(this);

Expand Down
6 changes: 5 additions & 1 deletion src/core/services/interimElement/interimElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ function InterimElementProvider() {
* Use optional autoHided and transition-in effects
*/
function createAndTransitionIn() {
return $q(function(resolve, reject){
return $q(function(resolve, reject) {

// Trigger onCompiling callback before the compilation starts.
// This is useful, when modifying options, which can be influenced by developers.
options.onCompiling && options.onCompiling(options);

compileElement(options)
.then(function( compiledData ) {
Expand Down
19 changes: 19 additions & 0 deletions src/core/services/interimElement/interimElement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,25 @@ describe('$$interimElement service', function() {
expect(autoClosed).toBe(true);
}));

it('calls onCompiling before onShowing', inject(function() {
var onCompilingCalled = false;

Service.show({
template: '<div>My Element</div>',
onCompiling: beforeCompile,
onShowing: beforeShow
});

function beforeCompile() {
onCompilingCalled = true;
}

function beforeShow() {
expect(onCompilingCalled).toBe(true);
}

}));

it('calls onShowing before onShow', inject(function() {
var onShowingCalled = false;

Expand Down