From 6180c9a56a9fab23fc03a00aafaec2cab0244f51 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sun, 24 Apr 2016 12:47:04 +0200 Subject: [PATCH] feat(ripple): add the ability to disable ripples globally Exposes the `$mdInkRippleProvider` during app config and allows the user to disable ripples globally via the `disableInkRipple` method. Fixes #5669. --- src/core/services/ripple/ripple.js | 78 +++++++++++++++++-------- src/core/services/ripple/ripple.spec.js | 14 +++++ 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/core/services/ripple/ripple.js b/src/core/services/ripple/ripple.js index ae33e59cc56..a9148490407 100644 --- a/src/core/services/ripple/ripple.js +++ b/src/core/services/ripple/ripple.js @@ -5,7 +5,7 @@ * Ripple */ angular.module('material.core') - .factory('$mdInkRipple', InkRippleService) + .provider('$mdInkRipple', InkRippleProvider) .directive('mdInkRipple', InkRippleDirective) .directive('mdNoInk', attrNoDirective) .directive('mdNoBar', attrNoDirective) @@ -84,32 +84,60 @@ function InkRippleDirective ($mdButtonInkRipple, $mdCheckboxInkRipple) { * } * }); * - */ - -/** - * @ngdoc method - * @name $mdInkRipple#attach * - * @description - * Attaching given scope, element and options to inkRipple controller + * ### Disabling ripples globally + * If you want to disable ink ripples globally, for all components, you can call the + * `disableInkRipple` method in your app's config. * - * @param {object=} scope Scope within the current context - * @param {object=} element The element the ripple effect should be applied to - * @param {object=} options (Optional) Configuration options to override the defaultRipple configuration - * * `center` - Whether the ripple should start from the center of the container element - * * `dimBackground` - Whether the background should be dimmed with the ripple color - * * `colorElement` - The element the ripple should take its color from, defined by css property `color` - * * `fitRipple` - Whether the ripple should fill the element + * + * app.config(function ($mdInkRippleProvider) { + * $mdInkRippleProvider.disableInkRipple(); + * }); */ -function InkRippleService ($injector) { - return { attach: attach }; - function attach (scope, element, options) { - if (element.controller('mdNoInk')) return angular.noop; - return $injector.instantiate(InkRippleCtrl, { - $scope: scope, - $element: element, - rippleOptions: options - }); + +function InkRippleProvider () { + var isDisabledGlobally = false; + + return { + disableInkRipple: disableInkRipple, + $get: function($injector) { + return { attach: attach }; + + /** + * @ngdoc method + * @name $mdInkRipple#attach + * + * @description + * Attaching given scope, element and options to inkRipple controller + * + * @param {object=} scope Scope within the current context + * @param {object=} element The element the ripple effect should be applied to + * @param {object=} options (Optional) Configuration options to override the defaultRipple configuration + * * `center` - Whether the ripple should start from the center of the container element + * * `dimBackground` - Whether the background should be dimmed with the ripple color + * * `colorElement` - The element the ripple should take its color from, defined by css property `color` + * * `fitRipple` - Whether the ripple should fill the element + */ + function attach (scope, element, options) { + if (isDisabledGlobally || element.controller('mdNoInk')) return angular.noop; + return $injector.instantiate(InkRippleCtrl, { + $scope: scope, + $element: element, + rippleOptions: options + }); + } + } + }; + + /** + * @ngdoc method + * @name $mdInkRipple#disableInkRipple + * + * @description + * A config-time method that, when called, disables ripples globally. + */ + function disableInkRipple () { + isDisabledGlobally = true; } } @@ -202,7 +230,7 @@ InkRippleCtrl.prototype.calculateColor = function () { InkRippleCtrl.prototype._parseColor = function parseColor (color, multiplier) { multiplier = multiplier || 1; var colorUtil = this.$mdColorUtil; - + if (!color) return; if (color.indexOf('rgba') === 0) return color.replace(/\d?\.?\d*\s*\)\s*$/, (0.1 * multiplier).toString() + ')'); if (color.indexOf('rgb') === 0) return colorUtil.rgbToRgba(color); diff --git a/src/core/services/ripple/ripple.spec.js b/src/core/services/ripple/ripple.spec.js index 2e71713f526..fef10839557 100644 --- a/src/core/services/ripple/ripple.spec.js +++ b/src/core/services/ripple/ripple.spec.js @@ -135,3 +135,17 @@ describe('mdInkRipple directive', function() { })); }); }); + +describe('disabling ripples globally', function() { + beforeEach(function() { + module('material.core', function($mdInkRippleProvider) { + $mdInkRippleProvider.disableInkRipple(); + }); + }); + + it('should not instantiate the ripple controller', inject(function ($compile, $rootScope) { + var elem = $compile('
')($rootScope.$new()); + var controller = elem.controller('mdInkRipple'); + expect(Object.keys(controller).length).toBe(0); + })); +});