2  * Angular Material Design
 
   3  * https://github.com/angular/material
 
   7 (function( window, angular, undefined ){
 
  12  * @name material.components.progressCircular
 
  13  * @description Module for a circular progressbar
 
  16 angular.module('material.components.progressCircular', ['material.core']);
 
  20  * @name mdProgressCircular
 
  21  * @module material.components.progressCircular
 
  25  * The circular progress directive is used to make loading content in your app as delightful and
 
  26  * painless as possible by minimizing the amount of visual change a user sees before they can view
 
  27  * and interact with content.
 
  29  * For operations where the percentage of the operation completed can be determined, use a
 
  30  * determinate indicator. They give users a quick sense of how long an operation will take.
 
  32  * For operations where the user is asked to wait a moment while something finishes up, and it’s
 
  33  * not necessary to expose what's happening behind the scenes and how long it will take, use an
 
  34  * indeterminate indicator.
 
  36  * @param {string} md-mode Select from one of two modes: **'determinate'** and **'indeterminate'**.
 
  38  * Note: if the `md-mode` value is set as undefined or specified as not 1 of the two (2) valid modes, then **'indeterminate'**
 
  39  * will be auto-applied as the mode.
 
  41  * Note: if not configured, the `md-mode="indeterminate"` will be auto injected as an attribute.
 
  42  * If `value=""` is also specified, however, then `md-mode="determinate"` would be auto-injected instead.
 
  43  * @param {number=} value In determinate mode, this number represents the percentage of the
 
  44  *     circular progress. Default: 0
 
  45  * @param {number=} md-diameter This specifies the diameter of the circular progress. The value
 
  46  * should be a pixel-size value (eg '100'). If this attribute is
 
  47  * not present then a default value of '50px' is assumed.
 
  49  * @param {boolean=} ng-disabled Determines whether to disable the progress element.
 
  53  * <md-progress-circular md-mode="determinate" value="..."></md-progress-circular>
 
  55  * <md-progress-circular md-mode="determinate" ng-value="..."></md-progress-circular>
 
  57  * <md-progress-circular md-mode="determinate" value="..." md-diameter="100"></md-progress-circular>
 
  59  * <md-progress-circular md-mode="indeterminate"></md-progress-circular>
 
  63 MdProgressCircularDirective['$inject'] = ["$window", "$mdProgressCircular", "$mdTheming", "$mdUtil", "$interval", "$log"];
 
  65   .module('material.components.progressCircular')
 
  66   .directive('mdProgressCircular', MdProgressCircularDirective);
 
  69 function MdProgressCircularDirective($window, $mdProgressCircular, $mdTheming,
 
  70                                      $mdUtil, $interval, $log) {
 
  72   // Note that this shouldn't use use $$rAF, because it can cause an infinite loop
 
  73   // in any tests that call $animate.flush.
 
  74   var rAF = $window.requestAnimationFrame ||
 
  75             $window.webkitRequestAnimationFrame ||
 
  78   var cAF = $window.cancelAnimationFrame ||
 
  79             $window.webkitCancelAnimationFrame ||
 
  80             $window.webkitCancelRequestAnimationFrame ||
 
  83   var MODE_DETERMINATE = 'determinate';
 
  84   var MODE_INDETERMINATE = 'indeterminate';
 
  85   var DISABLED_CLASS = '_md-progress-circular-disabled';
 
  86   var INDETERMINATE_CLASS = 'md-mode-indeterminate';
 
  96       '<svg xmlns="http://www.w3.org/2000/svg">' +
 
  97         '<path fill="none"/>' +
 
  99     compile: function(element, attrs) {
 
 102         'aria-valuemax': 100,
 
 103         'role': 'progressbar'
 
 106       if (angular.isUndefined(attrs.mdMode)) {
 
 107         var mode = attrs.hasOwnProperty('value') ? MODE_DETERMINATE : MODE_INDETERMINATE;
 
 108         attrs.$set('mdMode', mode);
 
 110         attrs.$set('mdMode', attrs.mdMode.trim());
 
 113       return MdProgressCircularLink;
 
 117   function MdProgressCircularLink(scope, element, attrs) {
 
 118     var node = element[0];
 
 119     var svg = angular.element(node.querySelector('svg'));
 
 120     var path = angular.element(node.querySelector('path'));
 
 121     var startIndeterminate = $mdProgressCircular.startIndeterminate;
 
 122     var endIndeterminate = $mdProgressCircular.endIndeterminate;
 
 123     var iterationCount = 0;
 
 124     var lastAnimationId = 0;
 
 129     element.toggleClass(DISABLED_CLASS, attrs.hasOwnProperty('disabled'));
 
 131     // If the mode is indeterminate, it doesn't need to
 
 132     // wait for the next digest. It can start right away.
 
 133     if(scope.mdMode === MODE_INDETERMINATE){
 
 134       startIndeterminateAnimation();
 
 137     scope.$on('$destroy', function(){
 
 138       cleanupIndeterminateAnimation();
 
 145     scope.$watchGroup(['value', 'mdMode', function() {
 
 146       var isDisabled = node.disabled;
 
 148       // Sometimes the browser doesn't return a boolean, in
 
 149       // which case we should check whether the attribute is
 
 151       if (isDisabled === true || isDisabled === false){
 
 155       return angular.isDefined(element.attr('disabled'));
 
 156     }], function(newValues, oldValues) {
 
 157       var mode = newValues[1];
 
 158       var isDisabled = newValues[2];
 
 159       var wasDisabled = oldValues[2];
 
 161       if (isDisabled !== wasDisabled) {
 
 162         element.toggleClass(DISABLED_CLASS, !!isDisabled);
 
 166         cleanupIndeterminateAnimation();
 
 168         if (mode !== MODE_DETERMINATE && mode !== MODE_INDETERMINATE) {
 
 169           mode = MODE_INDETERMINATE;
 
 170           attrs.$set('mdMode', mode);
 
 173         if (mode === MODE_INDETERMINATE) {
 
 174           startIndeterminateAnimation();
 
 176           var newValue = clamp(newValues[0]);
 
 178           cleanupIndeterminateAnimation();
 
 180           element.attr('aria-valuenow', newValue);
 
 181           renderCircle(clamp(oldValues[0]), newValue);
 
 187     // This is in a separate watch in order to avoid layout, unless
 
 188     // the value has actually changed.
 
 189     scope.$watch('mdDiameter', function(newValue) {
 
 190       var diameter = getSize(newValue);
 
 191       var strokeWidth = getStroke(diameter);
 
 192       var value = clamp(scope.value);
 
 193       var transformOrigin = (diameter / 2) + 'px';
 
 195         width: diameter + 'px',
 
 196         height: diameter + 'px'
 
 199       // The viewBox has to be applied via setAttribute, because it is
 
 200       // case-sensitive. If jQuery is included in the page, `.attr` lowercases
 
 201       // all attribute names.
 
 202       svg[0].setAttribute('viewBox', '0 0 ' + diameter + ' ' + diameter);
 
 204       // Usually viewBox sets the dimensions for the SVG, however that doesn't
 
 205       // seem to be the case on IE10.
 
 206       // Important! The transform origin has to be set from here and it has to
 
 207       // be in the format of "Ypx Ypx Ypx", otherwise the rotation wobbles in
 
 208       // IE and Edge, because they don't account for the stroke width when
 
 209       // rotating. Also "center" doesn't help in this case, it has to be a
 
 213         .css('transform-origin', transformOrigin + ' ' + transformOrigin + ' ' + transformOrigin);
 
 215       element.css(dimensions);
 
 217       path.attr('stroke-width', strokeWidth);
 
 218       path.attr('stroke-linecap', 'square');
 
 219       if (scope.mdMode == MODE_INDETERMINATE) {
 
 220         path.attr('d', getSvgArc(diameter, strokeWidth, true));
 
 221         path.attr('stroke-dasharray', (diameter - strokeWidth) * $window.Math.PI * 0.75);
 
 222         path.attr('stroke-dashoffset', getDashLength(diameter, strokeWidth, 1, 75));
 
 224         path.attr('d', getSvgArc(diameter, strokeWidth, false));
 
 225         path.attr('stroke-dasharray', (diameter - strokeWidth) * $window.Math.PI);
 
 226         path.attr('stroke-dashoffset', getDashLength(diameter, strokeWidth, 0, 100));
 
 227         renderCircle(value, value);
 
 232     function renderCircle(animateFrom, animateTo, easing, duration, iterationCount, maxValue) {
 
 233       var id = ++lastAnimationId;
 
 234       var startTime = $mdUtil.now();
 
 235       var changeInValue = animateTo - animateFrom;
 
 236       var diameter = getSize(scope.mdDiameter);
 
 237       var strokeWidth = getStroke(diameter);
 
 238       var ease = easing || $mdProgressCircular.easeFn;
 
 239       var animationDuration = duration || $mdProgressCircular.duration;
 
 240       var rotation = -90 * (iterationCount || 0);
 
 241       var dashLimit = maxValue || 100;
 
 243       // No need to animate it if the values are the same
 
 244       if (animateTo === animateFrom) {
 
 245         renderFrame(animateTo);
 
 247         lastDrawFrame = rAF(function animation() {
 
 248           var currentTime = $window.Math.max(0, $window.Math.min($mdUtil.now() - startTime, animationDuration));
 
 250           renderFrame(ease(currentTime, animateFrom, changeInValue, animationDuration));
 
 252           // Do not allow overlapping animations
 
 253           if (id === lastAnimationId && currentTime < animationDuration) {
 
 254             lastDrawFrame = rAF(animation);
 
 259       function renderFrame(value) {
 
 260         path.attr('stroke-dashoffset', getDashLength(diameter, strokeWidth, value, dashLimit));
 
 261         path.attr('transform','rotate(' + (rotation) + ' ' + diameter/2 + ' ' + diameter/2 + ')');
 
 265     function animateIndeterminate() {
 
 269         $mdProgressCircular.easeFnIndeterminate,
 
 270         $mdProgressCircular.durationIndeterminate,
 
 275       // The %4 technically isn't necessary, but it keeps the rotation
 
 276       // under 360, instead of becoming a crazy large number.
 
 277       iterationCount = ++iterationCount % 4;
 
 281     function startIndeterminateAnimation() {
 
 283         // Note that this interval isn't supposed to trigger a digest.
 
 284         interval = $interval(
 
 285           animateIndeterminate,
 
 286           $mdProgressCircular.durationIndeterminate,
 
 291         animateIndeterminate();
 
 294           .addClass(INDETERMINATE_CLASS)
 
 295           .removeAttr('aria-valuenow');
 
 299     function cleanupIndeterminateAnimation() {
 
 301         $interval.cancel(interval);
 
 303         element.removeClass(INDETERMINATE_CLASS);
 
 309    * Returns SVG path data for progress circle
 
 310    * Syntax spec: https://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
 
 312    * @param {number} diameter Diameter of the container.
 
 313    * @param {number} strokeWidth Stroke width to be used when drawing circle
 
 314    * @param {boolean} indeterminate Use if progress circle will be used for indeterminate
 
 316    * @returns {string} String representation of an SVG arc.
 
 318   function getSvgArc(diameter, strokeWidth, indeterminate) {
 
 319     var radius = diameter / 2;
 
 320     var offset = strokeWidth / 2;
 
 321     var start = radius + ',' + offset; // ie: (25, 2.5) or 12 o'clock
 
 322     var end = offset + ',' + radius;   // ie: (2.5, 25) or  9 o'clock
 
 323     var arcRadius = radius - offset;
 
 325          + 'A' + arcRadius + ',' + arcRadius + ' 0 1 1 ' + end // 75% circle
 
 326          + (indeterminate ? '' : 'A' + arcRadius + ',' + arcRadius + ' 0 0 1 ' + start); // loop to start
 
 330    * Return stroke length for progress circle
 
 332    * @param {number} diameter Diameter of the container.
 
 333    * @param {number} strokeWidth Stroke width to be used when drawing circle
 
 334    * @param {number} value Percentage of circle (between 0 and 100)
 
 335    * @param {number} limit Max percentage for circle
 
 337    * @returns {number} Stroke length for progres circle
 
 339   function getDashLength(diameter, strokeWidth, value, limit) {
 
 340     return (diameter - strokeWidth) * $window.Math.PI * ( (3 * (limit || 100) / 100) - (value/100) );
 
 344    * Limits a value between 0 and 100.
 
 346   function clamp(value) {
 
 347     return $window.Math.max(0, $window.Math.min(value || 0, 100));
 
 351    * Determines the size of a progress circle, based on the provided
 
 352    * value in the following formats: `X`, `Ypx`, `Z%`.
 
 354   function getSize(value) {
 
 355     var defaultValue = $mdProgressCircular.progressSize;
 
 358       var parsed = parseFloat(value);
 
 360       if (value.lastIndexOf('%') === value.length - 1) {
 
 361         parsed = (parsed / 100) * defaultValue;
 
 371    * Determines the circle's stroke width, based on
 
 372    * the provided diameter.
 
 374   function getStroke(diameter) {
 
 375     return $mdProgressCircular.strokeWidth / 100 * diameter;
 
 382  * @name $mdProgressCircular
 
 383  * @module material.components.progressCircular
 
 386  * Allows the user to specify the default options for the `progressCircular` directive.
 
 388  * @property {number} progressSize Diameter of the progress circle in pixels.
 
 389  * @property {number} strokeWidth Width of the circle's stroke as a percentage of the circle's size.
 
 390  * @property {number} duration Length of the circle animation in milliseconds.
 
 391  * @property {function} easeFn Default easing animation function.
 
 392  * @property {object} easingPresets Collection of pre-defined easing functions.
 
 394  * @property {number} durationIndeterminate Duration of the indeterminate animation.
 
 395  * @property {number} startIndeterminate Indeterminate animation start point.
 
 396  * @property {number} endIndeterminate Indeterminate animation end point.
 
 397  * @property {function} easeFnIndeterminate Easing function to be used when animating
 
 398  * between the indeterminate values.
 
 400  * @property {(function(object): object)} configure Used to modify the default options.
 
 404  *   myAppModule.config(function($mdProgressCircularProvider) {
 
 406  *     // Example of changing the default progress options.
 
 407  *     $mdProgressCircularProvider.configure({
 
 418   .module('material.components.progressCircular')
 
 419   .provider("$mdProgressCircular", MdProgressCircularProvider);
 
 421 function MdProgressCircularProvider() {
 
 422   var progressConfig = {
 
 428     durationIndeterminate: 1333,
 
 429     startIndeterminate: 1,
 
 430     endIndeterminate: 149,
 
 431     easeFnIndeterminate: materialEase,
 
 434       linearEase: linearEase,
 
 435       materialEase: materialEase
 
 440     configure: function(options) {
 
 441       progressConfig = angular.extend(progressConfig, options || {});
 
 442       return progressConfig;
 
 444     $get: function() { return progressConfig; }
 
 447   function linearEase(t, b, c, d) {
 
 448     return c * t / d + b;
 
 451   function materialEase(t, b, c, d) {
 
 452     // via http://www.timotheegroleau.com/Flash/experiments/easing_function_generator.htm
 
 453     // with settings of [0, 0, 1, 1]
 
 454     var ts = (t /= d) * t;
 
 456     return b + c * (6 * tc * ts + -15 * ts * ts + 10 * tc);
 
 460 })(window, window.angular);