|
| 1 | +<link rel="import" href="../polymer/polymer.html"> |
| 2 | +<link rel="import" href="../iron-flex-layout/iron-flex-layout.html"> |
| 3 | +<link rel="import" href="../iron-resizable-behavior/iron-resizable-behavior.html"> |
| 4 | +<link rel="import" href="../paper-styles/default-theme.html"> |
| 5 | + |
| 6 | +<!-- |
| 7 | +`s-circle-progress` |
| 8 | +Polymer-based web component displaying a circular progress bar. |
| 9 | +
|
| 10 | +@demo demo/index.html |
| 11 | +--> |
| 12 | + |
| 13 | +<dom-module id="s-circle-progress"> |
| 14 | +<template> |
| 15 | + |
| 16 | +<style> |
| 17 | + |
| 18 | +:host { |
| 19 | + @apply(--layout-vertical); |
| 20 | + @apply(--layout-center-center); |
| 21 | + |
| 22 | + position: relative; |
| 23 | + |
| 24 | + width: 64px; |
| 25 | + height: 64px; |
| 26 | + margin: 24px; |
| 27 | + |
| 28 | + border-radius: 50%; |
| 29 | +} |
| 30 | + |
| 31 | +svg { |
| 32 | + position: absolute; |
| 33 | + top: 0; |
| 34 | + left: 0; |
| 35 | + |
| 36 | + display: none; |
| 37 | +} |
| 38 | + |
| 39 | +.Circle-background { |
| 40 | + stroke: var(--s-circle-progress-bg-stroke-color, --paper-grey-100); |
| 41 | +} |
| 42 | + |
| 43 | +.Circle-foreground { |
| 44 | + transition: stroke-dashoffset 150ms; |
| 45 | + |
| 46 | + stroke: var(--s-circle-progress-stroke-color, --accent-color); |
| 47 | + stroke-linecap: var(--s-circle-progress-stroke-linecap, round); |
| 48 | +} |
| 49 | + |
| 50 | +</style> |
| 51 | + |
| 52 | +<svg id="circle" width="100%" height="100%"> |
| 53 | + <circle class="Circle-background" |
| 54 | + r$="[[_radius]]" |
| 55 | + cx$="[[_cx]]" |
| 56 | + cy$="[[_cy]]" |
| 57 | + fill="transparent" |
| 58 | + stroke-width$="[[strokeWidth]]" /> |
| 59 | + <circle class="Circle-foreground" |
| 60 | + r$="[[_radius]]" |
| 61 | + cx$="[[_cx]]" |
| 62 | + cy$="[[_cy]]" |
| 63 | + fill="transparent" |
| 64 | + stroke-width$="[[strokeWidth]]" |
| 65 | + stroke-dasharray$="[[_dasharray]]" |
| 66 | + stroke-dashoffset$="[[_dashoffset]]" |
| 67 | + transform$="[[_transform]]" /> |
| 68 | +</svg> |
| 69 | + |
| 70 | +<content></content> |
| 71 | + |
| 72 | +</template> |
| 73 | +<script> |
| 74 | + |
| 75 | +Polymer({ |
| 76 | + is: 's-circle-progress', |
| 77 | + |
| 78 | + behaviors: [ |
| 79 | + Polymer.IronResizableBehavior |
| 80 | + ], |
| 81 | + |
| 82 | + properties: { |
| 83 | + /** |
| 84 | + * Value of circular progress bar. |
| 85 | + */ |
| 86 | + value: { |
| 87 | + type: Number, |
| 88 | + value: 0 |
| 89 | + }, |
| 90 | + |
| 91 | + /** |
| 92 | + * Maximum of value. |
| 93 | + */ |
| 94 | + max: { |
| 95 | + type: Number, |
| 96 | + value: 100 |
| 97 | + }, |
| 98 | + |
| 99 | + /** |
| 100 | + * Stroke width of circle. |
| 101 | + */ |
| 102 | + strokeWidth: { |
| 103 | + type: Number, |
| 104 | + value: 4 |
| 105 | + }, |
| 106 | + |
| 107 | + /** |
| 108 | + * Starting angle of the progress. |
| 109 | + */ |
| 110 | + angle: { |
| 111 | + type: Number, |
| 112 | + value: -90 |
| 113 | + }, |
| 114 | + |
| 115 | + _cx: { |
| 116 | + type: Number, |
| 117 | + value: null |
| 118 | + }, |
| 119 | + |
| 120 | + _cy: { |
| 121 | + type: Number, |
| 122 | + value: null |
| 123 | + }, |
| 124 | + |
| 125 | + _radius: { |
| 126 | + type: Number, |
| 127 | + computed: '_computeRadius(_cx, _cy, strokeWidth)' |
| 128 | + }, |
| 129 | + |
| 130 | + _transform: { |
| 131 | + type: String, |
| 132 | + computed: '_computeTransform(angle, _cx, _cy)' |
| 133 | + }, |
| 134 | + |
| 135 | + _dasharray: { |
| 136 | + type: Number, |
| 137 | + computed: '_computeDashArray(_radius)' |
| 138 | + }, |
| 139 | + |
| 140 | + _dashoffset: { |
| 141 | + type: Number, |
| 142 | + computed: '_computeDashOffset(value, max, _dasharray)' |
| 143 | + } |
| 144 | + }, |
| 145 | + |
| 146 | + listeners: { |
| 147 | + "iron-resize": "_onIronResize" |
| 148 | + }, |
| 149 | + |
| 150 | + _computeDashArray: function(radius) { |
| 151 | + return 2 * Math.PI * radius; |
| 152 | + }, |
| 153 | + |
| 154 | + _computeDashOffset: function(value, max, dasharray) { |
| 155 | + return (1 - value / max) * dasharray; |
| 156 | + }, |
| 157 | + |
| 158 | + _computeRadius: function(cx, cy, strokeWidth) { |
| 159 | + return cx && cy ? Math.max(0, Math.min(cx, cy) - strokeWidth / 2) : 0; |
| 160 | + }, |
| 161 | + |
| 162 | + _computeTransform: function(angle, cx, cy) { |
| 163 | + return cx && cy ? 'rotate(' + angle + ', ' + cx + ', ' + cy + ')' : ''; |
| 164 | + }, |
| 165 | + |
| 166 | + _onIronResize: function() { |
| 167 | + if (this.offsetWidth && this.offsetHeight) { |
| 168 | + this._cx = this.offsetWidth / 2; |
| 169 | + this._cy = this.offsetHeight / 2; |
| 170 | + this.$.circle.style.display = 'block'; |
| 171 | + } |
| 172 | + } |
| 173 | +}); |
| 174 | + |
| 175 | +</script> |
| 176 | +</dom-module> |
0 commit comments