Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
feat(toast): simple with content string: '$mdToast.simple('my-content')`
Browse files Browse the repository at this point in the history
Closes #833.
  • Loading branch information
ajoslin committed Dec 3, 2014
1 parent c0bbad2 commit 554beff
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/components/bottomSheet/bottomSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ function MdBottomSheetProvider($$interimElementProvider) {
});

/* @ngInject */
function bottomSheetDefaults($animate, $mdConstant, $timeout, $$rAF, $compile, $mdTheming,
$mdBottomSheet, $rootElement) {
function bottomSheetDefaults($animate, $mdConstant, $timeout, $$rAF, $compile, $mdTheming, $mdBottomSheet, $rootElement) {
var backdrop;

return {
Expand Down Expand Up @@ -148,12 +147,12 @@ function MdBottomSheetProvider($$interimElementProvider) {

return $animate.enter(bottomSheet.element, options.parent)
.then(function() {
var focusableItems = angular.element(
var focusable = angular.element(
element[0].querySelector('button') ||
element[0].querySelector('a') ||
element[0].querySelector('[ng-click]')
);
focusableItems.eq(0).focus();
focusable.focus();

if (options.escapeToClose) {
options.rootElementKeyupCallback = function(e) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ function MdDialogProvider($$interimElementProvider) {
options: dialogDefaultOptions
})
.addPreset('alert', {
methods: alertDialogMethods,
methods: ['title', 'content', 'ariaLabel', 'ok'],
options: advancedDialogOptions
})
.addPreset('confirm', {
methods: alertDialogMethods.concat('cancel'),
methods: ['title', 'content', 'ariaLabel', 'ok', 'cancel'],
options: advancedDialogOptions
});

Expand Down
1 change: 1 addition & 0 deletions src/components/toast/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function MdToastProvider($$interimElementProvider) {
options: toastDefaultOptions
})
.addPreset('simple', {
argOption: 'content',
methods: ['content', 'action', 'highlightAction'],
options: /* @ngInject */ function($mdToast) {
return {
Expand Down
24 changes: 19 additions & 5 deletions src/core/services/interimElement/interimElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function InterimElementProvider() {
* as well as configuration of 'preset' methods (eg dialog.basic(): basic is a preset method)
*/
function createInterimElementProvider(interimFactoryName) {
var EXPOSED_METHODS = ['onHide', 'onShow'];
var providerConfig = {
presets: {}
};
Expand All @@ -51,7 +52,7 @@ function InterimElementProvider() {
* all interim elements will come with the 'build' preset
*/
provider.addPreset('build', {
methods: ['controller', 'controllerAs', 'onRemove', 'onShow', 'resolve',
methods: ['controller', 'controllerAs', 'resolve',
'template', 'templateUrl', 'themable', 'transformTemplate', 'parent']
});

Expand All @@ -65,6 +66,7 @@ function InterimElementProvider() {
providerConfig.methods = definition.methods;
return provider;
}

/**
* Save the configured preset to be used when the factory is instantiated
*/
Expand All @@ -80,8 +82,9 @@ function InterimElementProvider() {
throw new Error("Method '_options' in " + interimFactoryName + " is reserved!");
}
providerConfig.presets[name] = {
methods: definition.methods,
optionsFactory: definition.options
methods: definition.methods.concat(EXPOSED_METHODS),
optionsFactory: definition.options,
argOption: definition.argOption
};
return provider;
}
Expand Down Expand Up @@ -145,8 +148,19 @@ function InterimElementProvider() {
});

// eg $mdDialog.alert() will return a new alert preset
publicService[name] = function(options) {
return new Preset(options);
publicService[name] = function(arg) {
// If argOption is supplied, eg `argOption: 'content'`, then we assume
// if the argument is not an options object then it is the `argOption` option.
//
// @example `$mdToast.simple('hello')` // sets options.content to hello
// // because argOption === 'content'
if (arguments.length && definition.argOption && !angular.isObject(arg) &&
!angular.isArray(arg)) {
return (new Preset())[definition.argOption](arg);
} else {
return new Preset(arg);
}

};
});

Expand Down
18 changes: 18 additions & 0 deletions src/core/services/interimElement/interimElement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ describe('$$interimElement service', function() {
});
});

it('should allow argOption in a custom builder', function() {
createInterimProvider('interimTest')
.addPreset('banana', {
argOption: 'color',
methods: ['color']
});
inject(function(interimTest, $rootScope) {
var shown = false;
var builder = interimTest.banana('yellow').onShow(function(scope, element, options) {
expect(options.color).toBe('yellow');
shown = true;
});
interimTest.show(builder);
$rootScope.$apply();
expect(shown).toBe(true);
});
});

it('should show with proper options', function() {
createInterimProvider('interimTest')
.setDefaults({
Expand Down

0 comments on commit 554beff

Please sign in to comment.